Skip to content

fix: correctly validate head snippets on the server #15755

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 2 commits into from
Apr 13, 2025
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/dirty-zebras-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: correctly validate head snippets on the server
8 changes: 6 additions & 2 deletions packages/svelte/src/internal/server/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '../../html-tree-validation.js';
import { current_component } from './context.js';
import { invalid_snippet_arguments } from '../shared/errors.js';
import { Payload } from './payload.js';
import { HeadPayload, Payload } from './payload.js';

/**
* @typedef {{
Expand Down Expand Up @@ -105,7 +105,11 @@ export function pop_element() {
* @param {Payload} payload
*/
export function validate_snippet_args(payload) {
if (typeof payload !== 'object' || !(payload instanceof Payload)) {
if (
typeof payload !== 'object' ||
// for some reason typescript consider the type of payload as never after the first instanceof
!(payload instanceof Payload || /** @type {any} */ (payload) instanceof HeadPayload)
) {
invalid_snippet_arguments();
}
}
34 changes: 21 additions & 13 deletions packages/svelte/src/internal/server/payload.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
export class HeadPayload {
/** @type {Set<{ hash: string; code: string }>} */
css = new Set();
out = '';
uid = () => '';
title = '';

constructor(css = new Set(), out = '', title = '', uid = () => '') {
this.css = css;
this.out = out;
this.title = title;
this.uid = uid;
}
}

export class Payload {
/** @type {Set<{ hash: string; code: string }>} */
css = new Set();
out = '';
uid = () => '';

head = {
/** @type {Set<{ hash: string; code: string }>} */
css: new Set(),
title: '',
out: '',
uid: () => ''
};
head = new HeadPayload();

constructor(id_prefix = '') {
this.uid = props_id_generator(id_prefix);
Expand All @@ -30,12 +39,11 @@ export function copy_payload({ out, css, head, uid }) {
payload.css = new Set(css);
payload.uid = uid;

payload.head = {
title: head.title,
out: head.out,
css: new Set(head.css),
uid: head.uid
};
payload.head = new HeadPayload();
payload.head.out = head.out;
payload.head.css = new Set(head.css);
payload.head.title = head.title;
payload.head.uid = head.uid;

return payload;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { test } from '../../test';

export default test({
compileOptions: {
dev: true
},
mode: ['server'],
async test({ errors, assert }) {
assert.equal(errors, []);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{#snippet head()}
<title>Cool</title>
{/snippet}

<svelte:head>
{@render head()}
</svelte:head>