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 (h *HostAPIService) 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 (h *HostAPIService) hostVMMethod(mgr *plugin.Manager, pluginID, method string, run computerUseVMHandler) plugin.RPCRequestHandler { return h.hostMethod(mgr, pluginID, method, func(ctx context.Context, raw json.RawMessage) (any, error) { return run(ctx, raw, mgr, pluginID) }) }