Sandbox remote file responses at the browser relay boundary

This commit is contained in:
2026-11-08 22:27:43 +00:00
parent 3d88220430
commit 871f7cdfa0
2 changed files with 77 additions and 1 deletions
+17 -1
View File
@@ -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);
});
+60
View File
@@ -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(
'<script>fetch("/api/peers/private")</script><svg onload="alert(document.cookie)"></svg>',
);
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(), /<script>/);
}
});