You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is fine, as the styles are generated at compile time and don't contain any untrusted data. However, if an application has a TrustedTypes CSP set up, this line will throw an error. The only way for them to fix it is to add a default policy, which isn't ideal as it reduces the protection from the policy.
Describe the proposed solution
Use textContent to set the stylesheet content, rather than innerHTML.
// Instead ofthis.shadowRoot.innerHTML=`<style>${css}</style>`// we might be able to do:conststyle=document.createElement('style');style.textContent=css;this.shadowRoot.appendChild(style);
which might be preferable all round (not sure if there are any performance implications). I have a PR open with this fix #8135. I've tested using the our CSP in the version of Svelte from my fork and it fixes it nicely!
Alternatives considered
We could create a svelte-web-component-style-literal no-op policy which is used instead. That way, consumers can allow the svelte-web-component-style-literal policy if they use Svelte web-components and everything will just work. I have a branch with a working proof of concept https://github.com/fallaciousreasoning/svelte/tree/trustedTypes but I think it's a bit less elegant.
constpolicy=window.trustedTypes.createPolicy('svelte-web-component-style-literal',{createHTML: html=>html});// in our constructorthis.shadowRoot.innerHTML=policy.createHTML(`<style>${css}</style>`)
This is the same way LitHTML and Polymer have solved the problem:
Describe the problem
Currently, when compiling Svelte components to Web Components, the style is set via
innerHTML
:This is fine, as the styles are generated at compile time and don't contain any untrusted data. However, if an application has a TrustedTypes CSP set up, this line will throw an error. The only way for them to fix it is to add a default policy, which isn't ideal as it reduces the protection from the policy.
Describe the proposed solution
Use
textContent
to set the stylesheet content, rather thaninnerHTML
.which might be preferable all round (not sure if there are any performance implications). I have a PR open with this fix #8135. I've tested using the our CSP in the version of Svelte from my fork and it fixes it nicely!
Alternatives considered
We could create a
svelte-web-component-style-literal
no-op policy which is used instead. That way, consumers can allow thesvelte-web-component-style-literal
policy if they use Svelte web-components and everything will just work. I have a branch with a working proof of concept https://github.com/fallaciousreasoning/svelte/tree/trustedTypes but I think it's a bit less elegant.This is the same way LitHTML and Polymer have solved the problem:
Importance
i cannot use svelte without it (it breaks our CSP)
The text was updated successfully, but these errors were encountered: