Skip to content

security: avoid CSP conflict with sha/nonce during dev & add support for 'style-src-elem' - #11562

Merged
Rich-Harris merged 18 commits into
sveltejs:mainfrom
MathiasWP:csp-dev-fix
Jan 9, 2024
Merged

security: avoid CSP conflict with sha/nonce during dev & add support for 'style-src-elem'#11562
Rich-Harris merged 18 commits into
sveltejs:mainfrom
MathiasWP:csp-dev-fix

Conversation

@MathiasWP

@MathiasWP MathiasWP commented Jan 9, 2024

Copy link
Copy Markdown
Contributor

this is a continuation from #11485, which solved sveltekit related problems to CSP.

after updating sveltekit i tried to remove unsafe-inline from script-src-elem, style-src-attr & style-src-elem. it worked on script-src-elem, but style-src-attr and style-src-elem are not working. this is because of how svelte works with csp (sveltejs/svelte#7800).

to combat some the style-src-elem i've extended CSP so it automatically adds the SHA for the /* empty */ value if not already added to style-src-elem.

i'm not quite sure how style-src-attr should be fixed, i believe this has to be done via Svelte with CSSOM.

extra: i've also made it so that CSP removes hashes and nonces from style-src, style-src-attr or style-src-elem during dev when adding the "unsafe-inline" to solve this error message:

Screenshot 2024-01-09 at 18 39 51

i noticed this after adding the hash myself on my app when trying to solve the style-src-elem bug.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

@changeset-bot

changeset-bot Bot commented Jan 9, 2024

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 98271f7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@MathiasWP MathiasWP changed the title add nonce to script-src-elem csp directive if defined [security]: avoid CSP conflict with sha/nonce during dev & add support for 'style-src-elem' Jan 9, 2024
@benmccann benmccann changed the title [security]: avoid CSP conflict with sha/nonce during dev & add support for 'style-src-elem' security: avoid CSP conflict with sha/nonce during dev & add support for 'style-src-elem' Jan 9, 2024
Comment thread .changeset/giant-years-drum.md
Comment thread .changeset/giant-years-drum.md Outdated
MathiasWP and others added 2 commits January 9, 2024 20:54
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
@Rich-Harris
Rich-Harris merged commit 5b90b02 into sveltejs:main Jan 9, 2024
@Rich-Harris

Copy link
Copy Markdown
Member

thank you!

@github-actions github-actions Bot mentioned this pull request Jan 9, 2024
@bcaller

bcaller commented Jan 10, 2024

Copy link
Copy Markdown

If unsafe-inline is added to the CSP directives, nonces/hashes will break that. We should avoid messed-up CSPs in prod as well as dev. Would it be acceptable to apply this logic everywhere rather than just dev?

Rich-Harris pushed a commit that referenced this pull request Jul 28, 2026
This block was born triplicated in #11562, which had to rewrite all
three sites #11485 touched two days earlier.

The report-only check parsed as `?.length ?? (0 > 0)` and only worked
because `0` and `undefined` are both falsy; the replacement has the same
truth table.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants