Files
magpie/internal/plugin/agent.go
T
yetone 64b0f3a201 plugins: a community plugin's own page in magpie's window, shut in a sandboxed frame (P4)
A plugin's package.json may declare magpie.page {title, file, icon} and
magpie.permissions.reads. While the plugin is on, its page gets a tab in a
"Plugins" group after the built-in pages, drawn in an iframe sandboxed to
allow-scripts only (an opaque origin: no cookie, no storage, no parent).
Its files come from /_magpie/plugin/<token>/ (HMAC of the spec with a
per-process secret) under a CSP with connect-src 'none' and
frame-ancestors 'self'; a path that leaves the page's folder, a symlink
out, or an off plugin's token is refused. The window's postMessage bridge
answers only that frame (event.source checked) with theme and language,
the plugin's own options.get/set (throttled), and the reads it declared;
any other read is refused in the window and again by the server, and no
read carries a secret. /api refuses Origin: null and frame/iframe
fetch-dest requests (noFramedAPI), so the frame never reaches the GUI's
API even with the cookie. A page that throws or never says hello shows
the kernel's broken-page state with Try again. The Plugins page shows the
page and what it reads before the plugin is turned on.

Docs: gui-shell.md, plugin-runtime.md (P4 section), site docs en/zh/ja
with an example plugin; i18n zh, zh-TW, ja, de; fixture in
internal/gui/testdata/page-plugin.

Run (worktree at base 4df1ba0c6, -p 2, one package at a time):
- go test -count=1 -timeout 45m: internal/gui ok, internal/plugin ok,
  internal/pluginrt ok, plugins/... (budget groups middleware otel redact
  rotation search) ok.
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows clean.
- Playwright (chromium + webkit, 5 languages, narrow widths): plugin-page 4/4,
  plugin-kernel 4/4, builtins 4/4, i18n-keys, gui-de, gui-ja, gui-zh-tw 3/3,
  plugin-middleware 24/24, plugin-own-icon 22/22, plugin-options 22/22,
  plugin-local 8/8, plugin-agent 22/22, click-scroll 22/22.

Broken by hand, each went red: CSP without connect-src; apiFramed always
false; the server's read check removed; the noFramedAPI wrap removed from
api.go (red through the real Handler); inside() letting every path
through; CORP same-origin re-added; in JS the client read check removed,
the ev.source check removed, and allow-same-origin added to the sandbox.
Without the Array.isArray guard on /api/plugin-pages, plugin-kernel,
builtins and plugin-middleware/own-icon/options went red (their mocks
answer a non-list) - that was found by the run, then fixed.

Real use: built magpie, `magpie web` on 127.0.0.1:3472 (gateway 3471) in a
sandbox HOME with the example plugin added. curl: the page file loads
with the CSP and no cookie, traversal and a bad token are 404, /api with
Origin: null or Sec-Fetch-Dest: iframe is 403 even with the cookie, an
undeclared read 403, a declared one 200. Chromium and WebKit at 1100 and
420: the page draws its declared read, the probe's fetch/absolute
fetch/top access/undeclared read are all refused, an <img> of /api is
refused, no page errors. Real use found that a
Cross-Origin-Resource-Policy: same-origin header blocked the frame's own
files (its origin is opaque); it is gone and a test guards it.

Not tried: the desktop (Wails) window. Market cards of plugins not yet
installed don't show a page's reads. The Playwright suite mirrors the
CSP in a mock route rather than the Go server (the Go tests and real use
cover the server).
2026-10-15 22:36:59 +08:00

56 lines
1.6 KiB
Go

package plugin
import (
"encoding/json"
"os"
"path/filepath"
"strings"
)
// Agent is the agent a plugin at target adds magpie's wiring for: the
// file its package.json's magpie.agent names, or the file itself for a
// path to a *.agent.js (or .mjs). only is whether it is nothing else: no
// OpenCode plugin for the host to load (no main, no exports).
func Agent(target string) (file string, only bool) {
st, err := os.Stat(target)
if err != nil {
return "", false
}
if !st.IsDir() {
base := strings.ToLower(filepath.Base(target))
if strings.HasSuffix(base, ".agent.js") || strings.HasSuffix(base, ".agent.mjs") {
return target, true
}
return "", false
}
b, err := os.ReadFile(filepath.Join(target, "package.json"))
if err != nil {
return "", false
}
var pkg struct {
Main string `json:"main"`
Exports json.RawMessage `json:"exports"`
Magpie struct {
Agent string `json:"agent"`
} `json:"magpie"`
}
if json.Unmarshal(b, &pkg) != nil || strings.TrimSpace(pkg.Magpie.Agent) == "" {
return "", false
}
file = filepath.Join(target, filepath.FromSlash(strings.TrimSpace(pkg.Magpie.Agent)))
return file, pkg.Main == "" && len(pkg.Exports) == 0
}
// InMagpieOnly is whether everything a plugin at target has runs in
// magpie's own process, its gateway middleware, its agent and its page,
// so the host has nothing of it to load.
func InMagpieOnly(target string) bool {
mw, mwOnly := Middleware(target)
ag, agOnly := Agent(target)
pg, pgOnly := WindowPage(target)
if mw == "" && ag == "" && pg == nil {
return false
}
return (mw == "" || mwOnly) && (ag == "" || agOnly) && (pg == nil || pgOnly)
}