From 02f307efa8a7b021f5db91f0ae7e410ee07dfeaf Mon Sep 17 00:00:00 2001 From: Pieter Viljoen Date: Sun, 25 Oct 2026 15:35:32 -0700 Subject: [PATCH 1/5] Check Every Fetch on Both Sites Against Its Policy, and Refuse a Named Host check-csp-scripts.py becomes check-csp.py. Beyond scripts, frames, media, and objects, it reads images, srcset candidates, posters, SVG images, each loading link, and CSS url() and @import in stylesheets, style blocks, and style attributes. It runs over the built blog and the family tree, each against its own policy, with the site's own origin read as 'self'. A policy naming a host or a network scheme fails, so every resource either site loads is its own. Part of #529. Co-Authored-By: Claude Opus 5.5 --- .vscode/tasks.json | 4 +- CONTENT.md | 4 +- README.md | 4 +- checks/README.md | 4 +- checks/check-csp-browser.py | 5 +- checks/check-csp-scripts.py | 239 ----------- checks/check-csp.py | 391 ++++++++++++++++++ ...check_csp_scripts.py => test_check_csp.py} | 154 +++++-- deploy/README.md | 14 +- deploy/make-release.sh | 2 +- themes/README.md | 2 +- 11 files changed, 541 insertions(+), 282 deletions(-) delete mode 100755 checks/check-csp-scripts.py create mode 100755 checks/check-csp.py rename checks/tests/{test_check_csp_scripts.py => test_check_csp.py} (59%) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 4b3a34f5..0512ba31 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -64,9 +64,9 @@ "problemMatcher": [] }, { - "label": "Check: CSP Scripts", + "label": "Check: CSP", "type": "process", - "command": "checks/check-csp-scripts.py", + "command": "checks/check-csp.py", "args": [ "public" ], diff --git a/CONTENT.md b/CONTENT.md index 1db8eb19..9219a985 100644 --- a/CONTENT.md +++ b/CONTENT.md @@ -192,7 +192,7 @@ This is the reason this file exists, so it is worth stating plainly. | --- | --- | | `hugo --gc --minify --panicOnWarning` | yes, it must build | | [`checks/check-url-parity.py`][parity] | yes, for URLs, asset references, and the orphan count | -| [`checks/check-csp-scripts.py`][csp-scripts] | yes, for a script, inline handler, `javascript:` URL, frame, media file, or object a post's raw HTML adds | +| [`checks/check-csp.py`][csp] | yes, for a script, inline handler, `javascript:` URL, image, stylesheet, font, frame, media file, or object a post's raw HTML loads from another host | | `checks/check-live-urls.sh` | yes, against a running server | | markdownlint | no, `content/.markdownlint-cli2.jsonc` ignores the tree | | CSpell | no, `cspell.json` ignores the tree | @@ -211,7 +211,7 @@ A fact in a published post that proves wrong is corrected in the post. It is not [checks]: ./checks/ [codestyle]: ./CODESTYLE.md -[csp-scripts]: ./checks/check-csp-scripts.py +[csp]: ./checks/check-csp.py [operations]: ./OPERATIONS.md [metadata]: ./checks/check-media-metadata.py [normalize]: ./scripts/normalize-media.py diff --git a/README.md b/README.md index 682ca5e9..2ddb3066 100644 --- a/README.md +++ b/README.md @@ -189,12 +189,12 @@ For a defect in the site or the tooling, such as a broken link, a missing redire The required tools and their install commands are listed in [deploy/README.md][deploy-readme]. Hugo must be the extended build. -Build the site and verify it against the URL contract and the blog's Content-Security-Policy: +Build the site and verify it against the URL contract and both sites' Content-Security-Policies: ```sh hugo --gc --minify --panicOnWarning checks/check-url-parity.py public -checks/check-csp-scripts.py public +checks/check-csp.py public ``` Build a release and verify it against a running server: diff --git a/checks/README.md b/checks/README.md index 0dcd0663..1546482b 100644 --- a/checks/README.md +++ b/checks/README.md @@ -7,7 +7,7 @@ This README covers the URL contract alone. The other checks in this directory ea | Check | Fails when | | --- | --- | | [`check-csp-browser.py`][check-csp-browser] | a page on a running mirror makes the browser block something that `csp-browser-allow.txt` does not list | -| [`check-csp-scripts.py`][check-csp-scripts] | the scripts, frames, media, or objects the built site loads and the blog's policy disagree | +| [`check-csp.py`][check-csp] | a page or stylesheet on either site fetches something its site's policy does not allow, or a policy names another host | | [`check-env-docs.py`][check-env-docs] | a configuration value is declared without a description in `ENVIRONMENT.md`, or described there and declared nowhere | | [`check-github-env.py`][check-github-env] | the variables and secrets GitHub holds differ from the ones `ENVIRONMENT.md` lists | | [`check-media-metadata.py`][check-media-metadata] | carried media is not in a form the gate can vouch for | @@ -190,7 +190,7 @@ Three traps in that adjudication, each of which produced a wrong answer first an [caddy-dockerfile]: ./caddy/Dockerfile [capture-readme]: ../capture/README.md [check-csp-browser]: ./check-csp-browser.py -[check-csp-scripts]: ./check-csp-scripts.py +[check-csp]: ./check-csp.py [check-env-docs]: ./check-env-docs.py [check-github-env]: ./check-github-env.py [check-live-urls]: ./check-live-urls.sh diff --git a/checks/check-csp-browser.py b/checks/check-csp-browser.py index cc3fb038..6af97fae 100755 --- a/checks/check-csp-browser.py +++ b/checks/check-csp-browser.py @@ -1,8 +1,9 @@ #!/usr/bin/env python3 """Load a running site in headless Chromium and report what its Content-Security-Policy blocks. -`check-csp-scripts.py` proves the built scripts match the policy's hashes. It cannot see what a -browser blocks at run time: a style, a font, a fetch, an image, or a script a proxy injects. This +`check-csp.py` proves the built scripts match the policy's hashes and that every fetch the pages +and stylesheets name is one the policy allows. It cannot see what a browser blocks at run time: a +fetch a script makes, or a script a proxy injects. This check loads each page in a URL list against a mirror. It records every `securitypolicyviolation` event in the page and in each frame it holds, as it fires. The event fires for an enforcing policy and a Report-Only one alike. Each page is scrolled to its end so lazy images load, and its diff --git a/checks/check-csp-scripts.py b/checks/check-csp-scripts.py deleted file mode 100755 index f83261db..00000000 --- a/checks/check-csp-scripts.py +++ /dev/null @@ -1,239 +0,0 @@ -#!/usr/bin/env python3 -"""Verify that the blog's Content-Security-Policy allows every script and embed the built site loads. - -The policy is read from `checks/security-headers.txt`, and `deploy/Caddyfile`, which ships, must -send the same value. Without `script-src` the browser applies `default-src`, and so does this -check. Every inline script must carry a hash the policy lists. Every hash the policy lists -must match an inline script. A Hugo or theme update that changes a script's bytes then fails -here rather than in a reader's browser. A script loaded by URL must come from a host or a -scheme the policy names. An inline event handler or a `javascript:` URL fails outright, since a hash allows -neither without `'unsafe-hashes'`, which the blog policy does not carry. A script whose type the -browser runs is checked, an import map and a speculation rules block included. Any other type is -a data block that never runs, and the browser never checks an empty script, so neither needs a -hash. - -A frame, a media file, or an object loads under its own directive. A post may carry raw HTML, -so it can add one from a host the policy does not name. Each such load must come from a host or a -scheme its directive names. The browser's fallbacks apply: `child-src` then `default-src` for a -frame, and `default-src` for the others. An `about:` URL fetches nothing, so no directive applies. -""" - -import argparse -import base64 -import collections -import hashlib -import pathlib -import sys -from html.parser import HTMLParser -from urllib.parse import urlsplit - -CHECKS = pathlib.Path(__file__).resolve().parent -HEADERS = CHECKS / "security-headers.txt" -CADDYFILE = CHECKS.parent / "deploy" / "Caddyfile" -CSP = "Content-Security-Policy" - -SCRIPT_TYPES = { - "", - "module", - "importmap", - "speculationrules", - "application/ecmascript", - "application/javascript", - "application/x-ecmascript", - "application/x-javascript", - "text/ecmascript", - "text/javascript", - *(f"text/javascript1.{minor}" for minor in range(6)), - "text/jscript", - "text/livescript", - "text/x-ecmascript", - "text/x-javascript", -} - - -def blog_policy(headers_text): - """The blog's policy as security-headers.txt holds it, or None where the blog sends none.""" - for line in headers_text.splitlines(): - site, _, header = line.partition("\t") - name, _, value = header.partition(": ") - if site == "blog" and name == CSP: - return value - return None - - -FALLBACKS = { - "script-src": ("script-src", "default-src"), - "frame-src": ("frame-src", "child-src", "default-src"), - "media-src": ("media-src", "default-src"), - "object-src": ("object-src", "default-src"), -} - -EMBEDS = { - ("iframe", "src"): "frame-src", - ("frame", "src"): "frame-src", - ("audio", "src"): "media-src", - ("video", "src"): "media-src", - ("source", "src"): "media-src", - ("track", "src"): "media-src", - ("embed", "src"): "object-src", - ("object", "data"): "object-src", -} - - -def directive_sources(policy, directive): - """The sources the browser applies to a directive, its fallbacks included.""" - directives = {} - for part in policy.split(";"): - tokens = part.split() - if tokens: - directives.setdefault(tokens[0], tokens[1:]) - for name in FALLBACKS[directive]: - if name in directives: - return directives[name] - return [] - - -def script_sources(policy): - return directive_sources(policy, "script-src") - - -def digest(body): - return ( - "'sha256-" - + base64.b64encode(hashlib.sha256(body.encode()).digest()).decode() - + "'" - ) - - -class ScriptScan(HTMLParser): - def __init__(self): - super().__init__(convert_charrefs=False) - self.inline = [] - self.loaded = [] - self.embeds = [] - self.findings = [] - self.body = None - - def handle_starttag(self, tag, attrs): - for name, value in attrs: - url = (value or "").strip() - if name.startswith("on"): - self.findings.append(f"<{tag} {name}> is an inline event handler") - elif name in ("href", "src", "action") and url.lower().startswith( - "javascript:" - ): - self.findings.append(f"<{tag} {name}> is a javascript: URL") - elif (tag, name) in EMBEDS and url and not url.lower().startswith("about:"): - self.embeds.append((tag, name, url, EMBEDS[tag, name])) - if tag != "script": - return - attr = dict(attrs) - if (attr.get("type") or "").strip().lower() not in SCRIPT_TYPES: - return - if "src" not in attr: - self.body = [] - return - src = attr["src"] or "" - if not src.strip().lower().startswith("javascript:"): - self.loaded.append(src) - - def handle_data(self, data): - if self.body is not None: - self.body.append(data) - - def handle_endtag(self, tag): - if tag == "script" and self.body is not None: - self.inline.append("".join(self.body)) - self.body = None - - -def allowed(src, sources): - parts = urlsplit(src if not src.startswith("//") else "https:" + src) - if parts.netloc: - return f"{parts.scheme}://{parts.netloc}" in sources - if parts.scheme: - return f"{parts.scheme}:" in sources - return "'self'" in sources - - -def check(public, policy): - sources = script_sources(policy) - embed_sources = { - name: directive_sources(policy, name) for name in set(EMBEDS.values()) - } - used = collections.defaultdict(list) - findings = [] - for path in sorted(public.rglob("*.html")): - rel = path.relative_to(public).as_posix() - scan = ScriptScan() - scan.feed(path.read_text(encoding="utf-8")) - scan.close() - findings += [f"{rel}: {finding}" for finding in scan.findings] - findings += [ - f"{rel}: " +ORIGIN = "https://site.example.test" +OTHER = "https://cdn.example.test" def policy(*sources): @@ -33,7 +33,7 @@ def run_gate(self, pages, sources, csp=None): path.parent.mkdir(parents=True, exist_ok=True) path.write_text(html, encoding="utf-8") with contextlib.redirect_stdout(io.StringIO()): - return gate.check(public, csp or policy(*sources)) + return gate.check(public, csp or policy(*sources), ORIGIN) def test_a_listed_hash_for_every_inline_script_passes(self) -> None: sources = ["'self'", gate.digest(THEME)] @@ -95,8 +95,12 @@ def test_a_script_url_needs_its_origin_named(self) -> None: findings = self.run_gate({"index.html": html}, ["'self'"]) self.assertEqual(len(findings), 1) self.assertIn("does not name", findings[0]) - named = ["'self'", "https://cdn.example.test"] - self.assertEqual(self.run_gate({"index.html": other}, named), []) + named = ["'self'", OTHER] + findings = self.run_gate({"index.html": other}, named) + self.assertEqual( + findings, + [f"the policy names {OTHER} under script-src, which allows another host"], + ) def test_a_valueless_src_body_is_not_hashed(self) -> None: html = f"" @@ -149,15 +153,14 @@ def test_an_embed_from_the_site_itself_passes_under_default_src(self) -> None: html = '' self.assertEqual(self.run_gate({"index.html": html}, ["'self'"]), []) - def test_a_named_host_passes_and_the_fallback_order_holds(self) -> None: - html = '' - host = "https://maps.example.test" + def test_the_fallback_order_holds(self) -> None: + html = '' for csp, findings in ( - (f"default-src 'self'; frame-src {host}", 0), - (f"default-src 'self'; child-src {host}", 0), - (f"default-src {host}", 0), - (f"default-src {host}; frame-src 'self'", 1), - (f"default-src {host}; child-src 'self'", 1), + ("default-src 'self'; frame-src data:", 0), + ("default-src 'self'; child-src data:", 0), + ("default-src data:", 0), + ("default-src data:; frame-src 'self'", 1), + ("default-src data:; child-src 'self'", 1), ): with self.subTest(csp): found = self.run_gate({"index.html": html}, [], csp) @@ -183,9 +186,85 @@ def test_a_javascript_embed_is_reported_once(self) -> None: self.assertEqual(len(findings), 1) self.assertIn("javascript: URL", findings[0]) + def test_every_fetch_from_another_host_fails_under_its_directive(self) -> None: + for html, directive in ( + (f'', "img-src"), + (f'', "img-src"), + (f'', "img-src"), + (f'', "img-src"), + (f'', "img-src"), + (f'', "style-src"), + (f'', "img-src"), + (f'', "font-src"), + (f'', "default-src"), + (f'', "script-src"), + (f'', "manifest-src"), + (f'', "default-src"), + ('', "default-src"), + (f'

x

', "img-src"), + (f"", "style-src"), + ): + with self.subTest(html): + findings = self.run_gate({"index.html": html}, []) + self.assertEqual(len(findings), 1, findings) + self.assertIn(f"{directive} does not name", findings[0]) + + def test_a_link_that_loads_nothing_is_not_checked(self) -> None: + html = ( + f'' + f'' + '' + ) + self.assertEqual(self.run_gate({"index.html": html}, []), []) + + def test_the_site_itself_passes_by_path_and_by_its_own_origin(self) -> None: + html = ( + '' + f'' + '' + ) + self.assertEqual(self.run_gate({"index.html": html}, []), []) + elsewhere = html.replace(ORIGIN, ORIGIN + ":8443") + self.assertEqual(len(self.run_gate({"index.html": elsewhere}, [])), 1) + + def test_a_stylesheet_is_read_by_where_each_url_sits(self) -> None: + css = ( + "/* url(https://comment.example.test/a.png) */" + f'@import url("{OTHER}/a.css");' + f"@font-face {{ font-family: A; src: url('{OTHER}/a.woff2') format('woff2'); }}" + f"body {{ background: url({OTHER}/b.png), url(#grain); }}" + ) + findings = self.run_gate({"assets/site.css": css}, []) + self.assertEqual( + [f.split(" does not name")[0].rsplit(" ", 1)[1] for f in findings], + ["style-src", "font-src", "img-src"], + ) + self.assertTrue(all(f.startswith("assets/site.css: url(") for f in findings)) + + def test_a_listed_scheme_passes_in_a_stylesheet(self) -> None: + css = "html { background: url(\"data:image/svg+xml,%3Csvg filter='url(%23n)'/%3E\"); }" + csp = "default-src 'self'; img-src 'self' data:" + self.assertEqual(self.run_gate({"site.css": css}, [], csp), []) + self.assertEqual(len(self.run_gate({"site.css": css}, [])), 1) + + def test_a_style_block_and_a_font_face_in_it_are_read(self) -> None: + html = f"" + findings = self.run_gate({"index.html": html}, []) + self.assertEqual(len(findings), 1) + self.assertIn("", "style-src"), + (f'', "style-src"), + ( + f'', + "connect-src", + ), ): with self.subTest(html): findings = self.run_gate({"index.html": html}, []) @@ -281,16 +286,21 @@ def test_a_site_without_a_policy_reads_as_none(self) -> None: self.assertIsNone(gate.site_policy(text, "blog")) def test_script_src_is_read_and_default_src_stands_in_for_it(self) -> None: - sources = gate.script_sources(policy("'self'", "'sha256-AAAA'")) + sources = gate.directive_sources( + policy("'self'", "'sha256-AAAA'"), "script-src" + ) self.assertEqual(sources, ["'self'", "'sha256-AAAA'"]) - fallback = gate.script_sources("default-src 'self'; img-src 'self'") + fallback = gate.directive_sources( + "default-src 'self'; img-src 'self'", "script-src" + ) self.assertEqual(fallback, ["'self'"]) - self.assertEqual(gate.script_sources("img-src 'self'"), []) + self.assertEqual(gate.directive_sources("img-src 'self'", "script-src"), []) def test_a_host_or_a_network_scheme_is_named_and_nothing_else(self) -> None: csp = ( f"default-src 'self'; img-src 'self' data: blob: {OTHER} https:; " - "connect-src wss: *; script-src 'sha256-AAAA' 'unsafe-inline'" + "connect-src wss: *; script-src 'sha256-AAAA' 'unsafe-inline'; " + "sandbox allow-scripts; report-to csp-group" ) self.assertEqual( gate.named_hosts(csp), diff --git a/deploy/README.md b/deploy/README.md index 8367c524..f9b4b7c5 100644 --- a/deploy/README.md +++ b/deploy/README.md @@ -305,7 +305,7 @@ The HSTS value carries neither `includeSubDomains` nor `preload`. `includeSubDom **Each site sends its own `Content-Security-Policy`.** The family page loads few resources, so its policy names each kind: its own files and fonts, inline styles, and `data:` images. The blog policy allows its own files and fonts, and inline styles, which the theme and some archived posts carry. Both sites serve their fonts themselves, under `/fonts/`. Neither policy names another host, so every resource either site loads is its own. An embed from another host, such as a video or a map, is therefore not possible. The browser would block it, and the release build fails on it. A scheme such as `data:` names no host, so a directive may list one. Its `script-src` lists a hash for each inline script the theme writes, rather than allowing every inline script. -**A Hugo update, a theme update, or a theme setting in `hugo.yaml` can change those scripts.** [`checks/check-csp.py`][checks-csp] runs in the release build and fails when the hashes and the built scripts differ. It also fails when a page or stylesheet on either site fetches something its policy does not allow, or when a policy names another host. The fix is to copy each hash it reports into both the Caddyfile and `security-headers.txt`, and to drop each hash it reports as unused. +**A Hugo update, a theme update, or a theme setting in `hugo.yaml` can change those scripts.** [`checks/check-csp.py`][checks-csp] runs in the release build and fails when the hashes and the built scripts differ. For a hash, the fix is to copy each one it reports into both the Caddyfile and `security-headers.txt`, and to drop each one it reports as unused. It also fails when a page or stylesheet on either site fetches something its policy does not allow, or when a policy names another host. The fix there is to serve the resource from the site itself, and to drop the host from the policy. **The gate reads only the files each site ships, so a policy change is also tried in a browser.** [`checks/check-csp-browser.py`][checks-csp-browser] loads every page of a running mirror in headless Chromium. It fails on anything the policy blocks that [`checks/csp-browser-allow.txt`][checks-csp-browser-allow] does not list. A run over the whole default list also fails on an entry there that nothing matched. The list is empty today, so any blocked load fails the run. [OPERATIONS.md "Local Verification"][operations-local-verification] says when it runs and how. From d4b1886edf2c3e0e6a923e7e6d173280b82781c8 Mon Sep 17 00:00:00 2001 From: Pieter Viljoen Date: Sun, 25 Oct 2026 15:49:31 -0700 Subject: [PATCH 4/5] Read the Navigation Source Lists as Naming a Host Too base-uri, form-action, frame-ancestors, and navigate-to each list sources, so a host there fails like one under a -src directive. Split the deploy README's hash remedy into two sentences. Co-Authored-By: Claude Opus 5.5 --- checks/check-csp.py | 9 +++++---- checks/tests/test_check_csp.py | 3 ++- deploy/README.md | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/checks/check-csp.py b/checks/check-csp.py index 47bb88cb..42e903a2 100755 --- a/checks/check-csp.py +++ b/checks/check-csp.py @@ -140,6 +140,7 @@ def site_policy(headers_text, site): } NETWORK_SCHEMES = {"http", "https", "ws", "wss"} +NAVIGATION_LISTS = {"base-uri", "form-action", "frame-ancestors", "navigate-to"} COMMENT = re.compile(r"/\*.*?\*/", re.DOTALL) IMPORT = re.compile( @@ -169,14 +170,14 @@ def directive_sources(policy, directive): def named_hosts(policy): - """Each fetch directive's sources that allow a load from another host, as (directive, source). + """Each source that names another host, as (directive, source). - Only a `-src` directive lists sources, so a token under any other directive, such as a - `sandbox` flag, names no host. + A source list is held by each `-src` directive and by the navigation directives, such as + `form-action`. A token under any other directive, such as a `sandbox` flag, names no host. """ hosts = [] for directive, sources in directives(policy).items(): - if not directive.endswith("-src"): + if not directive.endswith("-src") and directive not in NAVIGATION_LISTS: continue for token in sources: scheme = token[:-1].lower() if token.endswith(":") else None diff --git a/checks/tests/test_check_csp.py b/checks/tests/test_check_csp.py index 3add4f4e..88591d11 100644 --- a/checks/tests/test_check_csp.py +++ b/checks/tests/test_check_csp.py @@ -300,7 +300,7 @@ def test_a_host_or_a_network_scheme_is_named_and_nothing_else(self) -> None: csp = ( f"default-src 'self'; img-src 'self' data: blob: {OTHER} https:; " "connect-src wss: *; script-src 'sha256-AAAA' 'unsafe-inline'; " - "sandbox allow-scripts; report-to csp-group" + f"sandbox allow-scripts; report-to csp-group; form-action 'self' {OTHER}" ) self.assertEqual( gate.named_hosts(csp), @@ -309,6 +309,7 @@ def test_a_host_or_a_network_scheme_is_named_and_nothing_else(self) -> None: ("img-src", "https:"), ("connect-src", "wss:"), ("connect-src", "*"), + ("form-action", OTHER), ], ) diff --git a/deploy/README.md b/deploy/README.md index f9b4b7c5..f4cd760c 100644 --- a/deploy/README.md +++ b/deploy/README.md @@ -305,7 +305,7 @@ The HSTS value carries neither `includeSubDomains` nor `preload`. `includeSubDom **Each site sends its own `Content-Security-Policy`.** The family page loads few resources, so its policy names each kind: its own files and fonts, inline styles, and `data:` images. The blog policy allows its own files and fonts, and inline styles, which the theme and some archived posts carry. Both sites serve their fonts themselves, under `/fonts/`. Neither policy names another host, so every resource either site loads is its own. An embed from another host, such as a video or a map, is therefore not possible. The browser would block it, and the release build fails on it. A scheme such as `data:` names no host, so a directive may list one. Its `script-src` lists a hash for each inline script the theme writes, rather than allowing every inline script. -**A Hugo update, a theme update, or a theme setting in `hugo.yaml` can change those scripts.** [`checks/check-csp.py`][checks-csp] runs in the release build and fails when the hashes and the built scripts differ. For a hash, the fix is to copy each one it reports into both the Caddyfile and `security-headers.txt`, and to drop each one it reports as unused. It also fails when a page or stylesheet on either site fetches something its policy does not allow, or when a policy names another host. The fix there is to serve the resource from the site itself, and to drop the host from the policy. +**A Hugo update, a theme update, or a theme setting in `hugo.yaml` can change those scripts.** [`checks/check-csp.py`][checks-csp] runs in the release build and fails when the hashes and the built scripts differ. For a hash, the fix is to copy each one it reports into both the Caddyfile and `security-headers.txt`. Drop each one it reports as unused. It also fails when a page or stylesheet on either site fetches something its policy does not allow, or when a policy names another host. The fix there is to serve the resource from the site itself, and to drop the host from the policy. **The gate reads only the files each site ships, so a policy change is also tried in a browser.** [`checks/check-csp-browser.py`][checks-csp-browser] loads every page of a running mirror in headless Chromium. It fails on anything the policy blocks that [`checks/csp-browser-allow.txt`][checks-csp-browser-allow] does not list. A run over the whole default list also fails on an entry there that nothing matched. The list is empty today, so any blocked load fails the run. [OPERATIONS.md "Local Verification"][operations-local-verification] says when it runs and how. From 7195d753ef91f665fae05ac9ed4d1930f48c891b Mon Sep 17 00:00:00 2001 From: Pieter Viljoen Date: Sun, 25 Oct 2026 16:00:27 -0700 Subject: [PATCH 5/5] Read report-uri as Naming a Host Its value is the URL violation reports go to, so a collector on another host would otherwise pass the rule that neither policy names one. Co-Authored-By: Claude Opus 5.5 --- checks/check-csp.py | 9 +++++---- checks/tests/test_check_csp.py | 4 +++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/checks/check-csp.py b/checks/check-csp.py index 42e903a2..99d04c10 100755 --- a/checks/check-csp.py +++ b/checks/check-csp.py @@ -140,7 +140,7 @@ def site_policy(headers_text, site): } NETWORK_SCHEMES = {"http", "https", "ws", "wss"} -NAVIGATION_LISTS = {"base-uri", "form-action", "frame-ancestors", "navigate-to"} +URL_LISTS = {"base-uri", "form-action", "frame-ancestors", "navigate-to", "report-uri"} COMMENT = re.compile(r"/\*.*?\*/", re.DOTALL) IMPORT = re.compile( @@ -172,12 +172,13 @@ def directive_sources(policy, directive): def named_hosts(policy): """Each source that names another host, as (directive, source). - A source list is held by each `-src` directive and by the navigation directives, such as - `form-action`. A token under any other directive, such as a `sandbox` flag, names no host. + Each `-src` directive holds a source list, and so do the navigation directives, such as + `form-action`. `report-uri` holds the URL reports go to. A token under any other directive, + such as a `sandbox` flag or a `report-to` group, names no host. """ hosts = [] for directive, sources in directives(policy).items(): - if not directive.endswith("-src") and directive not in NAVIGATION_LISTS: + if not directive.endswith("-src") and directive not in URL_LISTS: continue for token in sources: scheme = token[:-1].lower() if token.endswith(":") else None diff --git a/checks/tests/test_check_csp.py b/checks/tests/test_check_csp.py index 88591d11..20ff04e8 100644 --- a/checks/tests/test_check_csp.py +++ b/checks/tests/test_check_csp.py @@ -300,7 +300,8 @@ def test_a_host_or_a_network_scheme_is_named_and_nothing_else(self) -> None: csp = ( f"default-src 'self'; img-src 'self' data: blob: {OTHER} https:; " "connect-src wss: *; script-src 'sha256-AAAA' 'unsafe-inline'; " - f"sandbox allow-scripts; report-to csp-group; form-action 'self' {OTHER}" + f"sandbox allow-scripts; report-to csp-group; form-action 'self' {OTHER}; " + f"report-uri {OTHER}/csp" ) self.assertEqual( gate.named_hosts(csp), @@ -310,6 +311,7 @@ def test_a_host_or_a_network_scheme_is_named_and_nothing_else(self) -> None: ("connect-src", "wss:"), ("connect-src", "*"), ("form-action", OTHER), + ("report-uri", f"{OTHER}/csp"), ], )