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.
102 lines
3.7 KiB
Go
102 lines
3.7 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
"github.com/Nwflower/reims-go/pkg/pluginproto"
|
|
)
|
|
|
|
// hostMethodHandler serves one host/* request. The caller has already been
|
|
// authorized for the method.
|
|
type hostMethodHandler func(ctx context.Context, raw json.RawMessage) (any, error)
|
|
|
|
// hostMethodHost is what serving a host/* method needs from the plugin kernel.
|
|
// *plugin.Manager satisfies it; a test can supply a broker and a registry.
|
|
type hostMethodHost interface {
|
|
Broker() *plugin.Broker
|
|
Registry() *plugin.Registry
|
|
}
|
|
|
|
// authorizeHostMethod checks every permission the contract catalog lists for
|
|
// method. A method whose authorization depends on the request URL carries
|
|
// pluginproto.OriginPermission and is authorized by its own handler, which is
|
|
// the only place that sees the URL.
|
|
func authorizeHostMethod(host hostMethodHost, pluginID, method string) error {
|
|
spec, ok := pluginproto.Lookup(method)
|
|
if !ok {
|
|
return &plugin.RPCError{Code: plugin.RPCMethodNotFound, Message: "method not found: " + method}
|
|
}
|
|
if host == nil || host.Broker() == nil {
|
|
return errors.New("plugin manager is unavailable")
|
|
}
|
|
for _, perm := range spec.Permissions {
|
|
if perm == pluginproto.OriginPermission {
|
|
continue
|
|
}
|
|
if err := host.Broker().Authorize(pluginID, perm); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// strictHostParams reports whether requests of pluginID to method are decoded
|
|
// strictly. Only a plugin that declares pluginApi 1.3 or newer opts in, so a
|
|
// released plugin sending a field this host has not implemented keeps working
|
|
// until it is updated, while a new plugin learns about a typo at once.
|
|
func strictHostParams(host hostMethodHost, pluginID, method string) bool {
|
|
spec, ok := pluginproto.Lookup(method)
|
|
if !ok || !spec.StrictParams || host == nil {
|
|
return false
|
|
}
|
|
manifest, found := host.Registry().ManifestOf(pluginID)
|
|
if !found || manifest.Engines.PluginAPI == "" {
|
|
return false
|
|
}
|
|
return plugin.RangeRequiresAtLeast(manifest.Engines.PluginAPI, pluginproto.StrictParamsVersion)
|
|
}
|
|
|
|
// decodeHostParams decodes one host request through the shared contract types
|
|
// and answers a request the contract rejects with JSON-RPC -32602.
|
|
func decodeHostParams[T any](host hostMethodHost, pluginID, method string, raw json.RawMessage) (T, error) {
|
|
out, err := pluginproto.DecodeParams[T](method, raw, strictHostParams(host, pluginID, method))
|
|
if err != nil {
|
|
var invalid *pluginproto.InvalidParamsError
|
|
if errors.As(err, &invalid) {
|
|
return out, &plugin.RPCError{Code: plugin.RPCInvalidParams, Message: invalid.Error()}
|
|
}
|
|
return out, err
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// hostMethod wraps one host/* method: it authorizes from the contract catalog,
|
|
// audits the call under the method name, and hands the request to run. No
|
|
// handler states its own permission, so a permission cannot drift from the
|
|
// method it guards.
|
|
func (a *App) hostMethod(host hostMethodHost, pluginID, method string, run hostMethodHandler) plugin.RPCRequestHandler {
|
|
return func(ctx context.Context, raw json.RawMessage) (any, error) {
|
|
if err := authorizeHostMethod(host, pluginID, method); err != nil {
|
|
return nil, err
|
|
}
|
|
result, err := run(ctx, raw)
|
|
broker := host.Broker()
|
|
if err != nil {
|
|
broker.Audit(pluginID, "HOST_API_ERROR", method)
|
|
return nil, err
|
|
}
|
|
broker.Audit(pluginID, "HOST_API", method)
|
|
return result, nil
|
|
}
|
|
}
|
|
|
|
// hostVMMethod wraps a handler that needs the manager and the calling plugin.
|
|
func (a *App) hostVMMethod(mgr *plugin.Manager, pluginID, method string, run computerUseVMHandler) plugin.RPCRequestHandler {
|
|
return a.hostMethod(mgr, pluginID, method, func(ctx context.Context, raw json.RawMessage) (any, error) {
|
|
return run(ctx, raw, mgr, pluginID)
|
|
})
|
|
}
|