Skip to content

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

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 18 commits into from
Jan 9, 2024

Conversation

MathiasWP
Copy link
Contributor

@MathiasWP MathiasWP commented Jan 9, 2024

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.

Copy link

changeset-bot bot commented Jan 9, 2024

🦋 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
@Rich-Harris Rich-Harris merged commit 5b90b02 into sveltejs:main Jan 9, 2024
@Rich-Harris
Copy link
Member

thank you!

@github-actions github-actions bot mentioned this pull request Jan 9, 2024
@bcaller
Copy link

bcaller commented Jan 10, 2024

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?

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