diff --git a/web/src/peerRelay.js b/web/src/peerRelay.js index d0fcf58c..48bf40c2 100644 --- a/web/src/peerRelay.js +++ b/web/src/peerRelay.js @@ -7,6 +7,11 @@ import { } from "./peerTransport.js"; import { PeerRegistry } from "./peerRegistry.js"; +// Peer files are untrusted content, never privileged control-UI documents. +// Apply this even for older peers whose /files endpoint has no sandbox header. +const contentPolicy = + "sandbox; default-src 'none'; script-src 'none'; connect-src 'none'; style-src 'unsafe-inline'; img-src 'self' data:; media-src 'self' data:; base-uri 'none'; form-action 'none'; frame-ancestors 'self'"; + const allowed = (path) => path === "/files" || path === "/chat-uploads" || @@ -75,7 +80,18 @@ export class PeerRelay extends PeerRegistry { response.statusCode >= 300 && response.statusCode < 400 ? 502 : response.statusCode, - { ...safe, "Cache-Control": "no-store" }, + { + ...safe, + "Cache-Control": "private, no-store", + // Multiple CSP policies intersect: preserve a peer's stricter + // restrictions while always imposing the local sandbox boundary. + "Content-Security-Policy": response.headers[ + "content-security-policy" + ] + ? [response.headers["content-security-policy"], contentPolicy] + : contentPolicy, + "X-Content-Type-Options": "nosniff", + }, ); response.pipe(res); }); diff --git a/web/test/peerRelay.test.mjs b/web/test/peerRelay.test.mjs index 210995a0..6cee5a8c 100644 --- a/web/test/peerRelay.test.mjs +++ b/web/test/peerRelay.test.mjs @@ -76,6 +76,25 @@ async function fixture(t) { return; } res.setHeader("Set-Cookie", "do-not-forward=private"); + if ( + req.url.startsWith("/artifacts/") || + req.url.includes("untrusted.svg") + ) { + res.setHeader( + "Content-Type", + req.url.startsWith("/artifacts/") ? "text/html" : "image/svg+xml", + ); + if (req.url.startsWith("/artifacts/")) { + res.setHeader( + "Content-Security-Policy", + "sandbox; default-src 'none'; img-src 'none'", + ); + } + res.end( + '', + ); + return; + } res.end( JSON.stringify({ name, url: req.url, cookie: req.headers.cookie }), ); @@ -469,3 +488,44 @@ test("expired registry owners are closed and pruned before the capacity check", registry.forget({ token: "unknown-invalid-session" }); assert.equal(registry.sessions.size, before); }); + +test("raw peer HTML and legacy SVG files retain a local sandbox and no cookie authority", async (t) => { + const f = await fixture(t); + await f.register("one", { + endpoints: [f.remotes[0]], + password: "password-one", + }); + for (const path of [ + "/artifacts/7/2/raw.html", + "/files?path=%2Fhome%2Funtrusted.svg", + ]) { + const response = await fetch(f.base + "/peers/one" + path, { + headers: { Cookie: f.cookie }, + }); + assert.equal(response.status, 200); + const policy = response.headers.get("content-security-policy"); + for (const directive of [ + "sandbox;", + "default-src 'none'", + "script-src 'none'", + "connect-src 'none'", + "base-uri 'none'", + "form-action 'none'", + "frame-ancestors 'self'", + ]) { + assert.ok(policy.includes(directive), directive); + } + assert.equal(policy.includes("allow-same-origin"), false); + assert.equal(policy.includes("allow-scripts"), false); + assert.equal(response.headers.get("x-content-type-options"), "nosniff"); + assert.equal(response.headers.get("cache-control"), "private, no-store"); + assert.equal(response.headers.get("set-cookie"), null); + if (path.startsWith("/artifacts/")) + assert.ok( + policy.includes("img-src 'none'"), + "stricter remote policy preserved", + ); + // The response boundary, not text rewriting, isolates generated documents. + assert.match(await response.text(), /