Skip to content

feat: Add cspReport utility to generate CSP reports and recommendations - #58

Merged
foxable merged 1 commit into
mainfrom
feat/csp-report
Oct 14, 2026
Merged

foxable merged 1 commit into
mainfrom
feat/csp-report

Conversation

@foxable

@foxable foxable commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

Adds cspReport, which evaluates a Content-Security-Policy header against a set of recommended directives and renders the result as a Markdown report suitable for file snapshots. Nonces are masked deterministically, and missing fetch directives are evaluated via their default-src fallback. Also adds getCspHeader to extract the header from the (nullable) result of page.goto(), returning an empty string if it is missing.

Example

import { cspReport, getCspHeader } from "@cronn/playwright-utils";
import { expect, test } from "@playwright/test";

test("has a strict Content Security Policy", async ({ page }) => {
  const response = await page.goto("/");
  const cspHeader = await getCspHeader(response);

  expect(cspReport(cspHeader)).toMatchTextFile({
    fileExtension: "md",
  });
});
# CSP Report

## Detected Directives

| Directive                   | Value               |
| --------------------------- | ------------------- |
| `upgrade-insecure-requests` | _none_              |
| `trusted-types`             | `angular`           |
| `require-trusted-types-for` | `'script'`          |
| `default-src`               | `'self'`            |
| `script-src`                | `'strict-dynamic'`  |
| `style-src`                 | `'nonce-[NONCE_0]'` |
| `object-src`                | `'none'`            |
| `base-uri`                  | `'none'`            |
| `form-action`               | `'self'`            |
| `frame-ancestors`           | `'none'`            |

## Recommendations

- `script-src`: Add a nonce source

@foxable foxable self-assigned this Oct 6, 2026
@changeset-bot

changeset-bot Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 9bd0712

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@cronn/playwright-utils Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment thread tests/csp-report.spec.ts
Comment thread docs/src/security/csp-report.md
Comment thread tests/csp-report.spec.ts Outdated
Comment thread tests/csp-report.spec.ts
Comment thread src/security/csp-report.ts
@foxable
foxable force-pushed the feat/csp-report branch 4 times, most recently from 044b98a to 9bd0712 Compare October 12, 2026 12:16
@foxable
foxable merged commit 1679dbb into main Oct 14, 2026
3 checks passed
@foxable
foxable deleted the feat/csp-report branch October 14, 2026 08:36
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.

2 participants