Repository navigation
feat: Add cspReport utility to generate CSP reports and recommendations
#58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "@cronn/playwright-utils": minor | ||
| --- | ||
|
|
||
| Add `cspReport` to evaluate a `Content-Security-Policy` header against recommended directives and render the result as a Markdown report for file snapshots. | ||
|
|
||
| Add `getCspHeader` to extract the `Content-Security-Policy` header from a page response, falling back to an empty string. |
22 changes: 22 additions & 0 deletions
22
...ion/csp-report/does_not_require_base_directives_if_both_variants_are_defined.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # CSP Report | ||
|
|
||
| ## Detected Directives | ||
|
|
||
| | Directive | Value | | ||
| | --------------------------- | ------------------- | | ||
| | `upgrade-insecure-requests` | _none_ | | ||
| | `trusted-types` | `angular` | | ||
| | `require-trusted-types-for` | `'script'` | | ||
| | `default-src` | `'self'` | | ||
| | `script-src-elem` | `'nonce-[NONCE_0]'` | | ||
| | `script-src-attr` | `'none'` | | ||
| | `style-src-elem` | `'nonce-[NONCE_1]'` | | ||
| | `style-src-attr` | `'none'` | | ||
| | `object-src` | `'none'` | | ||
| | `base-uri` | `'none'` | | ||
| | `form-action` | `'self'` | | ||
| | `frame-ancestors` | `'none'` | | ||
|
|
||
| ## Recommendations | ||
|
|
||
| None |
20 changes: 20 additions & 0 deletions
20
...test/validation/csp-report/evaluates_missing_directives_using_their_fallback.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # CSP Report | ||
|
|
||
| ## Detected Directives | ||
|
|
||
| | Directive | Value | | ||
| | --------------------------- | ------------------- | | ||
| | `upgrade-insecure-requests` | _none_ | | ||
| | `trusted-types` | `angular` | | ||
| | `require-trusted-types-for` | `'script'` | | ||
| | `default-src` | `'self'` | | ||
| | `script-src` | `'nonce-[NONCE_0]'` | | ||
| | `object-src` | `'none'` | | ||
| | `base-uri` | `'none'` | | ||
| | `form-action` | `'self'` | | ||
| | `frame-ancestors` | `'none'` | | ||
|
|
||
| ## Recommendations | ||
|
|
||
| - `style-src`: Add a nonce source | ||
| - `style-src`: Remove `'self'` (inherited from `default-src`) |
20 changes: 20 additions & 0 deletions
20
data/test/validation/csp-report/recommends_adding_a_nonce_source.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # 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 |
26 changes: 26 additions & 0 deletions
26
data/test/validation/csp-report/recommends_adding_missing_sources.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # CSP Report | ||
|
|
||
| ## Detected Directives | ||
|
|
||
| | Directive | Value | | ||
| | --------------------------- | ---------------------------- | | ||
| | `upgrade-insecure-requests` | _none_ | | ||
| | `trusted-types` | `angular` | | ||
| | `require-trusted-types-for` | `'script'` | | ||
| | `default-src` | `'self'` | | ||
| | `script-src` | `'nonce-[NONCE_0]'` | | ||
| | `style-src` | `'nonce-[NONCE_1]'` | | ||
| | `img-src` | `https://images.example.com` | | ||
| | `object-src` | `https://cdn.example.com` | | ||
| | `base-uri` | `'none'` | | ||
| | `form-action` | `https://api.example.com` | | ||
| | `frame-ancestors` | `'none'` | | ||
|
|
||
| ## Recommendations | ||
|
|
||
| - `img-src`: Add `'self' data:` | ||
| - `img-src`: Remove `https://images.example.com` | ||
| - `object-src`: Add `'none'` | ||
| - `object-src`: Remove `https://cdn.example.com` | ||
| - `form-action`: Add `'self'` | ||
| - `form-action`: Remove `https://api.example.com` |
21 changes: 21 additions & 0 deletions
21
data/test/validation/csp-report/recommends_all_directives_for_an_empty_header.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # CSP Report | ||
|
|
||
| ## Detected Directives | ||
|
|
||
| None | ||
|
|
||
| ## Recommendations | ||
|
|
||
| - `upgrade-insecure-requests`: Missing, add `upgrade-insecure-requests` | ||
| - `trusted-types`: Missing, add `trusted-types` | ||
| - `require-trusted-types-for`: Missing, add `require-trusted-types-for 'script'` | ||
| - `default-src`: Missing, add `default-src 'self'` | ||
| - `script-src`: Missing, add `script-src 'nonce-[RANDOM]'` | ||
| - `style-src`: Missing, add `style-src 'nonce-[RANDOM]'` | ||
| - `img-src`: Missing, add `img-src 'self' data:` | ||
| - `font-src`: Missing, add `font-src 'self'` | ||
| - `connect-src`: Missing, add `connect-src 'self'` | ||
| - `object-src`: Missing, add `object-src 'none'` | ||
| - `base-uri`: Missing, add `base-uri 'self'` | ||
| - `form-action`: Missing, add `form-action 'self'` | ||
| - `frame-ancestors`: Missing, add `frame-ancestors 'none'` |
31 changes: 31 additions & 0 deletions
31
data/test/validation/csp-report/recommends_fixing_weak_directive_variants.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # CSP Report | ||
|
|
||
| ## Detected Directives | ||
|
|
||
| | Directive | Value | | ||
| | --------------------------- | ----------------------------------------- | | ||
| | `upgrade-insecure-requests` | _none_ | | ||
| | `trusted-types` | `angular` | | ||
| | `require-trusted-types-for` | `'script'` | | ||
| | `default-src` | `'self'` | | ||
| | `script-src` | `'nonce-[NONCE_0]'` | | ||
| | `script-src-elem` | `'unsafe-inline' https://cdn.example.com` | | ||
| | `script-src-attr` | `'unsafe-inline'` | | ||
| | `style-src` | `'nonce-[NONCE_1]'` | | ||
| | `style-src-elem` | `'nonce-[NONCE_1]' 'unsafe-inline'` | | ||
| | `style-src-attr` | `'unsafe-hashes'` | | ||
| | `object-src` | `'none'` | | ||
| | `base-uri` | `'none'` | | ||
| | `form-action` | `'self'` | | ||
| | `frame-ancestors` | `'none'` | | ||
|
|
||
| ## Recommendations | ||
|
|
||
| - `script-src-elem`: Add a nonce source | ||
| - `script-src-elem`: Remove `'unsafe-inline'` | ||
| - `script-src-elem`: Remove `https://cdn.example.com` | ||
| - `script-src-attr`: Add `'none'` | ||
| - `script-src-attr`: Remove `'unsafe-inline'` | ||
| - `style-src-elem`: Remove `'unsafe-inline'` | ||
| - `style-src-attr`: Add `'none'` | ||
| - `style-src-attr`: Remove `'unsafe-hashes'` |
20 changes: 20 additions & 0 deletions
20
data/test/validation/csp-report/recommends_nothing_for_a_strict_policy.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # CSP Report | ||
|
|
||
| ## Detected Directives | ||
|
|
||
| | Directive | Value | | ||
| | --------------------------- | ------------------- | | ||
| | `upgrade-insecure-requests` | _none_ | | ||
| | `trusted-types` | `angular` | | ||
| | `require-trusted-types-for` | `'script'` | | ||
| | `default-src` | `'self'` | | ||
| | `script-src` | `'nonce-[NONCE_0]'` | | ||
| | `style-src` | `'nonce-[NONCE_1]'` | | ||
| | `object-src` | `'none'` | | ||
| | `base-uri` | `'none'` | | ||
| | `form-action` | `'self'` | | ||
| | `frame-ancestors` | `'none'` | | ||
|
|
||
| ## Recommendations | ||
|
|
||
| None |
24 changes: 24 additions & 0 deletions
24
.../test/validation/csp-report/recommends_nothing_for_strict_directive_variants.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # CSP Report | ||
|
|
||
| ## Detected Directives | ||
|
|
||
| | Directive | Value | | ||
| | --------------------------- | ------------------- | | ||
| | `upgrade-insecure-requests` | _none_ | | ||
| | `trusted-types` | `angular` | | ||
| | `require-trusted-types-for` | `'script'` | | ||
| | `default-src` | `'self'` | | ||
| | `script-src` | `'nonce-[NONCE_0]'` | | ||
| | `script-src-elem` | `'nonce-[NONCE_0]'` | | ||
| | `script-src-attr` | `'none'` | | ||
| | `style-src` | `'nonce-[NONCE_1]'` | | ||
| | `style-src-elem` | `'nonce-[NONCE_1]'` | | ||
| | `style-src-attr` | `'none'` | | ||
| | `object-src` | `'none'` | | ||
| | `base-uri` | `'none'` | | ||
| | `form-action` | `'self'` | | ||
| | `frame-ancestors` | `'none'` | | ||
|
|
||
| ## Recommendations | ||
|
|
||
| None |
23 changes: 23 additions & 0 deletions
23
data/test/validation/csp-report/recommends_removing_extra_sources.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # CSP Report | ||
|
|
||
| ## Detected Directives | ||
|
|
||
| | Directive | Value | | ||
| | --------------------------- | ------------------------------------------------------------ | | ||
| | `upgrade-insecure-requests` | _none_ | | ||
| | `trusted-types` | `angular` | | ||
| | `require-trusted-types-for` | `'script'` | | ||
| | `default-src` | `'self'` | | ||
| | `script-src` | `'nonce-[NONCE_0]' 'strict-dynamic' https://cdn.example.com` | | ||
| | `style-src` | `'nonce-[NONCE_1]' 'unsafe-inline'` | | ||
| | `img-src` | `'self' data: https://images.example.com` | | ||
| | `object-src` | `'none'` | | ||
| | `base-uri` | `'none'` | | ||
| | `form-action` | `'self'` | | ||
| | `frame-ancestors` | `'none'` | | ||
|
|
||
| ## Recommendations | ||
|
|
||
| - `script-src`: Remove `https://cdn.example.com` | ||
| - `style-src`: Remove `'unsafe-inline'` | ||
| - `img-src`: Remove `https://images.example.com` |
21 changes: 21 additions & 0 deletions
21
...alidation/csp-report/requires_base_directives_if_only_one_variant_is_defined.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # CSP Report | ||
|
|
||
| ## Detected Directives | ||
|
|
||
| | Directive | Value | | ||
| | --------------------------- | ------------------- | | ||
| | `upgrade-insecure-requests` | _none_ | | ||
| | `trusted-types` | `angular` | | ||
| | `require-trusted-types-for` | `'script'` | | ||
| | `default-src` | `'self'` | | ||
| | `script-src-elem` | `'nonce-[NONCE_0]'` | | ||
| | `style-src` | `'nonce-[NONCE_1]'` | | ||
| | `object-src` | `'none'` | | ||
| | `base-uri` | `'none'` | | ||
| | `form-action` | `'self'` | | ||
| | `frame-ancestors` | `'none'` | | ||
|
|
||
| ## Recommendations | ||
|
|
||
| - `script-src`: Add a nonce source | ||
| - `script-src`: Remove `'self'` (inherited from `default-src`) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # CSP Report | ||
|
|
||
| A Content Security Policy is only effective if it is strict enough, and a weakened policy usually goes unnoticed because the application keeps working. `cspReport` evaluates a `Content-Security-Policy` header against a set of recommended directives and returns a Markdown report, which can be stored as a file snapshot to detect any change to the policy. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```ts | ||
| 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", | ||
| }); | ||
| }); | ||
| ``` | ||
|
|
||
| `getCspHeader` extracts the `Content-Security-Policy` header from a response. It accepts the nullable result of `page.goto()` and returns an empty string if the response or the header is missing, which `cspReport` reports as a policy without any directives. | ||
|
|
||
| The report lists all directives of the header, followed by the recommendations for the policy: | ||
|
|
||
| ```md | ||
| # CSP Report | ||
|
|
||
| ## 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 | ||
| ``` | ||
|
|
||
| Nonces are generated per response, so they are masked as `[NONCE_0]`, `[NONCE_1]`, … to keep the report deterministic. The same nonce used by several directives is masked with the same index. | ||
|
|
||
| A directive is considered compliant if it only contains recommended values or is set to `'none'`, so a stricter policy does not produce a recommendation. Extra sources which are not recommended are reported individually. If a directive contains none of the recommended values, the expected value is reported instead. | ||
|
|
||
| If one of the fetch directives `script-src`, `style-src`, `img-src`, `font-src` or `connect-src` is missing, it is evaluated using `default-src`. Recommendations resulting from the fallback are marked as `(inherited from default-src)`. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.