e145d48f22
* 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>
320 lines
11 KiB
Go
320 lines
11 KiB
Go
package plugins
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
goruntime "runtime"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// writeManifestOnly writes just a manifest.yaml (a "dir sideload": a
|
|
// version directory dropped straight onto disk, with no {id}.yml record and
|
|
// none of the other package files pkgtar.Install would extract).
|
|
func writeManifestOnly(t *testing.T, pluginsDir, id, version string) string {
|
|
t.Helper()
|
|
versionDir := filepath.Join(pluginsDir, id, version)
|
|
if err := os.MkdirAll(versionDir, 0o755); err != nil {
|
|
t.Fatalf("MkdirAll: %v", err)
|
|
}
|
|
manifestYAML := "id: " + id + "\napi_version: 1\nversion: \"" + version + "\"\ndisplay_name: Sideloaded\n" +
|
|
"runtime:\n type: binary\n executables:\n " + goruntime.GOOS + "-" + goruntime.GOARCH + ": server/plugin\n"
|
|
if err := os.WriteFile(filepath.Join(versionDir, "manifest.yaml"), []byte(manifestYAML), 0o644); err != nil {
|
|
t.Fatalf("WriteFile manifest.yaml: %v", err)
|
|
}
|
|
return versionDir
|
|
}
|
|
|
|
// dropTarball writes a valid plugin tar.gz package directly under
|
|
// pluginsDir (simulating an operator copying a package file onto disk
|
|
// instead of using the install UI/API).
|
|
func dropTarball(t *testing.T, pluginsDir, id, filename string) string {
|
|
t.Helper()
|
|
dest := filepath.Join(pluginsDir, filename)
|
|
data := testPackage(t, id, "1.0.0", false).Bytes()
|
|
if err := os.WriteFile(dest, data, 0o644); err != nil {
|
|
t.Fatalf("WriteFile %s: %v", filename, err)
|
|
}
|
|
return dest
|
|
}
|
|
|
|
func TestServiceSync_RegistersDirSideloadAsDisabled(t *testing.T) {
|
|
svc, dir, _, rt := newTestServiceWithDir(t)
|
|
versionDir := writeManifestOnly(t, dir, "kandev-plugin-side", "1.0.0")
|
|
|
|
result, err := svc.Sync(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("Sync() unexpected error: %v", err)
|
|
}
|
|
if len(result.Added) != 1 || result.Added[0] != "kandev-plugin-side" {
|
|
t.Fatalf("Sync().Added = %v, want [kandev-plugin-side]", result.Added)
|
|
}
|
|
|
|
rec, err := svc.Get("kandev-plugin-side")
|
|
if err != nil {
|
|
t.Fatalf("Get(): %v", err)
|
|
}
|
|
if rec.Status != StatusDisabled {
|
|
t.Fatalf("sideloaded record Status = %q, want %q", rec.Status, StatusDisabled)
|
|
}
|
|
if rec.InstallPath != versionDir {
|
|
t.Fatalf("sideloaded record InstallPath = %q, want %q", rec.InstallPath, versionDir)
|
|
}
|
|
if rec.Signed {
|
|
t.Fatal("sideloaded record Signed = true, want false (unverified sideload)")
|
|
}
|
|
if rt.Running("kandev-plugin-side") {
|
|
t.Fatal("Sync() must never spawn a disabled sideload")
|
|
}
|
|
}
|
|
|
|
func TestServiceSync_ManifestIDMismatchIsAnError(t *testing.T) {
|
|
svc, dir, _, _ := newTestServiceWithDir(t)
|
|
writeManifestOnly(t, dir, "wrong-id-inside", "1.0.0")
|
|
// Rename the directory so the on-disk id ("dir-id") does not match the
|
|
// manifest's own id ("wrong-id-inside").
|
|
if err := os.Rename(filepath.Join(dir, "wrong-id-inside"), filepath.Join(dir, "dir-id")); err != nil {
|
|
t.Fatalf("os.Rename: %v", err)
|
|
}
|
|
|
|
result, err := svc.Sync(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("Sync() unexpected error: %v", err)
|
|
}
|
|
if len(result.Added) != 0 {
|
|
t.Fatalf("Sync().Added = %v, want none (mismatched manifest id)", result.Added)
|
|
}
|
|
if len(result.Errors) != 1 {
|
|
t.Fatalf("Sync().Errors = %v, want exactly one entry", result.Errors)
|
|
}
|
|
if _, err := svc.Get("dir-id"); err == nil {
|
|
t.Fatal("Get(\"dir-id\") succeeded, want not found (mismatched manifest must not be registered)")
|
|
}
|
|
}
|
|
|
|
func TestServiceSync_MultipleVersionDirsPicksGreatestAndSkipsOthers(t *testing.T) {
|
|
svc, dir, _, _ := newTestServiceWithDir(t)
|
|
writeManifestOnly(t, dir, "kandev-plugin-multi", "1.0.0")
|
|
writeManifestOnly(t, dir, "kandev-plugin-multi", "2.0.0")
|
|
|
|
result, err := svc.Sync(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("Sync() unexpected error: %v", err)
|
|
}
|
|
if len(result.Added) != 1 || result.Added[0] != "kandev-plugin-multi" {
|
|
t.Fatalf("Sync().Added = %v, want [kandev-plugin-multi]", result.Added)
|
|
}
|
|
if len(result.Errors) != 1 {
|
|
t.Fatalf("Sync().Errors = %v, want exactly one skip entry for the 1.0.0 dir", result.Errors)
|
|
}
|
|
|
|
rec, err := svc.Get("kandev-plugin-multi")
|
|
if err != nil {
|
|
t.Fatalf("Get(): %v", err)
|
|
}
|
|
if rec.Version != "2.0.0" {
|
|
t.Fatalf("registered Version = %q, want %q (semver-greatest)", rec.Version, "2.0.0")
|
|
}
|
|
}
|
|
|
|
// TestServiceSync_MultipleVersionDirsPicksSemverGreatestNotLexical pins the
|
|
// fix for a plain lexical sort picking the wrong "latest" version: "9.0.0"
|
|
// sorts after "10.0.0" as a string, but 10.0.0 is the actually-newer
|
|
// semver.
|
|
func TestServiceSync_MultipleVersionDirsPicksSemverGreatestNotLexical(t *testing.T) {
|
|
svc, dir, _, _ := newTestServiceWithDir(t)
|
|
writeManifestOnly(t, dir, "kandev-plugin-semver", "9.0.0")
|
|
writeManifestOnly(t, dir, "kandev-plugin-semver", "10.0.0")
|
|
|
|
result, err := svc.Sync(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("Sync() unexpected error: %v", err)
|
|
}
|
|
if len(result.Added) != 1 || result.Added[0] != "kandev-plugin-semver" {
|
|
t.Fatalf("Sync().Added = %v, want [kandev-plugin-semver]", result.Added)
|
|
}
|
|
|
|
rec, err := svc.Get("kandev-plugin-semver")
|
|
if err != nil {
|
|
t.Fatalf("Get(): %v", err)
|
|
}
|
|
if rec.Version != "10.0.0" {
|
|
t.Fatalf("registered Version = %q, want %q (semver-greatest, not lexically greatest)", rec.Version, "10.0.0")
|
|
}
|
|
}
|
|
|
|
func TestServiceSync_TarballInstallSucceedsAndDeletesFile(t *testing.T) {
|
|
svc, dir, _, rt := newTestServiceWithDir(t)
|
|
tarPath := dropTarball(t, dir, "kandev-plugin-drop", "kandev-plugin-drop-1.0.0.tar.gz")
|
|
|
|
result, err := svc.Sync(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("Sync() unexpected error: %v", err)
|
|
}
|
|
if len(result.Installed) != 1 || result.Installed[0] != "kandev-plugin-drop" {
|
|
t.Fatalf("Sync().Installed = %v, want [kandev-plugin-drop]", result.Installed)
|
|
}
|
|
if !rt.Running("kandev-plugin-drop") {
|
|
t.Fatal("Sync() did not spawn the successfully installed tarball plugin")
|
|
}
|
|
rec, err := svc.Get("kandev-plugin-drop")
|
|
if err != nil {
|
|
t.Fatalf("Get(): %v", err)
|
|
}
|
|
if rec.Status != StatusActive {
|
|
t.Fatalf("installed-from-tarball record Status = %q, want %q", rec.Status, StatusActive)
|
|
}
|
|
if _, statErr := os.Stat(tarPath); !os.IsNotExist(statErr) {
|
|
t.Fatalf("dropped tarball still exists after successful install, stat err = %v", statErr)
|
|
}
|
|
}
|
|
|
|
func TestServiceSync_TarballValidationFailureLeavesFileAndAddsError(t *testing.T) {
|
|
svc, dir, _, _ := newTestServiceWithDir(t)
|
|
junkPath := filepath.Join(dir, "junk.tar.gz")
|
|
if err := os.WriteFile(junkPath, []byte("not a real gzip archive"), 0o644); err != nil {
|
|
t.Fatalf("WriteFile: %v", err)
|
|
}
|
|
|
|
result, err := svc.Sync(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("Sync() unexpected error: %v", err)
|
|
}
|
|
if len(result.Installed) != 0 {
|
|
t.Fatalf("Sync().Installed = %v, want none", result.Installed)
|
|
}
|
|
if len(result.Errors) != 1 || result.Errors[0].Path != junkPath {
|
|
t.Fatalf("Sync().Errors = %v, want one entry for %q", result.Errors, junkPath)
|
|
}
|
|
if _, statErr := os.Stat(junkPath); statErr != nil {
|
|
t.Fatalf("corrupt tarball was removed, want it left in place: %v", statErr)
|
|
}
|
|
}
|
|
|
|
func TestServiceSync_MissingInstallPathSetsErrorAndStopsRuntime(t *testing.T) {
|
|
svc, _, rt := newTestService(t)
|
|
rec := installTestPlugin(t, svc, "kandev-plugin-vanished")
|
|
if err := os.RemoveAll(rec.InstallPath); err != nil {
|
|
t.Fatalf("RemoveAll(InstallPath): %v", err)
|
|
}
|
|
|
|
result, err := svc.Sync(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("Sync() unexpected error: %v", err)
|
|
}
|
|
if len(result.Missing) != 1 || result.Missing[0] != "kandev-plugin-vanished" {
|
|
t.Fatalf("Sync().Missing = %v, want [kandev-plugin-vanished]", result.Missing)
|
|
}
|
|
if !rt.stopped("kandev-plugin-vanished") {
|
|
t.Fatal("Sync() did not stop the runtime process for a plugin with a missing install path")
|
|
}
|
|
got, err := svc.Get("kandev-plugin-vanished")
|
|
if err != nil {
|
|
t.Fatalf("Get(): %v", err)
|
|
}
|
|
if got.Status != StatusError {
|
|
t.Fatalf("Status after missing-install detection = %q, want %q", got.Status, StatusError)
|
|
}
|
|
}
|
|
|
|
// TestServiceSync_MissingInstallAlreadyErrorIsIdempotent proves the "direct
|
|
// status write if transition invalid" fallback: once a plugin is already
|
|
// StatusError, canTransition(error, error) is false (SetStatus rejects
|
|
// same-status "transitions"), so Sync must not itself fail or panic —
|
|
// running it again still reports the plugin as missing.
|
|
func TestServiceSync_MissingInstallAlreadyErrorIsIdempotent(t *testing.T) {
|
|
svc, _, _ := newTestService(t)
|
|
rec := installTestPlugin(t, svc, "kandev-plugin-vanished")
|
|
if err := os.RemoveAll(rec.InstallPath); err != nil {
|
|
t.Fatalf("RemoveAll(InstallPath): %v", err)
|
|
}
|
|
|
|
if _, err := svc.Sync(context.Background()); err != nil {
|
|
t.Fatalf("first Sync() unexpected error: %v", err)
|
|
}
|
|
|
|
result, err := svc.Sync(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("second Sync() unexpected error: %v", err)
|
|
}
|
|
if len(result.Missing) != 1 || result.Missing[0] != "kandev-plugin-vanished" {
|
|
t.Fatalf("second Sync().Missing = %v, want [kandev-plugin-vanished]", result.Missing)
|
|
}
|
|
got, err := svc.Get("kandev-plugin-vanished")
|
|
if err != nil {
|
|
t.Fatalf("Get(): %v", err)
|
|
}
|
|
if got.Status != StatusError {
|
|
t.Fatalf("Status after second Sync() = %q, want %q", got.Status, StatusError)
|
|
}
|
|
}
|
|
|
|
// TestServiceSync_ConcurrentCallsAreSerialized proves Sync is guarded by a
|
|
// mutex: a second concurrent Sync call must not proceed until the first one
|
|
// has fully returned. Pauses the first call mid-Install (inside the fake
|
|
// runtime's Start), starts the competing call, proves it is blocked out,
|
|
// then releases the first call and confirms the second only completes
|
|
// afterward.
|
|
func TestServiceSync_ConcurrentCallsAreSerialized(t *testing.T) {
|
|
svc, dir, _, rt := newTestServiceWithDir(t)
|
|
dropTarball(t, dir, "kandev-plugin-first", "kandev-plugin-first-1.0.0.tar.gz")
|
|
|
|
started, release := rt.blockNextStart()
|
|
|
|
firstDone := make(chan *SyncResult, 1)
|
|
go func() {
|
|
result, err := svc.Sync(context.Background())
|
|
if err != nil {
|
|
t.Errorf("first Sync() unexpected error: %v", err)
|
|
}
|
|
firstDone <- result
|
|
}()
|
|
|
|
select {
|
|
case <-started:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("first Sync() never reached the blocked runtime Start call")
|
|
}
|
|
|
|
secondDone := make(chan *SyncResult, 1)
|
|
go func() {
|
|
result, err := svc.Sync(context.Background())
|
|
if err != nil {
|
|
t.Errorf("second Sync() unexpected error: %v", err)
|
|
}
|
|
secondDone <- result
|
|
}()
|
|
|
|
// The second call must still be blocked on the sync mutex: give it a
|
|
// bounded window to (incorrectly) complete, and fail if it does.
|
|
select {
|
|
case <-secondDone:
|
|
t.Fatal("second Sync() completed while the first was still in flight — Sync is not mutex-guarded")
|
|
case <-time.After(200 * time.Millisecond):
|
|
}
|
|
|
|
release()
|
|
|
|
var firstResult, secondResult *SyncResult
|
|
select {
|
|
case firstResult = <-firstDone:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("first Sync() never returned after release")
|
|
}
|
|
select {
|
|
case secondResult = <-secondDone:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("second Sync() never returned after the first released the mutex")
|
|
}
|
|
|
|
if len(firstResult.Installed) != 1 || firstResult.Installed[0] != "kandev-plugin-first" {
|
|
t.Fatalf("first Sync().Installed = %v, want [kandev-plugin-first]", firstResult.Installed)
|
|
}
|
|
// The tarball is already gone by the time the second call scans, so it
|
|
// must not be double-installed.
|
|
if len(secondResult.Installed) != 0 {
|
|
t.Fatalf("second Sync().Installed = %v, want none (tarball already consumed)", secondResult.Installed)
|
|
}
|
|
}
|