diff --git a/scripts/check-browser-security.ts b/scripts/check-browser-security.ts index ba8acb51..b27fe808 100644 --- a/scripts/check-browser-security.ts +++ b/scripts/check-browser-security.ts @@ -1,6 +1,8 @@ -import { readdir } from "node:fs/promises"; +import { readdir, readFile } from "node:fs/promises"; import { spawnSync } from "node:child_process"; +import { cspPolicyProblems } from "./lib/csp-policy.ts"; + function requiredPnpmCli(): string { const executable = process.env.npm_execpath; if (!executable) { @@ -35,15 +37,32 @@ const forbidden = runPnpm([ const distFiles = await readdir("dist", { recursive: true }); const publicSourceMaps = distFiles.filter((file) => String(file).endsWith(".map")); -if (allowed.status !== 0 || forbidden.status === 0 || publicSourceMaps.length > 0) { +const SECURITY_HEADERS = "config/hosting/security-headers.json"; +const declared = ( + JSON.parse(await readFile(SECURITY_HEADERS, "utf8")) as { headers?: Record } +).headers?.["Content-Security-Policy"]; +const cspProblems = + declared === undefined ? ["no Content-Security-Policy header"] : cspPolicyProblems(declared); + +if ( + allowed.status !== 0 || + forbidden.status === 0 || + publicSourceMaps.length > 0 || + cspProblems.length > 0 +) { process.stderr.write(allowed.stderr || allowed.stdout); process.stderr.write(forbidden.stderr || forbidden.stdout); if (publicSourceMaps.length > 0) { process.stderr.write(`Public source maps found: ${publicSourceMaps.join(", ")}\n`); } + if (cspProblems.length > 0) { + process.stderr.write( + `${SECURITY_HEADERS} Content-Security-Policy: ${cspProblems.join("; ")}\n`, + ); + } process.exit(1); } process.stdout.write( - "Browser security fixtures: injection rejected, public source maps absent\n", + "Browser security fixtures: injection rejected, public source maps absent, declared CSP allows no unsafe source\n", ); diff --git a/scripts/lib/csp-policy.ts b/scripts/lib/csp-policy.ts new file mode 100644 index 00000000..126d02e5 --- /dev/null +++ b/scripts/lib/csp-policy.ts @@ -0,0 +1,43 @@ +/** + * 0929/13-04. What the declared Content-Security-Policy + * (config/hosting/security-headers.json) may not allow. The hosting fixtures + * copy it and scripts/verify-hosting-headers.ts compares them; this reads what + * it allows. check:browser-security (FE-GATE-013) runs it. + */ +const UNSAFE_SOURCES = new Set(["'unsafe-inline'", "'unsafe-eval'", "*"]); +/** Directives whose sources decide what script or plugin code may run or style may apply. */ +const CODE_DIRECTIVES = new Set([ + "default-src", + "script-src", + "script-src-elem", + "script-src-attr", + "style-src", + "style-src-elem", + "style-src-attr", + "object-src", +]); +/** A scheme-only source (`https:`) lets any host on it run script. */ +const SCHEME_ONLY = /^[a-z][a-z0-9+.-]*:$/u; +const SCRIPT_DIRECTIVES = new Set(["default-src", "script-src", "script-src-elem", "object-src"]); + +export function cspPolicyProblems(policy: string): string[] { + const directives = new Map(); + for (const part of policy.split(";")) { + const [name, ...sources] = part.trim().split(/\s+/u); + if (name) directives.set(name.toLowerCase(), sources); + } + const problems: string[] = []; + if (!directives.has("script-src") && !directives.has("default-src")) { + problems.push("no script-src and no default-src"); + } + for (const [name, sources] of directives) { + if (!CODE_DIRECTIVES.has(name)) continue; + for (const source of sources) { + if (UNSAFE_SOURCES.has(source)) problems.push(`${name} allows ${source}`); + else if (SCRIPT_DIRECTIVES.has(name) && SCHEME_ONLY.test(source)) { + problems.push(`${name} allows the whole scheme ${source}`); + } + } + } + return problems; +} diff --git a/tests/unit/csp-policy.test.ts b/tests/unit/csp-policy.test.ts new file mode 100644 index 00000000..61443f6c --- /dev/null +++ b/tests/unit/csp-policy.test.ts @@ -0,0 +1,43 @@ +import { readFileSync } from "node:fs"; + +import { describe, expect, it } from "vitest"; + +import { cspPolicyProblems } 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"); + }); +});