-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
[fix]: Add support for TrustedTypes in Svelte #16271
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
base: main
Are you sure you want to change the base?
[fix]: Add support for TrustedTypes in Svelte #16271
Conversation
🦋 Changeset detectedLatest commit: 0be07ca The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
|
Btw, I think configuring CSP should be added to the docs. Or adding |
Thank you, but I don't think this is the right fix — it's saying that all HTML is trusted regardless of its provenance, which is the opposite of the intent of What if |
(admittedly not sure how to support SVG and MathML in this case; might not be possible. Perhaps that's just the cost of CSP support though, to be borne by apps that need CSP support) |
I agree that this shouldn't affect |
Another CSP thing is that {#if condition}
<div>
<script>console.log("inline")</script>
<script src="/static/my-script.js"></script>
</div>
{/if} |
I think its okay to trust the output of Svelte's internal template generation - its what the other Frameworks do (i.e. Polymer/Lit). I'll try and work out how to carve out
{@html customPolicy.createHTML('<span>Hello</span>')} but I'm not going to dig into that here.
I think you should be able to allow dynamic script creation via some of the other policies (i.e. |
08c56ee
to
0be07ca
Compare
Okay, FWIW, I think this a pretty useful improvement as is because it means that apps which don't use I think the ideal solution for |
Before submitting the PR, please make sure you do the following
Resolves #14438
Resolves #10826
This PR makes it possible to use Svelte on pages which require
TrustedTypes
support via their CSP by wrapping assignments toinnerHTML
in aTrustedTypePolicy
calledsvelte-trusted-html
if theTrustedTypes
API exists.Servers can allowlist the policy by setting
require-trusted-types-for 'script'; trusted-types svelte-trusted-html
in theirContent-Security-Policy
header.feat:
,fix:
,chore:
, ordocs:
.packages/svelte/src
, add a changeset (npx changeset
).Tests and linting
Note: I haven't run the tests since I don't have
pnpm
setup properly.I have tested that:
TrustedTypes
support (i.e. Firefox, Safari)pnpm test
and lint the project withpnpm lint
My test project is here: https://github.com/fallaciousreasoning/svelte-tt-test/blob/master/src/routes/%2Bpage.server.js
The only changes to the default project is adding the CSP in
src/routes/page.server.js