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