Skip to content

fix: skip CSP nonce in style-src when using unsafe-inline #11575

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 3 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ninety-clouds-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

fix: only add nonce to `style-src` CSP directive when `unsafe-inline` is not present
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/page/csp.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class BaseProvider {
this.#style_src_elem.push(`sha256-${hash}`);
}
} else {
if (this.#style_src.length === 0) {
if (this.#style_src.length === 0 && !d['style-src']?.includes('unsafe-inline')) {
this.#style_src.push(`nonce-${this.#nonce}`);
}
if (d['style-src-attr']?.length) {
Expand Down
26 changes: 26 additions & 0 deletions packages/kit/src/runtime/server/page/csp.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,32 @@ test('skips nonce with unsafe-inline', () => {
assert.equal(csp.report_only_provider.get_header(), "default-src 'unsafe-inline'; report-uri /");
});

test('skips nonce in style-src when using unsafe-inline', () => {
const csp = new Csp(
{
mode: 'nonce',
directives: {
'style-src': ['self', 'unsafe-inline']
},
reportOnly: {
'style-src': ['self', 'unsafe-inline'],
'report-uri': ['/']
}
},
{
prerender: false
}
);

csp.add_style('');

assert.equal(csp.csp_provider.get_header(), "style-src 'self' 'unsafe-inline'");
assert.equal(
csp.report_only_provider.get_header(),
"style-src 'self' 'unsafe-inline'; report-uri /"
);
});

test('skips hash with unsafe-inline', () => {
const csp = new Csp(
{
Expand Down