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:
@@ -214,6 +214,37 @@ test("files require sign-in and proxy query, ranges and attachment headers", asy
|
||||
]);
|
||||
});
|
||||
|
||||
test("files and artifacts are sandboxed and home HTML is a download even from an old server", async (t) => {
|
||||
// An older server that sets none of the boundary headers.
|
||||
const upstream = createServer((req, res) => {
|
||||
const html = req.url.startsWith("/files") || req.url.startsWith("/artifacts/");
|
||||
res.writeHead(200, {
|
||||
"Content-Type": html ? "text/html; charset=utf-8" : "text/plain",
|
||||
"Content-Disposition": 'inline; filename="x.html"',
|
||||
});
|
||||
res.end("<script>alert(1)</script>");
|
||||
}).listen(0, "127.0.0.1");
|
||||
await once(upstream, "listening");
|
||||
t.after(() => new Promise((done) => upstream.close(done)));
|
||||
const port = await freePort();
|
||||
const child = spawn(process.execPath, ["serve.mjs", "--port", String(port),
|
||||
"--pipelines", `127.0.0.1:${upstream.address().port}`], {
|
||||
cwd: here,
|
||||
env: { ...process.env, PIPELINES_WEB_AUTH: "disabled" },
|
||||
stdio: ["ignore", "pipe", "inherit"],
|
||||
});
|
||||
t.after(async () => { const exited = once(child, "exit"); child.kill(); await exited; });
|
||||
await once(child.stdout, "data");
|
||||
const base = `http://127.0.0.1:${port}`;
|
||||
const file = await fetch(base + "/files?path=~/Downloads/x.html");
|
||||
assert.match(file.headers.get("content-security-policy"), /(^|, )sandbox; default-src 'none'; script-src 'none'/);
|
||||
assert.equal(file.headers.get("x-content-type-options"), "nosniff");
|
||||
assert.equal(file.headers.get("content-disposition"), 'attachment; filename="x.html"');
|
||||
const artifact = await fetch(base + "/artifacts/r/s/report.html");
|
||||
assert.match(artifact.headers.get("content-security-policy"), /sandbox; default-src 'none'/);
|
||||
assert.equal(artifact.headers.get("content-disposition"), 'inline; filename="x.html"');
|
||||
});
|
||||
|
||||
test("file uploads require sign-in and reach the server with their name header", async (t) => {
|
||||
const requests = [];
|
||||
const upstream = createServer(async (req, res) => {
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { CONTENT_POLICY, scriptCapable, untrustedHeaders } from "../src/untrustedContent.js";
|
||||
|
||||
test("script-capable types cover HTML, SVG and every XML flavour", () => {
|
||||
for (const type of [
|
||||
"text/html; charset=utf-8",
|
||||
"application/xhtml+xml",
|
||||
"image/svg+xml",
|
||||
"text/xml",
|
||||
"application/xml",
|
||||
"application/rss+xml",
|
||||
"TEXT/HTML",
|
||||
])
|
||||
assert.equal(scriptCapable(type), true, type);
|
||||
for (const type of ["text/plain", "image/png", "application/pdf", "application/octet-stream", "", undefined])
|
||||
assert.equal(scriptCapable(type), false, String(type));
|
||||
});
|
||||
|
||||
test("untrusted headers add the sandbox beside an upstream policy", () => {
|
||||
assert.equal(untrustedHeaders({})["content-security-policy"], CONTENT_POLICY);
|
||||
const both = untrustedHeaders({ "content-security-policy": "img-src 'none'" });
|
||||
assert.deepEqual(both["content-security-policy"], ["img-src 'none'", CONTENT_POLICY]);
|
||||
assert.equal(both["x-content-type-options"], "nosniff");
|
||||
});
|
||||
|
||||
test("downloads force script-capable types to attachments and keep the name", () => {
|
||||
const html = { "content-type": "text/html", "content-disposition": 'inline; filename="a.html"' };
|
||||
assert.equal(untrustedHeaders(html, { download: true })["content-disposition"], 'attachment; filename="a.html"');
|
||||
assert.equal(untrustedHeaders({ "content-type": "image/svg+xml" }, { download: true })["content-disposition"], "attachment");
|
||||
assert.equal(untrustedHeaders(html)["content-disposition"], 'inline; filename="a.html"');
|
||||
const text = { "content-type": "text/plain", "content-disposition": "inline" };
|
||||
assert.equal(untrustedHeaders(text, { download: true })["content-disposition"], "inline");
|
||||
});
|
||||
Reference in New Issue
Block a user