Sandbox home and artifact files served on the app origin (H1)

GET /files and /artifacts/ now send the artifact sandbox CSP and nosniff
through one helper, and /files serves HTML, XHTML, SVG, XML, XSL,
JavaScript and multipart/x-mixed-replace as attachments, including when
the type is sniffed from an extensionless file. The web proxy adds the same
headers to every proxied /files and /artifacts/ response (defence in depth
against an older server), and the peer relay shares the helper.

show_html previews are unchanged by operator decision.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 23:16:46 +00:00
parent 806b5eca46
commit e39a9560ba
9 changed files with 233 additions and 21 deletions
+11 -15
View File
@@ -8,11 +8,9 @@ import {
import { PeerRegistry } from "./peerRegistry.js";
import { withServerToken } from "./serverToken.js";
import { rememberDiscovered } from "./peerAddress.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'";
// Peer files are untrusted content, never privileged control-UI documents:
// the sandbox applies even for older peers whose /files has no such header.
import { untrustedHeaders } from "./untrustedContent.js";
const allowed = (path) =>
path === "/files" ||
@@ -98,23 +96,21 @@ export class PeerRelay extends PeerRegistry {
"content-range",
"accept-ranges",
"content-disposition",
"content-security-policy",
])
if (response.headers[name]) safe[name] = response.headers[name];
// Multiple CSP policies intersect: preserve a peer's stricter
// restrictions while always imposing the local sandbox boundary. A
// peer home file a browser could script is a download.
res.writeHead(
response.statusCode >= 300 && response.statusCode < 400
? 502
: response.statusCode,
{
...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",
...untrustedHeaders(safe, {
download: target.url.pathname === "/files",
}),
"cache-control": "private, no-store",
},
);
response.pipe(res);
+50
View File
@@ -0,0 +1,50 @@
// The response boundary for files the proxy relays but did not write: run
// artifacts, files under a home directory and a peer's files. `sandbox` gives
// such a document an opaque origin even when it is opened as a page, so a
// crafted HTML or SVG file cannot script the app origin. The Go server sets the
// same policy; the proxy adds it again so an older server, or a peer, cannot
// serve a document with the app's authority.
export const CONTENT_POLICY =
"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 SCRIPT_CAPABLE = new Set([
"text/html",
"application/xhtml+xml",
"image/svg+xml",
"text/xml",
"application/xml",
"text/xsl",
"application/xslt+xml",
"application/javascript",
"text/javascript",
"application/ecmascript",
"text/ecmascript",
"multipart/x-mixed-replace",
]);
/** Whether a browser could run script in a page of this content type. */
export function scriptCapable(contentType) {
const media = String(contentType ?? "").split(";")[0].trim().toLowerCase();
return SCRIPT_CAPABLE.has(media) || media.endsWith("+xml") || media.endsWith("/xml");
}
/**
* Copies [headers] (a Node response's headers, lower-case names) with the
* sandbox policy added. Multiple CSP headers intersect, so an upstream policy
* is kept beside the local one. With [download], a script-capable type is
* forced to an attachment, keeping any filename the upstream named.
*/
export function untrustedHeaders(headers, { download = false } = {}) {
const out = { ...headers };
const upstream = out["content-security-policy"];
out["content-security-policy"] = upstream
? [...(Array.isArray(upstream) ? upstream : [upstream]), CONTENT_POLICY]
: CONTENT_POLICY;
out["x-content-type-options"] = "nosniff";
if (download && scriptCapable(out["content-type"])) {
const current = String(out["content-disposition"] ?? "").trim();
if (/^inline\b/i.test(current)) out["content-disposition"] = current.replace(/^inline/i, "attachment");
else if (!/^attachment\b/i.test(current)) out["content-disposition"] = "attachment";
}
return out;
}