Files
kandev/apps/backend/internal/backendapp/plugins_test.go
T
João Salavisa e145d48f22 feat: add plugin system with grpc runtime and native ui plugins (#1742)
* feat: add plugin system with grpc runtime and native ui plugins

- go plugin backends ship as release tarballs (per-platform binaries, checksums) that
  kandev installs from url/upload or filesystem sideload+sync, then spawns and
  supervises via hashicorp/go-plugin (unix socket / loopback tcp+automtls)
- strict typed grpc contract (kandev.plugin.v1): DeliverEvent/InvokeTool/HandleWebhook
  plus a capability-gated host service (state, secrets, emit); author sdk in
  pkg/pluginsdk; packaging cli in cmd/plugin-pack
- native frontend bundles: window.registerKandevPlugin + registry/host loader with
  dynamic routes, nav items, slots and ws handlers; plugins settings page with
  install dialog, sync button and enable/disable/uninstall
- gated behind the plugins feature flag (prod off, dev/e2e on); covered by unit and
  integration tests plus playwright e2e against a real packaged fixture plugin

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): hydrate base app context on plugin routes

Plugin-owned SPA routes classify as RouteUnknown, so the boot payload shipped
only feature flags — no workspaces, workflows, or kanban context. Native plugin
UI (e.g. host.ui.TaskCreateDialog) then had an empty store. Hydrate the same
base context as the Home route for unknown SPA routes.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(plugins): expose TaskCreateDialog on host.ui

Lets a native plugin open kandev's real create-task modal (prefilled via
initialValues) instead of POSTing directly.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): harden filesystem/URL sinks against CodeQL findings

Add sink-level guards on the plugin trust boundary: securejoin containment on
archive extraction (zip-slip), plugin-id validation before every fs_store path,
http/https scheme validation on install-from-URL, and non-interpolated ws-handler
error logging.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): address review findings (races, resource caps, lifecycle)

- serialize per-plugin startup to close a TOCTOU double-spawn race, and let a
  plugin restart after exhausting attempts
- bound plugin startup with an explicit 30s timeout
- reject backslash paths and cap decompressed bytes by actual size in pkgtar;
  only mark a package Signed when a verifier actually ran and passed
- delete plugin_state on uninstall; validate webhook keys against the manifest
  and cap webhook body size; restart an active plugin when a new version installs

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): sanitize plugin-id path sinks for CodeQL

Pin the plugin id to a single path segment with filepath.Base at recordPath/
configPath and pkgtar.Remove — a barrier the taint analysis recognizes.
Behaviour-identical for valid ids (already validated by safePluginID); clears
the go/path-injection alerts.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): inline plugin-id validation at path sinks for CodeQL

filepath.Base does not strip '..', so it is not a path-injection barrier. Call
the recognized safePluginID / id-pattern guard inline in Delete/GetConfig/
SetConfig and pkgtar.Remove — the same barrier that already cleared the Get sink.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): address code-review findings (backend)

- add capability-gating tests for the Host RPC service (was untested)
- close a stop-during-start orphan race so Disable/Uninstall can't leave a
  supervised subprocess behind
- on failed upgrade, remove only the new version dir (not all versions + data);
  restart the prior version
- validate manifest version and reject ids ending in .config (record-file alias)
- make Deliverer.Refresh non-blocking and cancel in-flight delivery on worker stop
- clamp out-of-range webhook status, skip-and-warn corrupt records in List,
  write records atomically, semver-aware sideload version pick

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): address code-review findings (frontend)

- wrap plugin routes + settings routes in a shared PluginErrorBoundary so a
  throwing plugin page shows a fallback instead of white-screening the SPA
- fix disable/re-enable in one session: keep the cached registration and
  re-run initialize instead of relying on a cached ES-module re-import
- prefix root-relative bundle/style URLs with apiBaseUrl (split-origin/Tauri)
- surface partial-install warnings via toast.warning instead of a bare success
- dedupe/remove injected style links on unload; gate PluginNavItems on the flag

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): serialize per-plugin lifecycle + guard config writes

- add a per-plugin keyed lock in Service so concurrent Enable/Disable/Install/
  Uninstall/UpdateConfig for one id are serialized (no status-machine race)
