Skip to content

fix(browser): make the automation CSP apply, and test the Chrome paths - #183

Merged
JustMaris merged 2 commits into
mainfrom
test/chrome-fixtures
Oct 17, 2026
Merged

JustMaris merged 2 commits into
mainfrom
test/chrome-fixtures

Conversation

@JustMaris

Copy link
Copy Markdown
Member

A security layer that never ran

While writing a fixture for the CSP that the automation session adds to every page, the test showed the policy was not applied at all. Debug logging confirmed it: documents were paused only at the request stage, never at the response stage where the policy is added, so that code path (about 5 statements, flagged as uncovered) had never executed.

Two Chrome details were behind it, both fixed in restrictDocument / checkRequest:

  1. A document is only paused again at the response stage when its ContinueRequest sets InterceptResponse.
  2. Even then, Chrome applies only the sandbox part of a CSP header edited with Fetch.continueResponse. worker-src and connect-src are ignored. Fulfilling the response with its own body and the new header makes the whole policy count. Redirects continue untouched, and a response with nothing to read falls back to the header edit.

What this means: the request-level destination checks (the main control) were always working. This was the extra layer meant to stop popups, workers and stray channels the interceptor can't see. Popups opened with window.open were not blocked before; now they are, and a page-started worker is refused. TestInjectedCSPApplies runs a real page against it. All existing Chrome tests and the MCP demo workflow pass with the policy active.

Also in this PR

  • Screenshots are named by their real format. Chrome returns JPEG below quality 100, and these were JPEG data in .png files. Nothing depended on the name (the screenshot route sniffs the content type).
  • Chrome fixtures: why a control can't be used (option not found, wrong value, not a form field, unsupported action, removed or disabled after the snapshot), an unloadable page, and Chrome that can't start.
  • No-Chrome tests: session setup, snapshot fingerprints, policy edge cases, the private-address handler (now a method, so it can be tested with a fake event).
  • IMAP expunge warnings: archiving by COPY and EXPUNGE warns before and after when other mail is already flagged \Deleted, and the test shows the stray message really is removed. Another case checks it stays quiet otherwise.
  • docs/code-patterns.md records the Chrome quirk so it isn't reintroduced.

Verified: full suite, race tests, lint, and GOOS=windows go vet. Statement coverage 90.9% on this branch alone.

The document CSP that keeps popups, workers and stray connections out of an agent-driven page never ran: Chrome paused documents only at the request stage, and only applies the sandbox part of a CSP header edited with continueResponse. Documents are now paused at the response stage and fulfilled with the policy.

Screenshots are named by their real format (JPEG was written to .png files).

Adds Chrome fixtures for control errors, the CSP and unloadable pages, and tests for the IMAP expunge warnings.
@codecov

codecov Bot commented Oct 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.33333% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/browser/automation.go 81.25% 4 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

@JustMaris
JustMaris merged commit c6daf91 into main Oct 17, 2026
15 checks passed
@JustMaris
JustMaris deleted the test/chrome-fixtures branch October 17, 2026 14:15
@dnb-robot dnb-robot Bot mentioned this pull request Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant