merge: PR #31을 합친 검사 브랜치에 넣는다
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { readdir } from "node:fs/promises";
|
||||
import { readdir, readFile } from "node:fs/promises";
|
||||
import { spawnSync } from "node:child_process";
|
||||
|
||||
import { declaredCspProblems } from "./lib/csp-policy.ts";
|
||||
|
||||
function requiredPnpmCli(): string {
|
||||
const executable = process.env.npm_execpath;
|
||||
if (!executable) {
|
||||
@@ -35,15 +37,28 @@ 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 cspProblems = declaredCspProblems(await readFile(SECURITY_HEADERS, "utf8"));
|
||||
|
||||
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",
|
||||
);
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import {
|
||||
checkDeclaredCspFixtures,
|
||||
checkNonmatchingSecurityIncludeFixture,
|
||||
checkSecurityFixtures,
|
||||
} from "./lib/security-fixture-check.ts";
|
||||
|
||||
await checkNonmatchingSecurityIncludeFixture();
|
||||
await checkSecurityFixtures();
|
||||
await checkDeclaredCspFixtures();
|
||||
process.stdout.write(
|
||||
"Security fixtures: unmatched include rejected; 3 forbidden files detected\n",
|
||||
"Security fixtures: unmatched include rejected; 3 forbidden files detected; unsafe CSP fixture rejected\n",
|
||||
);
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* 0929/13-04. What the declared Content-Security-Policy
|
||||
* (config/hosting/security-headers.json) may not allow.
|
||||
* scripts/verify-hosting-headers.ts compares a host's answer with it; this
|
||||
* reads what it allows. check:browser-security (FE-GATE-013) runs it, and
|
||||
* check:security:fixtures (FE-GATE-029) holds it to a rejected fixture.
|
||||
*/
|
||||
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"]);
|
||||
|
||||
/**
|
||||
* The directives as a browser reads them (CSP3): names and keywords in any
|
||||
* case, and the first of a repeated directive, the later ones ignored.
|
||||
*/
|
||||
function directivesOf(policy: string): Map<string, readonly string[]> {
|
||||
const directives = new Map<string, readonly string[]>();
|
||||
for (const part of policy.split(";")) {
|
||||
const [name, ...sources] = part.trim().split(/\s+/u);
|
||||
const key = name?.toLowerCase();
|
||||
if (key && !directives.has(key)) {
|
||||
directives.set(key, sources.map((source) => source.toLowerCase()));
|
||||
}
|
||||
}
|
||||
return directives;
|
||||
}
|
||||
|
||||
export function cspPolicyProblems(policy: string): string[] {
|
||||
const directives = directivesOf(policy);
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* The problems of the policy a security-headers document (the shape of
|
||||
* config/hosting/security-headers.json) declares, its header name in any case.
|
||||
*/
|
||||
export function declaredCspProblems(securityHeadersJson: string): string[] {
|
||||
const document = JSON.parse(securityHeadersJson) as { headers?: Record<string, unknown> };
|
||||
const policy = Object.entries(document.headers ?? {}).find(
|
||||
([name]) => name.toLowerCase() === "content-security-policy",
|
||||
)?.[1];
|
||||
return typeof policy === "string"
|
||||
? cspPolicyProblems(policy)
|
||||
: ["no Content-Security-Policy header"];
|
||||
}
|
||||
@@ -3,6 +3,8 @@ import { mkdtemp, readFile, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import { declaredCspProblems } from "./csp-policy.ts";
|
||||
|
||||
type ScanResult = Readonly<{
|
||||
error?: Error;
|
||||
status: number | null;
|
||||
@@ -168,3 +170,25 @@ export async function checkNonmatchingSecurityIncludeFixture(
|
||||
await cleanup(directory);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 0929/13-04. The policy check check:browser-security runs rejects its
|
||||
* fixture (a keyword in another case, in the first of two script-src) and
|
||||
* accepts the declared policy.
|
||||
*/
|
||||
export async function checkDeclaredCspFixtures(): Promise<void> {
|
||||
const fixture = "tests/fixtures/security/csp/rejected-security-headers.json";
|
||||
const rejected = declaredCspProblems(await readFile(fixture, "utf8"));
|
||||
const expected = ["script-src allows 'unsafe-inline'"];
|
||||
if (JSON.stringify(rejected) !== JSON.stringify(expected)) {
|
||||
throw new Error(
|
||||
`${fixture}: expected ${expected.join(", ")}; received ${rejected.join(", ") || "no problem"}`,
|
||||
);
|
||||
}
|
||||
const declared = declaredCspProblems(
|
||||
await readFile("config/hosting/security-headers.json", "utf8"),
|
||||
);
|
||||
if (declared.length > 0) {
|
||||
throw new Error(`config/hosting/security-headers.json: ${declared.join("; ")}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,7 +131,20 @@ if (liveTarget?.passed && !slotEmpty) {
|
||||
const fixture = await readDocument(
|
||||
"tests/fixtures/hosting/response-headers.json",
|
||||
);
|
||||
responses = responseHeaders(fixture.responses);
|
||||
// 0929/13-04. The fixture host answers with the declared security headers
|
||||
// wherever the fixture names none, so a policy change is one file. A value
|
||||
// the fixture names is compared like a host's answer.
|
||||
const declaredAnswer = Object.fromEntries(
|
||||
Object.entries(securityHeaders).map(([header, value]) => [header.toLowerCase(), value]),
|
||||
);
|
||||
responses = Object.fromEntries(
|
||||
Object.entries(responseHeaders(fixture.responses)).map(([surface, headers]) => [
|
||||
surface,
|
||||
recordValue(cacheSurfaces[surface]).securityHeaders === true
|
||||
? { ...declaredAnswer, ...headers }
|
||||
: headers,
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
const results: ProbeResult[] = [...probeResults];
|
||||
|
||||
Reference in New Issue
Block a user