diff --git a/OPERATIONS.md b/OPERATIONS.md index d225c6b..c1799a1 100644 --- a/OPERATIONS.md +++ b/OPERATIONS.md @@ -104,7 +104,7 @@ checks/check-csp-browser.py "$SITE_BASE_URL" - **It needs Docker and `uv` on the host, and no browser.** Chromium runs in the Playwright image [`checks/browser/Dockerfile`](./checks/browser/Dockerfile) pins, where Dependabot moves it. `uv` installs the Playwright package inside it at the image tag's own version, so the two cannot drift. Its cache is `UV_CACHE_DIR` where that is set, or a directory under the system temporary root. - **The container shares the host network**, so it reaches a local mirror by the name the host resolves. -- **A violation fails the run unless [`checks/csp-browser-allow.txt`](./checks/csp-browser-allow.txt) lists it.** Each line is a directive, a blocked origin, and the reason, tab separated. A blocked scheme is written as the browser reports it, such as `data` with no colon. An entry that nothing matched fails a run over the default list, so an entry goes when its cause does. `--urls` takes another list, and such a partial run leaves unused entries alone. +- **A violation fails the run unless [`checks/csp-browser-allow.txt`](./checks/csp-browser-allow.txt) lists it.** Each line is a directive, a blocked origin, and the reason, tab separated. A blocked scheme is written as the browser reports it, such as `data` with no colon. An entry that nothing matched fails a run over the default list, so an entry goes when its cause does. `--urls` takes another list, and such a partial run leaves unused entries alone. It also loads `/search/` only where the list names it, since a list can be for the family site, which has no search page. - **A page that does not answer `200` fails the run**, since a trial over error pages proves nothing. - **It runs only against the local mirrors.** VPS staging sits behind the auth gate, which the check does not present. A full run is hundreds of pages. Production takes only a handful of probes per pass, since a burst there can get the reviewing network banned. - **It runs by hand rather than in the release.** It needs Docker, a browser image, and a running server, and neither the release build nor the unit suite has all three. diff --git a/checks/check-csp-browser.py b/checks/check-csp-browser.py index 6af97fa..2117af2 100755 --- a/checks/check-csp-browser.py +++ b/checks/check-csp-browser.py @@ -8,7 +8,7 @@ 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 code-copy buttons are clicked. The search page also gets a typed query, so the index fetch and the -results run. +results run. A run over the default list always loads it, and another list only where it names it. The browser runs in the Playwright image `browser/Dockerfile` pins, where Dependabot moves it. The Playwright package is installed at the image tag's own version, so the two never disagree. The @@ -48,15 +48,19 @@ def pinned_image(dockerfile_text): return found.group(1), found.group(2) -def read_paths(texts): - """The site paths the lists name, in order, once each, the search page always among them.""" +def read_paths(texts, search): + """The site paths the lists name, in order, once each, and the search page where asked. + + Only a run over the default list asks, since another list can be for a site with no search + page. A list that wants it names it. + """ paths = [] for text in texts: for line in text.splitlines(): path = line.strip() if path and path not in paths: paths.append(path) - if SEARCH_PATH not in paths: + if search and SEARCH_PATH not in paths: paths.append(SEARCH_PATH) return paths @@ -234,7 +238,10 @@ def main(argv=None): "--urls", type=pathlib.Path, action="append", - help=f"a list of site paths, repeatable (default {URLS.name})", + help=( + f"a list of site paths, repeatable (default {URLS.name}, plus {SEARCH_PATH}," + f" which another list names where it wants it)" + ), ) parser.add_argument( "--probe", @@ -253,7 +260,12 @@ def main(argv=None): image, version = pinned_image(DOCKERFILE.read_text(encoding="utf-8")) allow = read_allow(ALLOW.read_text(encoding="utf-8")) - paths = read_paths(p.read_text(encoding="utf-8") for p in (args.urls or [URLS])) + paths = read_paths( + (p.read_text(encoding="utf-8") for p in (args.urls or [URLS])), + search=args.urls is None, + ) + if not paths: + parser.error("the --urls lists name no page, so there is nothing to load") cache = pathlib.Path( os.environ.get("UV_CACHE_DIR") or pathlib.Path(tempfile.gettempdir()) / "blog-csp-browser-uv" diff --git a/checks/tests/test_check_csp_browser.py b/checks/tests/test_check_csp_browser.py index b650386..64586c6 100644 --- a/checks/tests/test_check_csp_browser.py +++ b/checks/tests/test_check_csp_browser.py @@ -56,13 +56,17 @@ def test_the_committed_allow_list_parses(self) -> None: class ListTest(unittest.TestCase): def test_paths_keep_order_drop_repeats_and_add_search(self) -> None: - paths = gate.read_paths(["/\n/a/\n\n", "/a/\n/b/\n"]) + paths = gate.read_paths(["/\n/a/\n\n", "/a/\n/b/\n"], search=True) self.assertEqual(paths, ["/", "/a/", "/b/", gate.SEARCH_PATH]) def test_a_listed_search_page_is_not_added_twice(self) -> None: - paths = gate.read_paths([f"{gate.SEARCH_PATH}\n/\n"]) + paths = gate.read_paths([f"{gate.SEARCH_PATH}\n/\n"], search=True) self.assertEqual(paths, [gate.SEARCH_PATH, "/"]) + def test_paths_not_asking_for_search_keep_only_their_own(self) -> None: + paths = gate.read_paths(["/en/\n/af/\n"], search=False) + self.assertEqual(paths, ["/en/", "/af/"]) + def test_an_allow_line_without_a_reason_is_refused(self) -> None: with self.assertRaises(ValueError): gate.read_allow("img-src\thttps://images.example\n") @@ -135,7 +139,9 @@ def setUp(self) -> None: def run_main(self, records, argv=()): with ( - mock.patch.object(gate, "run_probe", return_value=(records, None)), + mock.patch.object( + gate, "run_probe", return_value=(records, None) + ) as self.probe, mock.patch.object(gate, "docker_command", return_value=["true"]), mock.patch.object(gate, "read_allow", return_value=[]), contextlib.redirect_stdout(io.StringIO()) as out, @@ -144,11 +150,21 @@ def run_main(self, records, argv=()): return code, out.getvalue() def test_every_page_loaded_clean_passes(self) -> None: - paths = gate.read_paths([gate.URLS.read_text(encoding="utf-8")]) + paths = gate.read_paths([gate.URLS.read_text(encoding="utf-8")], search=True) code, out = self.run_main([loaded(path) for path in paths]) self.assertEqual(code, 0, out) self.assertIn("PASS", out) + def test_a_run_over_another_list_loads_only_its_own_pages(self) -> None: + with tempfile.TemporaryDirectory() as folder: + urls = pathlib.Path(folder) / "family-urls.txt" + urls.write_text("/en/\n/af/\n", encoding="utf-8") + code, out = self.run_main( + [loaded("/en/"), loaded("/af/")], ["--urls", str(urls)] + ) + self.assertEqual(code, 0, out) + self.assertEqual(self.probe.call_args.args[1], ["/en/", "/af/"]) + def test_a_page_that_never_reported_fails(self) -> None: code, out = self.run_main([loaded("/")]) self.assertEqual(code, 1) @@ -181,6 +197,19 @@ def test_a_relative_base_url_is_a_usage_error(self) -> None: gate.main(["mirror.example"]) self.assertEqual(raised.exception.code, 2) + def test_a_list_naming_no_page_is_a_usage_error(self) -> None: + with tempfile.TemporaryDirectory() as folder: + urls = pathlib.Path(folder) / "empty-urls.txt" + urls.write_text("\n \n", encoding="utf-8") + with ( + mock.patch.object(gate, "run_probe") as probe, + contextlib.redirect_stderr(io.StringIO()), + self.assertRaises(SystemExit) as raised, + ): + gate.main([BASE, "--urls", str(urls)]) + self.assertEqual(raised.exception.code, 2) + probe.assert_not_called() + class CommandTest(unittest.TestCase): def test_the_container_installs_the_tag_version_and_reaches_the_host(self) -> None: