-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat: Add CSP support for hydratable
#17338
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
elliott-with-the-longest-name-on-github
merged 33 commits into
main
from
elliott/hydratable-csp
Dec 12, 2025
+294
−30
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
6669b9a
tests
elliott-with-the-longest-name-on-github 66f3255
tweak
elliott-with-the-longest-name-on-github 2be2594
tweaks
elliott-with-the-longest-name-on-github 59301a3
Update packages/svelte/src/internal/server/renderer.js
elliott-with-the-longest-name-on-github 3e04518
docs
elliott-with-the-longest-name-on-github 342ec99
Merge branch 'elliott/hydratable-csp' of github.com:sveltejs/svelte i…
elliott-with-the-longest-name-on-github 7a5886d
typescript
elliott-with-the-longest-name-on-github 479deb6
node 18
elliott-with-the-longest-name-on-github 691cd47
ts
elliott-with-the-longest-name-on-github 25b9b93
ts
elliott-with-the-longest-name-on-github 4736669
tweak
elliott-with-the-longest-name-on-github f614efe
fix tests?
elliott-with-the-longest-name-on-github 25e4050
hopefully fix docs
elliott-with-the-longest-name-on-github 6460284
Apply suggestions from code review
elliott-with-the-longest-name-on-github ae7bbe0
base64_encode
elliott-with-the-longest-name-on-github 87aebaf
ugh
elliott-with-the-longest-name-on-github 2f394b8
eightieth times the charm
elliott-with-the-longest-name-on-github d3ff660
Update documentation/docs/06-runtime/05-hydratable.md
Rich-Harris 8a91d7c
Update documentation/docs/06-runtime/05-hydratable.md
Rich-Harris 833fb10
Update documentation/docs/06-runtime/05-hydratable.md
Rich-Harris e8aa7f9
Update documentation/docs/06-runtime/05-hydratable.md
Rich-Harris eb60df8
add remaining ts-ignores, use ts-expect-error instead where possible
Rich-Harris dc691ca
restore indentation
Rich-Harris 4d78970
oops
Rich-Harris f0f8d1a
oops
Rich-Harris 14a6a2f
example of using hashes in CSP header
Rich-Harris e872840
fix hash
Rich-Harris 22dda42
Merge branch 'main' into elliott/hydratable-csp
Rich-Harris 2c289a9
fix docs
Rich-Harris cdfed0e
switch to error
elliott-with-the-longest-name-on-github 22ffb7b
Merge branch 'elliott/hydratable-csp' of github.com:sveltejs/svelte i…
elliott-with-the-longest-name-on-github ad6513e
tweaks
elliott-with-the-longest-name-on-github 8c901a2
changeset
Rich-Harris 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
Some comments aren't visible on the classic Files Changed page.
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 @@ | ||
| --- | ||
| "svelte": minor | ||
| --- | ||
|
|
||
| feat: Add `csp` option to `render(...)`, and emit hashes when using `hydratable` |
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,41 @@ | ||
| import { BROWSER } from 'esm-env'; | ||
|
|
||
| let text_encoder; | ||
| // TODO - remove this and use global `crypto` when we drop Node 18 | ||
| let crypto; | ||
|
|
||
| /** @param {string} data */ | ||
| export async function sha256(data) { | ||
| text_encoder ??= new TextEncoder(); | ||
|
|
||
| // @ts-expect-error | ||
| crypto ??= globalThis.crypto?.subtle?.digest | ||
| ? globalThis.crypto | ||
| : // @ts-ignore - we don't install node types in the prod build | ||
| (await import('node:crypto')).webcrypto; | ||
|
|
||
| const hash_buffer = await crypto.subtle.digest('SHA-256', text_encoder.encode(data)); | ||
|
|
||
| return base64_encode(hash_buffer); | ||
| } | ||
|
|
||
| /** | ||
| * @param {Uint8Array} bytes | ||
| * @returns {string} | ||
| */ | ||
| export function base64_encode(bytes) { | ||
| // Using `Buffer` is faster than iterating | ||
| // @ts-ignore | ||
| if (!BROWSER && globalThis.Buffer) { | ||
| // @ts-ignore | ||
| return globalThis.Buffer.from(bytes).toString('base64'); | ||
| } | ||
|
|
||
| let binary = ''; | ||
|
|
||
| for (let i = 0; i < bytes.length; i++) { | ||
| binary += String.fromCharCode(bytes[i]); | ||
| } | ||
|
|
||
| return btoa(binary); | ||
| } |
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,15 @@ | ||
| import { assert, test } from 'vitest'; | ||
| import { sha256 } from './crypto.js'; | ||
|
|
||
| const inputs = [ | ||
| ['hello world', 'uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek='], | ||
| ['', '47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU='], | ||
| ['abcd', 'iNQmb9TmM40TuEX88olXnSCciXgjuSF9o+Fhk28DFYk='], | ||
| ['the quick brown fox jumps over the lazy dog', 'Bcbgjx2f2voDFH/Lj4LxJMdtL3Dj2Ynciq2159dFC+w='], | ||
| ['工欲善其事,必先利其器', 'oPOthkQ1c5BbPpvrr5WlUBJPyD5e6JeVdWcqBs9zvjA='] | ||
| ]; | ||
|
|
||
| test.each(inputs)('sha256("%s")', async (input, expected) => { | ||
| const actual = await sha256(input); | ||
| assert.equal(actual, expected); | ||
| }); | ||
Ocean-OS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
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.