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).
72 lines
3.0 KiB
JavaScript
72 lines
3.0 KiB
JavaScript
// The page's side of a plugin page's messages to magpie's window
|
|
// (docs/subsystems/plugin-runtime.md, P4). A plugin's page loads it with
|
|
// <script src="_magpie/bridge.js"></script>; it is served beside every
|
|
// page's own files.
|
|
//
|
|
// await magpie.ready the window's theme, once it has answered
|
|
// magpie.theme { dark, lang, tokens: { bg, fg, … } }
|
|
// magpie.onTheme(fn) fn(theme) on each change of it
|
|
// await magpie.options.get() the plugin's own options (plugins.json)
|
|
// await magpie.options.set(o) sets them, and answers with them
|
|
// await magpie.read(name, p) a read its package.json's
|
|
// magpie.permissions declares
|
|
//
|
|
// The page can't reach the network or magpie's API itself: everything goes
|
|
// through the window, which answers only what the package declared. An
|
|
// error the page doesn't catch shows the window's "This page stopped
|
|
// working" in its place.
|
|
(() => {
|
|
const waiting = new Map();
|
|
let next = 0;
|
|
const themed = [];
|
|
function call(method, params) {
|
|
const id = ++next;
|
|
return new Promise((resolve, reject) => {
|
|
waiting.set(id, { resolve, reject });
|
|
parent.postMessage({ magpie: 1, id, method, params }, "*");
|
|
});
|
|
}
|
|
function paint(theme) {
|
|
const root = document.documentElement;
|
|
root.dataset.theme = theme.dark ? "dark" : "light";
|
|
// the frame's canvas matches the window's, not a white sheet in dark
|
|
root.style.colorScheme = theme.dark ? "dark" : "light";
|
|
root.lang = theme.lang || "en";
|
|
for (const [k, v] of Object.entries(theme.tokens || {})) root.style.setProperty("--magpie-" + k, v);
|
|
}
|
|
addEventListener("message", (e) => {
|
|
if (e.source !== parent) return;
|
|
const m = e.data;
|
|
if (!m || m.magpie !== 1) return;
|
|
if (m.event === "theme") {
|
|
api.theme = m.data;
|
|
paint(m.data);
|
|
for (const fn of themed) { try { fn(m.data); } catch (err) { report(err); } }
|
|
return;
|
|
}
|
|
const w = waiting.get(m.id);
|
|
if (!w) return;
|
|
waiting.delete(m.id);
|
|
if (m.error) w.reject(new Error(m.error)); else w.resolve(m.result);
|
|
});
|
|
function report(err) {
|
|
parent.postMessage({ magpie: 1, method: "error", params: { message: String(err?.message || err) } }, "*");
|
|
}
|
|
// a script's error reaches a sandboxed page only as "Script error." (its
|
|
// files are another origin's to it); the window then says "The page hit
|
|
// an error" in the reader's language, and the console has the rest
|
|
addEventListener("error", (e) => report(e.error || (e.message === "Script error." ? "" : e.message)));
|
|
addEventListener("unhandledrejection", (e) => report(e.reason));
|
|
const api = {
|
|
theme: null,
|
|
onTheme(fn) { themed.push(fn); },
|
|
options: {
|
|
get: () => call("options.get"),
|
|
set: (options) => call("options.set", { options }),
|
|
},
|
|
read: (name, params) => call("read", { name, params }),
|
|
};
|
|
api.ready = call("hello").then((theme) => { api.theme = theme; paint(theme); return theme; });
|
|
window.magpie = api;
|
|
})();
|