Skip to content

Commit

Permalink
fix: add CSP hashes/nonces to inline styles when using (#13232)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris authored Dec 24, 2024
1 parent 64c8e04 commit 12ce7eb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-geckos-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: add CSP hashes/nonces to inline styles when using `bundleStrategy: 'inline'`
4 changes: 0 additions & 4 deletions packages/kit/src/runtime/server/page/csp.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,6 @@ class BaseProvider {
this.#style_src.push(source);
}

if (this.#style_src_needs_csp) {
this.#style_src.push(source);
}

if (this.#style_src_attr_needs_csp) {
this.#style_src_attr.push(source);
}
Expand Down
15 changes: 7 additions & 8 deletions packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,19 +228,18 @@ export async function render_response({
return `${assets}/${path}`;
};

if (client.inline?.style) {
head += `\n\t<style>${client.inline.style}</style>`;
}

if (inline_styles.size > 0) {
const content = Array.from(inline_styles.values()).join('\n');
// inline styles can come from `bundleStrategy: 'inline'` or `inlineStyleThreshold`
const style = client.inline
? client.inline?.style
: Array.from(inline_styles.values()).join('\n');

if (style) {
const attributes = __SVELTEKIT_DEV__ ? [' data-sveltekit'] : [];
if (csp.style_needs_nonce) attributes.push(` nonce="${csp.nonce}"`);

csp.add_style(content);
csp.add_style(style);

head += `\n\t<style${attributes.join('')}>${content}</style>`;
head += `\n\t<style${attributes.join('')}>${style}</style>`;
}

for (const dep of stylesheets) {
Expand Down

0 comments on commit 12ce7eb

Please sign in to comment.