import { readFileSync } from "node:fs"; import { describe, expect, it } from "vitest"; import { cspPolicyProblems, declaredCspProblems } from "../../scripts/lib/csp-policy.ts"; /** * 0929/13-04. The hosting fixtures copy the declared policy and a check * compares them, but nothing read what the policy allows: a declaration with * 'unsafe-inline' in script-src passed every gate. */ const DECLARED = ( JSON.parse(readFileSync("config/hosting/security-headers.json", "utf8")) as { headers: Record; } ).headers["Content-Security-Policy"] ?? ""; describe("cspPolicyProblems", () => { it("accepts the declared policy", () => { expect(DECLARED).not.toBe(""); expect(cspPolicyProblems(DECLARED)).toEqual([]); }); it.each([ ["script-src 'self' 'unsafe-inline'", "script-src allows 'unsafe-inline'"], ["script-src 'self' 'unsafe-eval'", "script-src allows 'unsafe-eval'"], ["script-src *", "script-src allows *"], ["script-src 'self' https:", "script-src allows the whole scheme https:"], ["style-src 'self' 'unsafe-inline'", "style-src allows 'unsafe-inline'"], ["object-src *", "object-src allows *"], ])("refuses %s", (directive, problem) => { const policy = DECLARED.split(/;\s*/u) .filter((part) => !part.startsWith(directive.slice(0, directive.indexOf(" ")))) .concat(directive) .join("; "); expect(cspPolicyProblems(policy)).toContain(problem); }); it("refuses a policy without script-src or default-src", () => { expect(cspPolicyProblems("img-src 'self'")).toContain("no script-src and no default-src"); }); // round9 confirm review. A browser uses the first of a repeated directive // and reads keywords in any case (CSP3), so the check does too. it.each([ ["script-src 'self' 'unsafe-inline'; script-src 'self'", "script-src allows 'unsafe-inline'"], ["Script-Src 'self' 'unsafe-inline'; script-src 'self'", "script-src allows 'unsafe-inline'"], ["script-src 'self' 'Unsafe-Inline'", "script-src allows 'unsafe-inline'"], ["script-src 'self' 'UNSAFE-EVAL'", "script-src allows 'unsafe-eval'"], ["script-src 'self' HTTPS:", "script-src allows the whole scheme https:"], ])("reads %s as a browser does", (policy, problem) => { expect(cspPolicyProblems(policy)).toContain(problem); }); it("does not read a repeated directive's later sources", () => { expect(cspPolicyProblems("script-src 'self'; script-src 'unsafe-inline'")).toEqual([]); }); }); // The check check:browser-security runs on config/hosting/security-headers.json. describe("declaredCspProblems", () => { it.each([ ["the declared file", readFileSync("config/hosting/security-headers.json", "utf8"), []], ["a header name in another case", JSON.stringify({ headers: { "content-security-policy": "script-src 'self'" } }), []], ["no policy", JSON.stringify({ headers: { "X-Frame-Options": "DENY" } }), ["no Content-Security-Policy header"]], [ "an unsafe policy", JSON.stringify({ headers: { "Content-Security-Policy": "script-src 'self' 'unsafe-inline'" } }), ["script-src allows 'unsafe-inline'"], ], ])("reads %s", (_label, text, problems) => { expect(declaredCspProblems(text)).toEqual(problems); }); });