-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[feat] add CSP nonces to script/style tags
#2394
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
Closed
Closed
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
bc57637
Add nonce generation to request handling.
Karlinator c1f00ec
Changeset
Karlinator 03cc653
[docs] document CSP Nonces.
Karlinator 6e723ed
Use CSP nonce generator shimmed by the adapter.
Karlinator ed86bab
Add CspNonceGenerator shim to adapter-node.
Karlinator 8003b51
Add CspNonceGenerator shim to dev.
Karlinator 57ade88
Add typing for nonce generator.
Karlinator e5c3f18
Fix failing test case.
Karlinator e886aed
[docs] Apply suggestions from code review
Karlinator dec6a4c
Add generateCspNonce shim to all adapters.
Karlinator a557b47
Add changeset for adapters
Karlinator 416d86e
Fix deendency error in adapters.
Karlinator fc0fd83
Add error handling for missing generateCspNonce.
Karlinator 03579da
Fix which tags get nonces.
Karlinator 3d4ac9c
Fix lint error.
Karlinator 3ee9281
[docs] fix syntax error in CSP code example.
Karlinator 7c137a1
Change adapter nonce API to be more generic.
Karlinator c98de8a
[docs] Document adapter changes required by #2394
Karlinator bcbda45
[docs] Improve CSP hook example.
Karlinator 449a456
Fix injecting the shims.
Karlinator d873cac
Change nonce to be generated/supplied by adapter
Karlinator ecb23aa
Add nonces to preview server
Karlinator d906254
Fix test case
Karlinator c9c9736
Update cahngeset message for adapters.
Karlinator 3a2b756
Remove disused file.
Karlinator 8826933
Implement suggestion from code review.
Karlinator 18e08ca
[Docs] Suggestions from code review.
Karlinator 9ff68e6
[docs] Update documentation/docs/14-content-security-policy.md
Karlinator cd269ac
[docs] Apply suggestions from code review
Karlinator 449bd51
Fix misnamed option.
Karlinator 4ec250f
Disable prerendering if nonces are to be generated
Karlinator a13c758
Fix adapter-node nonce generation
Karlinator 3899598
Fix Cloudflare workers nonce generation
Karlinator b2a35cc
Fix netlify nonce generation
Karlinator 51a0ba1
Fix Vercel adapter nonce generation
Karlinator eb081b6
[docs] clarify `cspNonce` disables prerendering
Karlinator c81dc3a
Change adapter nonce API to be more generic.
Karlinator d5cc05b
Fix injecting the shims.
Karlinator a7b8003
Change nonce to be generated/supplied by adapter
Karlinator 7e693d5
Implement suggestion from code review.
Karlinator cf26271
Add nonces to adapter-cloudflare.
Karlinator 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,5 @@ | ||
| --- | ||
| '@sveltejs/kit': patch | ||
| --- | ||
|
|
||
| Add cspNonce config to generate CSP nonces for all scripts and stylesheets. |
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,8 @@ | ||
| --- | ||
| '@sveltejs/adapter-cloudflare-workers': patch | ||
| '@sveltejs/adapter-netlify': patch | ||
| '@sveltejs/adapter-node': patch | ||
| '@sveltejs/adapter-vercel': patch | ||
| --- | ||
|
|
||
| Add support for generating CSP nonces when `kit.cspNonce` is set. |
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,51 @@ | ||
| --- | ||
| title: Content Security Policy | ||
| --- | ||
|
|
||
| At the moment, SvelteKit supports adding Content Security Policy via hooks. In environments with a server-side runtime, HTTP headers can be added to the response object. | ||
|
|
||
| However, SvelteKit also requires some small pieces of inline JavaScript for hydration. To avoid using `'unsafe-inline'` (which, as the name suggests, should be avoided), SvelteKit can be configured to inject CSP nonces into the HTML it generates. | ||
|
|
||
| The nonce value is available to hooks as `request.locals.nonce`. A basic CSP handler hook might then look like this: | ||
|
|
||
| ```javascript | ||
| export async function handle({ request, resolve }) { | ||
| const response = await resolve(request); | ||
|
|
||
| if (response.headers['content-type'] !== 'text/html') { | ||
| return response; | ||
| } | ||
|
|
||
| const nonce = request.locals.nonce; | ||
|
|
||
| const directives = { | ||
| 'default-src': ["'self'", 'static.someotherdomain.com'], | ||
| 'script-src': ["'strict-dynamic'", `'nonce-${nonce}'`], | ||
| 'style-src': ["'self'", `'nonce-${nonce}'`] | ||
| }; | ||
|
|
||
| if (process.env.NODE_ENV === 'development') { | ||
| // Because of the way Vite performs hot reloads of stylesheets, | ||
| // 'unsafe-inline' is required in dev mode. | ||
| directives['style-src'].push('unsafe-inline'); | ||
| } | ||
|
|
||
| const csp = Object.entries(directives) | ||
| .map(([key, arr]) => key + ' ' + arr.join(' ')) | ||
| .join('; '); | ||
|
|
||
| return { | ||
| ...response, | ||
| headers: { | ||
| ...response.headers, | ||
| 'Content-Security-Policy': csp | ||
| } | ||
| }; | ||
| } | ||
| ``` | ||
|
|
||
| Be warned: some other features of Svelte ([in particular CSS transitions and animations](https://github.com/sveltejs/svelte/issues/6662)) might run afoul of this Content Security Policy and require either rewriting to JS-based transitions or enabling `style-src: 'unsafe-inline'`. | ||
|
|
||
| The `'strict-dynamic'` directive is optional but supported by Kit. If not using it you must allow `'self'`. | ||
|
|
||
| The nonce placeholders can be toggled with the `kit.cspNonce` configuration option. Since nonces must be uniquely generated for each request, this also disables prerendering. | ||
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
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
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.