Files
clean-architecture-frontend…/scripts/lib/csp-policy.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

44 lines
1.7 KiB
TypeScript

/**
* 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<string, readonly string[]>();
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;
}