Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion OPERATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Expect a `PASS` naming the number of URLs honored. That is the three lists' comb

A documentation-only or workflow-only change does not need this. A change to the four paths above does, because for those CI's green is not evidence.

**A change to a `Content-Security-Policy` in `security-headers.txt` is also tried in a browser**, on the local staging mirror, before its pull request opens. So is a theme or `layouts/` change that adds a style, a font, a fetch, or a script. [`checks/check-csp-browser.py`](./checks/check-csp-browser.py) loads every page of `golden-urls.txt` plus `/search/` in headless Chromium. It scrolls each page to its end, clicks its code-copy buttons, and types a search query. It then reports each `securitypolicyviolation` event by directive and blocked origin, with the first page it occurred on. The event fires for an enforcing policy and a `Content-Security-Policy-Report-Only` one alike, so a trial can run Report-Only first:
**A change to a `Content-Security-Policy` in `security-headers.txt` is also tried in a browser**, on the local staging mirror, before its pull request opens. So is a theme or `layouts/` change that adds a style, a font, a fetch, or a script. [`checks/check-csp-browser.py`](./checks/check-csp-browser.py) loads every page of `golden-urls.txt` plus `/search/` in headless Chromium. It scrolls each page to its end, clicks its code-copy buttons, and types a search query. It reads each `securitypolicyviolation` event from the page and from each frame in it. It reports them by directive and blocked origin, with the first page each occurred on. The event fires for an enforcing policy and a `Content-Security-Policy-Report-Only` one alike, so a trial can run Report-Only first:

```sh
set -e
Expand Down
19 changes: 12 additions & 7 deletions checks/check-csp-browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
`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 loads each page in a URL list against a mirror. It records every `securitypolicyviolation`
event, which 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.
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.

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
Expand Down Expand Up @@ -179,8 +180,7 @@ def run_probe(command, paths, timeout):


LISTENER = """
window.__csp = [];
document.addEventListener('securitypolicyviolation', (e) => window.__csp.push({
document.addEventListener('securitypolicyviolation', (e) => window.__cspReport({
directive: e.effectiveDirective, blocked: e.blockedURI, disposition: e.disposition}));
"""

Expand All @@ -195,6 +195,10 @@ def emit(record):
with sync_playwright() as driver:
browser = driver.chromium.launch()
context = browser.new_context()
violations = []
context.expose_binding(
"__cspReport", lambda _, record: violations.append(record)
)
context.add_init_script(LISTENER)
page = context.new_page()
for path in paths:
Expand All @@ -214,10 +218,11 @@ def emit(record):
)
page.locator("#searchResults li").first.wait_for()
page.wait_for_load_state("networkidle")
for violation in page.evaluate("window.__csp"):
emit({"page": path, **violation})
except Error as error:
emit({"page": path, "error": str(error).splitlines()[0]})
for violation in violations:
emit({"page": path, **violation})
violations.clear()
browser.close()


Expand Down
Loading