Skip to content

feat(rules): security/csp reports whether the policy would contain a hijacked third-party script #2087 - #2258

Merged
nc9 merged 1 commit into
mainfrom
boat/csp-third-party-2087
Nov 22, 2026
Merged

nc9 merged 1 commit into
mainfrom
boat/csp-third-party-2087

Conversation

@nc9

@nc9 nc9 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Closes #2087

Summary

security/csp now says whether an enforced policy would contain a hijacked third-party script, on the judged page (the entry page, or each listed page on a targeted run) when it loads at least one third-party <script src>:

  • csp-connect-unrestricted (warn): no connect-src and no default-src, or the one browsers read holds *, a * host (https://*) or a network scheme-only source (https:, http:, wss:, ws:). Message: Script on the page can send data to any host.
  • csp-strict-dynamic-third-party (info): the script list for elements holds 'strict-dynamic', so scripts vendors insert are trusted too.
  • csp-no-reporting (info): enforced policy with neither report-to nor report-uri.
  • csp-unsafe-scripts: when 'unsafe-eval' is allowed and the page loads third-party scripts, the value names the vendor hosts and details.thirdPartyHosts is added. Without third-party scripts the check is byte-for-byte unchanged.

Design notes:

  • Extended security/csp instead of a new rule, so the judgement stays on the entry page and a site-wide policy is one finding, not one per page.
  • csp.ts is part of the dependency-free @squirrelscan/rules/security-headers Worker entry (guarded by security-headers-entry.test.ts), so it cannot import tldts or the DOM helpers. Script srcs come from the page-time collected signals (streaming) or a duck-typed read of the live document (v1), skipping <noscript>. "Third party" is a host outside the page's own domain: last two labels of the page host, three for a country suffix like co.uk. Without a public suffix list this errs toward first party (another tenant of a shared hosting domain is missed); documented.
  • Report-only policies get none of the new checks (csp-report-only already covers them). Comma-joined policies are judged per policy: one restricting connections restricts them; 'strict-dynamic' counts only if every script-controlling policy has it.

Acceptance criteria

  • No connect-src / no restricting default-src with a third-party script: finding. Test csp-connect-unrestricted (#2087) > no connect-src and no default-src: script can send data anywhere.
  • *, https:, other scheme-only (wss:), https://*, inherited default-src */https: fire; host lists, 'self', 'none', subdomain wildcard, data:/blob:, a second restricting policy do not. Tests ... %s fires and names the directive (6) and ... %s does not fire (7).
  • 'strict-dynamic' with third-party scripts: info. Tests in csp-strict-dynamic-third-party (#2087) (4).
  • 'unsafe-eval' with third-party scripts reported with that context; unchanged without them. Tests in csp-unsafe-scripts 'unsafe-eval' context (#2087) (3).
  • Enforced policy without report-to/report-uri: info. Tests in csp-no-reporting (#2087) (4).
  • No third-party script: none of the new findings (first-party and same-site subdomain scripts, no scripts, vendor in <noscript>, non-http src, report-only, collected first-party srcs). Tests in no third-party script, no new findings and streaming path reads the collected script srcs.
  • Each new check has a firing and a non-firing fixture; existing csp tests pass (csp.test.ts, csp-evidence-1735, csp-script-src-elem-1600, csp-self-proxy-1604, csp-chunk-only-776, targeted-header-rules-1778, security-header-check-names-1737). Reverting csp.ts makes 13 of the new tests fail (all the positive cases).
  • Docs: docs/rules/security/csp.mdx describes each check, a "Third-party scripts" section with the limits (reads the header and fetched HTML, executes nothing, no PSL), the check-names table and a fix example. No new rule, so README and category tables are unchanged.

Gates

  • packages/rules: bun run typecheck pass; oxfmt --check and oxlint on the changed files pass.
  • bun run scripts/test-suites.ts --run rules: 12680 pass, 0 fail (416 files).
  • csp-third-party-containment-2087.test.ts: 33+7 = 40 tests pass; security-headers-entry.test.ts pass.
  • audit-engine: golden-finding-messages-229, streaming-rules-canonical-golden, targeted-site-rules-1778-e2e: 12 pass, 0 fail.
  • docs/: make validate exit 0. bun scripts/changelog.ts check OK (entry changelog.d/2087-csp-third-party-containment.added.md).

Agent skill

No change needed to the squirrelscan agent skill: new check names surface through the existing report output and docs.

…hijacked third-party script #2087

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 marked this pull request as ready for review November 22, 2026 21:13
@nc9
nc9 merged commit 9ad93b0 into main Nov 22, 2026
10 checks passed
@nc9
nc9 deleted the boat/csp-third-party-2087 branch November 22, 2026 21:14
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.

security/csp: report whether the policy would contain a hijacked third-party script (connect-src, 'strict-dynamic', 'unsafe-eval', reporting)

1 participant