Skip to content
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

fix: provide a workaround for unsafe-inline CSP that also works in Safari #7800

Merged

Conversation

Karlinator
Copy link
Contributor

@Karlinator Karlinator commented Aug 19, 2022

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
  • Prefix your PR title with [feat], [fix], [chore], or [docs].
  • 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 npm test and lint the project with npm run lint

Ref #6662.

Currently, CSS transitions require style-src: unsafe-inline CSP. There is almost a workaround, but it doesn't work in Safari (and iOS<15). This fixes that workaround to also work in Safari.

This changes the inserted style element for transitions to initially include the string '/* empty */'. This allows you to work around requiring unsafe-inline CSP discussed in #6662 by adding a hash to your CSP:

'sha256-9OlNO0DNEeaVzHL4RZwCLsBHA8WBQ8toBp/4F5XV2nc='

Using the hash of the empty string, 'sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=', works fine in most browsers, but Safari for some reason thinks the empty string should have a different hash.

Users currently using the empty string hash (like me) will have their apps break from this, but it currently only half works anyway so I don't think that's a big deal.

As @Rich-Harris has said this isn't very elegant, and the eventual solution is to use the Web Animations API instead, but I'd still like for this to work on iOS in the mean time :)

@Karlinator Karlinator changed the title Provide a workaround for unsafe-inline CSP that also works in Safari [fix] Provide a workaround for unsafe-inline CSP that also works in Safari Aug 19, 2022
@schuetzm
Copy link

Have you tried explicitly assigning it an empty string? Maybe Safari treats it as undefined by default, which would mess up the hash?

@Karlinator
Copy link
Contributor Author

@schuetzm nope, that does not appear to work. I suspect the Safari bug is later in the process maybe? Regardless, it chokes just as happily when explicitly setting the textContent to ''.

@Karlinator Karlinator force-pushed the fix/empty-style-element-safari branch from 42e2e27 to da5ddd4 Compare September 21, 2022 17:29
@benmccann benmccann changed the title [fix] Provide a workaround for unsafe-inline CSP that also works in Safari fix: provide a workaround for unsafe-inline CSP that also works in Safari Mar 14, 2023
@benmccann
Copy link
Member

Users currently using the empty string hash (like me) will have their apps break from this, but it currently only half works anyway so I don't think that's a big deal.

To be conservative, I will add this to the 4.x milestone

@benmccann benmccann added this to the 4.x milestone Apr 19, 2023
src/runtime/internal/dom.ts Outdated Show resolved Hide resolved
@dummdidumm dummdidumm changed the base branch from master to version-4 May 4, 2023 14:04
@vercel
Copy link

vercel bot commented May 4, 2023

@dummdidumm is attempting to deploy a commit to the Svelte Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants