diff --git a/packages/layerfs/README.md b/packages/layerfs/README.md index 54ee294..fc22c69 100644 --- a/packages/layerfs/README.md +++ b/packages/layerfs/README.md @@ -177,7 +177,9 @@ serves its chat page: - **The installed page only jumps.** `page/index.html` does one thing: it reads the port from the plugin's options (`pagePort`, default 43717) and `location.replace`s its frame to - `http://127.0.0.1:/`. The install's folder is never + `http://127.0.0.1:/`, or, when magpie forwards the service + on its own address (`magpie.page.service`; a browser on another + computer, magpie in Docker), to that forward. The install's folder is never renamed, linked, or written into. The service starts when the host loads, store or no store; a user with no store gets the page's empty state and nothing written anywhere. @@ -237,6 +239,18 @@ node tools/gen-trajectory.mjs # regenerate the page's bundled sample trajector Zero runtime dependencies; the arguments are JSON Schema, like the other tools packages. MIT. +## Changes in 0.10.3 + +The page now opens in a browser on another computer (`magpie web`, +magpie in Docker). It used to send its frame to `127.0.0.1:43717`, +which there is the viewing computer, so the frame said "refused to +connect" (#1228). The package now declares `magpie.page.service` +(port 43717, moved by `pagePort`), and the page goes to the forward +magpie gives it on its own address. The service still listens on +127.0.0.1 only, and its Host and Origin checks are unchanged: the +forward sends the service's own Host and no Origin. With an older +magpie that gives no forward, the page jumps to 127.0.0.1 as before. + ## Changes in 0.10.2 The page no longer hangs on "Connecting to the local trajectory diff --git a/packages/layerfs/layerfs.test.mjs b/packages/layerfs/layerfs.test.mjs index a4c8b0d..5f07668 100644 --- a/packages/layerfs/layerfs.test.mjs +++ b/packages/layerfs/layerfs.test.mjs @@ -752,6 +752,17 @@ test("0.10.0: the service refuses cross-origin reads, foreign hosts, and non-roo } }, 30000) +test("#1228: the request magpie's page forward sends (the service's own Host, no Origin, the language query) is served", async () => { + const { call, url, port } = await simTools() + await call("provision", { project: "website" }) + await serviceData(url, (x) => !!x.projects.find((p) => p.name === "website")) + // internal/gui/pageservice.go: Host is 127.0.0.1:, the cookie is + // taken off, and a navigation from the page's frame carries no Origin + const r = await rawRequest(port, `127.0.0.1:${port}`, "/?lang=zh") + expect(r).toMatch(/^HTTP\/1\.1 200/) + expect(r).toContain('id="graph"') +}, 30000) + test("0.10.0: a commit message cannot break out of the embedded data script", async () => { const { call, url } = await simTools() const evil = "xy" diff --git a/packages/layerfs/package.json b/packages/layerfs/package.json index 5819f9c..4d16f71 100644 --- a/packages/layerfs/package.json +++ b/packages/layerfs/package.json @@ -33,6 +33,10 @@ "page": { "file": "page/index.html", "icon": "page/icon.svg", + "service": { + "port": 43717, + "option": "pagePort" + }, "title": "LayerFS trajectory", "titles": { "ja": "LayerFS の軌跡", @@ -49,5 +53,5 @@ "directory": "packages/layerfs" }, "type": "module", - "version": "0.10.2" + "version": "0.10.3" } diff --git a/packages/layerfs/page.test.mjs b/packages/layerfs/page.test.mjs index 82ffb0e..2b7e533 100644 --- a/packages/layerfs/page.test.mjs +++ b/packages/layerfs/page.test.mjs @@ -412,3 +412,29 @@ test("0.10.2: the empty state reloads itself, so the first project appears witho const empty = app.slice(app.indexOf("if (!projects.length) {"), app.indexOf("$(\"stamp\")")) expect(empty).toContain("location.reload()") }) + +// boot.js run as the page runs it, with the bridge and location stubbed: +// where does the frame go? +async function bootTarget(magpie) { + let target = null + const window = { magpie } + const location = { replace: (u) => { target = u } } + await new Function("window", "location", `return (${read("boot.js").trim().replace(/;?\s*$/, "")})`)(window, location) + return target +} + +test("#1228: with magpie's forward of the service, the frame goes there, not to 127.0.0.1", async () => { + expect(pkg.magpie.page.service).toEqual({ port: 43717, option: "pagePort" }) + expect(pkg.magpie.options.pagePort).toBe(pkg.magpie.page.service.port) + const service = "http://nas.lan:3430/_magpie/plugin/tok/_magpie/service/" + expect(await bootTarget({ ready: Promise.resolve({ lang: "zh" }), service, options: { get: async () => ({ pagePort: 43800 }) } })) + .toBe(`${service}?lang=zh`) + expect(await bootTarget({ ready: Promise.resolve({}), service })).toBe(service) +}) + +test("#1228: an older magpie without a forward, or a value that isn't a web address, keeps the jump to 127.0.0.1", async () => { + expect(await bootTarget({ ready: Promise.resolve({ lang: "ja" }), options: { get: async () => ({ pagePort: 43800 }) } })) + .toBe("http://127.0.0.1:43800/?lang=ja") + expect(await bootTarget({ ready: Promise.resolve({}), service: "javascript:alert(1)" })).toBe("http://127.0.0.1:43717/") + expect(await bootTarget(undefined)).toBe("http://127.0.0.1:43717/") +}) diff --git a/packages/layerfs/page/boot.js b/packages/layerfs/page/boot.js index d7adee3..84b567b 100644 --- a/packages/layerfs/page/boot.js +++ b/packages/layerfs/page/boot.js @@ -15,6 +15,15 @@ // leaves its page pointing at the first host's service, which serves // the same store — the same behaviour page-chat relies on. // +// In a browser on another computer (`magpie web`, or magpie in Docker), +// 127.0.0.1 is that computer, not the one running the service, and the +// frame showed "127.0.0.1 refused to connect" (#1228). package.json's +// magpie.page.service lets magpie forward the service on its own +// address, under the page's token; the bridge names it magpie.service. +// When it is there the frame goes to it; when it isn't (an older +// magpie), to 127.0.0.1 as before. magpie sends the service's own Host +// and no Origin for a navigation, so the service's guards still hold. +// // The language rides along as a query: the served page is no longer // inside magpie, so it cannot ask magpie.ready for it. The service // hands it to the UI as LayerfsTheme; the theme itself stays with the @@ -23,12 +32,18 @@ const DEFAULT_PORT = 43717 let port = DEFAULT_PORT let lang = "" + let service = "" try { const t = await window.magpie?.ready if (t && typeof t.lang === "string") lang = t.lang } catch { /* no bridge: the service falls back to English */ } + try { + const s = window.magpie?.service + if (typeof s === "string" && /^https?:\/\//.test(s)) service = s + } catch { /* an older magpie names no service */ } + try { const o = await window.magpie?.options?.get?.() const n = Number(o?.pagePort) @@ -38,5 +53,6 @@ const q = new URLSearchParams() if (lang) q.set("lang", lang) const qs = q.toString() - location.replace(`http://127.0.0.1:${port}/${qs ? `?${qs}` : ""}`) + const base = service || `http://127.0.0.1:${port}/` + location.replace(`${base}${qs ? `?${qs}` : ""}`) })()