Files
千川白浪 d55241cba2 feat(plugin): catalog the Host API, open secret typing and extend the SDK
Give the Sidecar Host API one authoritative catalog in pkg/pluginproto: every
host/* method's status, required permissions, handshake feature and request and
result shapes. The host, pkg/pluginsdk and plugins share those types, so a field
can no longer exist on one side only, and permissions come from the catalog
instead of from each handler. Tests gate that every served method is
catalogued, every stable catalogued method is served, and every catalogued
permission is one the host recognizes.

Open the interface up instead of adding consent gates. Typing a secret setting
is no longer reserved for reims.agent: any plugin that holds qmp:write and that
the user authorized for the VM can have the host type the plugin's own setting,
which the plugin never reads back. Restoring a snapshot works on any authorized
VM, a plugin names its own clones, a held key may carry modifiers, and a
screenshot width outside the supported range is brought into range rather than
refused. Deleting a VM stays limited to the temporary VMs a plugin created.

Record the keys and pointer buttons each plugin holds per VM and lift them when
the plugin's process exits, its scope is disposed, the VM stops or it stops
calling, so a guest never stays stuck with a modifier down. Requests from
plugins that declare pluginApi 1.3 or newer are decoded strictly and answer an
undefined field with JSON-RPC -32602, while older plugins keep tolerant
decoding.

Add typed VM and console methods to pkg/pluginsdk so a plugin no longer needs
its own JSON-RPC client. A method the contract does not catalog, or one the
host did not advertise in the handshake, is refused before it is sent, and the
host's JSON-RPC error code reaches the caller.
2026-12-05 01:43:28 -07:00

258 lines
9.1 KiB
Go

// Package pluginproto is the single source of truth for the Sidecar Host API:
// which host/* methods exist, which permissions each one needs, and the request
// and result shapes both sides decode. It imports only the standard library, so
// the host, pkg/pluginsdk and third-party plugins share one copy of the wire
// contract instead of each declaring its own anonymous structs.
//
// A method that is not in HostMethods is not part of the contract: the host
// answers it with JSON-RPC -32601, and a plugin must not call it.
package pluginproto
import (
"bytes"
"encoding/json"
"errors"
"io"
"sort"
"strings"
)
// Status marks whether the host acts on a method.
type Status string
const (
// StatusStable methods are implemented by the host and covered by the
// compatibility promise.
StatusStable Status = "stable"
// StatusExperimental methods are published so plugins can feature-detect
// them, but carry no compatibility promise.
StatusExperimental Status = "experimental"
)
// OriginPermission marks a method whose authorization depends on the request
// URL: the caller must declare net:<origin> for the URL it passes.
const OriginPermission = "net:<origin>"
// StrictParamsVersion is the pluginApi version from which a plugin's requests
// are decoded strictly. Older plugins keep tolerant decoding so a released
// plugin that sends a field this host has not implemented yet keeps working.
const StrictParamsVersion = "1.3.0"
// MethodSpec describes one host/* method.
type MethodSpec struct {
Status Status
// Permissions lists every permission the caller must hold. An empty list
// means the method needs none beyond being a registered plugin.
Permissions []string
// Feature is the handshake feature that advertises the method. An empty
// value derives one from the method name.
Feature string
// StrictParams makes the host reject request fields the contract does not
// define (JSON-RPC -32602) instead of ignoring them.
StrictParams bool
// Summary is one line for `plugin doctor` and the generated reference.
Summary string
}
// HostMethods is the authoritative catalog of reverse Host API methods.
var HostMethods = map[string]MethodSpec{
"host/notify": {
Status: StatusStable, Permissions: []string{"ui:notify"},
Summary: "Show a host notification.",
},
"host/audit": {
Status: StatusStable,
Summary: "Append one line to the plugin's audit log (rate limited).",
},
"host/vm.spawn": {
Status: StatusStable, Permissions: []string{"process:spawn"}, Feature: "host.vm.spawn.ref",
Summary: "Start a process for a VM and put it into that VM's job object.",
},
"host/vm.stop": {
Status: StatusStable, Permissions: []string{"vm:control"},
Summary: "Terminate a process this plugin started, by pid or by VM ref.",
},
"host/vm.isRunning": {
Status: StatusStable,
Summary: "Report whether a process this plugin started is still alive.",
},
"host/vm.reportState": {
Status: StatusStable, Permissions: []string{"driver:vm"}, Feature: "host.vm.reportState",
Summary: "Push the state of a VM the calling driver owns.",
},
"host/download.start": {
Status: StatusStable, Permissions: []string{OriginPermission}, Feature: "host.download",
Summary: "Download a checksum-verified file in the background.",
},
"host/download.cancel": {
Status: StatusStable, Feature: "host.download",
Summary: "Cancel one of this plugin's downloads.",
},
"host/secret.materialize": {
Status: StatusStable, Permissions: []string{"driver:vm", "fs:vm_dir"}, Feature: "host.secret.materialize",
Summary: "Have the host write one secret setting into a VM folder without revealing it.",
},
"host/secret.discard": {
Status: StatusStable, Permissions: []string{"driver:vm"}, Feature: "host.secret.materialize",
Summary: "Delete the secret files the host materialized for a VM.",
},
"host/elevate.request": {
Status: StatusStable, Permissions: []string{"driver:vm"}, Feature: "host.elevate.request",
Summary: "Ask the user to confirm one whitelisted Windows elevation action.",
},
"host/vm.list": {
Status: StatusStable, Permissions: []string{"vm:read"}, StrictParams: true,
Summary: "List the VMs this plugin may see.",
},
"host/vm.get": {
Status: StatusStable, Permissions: []string{"vm:read"}, StrictParams: true,
Summary: "Read one VM's details.",
},
"host/vm.start": {
Status: StatusStable, Permissions: []string{"vm:control"}, StrictParams: true,
Summary: "Start a VM.",
},
"host/vm.shutdown": {
Status: StatusStable, Permissions: []string{"vm:control"}, StrictParams: true,
Summary: "Shut a VM down, or stop it outright when force is set.",
},
"host/vm.suspend": {
Status: StatusStable, Permissions: []string{"vm:control"}, StrictParams: true,
Summary: "Suspend a VM.",
},
"host/vm.resume": {
Status: StatusStable, Permissions: []string{"vm:control"}, StrictParams: true,
Summary: "Resume a VM.",
},
"host/vm.clone": {
Status: StatusStable, Permissions: []string{"vm:control"}, StrictParams: true,
Summary: "Clone a VM into a temporary VM this plugin owns.",
},
"host/vm.delete": {
Status: StatusStable, Permissions: []string{"vm:control"}, StrictParams: true,
Summary: "Delete a temporary VM this plugin owns.",
},
"host/vm.snapshot.list": {
Status: StatusStable, Permissions: []string{"vm:read"}, StrictParams: true,
Summary: "List a VM's snapshots.",
},
"host/vm.snapshot.create": {
Status: StatusStable, Permissions: []string{"vm:control"}, StrictParams: true,
Summary: "Create a snapshot.",
},
"host/vm.snapshot.restore": {
Status: StatusStable, Permissions: []string{"vm:control"}, StrictParams: true,
Summary: "Restore a snapshot of a VM the user authorized this plugin to access.",
},
"host/console.screenshot": {
Status: StatusStable, Permissions: []string{"vm:screen"}, StrictParams: true,
Summary: "Capture the guest screen, optionally one region of it, as JPEG.",
},
"host/console.pointer": {
Status: StatusStable, Permissions: []string{"qmp:write"}, StrictParams: true,
Summary: "Move the pointer, press or release a button, click or scroll.",
},
"host/console.key": {
Status: StatusStable, Permissions: []string{"qmp:write"}, StrictParams: true,
Summary: "Press a key, or hold and release it with down.",
},
"host/console.type": {
Status: StatusStable, Permissions: []string{"qmp:write"}, StrictParams: true,
Summary: "Type text as US-layout key strokes.",
},
"host/console.typeSecret": {
Status: StatusStable, Permissions: []string{"qmp:write"}, StrictParams: true,
Summary: "Type one of this plugin's own secret settings without revealing it.",
},
"host/console.release": {
Status: StatusStable, Permissions: []string{"qmp:write"}, StrictParams: true,
Summary: "Release the keys and pointer buttons this plugin still holds.",
},
}
// Lookup returns the spec of one method.
func Lookup(method string) (MethodSpec, bool) {
spec, ok := HostMethods[method]
return spec, ok
}
// Methods lists every catalogued method, sorted.
func Methods() []string {
out := make([]string, 0, len(HostMethods))
for method := range HostMethods {
out = append(out, method)
}
sort.Strings(out)
return out
}
// Feature returns the handshake feature that advertises method.
func Feature(method string) string {
if spec, ok := HostMethods[method]; ok && spec.Feature != "" {
return spec.Feature
}
return "host." + strings.ReplaceAll(strings.TrimPrefix(method, "host/"), "/", ".")
}
// Features lists the handshake features of every stable method, sorted and
// without duplicates. Plugins feature-detect with these instead of comparing
// version numbers.
func Features() []string {
seen := map[string]bool{}
out := []string{}
for method, spec := range HostMethods {
if spec.Status != StatusStable {
continue
}
feature := Feature(method)
if seen[feature] {
continue
}
seen[feature] = true
out = append(out, feature)
}
sort.Strings(out)
return out
}
// InvalidParamsError reports a request the contract does not accept. The host
// answers it with JSON-RPC -32602.
type InvalidParamsError struct {
Method string
Reason string
}
func (e *InvalidParamsError) Error() string {
if e.Method == "" {
return e.Reason
}
return e.Method + ": " + e.Reason
}
// DecodeParams decodes one request into out. An absent or null params member
// leaves out at its zero value. With strict set, a field the contract does not
// define, or trailing data after the request object, is an error instead of
// being dropped silently.
func DecodeParams[T any](method string, raw json.RawMessage, strict bool) (T, error) {
var out T
trimmed := bytes.TrimSpace(raw)
if len(trimmed) == 0 || bytes.Equal(trimmed, []byte("null")) {
return out, nil
}
if !strict {
if err := json.Unmarshal(trimmed, &out); err != nil {
return out, &InvalidParamsError{Method: method, Reason: err.Error()}
}
return out, nil
}
dec := json.NewDecoder(bytes.NewReader(trimmed))
dec.DisallowUnknownFields()
if err := dec.Decode(&out); err != nil {
return out, &InvalidParamsError{Method: method, Reason: err.Error()}
}
if _, err := dec.Token(); !errors.Is(err, io.EOF) {
return out, &InvalidParamsError{Method: method, Reason: "unexpected data after the request object"}
}
return out, nil
}