Files
clean-architecture-frontend…/scripts/check-browser-security.ts
T
DongHyeonka 9f469b6065 fix(security): 선언한 CSP가 unsafe 출처를 허용하면 check:browser-security가 실패한다
config/hosting/security-headers.json의 Content-Security-Policy는 hosting
fixture가 복사하고 verify-hosting-headers가 둘을 대조한다. 그러나 정책이
무엇을 허용하는지 보는 검사는 없다. script-src에 'unsafe-inline'을 넣은
선언도 모든 게이트를 지난다.

고른 방법: scripts/lib/csp-policy.ts의 cspPolicyProblems가 code 지시어
(default-src, script-src*, style-src*, object-src)의 'unsafe-inline'·
'unsafe-eval'·*와 script 계열의 scheme 통째 허용(https:)을 거절하고,
script-src·default-src가 없는 정책도 거절한다. check:browser-security
(FE-GATE-013)가 선언 파일에 부른다. 버린 대안: fixture를 선언에서 만들기
(대조 검사가 이미 같은 값을 지킨다. 허용 내용은 여전히 안 본다).

위험·한계: 제품이 인라인 스크립트가 필요한 CSP로 바꾸면 이 검사가
막는다. 그때는 nonce·hash로 바꾼다. connect-src의 https:는 보지 않는다
(스크립트 실행이 아니다).

검증: tests/unit/csp-policy.test.ts가 고치기 전에는 모듈이 없어 실패하고
고친 뒤 통과한다(desktop, PR 본문). check:browser-security는
`parts FE-GATE-013` 뒤 명령으로 돈다.

Refs: 0929/13-04
2026-11-12 22:58:12 +09:00

69 lines
1.9 KiB
TypeScript

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) {
throw new Error("check:browser-security must run through the pnpm script");
}
return executable;
}
const pnpmCli = requiredPnpmCli();
function runPnpm(arguments_: string[]) {
return spawnSync(process.execPath, [pnpmCli, ...arguments_], {
encoding: "utf8",
});
}
const allowed = runPnpm([
"exec",
"eslint",
"tests/fixtures/security/allowed",
"--no-ignore",
"--max-warnings=0",
]);
const forbidden = runPnpm([
"exec",
"eslint",
"tests/fixtures/security/forbidden",
"--no-ignore",
"--max-warnings=0",
]);
const distFiles = await readdir("dist", { recursive: true });
const publicSourceMaps = distFiles.filter((file) => String(file).endsWith(".map"));
const SECURITY_HEADERS = "config/hosting/security-headers.json";
const declared = (
JSON.parse(await readFile(SECURITY_HEADERS, "utf8")) as { headers?: Record<string, string> }
).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, declared CSP allows no unsafe source\n",
);