mirror of https://github.com/sveltejs/svelte
[fix]: Add support for TrustedTypes in Svelte (#16271)
### Before submitting the PR, please make sure you do the following Resolves https://github.com/sveltejs/svelte/issues/14438 Resolves https://github.com/sveltejs/svelte/issues/10826 This PR makes it possible to use Svelte on pages which require `TrustedTypes` support via their CSP by wrapping assignments to `innerHTML` in a `TrustedTypePolicy` called `svelte-trusted-html` if the `TrustedTypes` API exists. Servers can allowlist the policy by setting `require-trusted-types-for 'script'; trusted-types svelte-trusted-html` in their `Content-Security-Policy` header. - [x] 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 - [x] Prefix your PR title with `feat:`, `fix:`, `chore:`, or `docs:`. - [x] This message body should clearly illustrate what problems it solves. - [ ] Ideally, include a test that fails without this PR but passes with it. - [x] If this PR changes code within `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: 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 no `TrustedTypes` support (i.e. Firefox, Safari) - [ ] Run the tests with `pnpm test` and lint the project with `pnpm 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` --------- Co-authored-by: 7nik <kfiiranet@gmail.com> Co-authored-by: Rich Harris <rich.harris@vercel.com> Co-authored-by: Rich Harris <hello@rich-harris.dev>pull/17698/head
parent
75e1992141
commit
6c15e711e2
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': minor
|
||||
---
|
||||
|
||||
feat: Use `TrustedTypes` for HTML handling where supported
|
||||
@ -1,8 +1,29 @@
|
||||
/** @import {} from 'trusted-types' */
|
||||
|
||||
import { create_element } from './operations.js';
|
||||
|
||||
const policy = /* @__PURE__ */ globalThis?.window?.trustedTypes?.createPolicy(
|
||||
'svelte-trusted-html',
|
||||
{
|
||||
/** @param {string} html */
|
||||
createHTML: (html) => {
|
||||
return html;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
/** @param {string} html */
|
||||
export function create_fragment_from_html(html) {
|
||||
function create_trusted_html(html) {
|
||||
return /** @type {string} */ (policy?.createHTML(html) ?? html);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} html
|
||||
* @param {boolean} trusted
|
||||
*/
|
||||
export function create_fragment_from_html(html, trusted = false) {
|
||||
var elem = create_element('template');
|
||||
elem.innerHTML = html.replaceAll('<!>', '<!---->'); // XHTML compliance
|
||||
html = html.replaceAll('<!>', '<!---->'); // XHTML compliance
|
||||
elem.innerHTML = trusted ? create_trusted_html(html) : html;
|
||||
return elem.content;
|
||||
}
|
||||
|
||||
Loading…
Reference in new issue