Skip to content

feat(rules): security/csp-violations reports what the browser blocked during rendered crawls #425 - #2265

Merged
nc9 merged 1 commit into
mainfrom
boat/csp-violations-425
Nov 22, 2026
Merged

nc9 merged 1 commit into
mainfrom
boat/csp-violations-425

Conversation

@nc9

@nc9 nc9 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Summary

New page rule security/csp-violations: on a rendered crawl it reports what the browser actually blocked under the page's Content-Security-Policy, grouped by enforced directive and blocked host, with the host to add. Closes #425.

Data path (public half; the renderer listener is private and tracked there):

  • core-contracts: RenderCspViolation and RenderRuntimeDiagnostics.cspViolations (absent = not recorded, [] = none seen), MAX_CSP_VIOLATIONS_PER_PAGE, RenderRuntimeSink.
  • audit-engine/cloud-fetcher: mapRenderItemToResponse passes the render item's runtime block through as FetchResponse.renderRuntime, via boundRenderRuntime (flat copies, count caps, 1 KB per string, a 64 KB per-page budget with CSP violations kept first).
  • fetchers / crawler: renderRuntime on the fetch result; new opt-in renderRuntimeSink (set or cleared on every page commit), like renderedRequestSink.
  • audit-engine/streaming + adapter: renderRuntime option sets PageData.renderRuntime; a page with runtime never replays from the rule cache (its key does not hash this in-memory data).
  • CLI controller wires the sink from crawl to the streamed rules pass and clears it after.
  • Rule: fail when any blocked load is a known analytics, ads, payments, captcha, form or booking vendor (the csp-vendors host table, plus www.google.com vendor paths /g/collect, /pagead/, /ads/, /recaptcha/), warn otherwise. Report-only violations are ignored. Query strings are never printed. Raw pages skip with a reason pointing at --render; rendered pages without recorded violations skip with runtime-not-recorded (listed as not checked, never a pass). Listed in RUNS_ON_UNREAD_PAGE (reads no parsed field) and declares meta.checks.

The "analytics detected but no hit left the page" follow-on from the issue is not in this PR.

Acceptance criteria

  • Script injected after load from a host script-src omits: one finding naming the host and script-src-elem. packages/rules/tests/csp-violations.test.ts "a script injected after load ..." (violations produced by a browser stand-in that judges each load with the shared CSP source matcher).
  • Ads-linked gtag page, connect-src limited to www.google-analytics.com: status fail (error), items analytics.google.com/g/collect, www.google.com/g/collect, stats.g.doubleclick.net/g/collect with categories analytics/analytics/ads. Same file, "an Ads-linked gtag page ...".
  • Same fixtures with a corrected policy pass: "the same pages with a corrected policy pass" (the stand-in produces no violations because the policy now allows the loads).
  • HTTP-only audits skip with a clear reason: "an HTTP-only (non-rendered) page skips ..."; golden finding-messages.json pins the skip line on all 500 synthetic pages.
  • (public half) Rendered page results carry CSP violations, failed requests and page errors: cloud-fetcher-runtime-425.test.ts, render-runtime-stream-425.test.ts, and end to end apps/cli/tests/audit/csp-violations-controller-425.test.ts (fails with skipped when the controller wiring is removed). Docs page docs/rules/security/csp-violations.mdx. The renderer side (a securitypolicyviolation init-script listener kept a few seconds after load, filling runtime.cspViolations) is private and not in this PR; until it ships, rendered pages report runtime-not-recorded, as the docs say.

Verification

  • Typecheck: core-contracts, fetchers, crawler, rules, audit-engine, apps/cli: clean.
  • apps/cli format:check and lint: clean. Changed packages/* files: oxfmt --check and oxlint clean on new files; existing-file lint hits are on untouched lines.
  • scripts/test-suites.ts --run rules 12653 pass 0 fail; --run audit-engine 2426 pass 0 fail; --run crawler 1186 pass 0 fail; --run rest 3946 pass 0 fail; --run cli 4664 pass 0 fail; bun run test:engine 273 pass 0 fail; --check ok.
  • Goldens: security/csp-violations: 500 (one skip per synthetic page), health score unchanged at 58; finding-messages snapshot adds the one skip line.
  • bun run docs:check: pass. bun scripts/changelog.ts check: OK.
  • Mutation checks: removing the /g/collect vendor path, the raw-page skip, or the report-only filter each fails its test; removing the controller wiring fails the controller test.

Docs: new rules/security/csp-violations, security hub row and counts (32 to 33) in README, docs index and rules index, CSP glossary term. Skill: none (no agent behaviour changes).

… during rendered crawls #425

Render results can now carry the CSP violations the browser raised
(RenderRuntimeDiagnostics.cspViolations). The cloud fetcher passes the
runtime block through, bounded and shape-checked; the crawler keeps it in
an in-memory sink keyed by page; the streamed page pass hands it to rules as
PageData.renderRuntime (such pages skip the rule cache, whose key does not
cover it). The new page rule groups enforced violations by directive and
host, fails for known analytics, ads, payments, captcha, form or booking
vendors and warns otherwise. Raw pages skip with a reason; rendered pages
without recorded violations are the runtime-not-recorded gap.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 marked this pull request as ready for review November 22, 2026 21:50
@nc9
nc9 merged commit 16d4ff8 into main Nov 22, 2026
10 checks passed
@nc9
nc9 deleted the boat/csp-violations-425 branch November 22, 2026 22:28
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.

New rule: security/csp-violations - report what the browser actually blocked during rendered crawls

1 participant