Files
magpie/internal/plugin/page_test.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

72 lines
2.7 KiB
Go

package plugin
import (
"os"
"path/filepath"
"slices"
"testing"
)
// A plugin's page (magpie.page) is a file inside the plugin's folder: one
// that names a path out of it, by "..", an absolute path or a symbolic
// link, is no page. Its reads are those magpie has, and a plugin that is
// only a page is nothing for the host to load, nor "not a plugin".
func TestWindowPageStaysInside(t *testing.T) {
root := t.TempDir()
outside := filepath.Join(root, "secret.html")
if err := os.WriteFile(outside, []byte("<p>secret</p>"), 0o644); err != nil {
t.Fatal(err)
}
mk := func(name, pkg string) string {
t.Helper()
d := filepath.Join(root, name)
if err := os.MkdirAll(filepath.Join(d, "page"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(d, "page", "index.html"), []byte("<p>hi</p>"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(d, "package.json"), []byte(pkg), 0o644); err != nil {
t.Fatal(err)
}
return d
}
good := mk("good", `{"name":"good","magpie":{"page":{"title":"Good","file":"page/index.html","icon":"page/index.html"},"permissions":["usage:summary","secrets:all","usage:summary"]}}`)
p, only := WindowPage(good)
if p == nil || !only || p.Title != "Good" || p.Icon != "index.html" {
t.Fatalf("good: %+v only=%v", p, only)
}
if !slices.Equal(p.Reads, []string{"usage:summary"}) {
t.Errorf("reads %v, want only the one magpie has, once", p.Reads)
}
if !InMagpieOnly(good) || notPlugin(good) != nil {
t.Errorf("a page-only plugin: InMagpieOnly %v, notPlugin %v", InMagpieOnly(good), notPlugin(good))
}
if _, only := WindowPage(mk("main", `{"name":"m","main":"index.js","magpie":{"page":{"file":"page/index.html"}}}`)); only {
t.Error("a plugin with a main is more than its page")
}
link := mk("link", `{"name":"l","magpie":{"page":{"file":"page/out.html"}}}`)
if err := os.Symlink(outside, filepath.Join(link, "page", "out.html")); err != nil {
t.Fatal(err)
}
for name, pkg := range map[string]string{
"dotdot": `{"name":"d","magpie":{"page":{"file":"../secret.html"}}}`,
"abs": `{"name":"a","magpie":{"page":{"file":"` + filepath.ToSlash(outside) + `"}}}`,
"notml": `{"name":"n","magpie":{"page":{"file":"package.json"}}}`,
"none": `{"name":"x","magpie":{"page":{"file":"page/missing.html"}}}`,
} {
if p, _ := WindowPage(mk(name, pkg)); p != nil {
t.Errorf("%s: %+v is a page", name, p)
}
}
if p, _ := WindowPage(link); p != nil {
t.Errorf("a symbolic link out of the folder: %+v is a page", p)
}
if f, ok := p.PageFile("../package.json"); ok {
t.Errorf("PageFile served %s", f)
}
if f, ok := p.PageFile("index.html"); !ok || filepath.Base(f) != "index.html" {
t.Errorf("PageFile(index.html) = %s %v", f, ok)
}
}