Skip to content

fix: don't execute scripts inside @html when instantiated on the client #10556

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

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/moody-carrots-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: don't execute scripts inside `@html` when instantiated on the client
4 changes: 3 additions & 1 deletion packages/svelte/src/internal/client/reconciler.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ export function reconcile_html(target, value, svg) {
if (svg) {
html = `<svg>${html}</svg>`;
}
var content = create_fragment_with_script_from_html(html);
// Don't use create_fragment_with_script_from_html here because that would mean script tags are executed.
// @html is basically `.innerHTML = ...` and that doesn't execute scripts either due to security reasons.
var content = create_fragment_from_html(html);
if (svg) {
content = /** @type {DocumentFragment} */ (/** @type {unknown} */ (content.firstChild));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { test } from '../../assert';

export default test({
// Test that @html does not execute scripts when instantiated in the client.
// Needs to be in this test suite because JSDOM does not quite get this right.
html: `<div></div><script>document.body.innerHTML = 'this should not be executed'</script>`,
skip_if_ssr: 'permanent',
skip_if_hydrate: 'permanent'
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div></div>
{@html `<script>document.body.innerHTML = 'this should not be executed'</script>`}