diff --git a/scripts/test-web-console-embed.mjs b/scripts/test-web-console-embed.mjs index 4166f00..06c3c1f 100644 --- a/scripts/test-web-console-embed.mjs +++ b/scripts/test-web-console-embed.mjs @@ -112,3 +112,15 @@ test("anything but exactly one allowed origin keeps frame-ancestors 'none'", asy assert.match(csp, /frame-ancestors 'none'$/, `${JSON.stringify(origin)}: ${csp}`); } }); + +test("the page-closed signal is a keepalive fetch, which carries the page's origin, not a beacon", () => { + // A body-less sendBeacon is a no-cors POST: under the console's + // `referrer-policy: no-referrer` it goes out with `Origin: null`, which /bye's + // origin check refuses (WebKit showed it; the Fetch standard says every engine + // does the same). A keepalive fetch is a cors request and sends the real origin. + const core = fs.readFileSync(path.join(ROOT, "src", "web-console", "public", "screens", "core.js"), "utf8"); + const wire = core.slice(core.indexOf("export function wireChrome()")); + const pagehide = wire.slice(0, wire.indexOf("});")); + assert.match(pagehide, /fetch\("\/bye", \{ method: "POST", keepalive: true \}\)/, pagehide); + assert.doesNotMatch(core, /navigator\.sendBeacon\(/, "no beacon call left anywhere in the page's core"); +}); diff --git a/src/web-console/public/screens/core.js b/src/web-console/public/screens/core.js index 0db1f12..49ab004 100644 --- a/src/web-console/public/screens/core.js +++ b/src/web-console/public/screens/core.js @@ -2503,8 +2503,11 @@ async function openOutside(href) { export function wireChrome() { addEventListener("pagehide", () => { + // A keepalive fetch, not sendBeacon: WebKit sends a beacon from a page + // with `referrer-policy: no-referrer` with `Origin: null`, which /bye's + // origin check refuses. try { - navigator.sendBeacon("/bye"); + fetch("/bye", { method: "POST", keepalive: true }).catch(() => {}); } catch {} // A page that goes away leaves no dot on the app's taskbar behind it. tellAttention("plain");