Files
kandev/apps/backend/internal/plugins/manifest/validate.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

236 lines
8.1 KiB
Go

package manifest
import (
"errors"
"fmt"
"path"
"regexp"
"strings"
)
// idPattern matches the required plugin id shape: lowercase alphanumerics,
// dots, underscores, and hyphens, starting with a lowercase alphanumeric.
var idPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9._-]*$`)
// supportedAPIVersion is the only api_version this kandev build accepts.
const supportedAPIVersion = 1
// validCategories are the allowed values for Manifest.Categories entries.
var validCategories = map[string]bool{
"connector": true,
"automation": true,
"tools": true,
"analytics": true,
}
// validUISurfaces are the allowed values for UIPage.Surface.
var validUISurfaces = map[string]bool{
"settings": true,
"task-panel": true,
"main-nav": true,
}
// Validate checks the manifest against the plugin registration rules
// described in docs/specs/plugins/spec.md. It returns nil if the manifest
// is well-formed, or a joined error describing every violation found.
func (m *Manifest) Validate() error {
var errs []error
errs = append(errs, m.validateIdentity()...)
errs = append(errs, m.validateVersion()...)
errs = append(errs, m.validateRuntimeType()...)
if m.IsManaged() {
errs = append(errs, m.validateManagedRuntime()...)
} else {
errs = append(errs, m.validateEndpoints()...)
}
errs = append(errs, m.validateCategories()...)
errs = append(errs, m.validateUIPages()...)
errs = append(errs, m.validateUIBundle()...)
errs = append(errs, m.validateTools()...)
errs = append(errs, m.validateWebhooks()...)
return errors.Join(errs...)
}
// dotConfigSuffix mirrors store.dotConfigSuffix: an id ending in ".config"
// would make its "<id>.yml" record filename collide with FSStore's
// "<id>.config.yml" operator-config naming convention (store.isRecordFile),
// silently hiding the record from FSStore.List(). Rejected here too so
// registration fails fast instead of relying solely on the store-level
// guard.
const dotConfigSuffix = ".config"
// validateIdentity checks id pattern and api_version.
func (m *Manifest) validateIdentity() []error {
var errs []error
if !idPattern.MatchString(m.ID) {
errs = append(errs, fmt.Errorf("invalid plugin id %q: must match %s", m.ID, idPattern.String()))
} else if strings.HasSuffix(m.ID, dotConfigSuffix) {
errs = append(errs, fmt.Errorf("invalid plugin id %q: must not end in %q", m.ID, dotConfigSuffix))
}
if m.APIVersion != supportedAPIVersion {
errs = append(errs, fmt.Errorf("unsupported api_version %d: only %d is supported", m.APIVersion, supportedAPIVersion))
}
return errs
}
// validateVersion checks that version is a non-empty, path-safe single path
// segment: it is used as a filesystem directory name both at install time
// (pkgtar.extractPackage joins destRoot/<id>/<version>) and to resolve an
// already-installed plugin's data on disk, so an empty, "."/".."-only, or
// separator-containing value would either produce a confusing deep failure
// (securejoin does reject traversal, but only after a misleading top-level
// error) or silently collide with another version's directory.
func (m *Manifest) validateVersion() []error {
v := m.Version
if v == "" {
return []error{errors.New("version must not be empty")}
}
if strings.TrimSpace(v) != v || strings.ContainsAny(v, " \t\n\r") {
return []error{fmt.Errorf("version %q must not contain whitespace", v)}
}
if strings.ContainsAny(v, "/\\") {
return []error{fmt.Errorf("version %q must be a single path segment (no \"/\" or \"\\\")", v)}
}
if v == "." || v == ".." {
return []error{fmt.Errorf("version %q must not be %q or %q", v, ".", "..")}
}
return nil
}
// validateRuntimeType checks that runtime.type, when set, is the only
// currently supported value ("binary"). An empty runtime.type is valid: it
// means the manifest is legacy-remote (base_url/endpoints).
func (m *Manifest) validateRuntimeType() []error {
if m.Runtime.Type != "" && m.Runtime.Type != runtimeTypeBinary {
return []error{fmt.Errorf("runtime.type %q is invalid: only %q is supported", m.Runtime.Type, runtimeTypeBinary)}
}
return nil
}
// validateManagedRuntime checks the rules that apply to a runtime-managed
// manifest (runtime.type: binary): at least one executables entry, every
// entry a clean package-relative path, and base_url/endpoints absent since
// a managed plugin is spawned by kandev rather than registered remotely.
func (m *Manifest) validateManagedRuntime() []error {
var errs []error
if len(m.Runtime.Executables) == 0 {
errs = append(errs, errors.New("runtime.executables must declare at least one entry when runtime.type is \"binary\""))
}
for platformKey, execPath := range m.Runtime.Executables {
if err := validateRelativePackagePath(execPath); err != nil {
errs = append(errs, fmt.Errorf("runtime.executables[%q]: %w", platformKey, err))
}
}
if m.BaseURL != "" {
errs = append(errs, errors.New("base_url must be empty for a runtime-managed plugin (runtime.type: binary)"))
}
if m.Endpoints != (Endpoints{}) {
errs = append(errs, errors.New("endpoints must be empty for a runtime-managed plugin (runtime.type: binary)"))
}
return errs
}
// validateRelativePackagePath checks that p is a non-empty, clean,
// package-relative path: no leading "/" and no ".." segment.
func validateRelativePackagePath(p string) error {
if p == "" {
return errors.New("path must not be empty")
}
if path.IsAbs(p) {
return fmt.Errorf("path %q must be relative", p)
}
cleaned := path.Clean(p)
if cleaned != p || cleaned == ".." || strings.HasPrefix(cleaned, "../") {
return fmt.Errorf("path %q must be a clean relative path with no \"..\" segments", p)
}
return nil
}
// validateEndpoints checks base_url and the required endpoint paths.
func (m *Manifest) validateEndpoints() []error {
var errs []error
if m.BaseURL == "" {
errs = append(errs, errors.New("base_url is required"))
}
if m.Endpoints.Health == "" {
errs = append(errs, errors.New("endpoints.health is required"))
}
if m.Endpoints.Events == "" {
errs = append(errs, errors.New("endpoints.events is required"))
}
if m.Endpoints.Tools == "" {
errs = append(errs, errors.New("endpoints.tools is required"))
}
if m.Endpoints.Webhooks == "" {
errs = append(errs, errors.New("endpoints.webhooks is required"))
}
return errs
}
// validateCategories checks each category against the known enum.
func (m *Manifest) validateCategories() []error {
var errs []error
for _, c := range m.Categories {
if !validCategories[c] {
errs = append(errs, fmt.Errorf("unknown category %q", c))
}
}
return errs
}
// validateUIPages checks each declared UI page's surface against the known
// enum (settings | task-panel | main-nav).
func (m *Manifest) validateUIPages() []error {
var errs []error
for _, p := range m.UI.Pages {
if !validUISurfaces[p.Surface] {
errs = append(errs, fmt.Errorf("unknown ui surface %q for page %q", p.Surface, p.Key))
}
}
return errs
}
// validateUIBundle checks that ui.bundle, if set, and every ui.styles entry
// are root-relative paths (start with "/"). ui.pages remain optional/valid
// on their own; a bundle-only manifest (no pages) is valid.
func (m *Manifest) validateUIBundle() []error {
var errs []error
if m.UI.Bundle != "" && !strings.HasPrefix(m.UI.Bundle, "/") {
errs = append(errs, fmt.Errorf("ui.bundle %q must be a root-relative path (start with \"/\")", m.UI.Bundle))
}
for _, style := range m.UI.Styles {
if !strings.HasPrefix(style, "/") {
errs = append(errs, fmt.Errorf("ui.styles entry %q must be a root-relative path (start with \"/\")", style))
}
}
return errs
}
// validateTools checks for duplicate tool names.
func (m *Manifest) validateTools() []error {
seen := make(map[string]bool, len(m.Tools))
var errs []error
for _, tool := range m.Tools {
if seen[tool.Name] {
errs = append(errs, fmt.Errorf("duplicate tool name %q", tool.Name))
continue
}
seen[tool.Name] = true
}
return errs
}
// validateWebhooks checks for duplicate webhook keys.
func (m *Manifest) validateWebhooks() []error {
seen := make(map[string]bool, len(m.Webhooks))
var errs []error
for _, wh := range m.Webhooks {
if seen[wh.Key] {
errs = append(errs, fmt.Errorf("duplicate webhook key %q", wh.Key))
continue
}
seen[wh.Key] = true
}
return errs
}