"use strict"; const test = require("node:test"); const assert = require("node:assert"); const fs = require("node:fs"); const path = require("node:path"); const ROOT = path.join(__dirname, ".."); const INDEX_JS = path.join(ROOT, "index.js"); const indexSource = fs.readFileSync(INDEX_JS, "utf8"); // Allowed immutable, version-pinned jsDelivr script prefixes (trailing slash = directory prefix). // Monaco is no longer here: it is self-hosted under public/vendor/monaco-editor // (sha512-verified against the npm registry) and covered by script-src 'self'. const ALLOWED_JSDELIVR_PATHS = [ "https://cdn.jsdelivr.net/npm/ace-builds@1.36.5/src-min-noconflict/", ]; // Extract the array literal body of a `name: [` ... `]` directive, robust to whitespace. function extractDirectiveArray(source, name) { const marker = new RegExp(`${name}\\s*:\\s*\\[`); const match = marker.exec(source); assert.ok(match, `directive ${name} not found in index.js`); const bodyStart = match.index + match[0].length; let depth = 1; let end = -1; for (let i = bodyStart; i < source.length; i += 1) { if (source[i] === "[") depth += 1; else if (source[i] === "]") { depth -= 1; if (depth === 0) { end = i; break; } } } assert.ok(end > bodyStart, `unbalanced brackets in directive ${name}`); return source.slice(bodyStart, end); } // Pull the quoted string tokens out of the directive body (skips comments, commas, whitespace, // and non-string entries such as the nonce function's identifiers). function extractSources(body) { const tokenRe = /"([^"]*)"|'([^']*)'|`([^`]*)`/g; const sources = []; let m; while ((m = tokenRe.exec(body)) !== null) { const value = m[1] !== undefined ? m[1] : m[2] !== undefined ? m[2] : m[3]; if (value && value.trim().length > 0) sources.push(value.trim()); } return sources; } const scriptSrcBody = extractDirectiveArray(indexSource, "scriptSrc"); const scriptSources = extractSources(scriptSrcBody); const styleSrcBody = extractDirectiveArray(indexSource, "styleSrc"); const styleSources = extractSources(styleSrcBody); const styleSrcAttrBody = extractDirectiveArray(indexSource, "styleSrcAttr"); const styleSrcAttrSources = extractSources(styleSrcAttrBody); test("CSP hardening: scriptSrc excludes broad cdnjs.cloudflare.com host", () => { for (const source of scriptSources) { assert.ok( source !== "https://cdnjs.cloudflare.com" && !source.startsWith("https://cdnjs.cloudflare.com/"), `scriptSrc must not allow cdnjs.cloudflare.com, found: ${source}` ); } }); test("CSP hardening: scriptSrc excludes bare cdnjs.cloudflare.com anywhere in the directive", () => { assert.ok( !scriptSrcBody.includes("cdnjs.cloudflare.com"), "scriptSrc directive body must not reference cdnjs.cloudflare.com at all" ); }); // adpanel:auth-csp-no-cdnjs — the auth-page CSP builder (buildAuthPageCsp) must // never allow a third-party style or font origin. The login and forgot-password // pages load all CSS and icon fonts same-origin (views/login.ejs), and an // unused allowance for a documented CSP-bypass host like cdnjs.cloudflare.com // is pure attack surface for the next markup-injection bug. The GLOBAL helmet // config further down intentionally keeps cdnjs for the non-auth pages that // still load FontAwesome from it — this pin covers ONLY the auth-page builder. // Like login-dead-assets.test.js: strip line comments so the builder's own // adpanel: comment (which names the removed option/host for auditor // greppability) does not trip the ban. function extractFunctionSource(source, signature) { const start = source.indexOf(signature); assert.ok(start >= 0, `${signature} not found`); assert.strictEqual( source.indexOf(signature, start + 1), -1, `${signature} must appear exactly once in index.js` ); let depth = 0; for (let i = start + signature.length - 1; i < source.length; i += 1) { if (source[i] === "{") depth += 1; else if (source[i] === "}") { depth -= 1; if (depth === 0) return source.slice(start, i + 1); } } assert.fail(`unbalanced braces after ${signature}`); } const buildAuthPageCspSource = extractFunctionSource( indexSource, "function buildAuthPageCsp(req, res, options = {}) {" ); const buildAuthPageCspCode = buildAuthPageCspSource.replace(/^[ \t]*\/\/.*$/gm, ""); test("CSP hardening: auth-page styleSrc/fontSrc never allow cdnjs.cloudflare.com", () => { assert.ok( !buildAuthPageCspCode.includes("cdnjs.cloudflare.com"), "buildAuthPageCsp must not allow cdnjs.cloudflare.com in ANY directive it emits (style-src, font-src, script-src, ...): auth pages load no third-party styles or fonts" ); assert.ok( buildAuthPageCspCode.includes(`const styleSrc = ["'self'", nonce];`), "the auth-page styleSrc must stay 'self' + nonce only (no third-party style origin)" ); assert.ok( buildAuthPageCspCode.includes(`const fontSrc = ["'self'", "data:"];`), "the auth-page fontSrc must stay 'self' + data: only (no third-party font origin)" ); }); test("CSP hardening: the dead includeFontAwesome option stays removed from the auth-page builder", () => { const codeWithoutComments = indexSource.replace(/^[ \t]*\/\/.*$/gm, ""); assert.ok( !codeWithoutComments.includes("includeFontAwesome"), "includeFontAwesome was removed as a dead option (its cdnjs pushes were unused: the login page loads FontAwesome same-origin); do not reintroduce it" ); }); test("CSP hardening: scriptSrc allows only the pinned jsDelivr paths, never the bare host", () => { for (const source of scriptSources) { if (source === "https://cdn.jsdelivr.net") { assert.fail("scriptSrc must not allow the bare https://cdn.jsdelivr.net host"); } if (source.startsWith("https://cdn.jsdelivr.net/") && !ALLOWED_JSDELIVR_PATHS.includes(source)) { assert.fail( `scriptSrc contains non-pinned jsDelivr path: ${source}; only ${ALLOWED_JSDELIVR_PATHS.join(", ")} are allowed` ); } } }); test("CSP hardening: scriptSrc retains self and the pinned editor CDN paths only", () => { assert.ok(scriptSources.includes("'self'"), "scriptSrc must keep 'self'"); for (const allowed of ALLOWED_JSDELIVR_PATHS) { assert.ok( scriptSources.includes(allowed), `scriptSrc must include pinned path ${allowed}` ); } assert.ok( !scriptSources.some((s) => s.includes("monaco-editor")), "scriptSrc must not allow the Monaco CDN path: Monaco is self-hosted under /vendor/monaco-editor" ); }); test("CSP hardening: styleSrc excludes 'unsafe-inline' (inline styles are nonce-gated)", () => { assert.ok( !styleSources.includes("'unsafe-inline'"), "styleSrc must not allow 'unsafe-inline'" ); assert.ok( !styleSrcBody.includes("'unsafe-inline'"), "styleSrc directive body must not reference 'unsafe-inline' at all" ); }); test("CSP hardening: styleSrc includes the per-request nonce source", () => { assert.ok( styleSrcBody.includes("nonce-${res.locals.cspNonce}"), "styleSrc must include the nonce function (req, res) => `nonce-${res.locals.cspNonce}`" ); assert.ok( styleSources.includes("'nonce-${res.locals.cspNonce}'"), "styleSrc token list must include the nonce source value" ); }); test("CSP hardening: styleSrcAttr keeps 'unsafe-inline' for style= attributes", () => { assert.ok( styleSrcAttrSources.includes("'unsafe-inline'"), "styleSrcAttr must keep 'unsafe-inline': templates and client JS rely on 600+ style attributes" ); }); test("CSP hardening: every