- take s.mu in SetConfig/GetConfig and write config atomically
- bound each plugin initialize() with a timeout so one hung bundle can't stall
  the rest of the boot load

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* test(plugins): split service_test.go to satisfy file-length lint

Move the install/URL test group into service_install_test.go; no logic change.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* ci: build and ship the e2e fixture plugin package to E2E shards

The plugin E2E specs' global-setup requires apps/backend/.build/kandev-plugin-e2e-1.0.0.tar.gz.
Build it in the E2E build job, upload it as an artifact, and download it in both
the regular and container shard jobs.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-17 12:00:26 +01:00

199 lines
6.7 KiB
Go

package backendapp
import (
"bytes"
"context"
"fmt"
goruntime "runtime"
"testing"
"github.com/kandev/kandev/internal/common/config"
"github.com/kandev/kandev/internal/common/logger"
"github.com/kandev/kandev/internal/plugins"
"github.com/kandev/kandev/internal/plugins/delivery"
"github.com/kandev/kandev/internal/plugins/pkgtar/pkgtartest"
"github.com/kandev/kandev/internal/plugins/store"
"github.com/kandev/kandev/pkg/pluginsdk"
)
func testPluginsLogger(t *testing.T) *logger.Logger {
t.Helper()
log, err := logger.NewLogger(logger.LoggingConfig{Level: "error", Format: "console"})
if err != nil {
t.Fatalf("new logger: %v", err)
}
return log
}
// alwaysUpRuntime is a minimal plugins.PluginRuntime fake that always
// reports a successful spawn without touching a real subprocess, so
// Service.Install/Enable can reach StatusActive in tests that only exercise
// boot-payload/registry/status behavior.
type alwaysUpRuntime struct{}
func (alwaysUpRuntime) Start(context.Context, *store.Record, func(string) pluginsdk.Host) error {
return nil
}
func (alwaysUpRuntime) Stop(string) {}
func (alwaysUpRuntime) Get(string) (*pluginsdk.RemotePlugin, bool) { return nil, true }
func (alwaysUpRuntime) Ping(string) error { return nil }
func (alwaysUpRuntime) Running(string) bool { return true }
func (alwaysUpRuntime) RestartCount(string) int { return 0 }
func (alwaysUpRuntime) StopAll() {}
// testPluginPackage builds a valid, runtime-managed plugin tar.gz for the
// current host platform, with a capabilities.events subscription and
// (optionally) a UI bundle — mirroring internal/plugins/service_test.go's
// testPackage (kept separate here since that helper is unexported to that
// package).
func testPluginPackage(t *testing.T, id string, withUIBundle bool) *bytes.Buffer {
t.Helper()
platformKey := goruntime.GOOS + "-" + goruntime.GOARCH
manifestYAML := fmt.Sprintf(`
id: %s
api_version: 1
version: "1.0.0"
display_name: Test Plugin %s
capabilities:
events: ["task.created"]
runtime:
type: binary
executables:
%s: server/plugin
`, id, id, platformKey)
if withUIBundle {
manifestYAML += "ui:\n bundle: \"/ui/bundle.js\"\n styles: [\"/ui/style.css\"]\n"
}
var buf bytes.Buffer
files := map[string][]byte{
"manifest.yaml": []byte(manifestYAML),
"server/plugin": []byte("#!/bin/sh\necho fake\n"),
}
if withUIBundle {
files["ui/bundle.js"] = []byte("export default {};")
files["ui/style.css"] = []byte("body{}")
}
if err := pkgtartest.WritePackage(&buf, files); err != nil {
t.Fatalf("WritePackage: %v", err)
}
return &buf
}
// newTestPluginsService returns a plugins.Service backed by a temp-dir
// FSStore and an always-succeeding fake runtime, mirroring
// internal/plugins' own test helpers (kept separate here since those are
// unexported to that package). Install/Enable reach StatusActive without
// spawning a real subprocess.
func newTestPluginsService(t *testing.T) *plugins.Service {
t.Helper()
dir := t.TempDir()
fsStore := store.NewFSStore(dir)
registry := plugins.NewRegistry()
if err := registry.Load(fsStore); err != nil {
t.Fatalf("load registry: %v", err)
}
svc := plugins.NewService(fsStore, registry, nil, testPluginsLogger(t))
svc.SetPluginsDir(dir)
svc.SetRuntime(alwaysUpRuntime{})
return svc
}
func installTestPluginForBoot(t *testing.T, svc *plugins.Service, id string, withUIBundle bool) *store.Record {
t.Helper()
rec, err := svc.Install(context.Background(), testPluginPackage(t, id, withUIBundle))
if err != nil {
t.Fatalf("Install(%q): %v", id, err)
}
return rec
}
func TestBootActivePluginsGatedOnFeatureFlag(t *testing.T) {
svc := newTestPluginsService(t)
installTestPluginForBoot(t, svc, "kandev-plugin-hello", true)
got := bootActivePlugins(routeParams{
features: config.FeaturesConfig{Plugins: false},
services: &Services{Plugins: svc},
})
if got != nil {
t.Fatalf("bootActivePlugins() with features.Plugins=false = %v, want nil", got)
}
}
func TestBootActivePluginsPopulatesFromActiveUIPlugins(t *testing.T) {
svc := newTestPluginsService(t)
installTestPluginForBoot(t, svc, "kandev-plugin-hello", true)
// Disabled — must be excluded even though it declares a bundle.
installTestPluginForBoot(t, svc, "kandev-plugin-inactive", true)
if err := svc.Disable("kandev-plugin-inactive"); err != nil {
t.Fatalf("Disable(inactive): %v", err)
}
got := bootActivePlugins(routeParams{
features: config.FeaturesConfig{Plugins: true},
services: &Services{Plugins: svc},
})
if len(got) != 1 {
t.Fatalf("bootActivePlugins() len = %d, want 1: %+v", len(got), got)
}
entry := got[0]
if entry.ID != "kandev-plugin-hello" {
t.Fatalf("entry.ID = %q, want %q", entry.ID, "kandev-plugin-hello")
}
if entry.BundleURL != "/api/plugins/kandev-plugin-hello/bundle" {
t.Fatalf("entry.BundleURL = %q, want %q", entry.BundleURL, "/api/plugins/kandev-plugin-hello/bundle")
}
if len(entry.StyleURLs) != 1 || entry.StyleURLs[0] != "/api/plugins/kandev-plugin-hello/ui/ui/style.css" {
t.Fatalf("entry.StyleURLs = %v, want [/api/plugins/kandev-plugin-hello/ui/ui/style.css]", entry.StyleURLs)
}
}
func TestBootActivePluginsNoServiceReturnsNil(t *testing.T) {
got := bootActivePlugins(routeParams{features: config.FeaturesConfig{Plugins: true}, services: &Services{}})
if got != nil {
t.Fatalf("bootActivePlugins() with nil Plugins service = %v, want nil", got)
}
}
// --- pluginActivePluginsAdapter ---
func TestPluginActivePluginsAdapterIncludesActiveAndErrorOnly(t *testing.T) {
svc := newTestPluginsService(t)
installTestPluginForBoot(t, svc, "kandev-plugin-active", false) // active after install
installTestPluginForBoot(t, svc, "kandev-plugin-error", false)
if err := svc.SetStatus("kandev-plugin-error", plugins.StatusError); err != nil {
t.Fatalf("SetStatus(error): %v", err)
}
installTestPluginForBoot(t, svc, "kandev-plugin-disabled", false)
if err := svc.Disable("kandev-plugin-disabled"); err != nil {
t.Fatalf("Disable(disabled): %v", err)
}
adapter := pluginActivePluginsAdapter{svc: svc}
records := adapter.ActivePlugins()
byID := make(map[string]delivery.PluginRecord, len(records))
for _, rec := range records {
byID[rec.ID] = rec
}
if len(byID) != 2 {
t.Fatalf("ActivePlugins() len = %d, want 2: %+v", len(byID), records)
}
if _, ok := byID["kandev-plugin-disabled"]; ok {
t.Fatal("ActivePlugins() must not include a StatusDisabled plugin")
}
rec, ok := byID["kandev-plugin-active"]
if !ok {
t.Fatal("ActivePlugins() missing kandev-plugin-active")
}
if len(rec.EventSubjects) != 1 || rec.EventSubjects[0] != "task.created" {
t.Fatalf("ActivePlugins() record = %+v, want EventSubjects=[task.created]", rec)
}
}