Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
fallaciousreasoning wants to merge3 commits intosveltejs:main
base:main
Choose a base branch
Loading
fromfallaciousreasoning:trustedTypes

Conversation

fallaciousreasoning
Copy link
Contributor

@fallaciousreasoningfallaciousreasoning commentedJul 1, 2025
edited
Loading

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 requireTrustedTypes support via their CSP by wrapping assignments toinnerHTML in aTrustedTypePolicy calledsvelte-trusted-html if theTrustedTypes API exists.

Servers can allowlist the policy by settingrequire-trusted-types-for 'script'; trusted-types svelte-trusted-html in theirContent-Security-Policy header.

  • 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 withfeat:,fix:,chore:, ordocs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code withinpackages/svelte/src, add a changeset (npx changeset).

Tests and linting

Note: I haven't run the tests since I don't havepnpm setup properly.

I have tested that:

  1. A project with a CSP fails with Tip of Tree Svelte
  2. That project works when installing this revision of Svelte
  3. The project (with this revision) works in Browsers with noTrustedTypes support (i.e. Firefox, Safari)
  • Run the tests withpnpm 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 insrc/routes/page.server.js

AlanBreck, Jerboas86, and willfarrell reacted with heart emoji
@changeset-botchangeset-bot
Copy link

changeset-botbot commentedJul 1, 2025
edited
Loading

🦋 Changeset detected

Latest commit:0be07ca

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

This PR includes changesets to release 1 package
NameType
svelteMinor

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

@svelte-docs-bot
Copy link

@github-actionsGitHub Actions
Copy link
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@16271

@7nik
Copy link
Contributor

7nik commentedJul 1, 2025

Btw, I think configuring CSP should be added to the docs. Or addingtrusted-types svelte-trusted-html integrated to SvelteKit.

@Rich-Harris
Copy link
Member

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 ofTrustedTypes.

What if{@html ...} accepted either a string or an instance ofTrustedHTML? That way people can define their own policies with their own sanitization logic. It becomes slightly more cumbersome to use component libraries that inject HTML — those libraries would need to either also acceptTrustedHTML or a function thatcreatedTrustedHTML given some input — but that's a feature rather than a bug.

@Rich-Harris
Copy link
Member

(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)

@dummdidumm
Copy link
Member

I agree that this shouldn't affect{@html ...}, but it's also needed for all of our internal template generation which is known to be safe. So if we can adjust it so that{@html ...} does not use the svelte-html trusted types then this should be a good addition.

AlanBreck and fallaciousreasoning reacted with heart emoji

@7nik
Copy link
Contributor

7nik commentedJul 1, 2025

@html andbind:innerHTML can use a separate policy name. Or implement#15528, so users can sanitize and trustify html in it.

Another CSP thing is thatrequire-trusted-types-for 'script' breaks dynamic creation of scripts:

{#ifcondition}  <div>    <script>console.log("inline")</script>    <scriptsrc="/static/my-script.js"></script>  </div>{/if}

@fallaciousreasoning
Copy link
ContributorAuthor

fallaciousreasoning commentedJul 1, 2025
edited
Loading

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 TrustedTypes.

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 ...} 😄

@html and bind:innerHTML can use a separate policy name. Or implement#15528, so users can sanitize and trustify html in it.

@7nik - I think I'll try and get it working with a separate policy for now Actually, thinking about this, its untrusted so it should go through the default policy - the ideal would be if users could do:

{@htmlcustomPolicy.createHTML('<span>Hello</span>')}

but I'm not going to dig into that here.

Another CSP thing is that require-trusted-types-for 'script' breaks dynamic creation of scripts:

I think you should be able to allow dynamic script creation via some of the other policies (i.e.unsafe-inline or whitelisting the script URL). Either way, I'd prefer it to be tackled separately.

@fallaciousreasoning
Copy link
ContributorAuthor

fallaciousreasoning commentedJul 1, 2025
edited
Loading

Okay,@html doesn't go throughpolicy now. I tested andbind:innerHTML already wasn't going through the policy so that should be fine.

FWIW, I think this a pretty useful improvement as is because it means that apps which don't usebind:innerHTML and@html will be able to use Svelte withrequire-trusted-types-for 'script' now.

I think the ideal solution for@html andbind:innerHTML would be to allow authors to pass inTrustedHTML rather than creating a custom policy for them (which would throw an error if we try to register it and it isn't supported).

@7nik
Copy link
Contributor

7nik commentedJul 2, 2025
edited
Loading

What about usingtrustedTypes?.isHTML(html) to check if the passed value isTrustedHTML?

Offtop: it's funny thatwindow.trustedTypes is protected from writing, buttrustedTypes.isHTML = () => true andTrustedTypePolicyFactory.prototype.isHTML = () => true; are still allowed 😄

@fallaciousreasoning
Copy link
ContributorAuthor

What about using trustedTypes?.isHTML(html) to check if the passed value is TrustedHTML?

You mean to be able to set{@html ...} to TrustedHTML? Yeah, that's probably a good approach - tbh, I didn't really dig into why it wasn't working, and maybe we won't even need to check 😆

Just wondering if the Svelte team would consider merging without it? This PR fixes Svelte for our use case (so we don't need to create a default policy).

Offtop: it's funny that window.trustedTypes is protected from writing, but trustedTypes.isHTML = () => true and TrustedTypePolicyFactory.prototype.isHTML = () => true; are still allowed

Huh, that is kind of weird - I guess it'll still fail when you try and assign a sink to the non-trusted HTML though.

@7nik
Copy link
Contributor

7nik commentedJul 3, 2025

Look at

varhtml=value+'';
if(svg)html=`<svg>${html}</svg>`;
elseif(mathml)html=`<math>${html}</math>`;

In the case of SVG, to create elements with the correct namespace, code is wrapped in<svg> so it loses its trustiness and the trustiness should be checked before it somehow. But SVG has<foreignObject> that allows to include arbitrary HTML.

fallaciousreasoning reacted with thumbs up emoji

@fallaciousreasoning
Copy link
ContributorAuthor

Okay, I think something like

var html = trustedTypes.isTrusted(value) ? value : (value + '') should work for the non-SVG / non-MATH elements - before going and changing that, just want to confirm with@dummdidumm that this is something Svelte would consider merging.

@7nik
Copy link
Contributor

7nik commentedJul 4, 2025

I think this should allow creating fragments with the desired namespace without losing trustiness:

exportfunctioncreate_fragment_from_html(html,untrusted=false,namespace=null){vartemplate=document.createElement('template');if(namespace){varcontainer=namespace=='svg'                ?document.createElementNS(NAMESPACE_SVG,'svg')                :document.createElementNS(NAMESPACE_MATHML,'mathml');container.innerHTML=untrusted ?html :create_trusted_html(html.replaceAll('<!>','<!---->'));template.append(...container.childNodes);}else{template.innerHTML=untrusted ?html :create_trusted_html(html.replaceAll('<!>','<!---->'));}returntemplate.content;}

plus a bit simplifieshtml() function

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
4 participants
@fallaciousreasoning@7nik@Rich-Harris@dummdidumm

[8]ページ先頭

©2009-2025 Movatter.jp