-
Notifications
You must be signed in to change notification settings - Fork 2
[ALS-9583][ALS-9584] Own the CSP in the app so 'unsafe-eval' and 'unsafe-inline' can go #752
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
Open
JamesPeck
wants to merge
5
commits into
main
Choose a base branch
from
ALS-9583-csp-server-headers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6db788a
[ALS-9583][ALS-9584] Own the CSP in the app so 'unsafe-eval' and 'uns…
JamesPeck 25a09cf
Reject pre-quoted unsafe sources in the CSP_EXTRA_* build vars
JamesPeck b7d1460
Drop explanatory comments from the CSP changes
JamesPeck 413636d
Note why css() parses through a probe element
JamesPeck 088979b
Generalise the CSP nonce warning beyond Plotly
JamesPeck 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
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
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
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
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
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
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,29 @@ | ||
| export function withStyleNonce(csp: string | null): string | null { | ||
| if (!csp) return csp; | ||
|
|
||
| const nonce = /'nonce-([^']+)'/.exec(csp)?.[1]; | ||
| if (!nonce) return csp; | ||
|
|
||
| return csp.replace(/(^|;\s*)style-src ([^;]*)/, (match, separator: string, sources: string) => { | ||
| // In dev, SvelteKit deliberately rewrites style-src to 'unsafe-inline' so Vite can inject | ||
| // stylesheets at runtime, and Vite only nonces those when the document carries a | ||
| // meta[property=csp-nonce] - which app.html does not. Adding a nonce here would make the | ||
| // browser ignore 'unsafe-inline' (CSP3) and leave the dev server completely unstyled. | ||
| if (sources.includes("'unsafe-inline'") || sources.includes(`'nonce-${nonce}'`)) return match; | ||
| return `${separator}style-src ${sources} 'nonce-${nonce}'`; | ||
| }); | ||
| } | ||
|
|
||
| export function findStyleNonceProblem(csp: string | null): string | null { | ||
| if (!csp) return null; | ||
|
|
||
| const styleSrc = /(?:^|;\s*)style-src ([^;]*)/.exec(csp)?.[1]; | ||
| if (!styleSrc) return 'the policy declares no style-src directive'; | ||
|
|
||
| // Dev relaxes style-src to 'unsafe-inline', where the seed needs no nonce. | ||
| if (styleSrc.includes("'unsafe-inline'")) return null; | ||
|
|
||
| if (!styleSrc.includes("'nonce-")) return 'style-src carries no nonce'; | ||
|
|
||
| return null; | ||
| } |
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,36 @@ | ||
| import type { Attachment } from 'svelte/attachments'; | ||
|
|
||
| type Styles = string | Record<string, string | number>; | ||
| type Declaration = { property: string; value: string; priority: string }; | ||
|
|
||
| let probe: HTMLElement | undefined; | ||
|
|
||
| function parse(styles: Styles): Declaration[] { | ||
| if (typeof styles !== 'string') { | ||
| return Object.entries(styles).map(([property, value]) => ({ | ||
| property, | ||
| value: String(value), | ||
| priority: '', | ||
| })); | ||
| } | ||
| // Parse via the browser so values and !important survive the replay through setProperty. | ||
| probe ??= document.createElement('div'); | ||
| probe.style.cssText = styles; | ||
| return Array.from(probe.style).map((property) => ({ | ||
| property, | ||
| value: probe!.style.getPropertyValue(property), | ||
| priority: probe!.style.getPropertyPriority(property), | ||
| })); | ||
| } | ||
|
|
||
| export function css(styles: Styles): Attachment<HTMLElement> { | ||
| return (node) => { | ||
| const declarations = parse(styles); | ||
| for (const { property, value, priority } of declarations) { | ||
| node.style.setProperty(property, value, priority); | ||
| } | ||
| return () => { | ||
| for (const { property } of declarations) node.style.removeProperty(property); | ||
| }; | ||
| }; | ||
| } | ||
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,68 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
|
|
||
| import { findStyleNonceProblem, withStyleNonce } from '$lib/server/csp'; | ||
|
|
||
| const NONCE = 'abc123=='; | ||
| const prod = (styleSrc: string) => | ||
| `default-src 'self'; script-src 'self' 'nonce-${NONCE}'; style-src ${styleSrc}; base-uri 'self'`; | ||
|
|
||
| describe('withStyleNonce', () => { | ||
| it("copies the script nonce into style-src so app.html's Plotly seed is allowed", () => { | ||
| expect(withStyleNonce(prod("'self'"))).toContain(`style-src 'self' 'nonce-${NONCE}'`); | ||
| }); | ||
|
|
||
| it('leaves every other directive untouched', () => { | ||
| const result = withStyleNonce(prod("'self'")) ?? ''; | ||
| expect(result).toContain("default-src 'self'"); | ||
| expect(result).toContain("base-uri 'self'"); | ||
| expect(result).toContain(`script-src 'self' 'nonce-${NONCE}'`); | ||
| }); | ||
|
|
||
| it('does not disturb style-src-attr, which has no nonce mechanism', () => { | ||
| const csp = `${prod("'self'")}; style-src-attr 'unsafe-inline'`; | ||
| expect(withStyleNonce(csp)).toContain("style-src-attr 'unsafe-inline'"); | ||
| }); | ||
|
|
||
| it("makes no change in dev, where style-src already carries 'unsafe-inline'", () => { | ||
| const dev = prod("'self' 'unsafe-inline'"); | ||
| expect(withStyleNonce(dev)).toBe(dev); | ||
| }); | ||
|
|
||
| it('is idempotent', () => { | ||
| const once = withStyleNonce(prod("'self'")); | ||
| expect(withStyleNonce(once)).toBe(once); | ||
| }); | ||
|
|
||
| it('returns the policy unchanged when no nonce is present', () => { | ||
| const csp = "default-src 'none'; style-src 'self'"; | ||
| expect(withStyleNonce(csp)).toBe(csp); | ||
| }); | ||
|
|
||
| it('passes through a missing header', () => { | ||
| expect(withStyleNonce(null)).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('findStyleNonceProblem', () => { | ||
| it('accepts the production policy once the nonce has been copied across', () => { | ||
| expect(findStyleNonceProblem(withStyleNonce(prod("'self'")))).toBeNull(); | ||
| }); | ||
|
|
||
| it("accepts dev, where style-src is deliberately relaxed to 'unsafe-inline'", () => { | ||
| expect(findStyleNonceProblem(prod("'self' 'unsafe-inline'"))).toBeNull(); | ||
| }); | ||
|
|
||
| it('reports a policy with no style-src directive', () => { | ||
| const csp = `default-src 'self'; script-src 'self' 'nonce-${NONCE}'`; | ||
| expect(findStyleNonceProblem(csp)).toMatch(/no style-src/); | ||
| }); | ||
|
|
||
| it('reports a style-src that never received a nonce', () => { | ||
| const csp = "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self'"; | ||
| expect(findStyleNonceProblem(csp)).toMatch(/no nonce/); | ||
| }); | ||
|
|
||
| it('ignores responses that carry no policy', () => { | ||
| expect(findStyleNonceProblem(null)).toBeNull(); | ||
| }); | ||
| }); |
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.