Public: anyone can read this; the crew can change it.
CSP reports are bounded per day whatever directive they name #14987
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14962-csp-reports-are-bounded-per-day-whatever → main 2 commits fork
2 files
changed +44
−4
src/http/csp_report.ts +26 −4
| @@ -9,7 +9,10 @@ | ||
| 9 | 9 | * and stores nothing. Only three things are kept: the directive, the blocked |
| 10 | 10 | * host (never a path or query string) and the page's path, as one count per |
| 11 | 11 | * UTC hour (`day` holds `YYYY-MM-DDTHH`), so a window's sum holds only its own |
| 12 | * hours. Rows a day are bounded (`DAY_ROWS`, the rest fold into one), and | |
| 12 | * hours. A directive not on the CSP list (`DIRECTIVES`) is counted as | |
| 13 | * `other`, so a caller cannot mint keys by inventing names; past `DAY_ROWS` | |
| 14 | * rows a day the rest fold into one per directive and hour, at most | |
| 15 | * `24 × (DIRECTIVES.size + 1)` more (#14962), and | |
| 13 | 16 | * days past `KEEP_DAYS` are dropped as new ones arrive. Anonymous callers are |
| 14 | 17 | * limited per client and per forge like the page-view beacon. Always 204. |
| 15 | 18 | */ |
| @@ -37,6 +40,20 @@ const PATH_MAX = 200; | ||
| 37 | 40 | const DAY_ROWS = 500; |
| 38 | 41 | const KEEP_DAYS = 30; |
| 39 | 42 | const OVERFLOW = "(more)"; |
| 43 | /** What an unknown directive is counted as. */ | |
| 44 | export const OTHER_DIRECTIVE = "other"; | |
| 45 | /** | |
| 46 | * The directive names CSP Level 3 and its drafts define, and the legacy ones browsers still report. | |
| 47 | */ | |
| 48 | export const DIRECTIVES: ReadonlySet<string> = new Set([ | |
| 49 | "default-src", "child-src", "connect-src", "font-src", "frame-src", "fenced-frame-src", "img-src", "manifest-src", | |
| 50 | "media-src", "object-src", "prefetch-src", "script-src", "script-src-elem", "script-src-attr", "style-src", | |
| 51 | "style-src-elem", "style-src-attr", "worker-src", "base-uri", "sandbox", "form-action", "frame-ancestors", | |
| 52 | "navigate-to", "report-uri", "report-to", "require-trusted-types-for", "trusted-types", "upgrade-insecure-requests", | |
| 53 | "block-all-mixed-content", "plugin-types", "require-sri-for", "webrtc", | |
| 54 | ]); | |
| 55 | /** A known directive name as is; any other folds to `other`. */ | |
| 56 | export const knownDirective = (name: string): string => (DIRECTIVES.has(name) ? name : OTHER_DIRECTIVE); | |
| 40 | 57 | const PER_CLIENT = 30; |
| 41 | 58 | const PER_FORGE = 600; |
| 42 | 59 | |
| @@ -54,10 +71,14 @@ const answer = (status: number, headers?: Record<string, string>): Response => | ||
| 54 | 71 | |
| 55 | 72 | const text = (v: unknown): string | undefined => (typeof v === "string" ? v : undefined); |
| 56 | 73 | |
| 57 | /** `script-src-elem 'self'` → `script-src-elem`; anything not a directive name → undefined. */ | |
| 74 | /** | |
| 75 | * `script-src-elem 'self'` → `script-src-elem`; a well-formed unknown name → `other`; not a name → | |
| 76 | * undefined. | |
| 77 | */ | |
| 58 | 78 | function directiveOf(raw: string | undefined): string | undefined { |
| 59 | const name = raw?.trim().split(/\s+/)[0]?.toLowerCase(); | |
| 60 | return name !== undefined && DIRECTIVE.test(name) ? name : undefined; | |
| 79 | // A name is at most 40 characters: only the head of a long value is read. | |
| 80 | const name = raw?.trimStart().slice(0, 64).split(/\s/)[0]?.toLowerCase(); | |
| 81 | return name !== undefined && DIRECTIVE.test(name) ? knownDirective(name) : undefined; | |
| 61 | 82 | } |
| 62 | 83 | |
| 63 | 84 | /** A URL's host (an http or ws scheme), its scheme otherwise (`data`), or a keyword (`inline`, `eval`). */ |
| @@ -130,6 +151,7 @@ export function recordViolation(db: Database, v: Violation, now: Date = new Date | ||
| 130 | 151 | const seen = now.getTime(); |
| 131 | 152 | const hour = hourOf(seen); |
| 132 | 153 | const day = hour.slice(0, 10); |
| 154 | v = { ...v, directive: knownDirective(v.directive) }; | |
| 133 | 155 | const bump = db.query<unknown, [string, string, string, string, number]>( |
| 134 | 156 | "UPDATE csp_violation SET n = n + 1, seen = ?5 WHERE day = ?1 AND directive = ?2 AND blocked = ?3 AND path = ?4", |
| 135 | 157 | ); |
test/csp-report.test.ts +18
| @@ -114,6 +114,24 @@ describe("the security page", () => { | ||
| 114 | 114 | expect(html).not.toContain("frame-src"); |
| 115 | 115 | }); |
| 116 | 116 | |
| 117 | test("invented directives fold to 'other', so a day's rows stay at their bound (#14962)", () => { | |
| 118 | expect(parseCspReport(legacy({ "document-uri": `${base}/`, "violated-directive": "made-up-thing 'self'", "blocked-uri": "inline" }))).toEqual([ | |
| 119 | { directive: "other", blocked: "inline", path: "/" }, | |
| 120 | ]); | |
| 121 | app.db.run("DELETE FROM csp_violation"); | |
| 122 | const now = new Date("2026-11-20T12:00:00Z"); | |
| 123 | for (let i = 0; i < 500; i++) recordViolation(app.db, { directive: "img-src", blocked: `h${i}.example`, path: "/" }, now); | |
| 124 | for (let i = 0; i < 2000; i++) { | |
| 125 | const [v] = parseCspReport(legacy({ "document-uri": `${base}/`, "violated-directive": `x-${i}`, "blocked-uri": "inline" })) as [Parameters<typeof recordViolation>[1]]; | |
| 126 | recordViolation(app.db, v, now); | |
| 127 | recordViolation(app.db, { directive: `y-${i}`, blocked: "inline", path: "/" }, now); | |
| 128 | } | |
| 129 | const count = (app.db.query("SELECT COUNT(*) AS n FROM csp_violation").get() as { n: number }).n; | |
| 130 | expect(count).toBe(501); | |
| 131 | expect(rows().find((r) => r.directive === "other")).toMatchObject({ blocked: "(more)", n: 4000 }); | |
| 132 | app.db.run("DELETE FROM csp_violation"); | |
| 133 | }); | |
| 134 | ||
| 117 | 135 | test("a parse never keeps a query string", () => { |
| 118 | 136 | const got = parseCspReport(legacy({ "document-uri": "https://f.example/p?x=1#h", "violated-directive": "img-src", "blocked-uri": "https://h.example/i.png?k=v" })); |
| 119 | 137 | expect(got).toEqual([{ directive: "img-src", blocked: "h.example", path: "/p" }]); |
new above
offline