Files
pipelines/web/test/untrustedContent.test.mjs
T
alan e39a9560ba 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>
2026-12-03 23:16:46 +00:00

35 lines
1.7 KiB
JavaScript

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");
});