diff --git a/docs/subsystems/plugin-runtime.md b/docs/subsystems/plugin-runtime.md
index a7a746c63..81aca50f7 100644
--- a/docs/subsystems/plugin-runtime.md
+++ b/docs/subsystems/plugin-runtime.md
@@ -339,6 +339,18 @@ starts a Bun host.
`TestServiceSelf` and `TestPluginPageServiceProxied` check the policies, and
`TestPluginPageLoadsItsFilesInTheWindow` loads a page through
`ownWindow` in a real WKWebView; Playwright, on http, can't see it.
+ Behind a proxy that signs readers in by a SameSite=Lax cookie (Cloudflare
+ Access) the sandboxed frame's files carried none and were redirected to
+ the sign-in (xuanwo on X, Day card). In `magpie web` the page's HTML comes
+ as one document (`inlinePage`, pageinline.go): its classic scripts (the
+ bridge included) and url()-free stylesheets are written in, the scripts
+ allowed by hash, and a script the page adds later is fetched by the window
+ (`asset` in pluginpages.js, under the page's folder only) and loaded from
+ a `data:` address (`data:` in script-src; WebKit finds no blob made in a
+ sandboxed frame). Left as they were: modules, stylesheets with url(),
+ images and fonts the page loads itself. index.html's manifest link asks
+ with `crossorigin="use-credentials"`. `TestPluginPageWebComesAsOneDocument`,
+ `plugin-page-cookie-proxy.test.cjs`.
- **Slots.** A page names a slot with `
` in its markup, or app.js draws one with
`magpie.slot(name, context)` ([`pluginpages.js`](../../internal/gui/assets/pluginpages.js)).
diff --git a/internal/gui/assets/index.html b/internal/gui/assets/index.html
index 9732618e2..7bea8cb29 100644
--- a/internal/gui/assets/index.html
+++ b/internal/gui/assets/index.html
@@ -11,7 +11,7 @@
the page's cookie.
The theme colours are the window's in standalone alone, so a browser's
tab and the app's own windows look as they did -->
-
+
diff --git a/internal/gui/assets/pluginpages.js b/internal/gui/assets/pluginpages.js
index 692c4b7c0..4e8fa9045 100644
--- a/internal/gui/assets/pluginpages.js
+++ b/internal/gui/assets/pluginpages.js
@@ -141,6 +141,17 @@
if (!e.reads.includes(name)) throw new Error(t("{name} doesn't ask to read {what}", { name: titled(e), what: name }));
return api("plugin-pages/read", { spec: e.spec, read: name, params: m.params?.params || {} });
}
+ case "asset": {
+ // a file of the page's own folder, for a page that cannot ask for
+ // it itself (pageinline.go): this request carries the window's
+ // cookie
+ const dir = new URL(e.url.replace(/[^/]*$/, ""), location.href).href;
+ const u = new URL(String(m.params?.url || ""), location.href);
+ if (!u.href.startsWith(dir) || u.hash) throw new Error(t("No such call: {what}", { what: "asset" }));
+ const res = await fetch(u.href, { credentials: "same-origin" });
+ if (!res.ok) throw new Error(String(res.status));
+ return { type: res.headers.get("content-type") || "", data: await res.arrayBuffer() };
+ }
case "image.copy":
case "image.save":
return import("./pageshare.js").then(() => pageShare(e.spec, m.method === "image.save", m.params));
diff --git a/internal/gui/pagebridge.js b/internal/gui/pagebridge.js
index 49b73ade3..09314e678 100644
--- a/internal/gui/pagebridge.js
+++ b/internal/gui/pagebridge.js
@@ -65,6 +65,36 @@
parent.postMessage({ magpie: 1, id, method, params }, "*");
});
}
+ // A page that came as one document (pageinline.go) can't ask for its
+ // other files itself: a proxy that signs readers in by cookie turns away
+ // a sandboxed frame's requests. A script it adds is fetched by the
+ // window, which has the cookie, and loaded from a data: address.
+ if (globalThis.__magpieInlined) {
+ const put = (node, ref) => {
+ let abs;
+ try { abs = new URL(ref, location.href).href; } catch { return false; }
+ call("asset", { url: abs }).then(({ type, data }) => {
+ // a data: address, not a blob's: WebKit finds no blob made in a
+ // sandboxed frame
+ let bin = "";
+ const bytes = new Uint8Array(data);
+ for (let i = 0; i < bytes.length; i += 8192) bin += String.fromCharCode(...bytes.subarray(i, i + 8192));
+ node.setAttribute("src", "data:text/javascript;base64," + btoa(bin));
+ node.__magpiePut(node);
+ }, () => node.__magpiePut(node));
+ return true;
+ };
+ const add = (orig) => function (node, ...rest) {
+ if (node instanceof HTMLScriptElement && node.hasAttribute("src") && !node.__magpieDone && !/^(?:[a-z][a-z0-9+.-]*:|\/\/)/i.test(node.getAttribute("src"))) {
+ const ref = node.getAttribute("src");
+ node.__magpiePut = (n) => { n.__magpieDone = true; orig.call(this, n, ...rest); };
+ if (put(node, ref)) return node;
+ }
+ return orig.call(this, node, ...rest);
+ };
+ Node.prototype.appendChild = add(Node.prototype.appendChild);
+ Node.prototype.insertBefore = add(Node.prototype.insertBefore);
+ }
function paint(theme) {
const root = document.documentElement;
root.dataset.theme = theme.dark ? "dark" : "light";
diff --git a/internal/gui/pageinline.go b/internal/gui/pageinline.go
new file mode 100644
index 000000000..dac44038b
--- /dev/null
+++ b/internal/gui/pageinline.go
@@ -0,0 +1,134 @@
+package gui
+
+import (
+ "crypto/sha256"
+ "encoding/base64"
+ "net/url"
+ "os"
+ "regexp"
+ "strings"
+
+ "github.com/yetone/magpie/internal/plugin"
+)
+
+// A plugin's page is framed with sandbox="allow-scripts", so its origin is
+// opaque and every file it loads is a cross-site request that carries no
+// cookie of SameSite=Lax (Cloudflare Access's CF_Authorization is one).
+// Behind a proxy that signs readers in by cookie, the page's HTML came
+// (the frame's own navigation is same-site) but its stylesheet and
+// scripts were redirected to the sign-in page, and the page said it
+// "didn't answer" (xuanwo on X, Day card).
+//
+// A page served to a browser therefore comes as one document: its own
+// classic scripts and stylesheets are written into the HTML, the scripts
+// allowed by their hashes in the page's policy. Loads the page makes
+// later (a script it adds) ask the window for the file (pagebridge.js's
+// asset call), which carries the cookie.
+
+var (
+ inlineScript = regexp.MustCompile(`(?is)`)
+ inlineLink = regexp.MustCompile(`(?is)]*>`)
+ attrRel = regexp.MustCompile(`(?is)\brel\s*=\s*["']?\s*stylesheet\b`)
+ attrHref = regexp.MustCompile(`(?is)\bhref\s*=\s*(?:"([^"]*)"|'([^']*)')`)
+ attrType = regexp.MustCompile(`(?is)\btype\s*=\s*["']?\s*([^"'\s>]*)`)
+ attrDefer = regexp.MustCompile(`(?is)\bdefer\b`)
+ bodyClose = regexp.MustCompile(`(?i)