From 39879319c1a8070f47d06c46b070cb366db99de7 Mon Sep 17 00:00:00 2001 From: Alexander Lichter Date: Tue, 5 May 2026 10:19:10 +0200 Subject: [PATCH 1/4] fix(pages): escape inline page-data so embedded doesn't truncate `serializePageData` in the Vue / Svelte / Solid renderer modules is defined inside a template literal that is later emitted as the SSR entry source. The replacement strings used `"\\u003c"` etc., which the template literal evaluation collapses to `"<"` in the emitted code; the JS parser then decodes that unicode escape, leaving `replaceAll("<", "<")` -- a no-op. As a result, raw `<`, `>`, `&`, U+2028, U+2029 leak into the inline `` (e.g. an HTML artifact persisted in user data) closes the tag early and produces `JSON.parse: unterminated string` on hydration. Doubling the backslashes in the replacement values restores the intended `\uXXXX` escapes in the emitted code, so the inline JSON survives the script tag and round-trips through `JSON.parse`. The React adapter's `serializePageData` lives in a regular module file (not a template literal) and is unaffected. --- packages/solid/src/plugin-pages.ts | 10 +++++----- packages/svelte/src/plugin-pages.ts | 10 +++++----- packages/vue/src/plugin-pages.ts | 10 +++++----- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/solid/src/plugin-pages.ts b/packages/solid/src/plugin-pages.ts index 73f2225da..8e2ce30b4 100644 --- a/packages/solid/src/plugin-pages.ts +++ b/packages/solid/src/plugin-pages.ts @@ -135,11 +135,11 @@ ${generateComponentManifest(scan, pagesDir)} function serializePageData(pageObj) { return JSON.stringify(pageObj) - .replaceAll("<", "\\u003c") - .replaceAll(">", "\\u003e") - .replaceAll("&", "\\u0026") - .replaceAll("\u2028", "\\u2028") - .replaceAll("\u2029", "\\u2029"); + .replaceAll("<", "\\\\u003c") + .replaceAll(">", "\\\\u003e") + .replaceAll("&", "\\\\u0026") + .replaceAll("\u2028", "\\\\u2028") + .replaceAll("\u2029", "\\\\u2029"); } export async function renderPage(pageObj, assetTags) { diff --git a/packages/svelte/src/plugin-pages.ts b/packages/svelte/src/plugin-pages.ts index 708e333af..7b3a193c5 100644 --- a/packages/svelte/src/plugin-pages.ts +++ b/packages/svelte/src/plugin-pages.ts @@ -136,11 +136,11 @@ ${generateComponentManifest(scan, pagesDir)} function serializePageData(pageObj) { return JSON.stringify(pageObj) - .replaceAll("<", "\\u003c") - .replaceAll(">", "\\u003e") - .replaceAll("&", "\\u0026") - .replaceAll("\u2028", "\\u2028") - .replaceAll("\u2029", "\\u2029"); + .replaceAll("<", "\\\\u003c") + .replaceAll(">", "\\\\u003e") + .replaceAll("&", "\\\\u0026") + .replaceAll("\u2028", "\\\\u2028") + .replaceAll("\u2029", "\\\\u2029"); } export async function renderPage(pageObj, assetTags) { diff --git a/packages/vue/src/plugin-pages.ts b/packages/vue/src/plugin-pages.ts index bd8797c8e..784e661bb 100644 --- a/packages/vue/src/plugin-pages.ts +++ b/packages/vue/src/plugin-pages.ts @@ -218,11 +218,11 @@ ${generateComponentManifest(scan, pagesDir)} function serializePageData(pageObj) { return JSON.stringify(pageObj) - .replaceAll("<", "\\u003c") - .replaceAll(">", "\\u003e") - .replaceAll("&", "\\u0026") - .replaceAll("\u2028", "\\u2028") - .replaceAll("\u2029", "\\u2029"); + .replaceAll("<", "\\\\u003c") + .replaceAll(">", "\\\\u003e") + .replaceAll("&", "\\\\u0026") + .replaceAll("\u2028", "\\\\u2028") + .replaceAll("\u2029", "\\\\u2029"); } function patchProps(comp, props) { From d1af7a4ebd8515596f1e4c7cb8ec24f21e4ab303 Mon Sep 17 00:00:00 2001 From: Alexander Lichter Date: Tue, 5 May 2026 13:38:27 +0200 Subject: [PATCH 2/4] test(pages): cover serializePageData script-tag escaping Regression test for the previous commit. Exports generateRendererModule from the vue/svelte/solid pages plugins so a unit test can extract the emitted serializePageData function, evaluate it, and assert that input containing , U+2028, U+2029, and `&` never leaks unescaped into the inline JSON and round-trips through JSON.parse. Without the fix in 3987931, all six assertions fail because the single-backslash escapes collapse during template-literal evaluation and the JS parser then decodes them back to the raw characters, leaving the .replaceAll calls as no-ops. --- packages/solid/src/plugin-pages.ts | 2 +- .../test/unit/serialize-page-data.test.ts | 41 +++++++++++++++++ packages/svelte/src/plugin-pages.ts | 6 ++- .../test/unit/serialize-page-data.test.ts | 42 +++++++++++++++++ packages/vue/src/plugin-pages.ts | 2 +- .../vue/test/unit/serialize-page-data.test.ts | 45 +++++++++++++++++++ 6 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 packages/solid/test/unit/serialize-page-data.test.ts create mode 100644 packages/svelte/test/unit/serialize-page-data.test.ts create mode 100644 packages/vue/test/unit/serialize-page-data.test.ts diff --git a/packages/solid/src/plugin-pages.ts b/packages/solid/src/plugin-pages.ts index 8e2ce30b4..e540e40fd 100644 --- a/packages/solid/src/plugin-pages.ts +++ b/packages/solid/src/plugin-pages.ts @@ -123,7 +123,7 @@ export function pagesPlugin(pagesDir: string, options?: PagesPluginOptions): Plu }; } -function generateRendererModule(scan: PageScanResult, pagesDir: string): string { +export function generateRendererModule(scan: PageScanResult, pagesDir: string): string { return ` import { renderToStringAsync, generateHydrationScript } from "solid-js/web"; import { createComponent } from "solid-js"; diff --git a/packages/solid/test/unit/serialize-page-data.test.ts b/packages/solid/test/unit/serialize-page-data.test.ts new file mode 100644 index 000000000..a3f997394 --- /dev/null +++ b/packages/solid/test/unit/serialize-page-data.test.ts @@ -0,0 +1,41 @@ +import { describe, expect, it } from 'vitest'; +import { generateRendererModule } from '../../src/plugin-pages.ts'; + +const LS = String.fromCharCode(0x2028); +const PS = String.fromCharCode(0x2029); + +function loadSerializePageData(): (pageObj: unknown) => string { + const emitted = generateRendererModule( + { pages: [], layouts: [], namedLayouts: [] }, + '/tmp/void-test-pages', + ); + const match = emitted.match(/function serializePageData\(pageObj\) \{[\s\S]*?\n\}/); + if (!match) { + throw new Error('Could not locate serializePageData in generated renderer module'); + } + return new Function(`${match[0]}\nreturn serializePageData;`)() as (pageObj: unknown) => string; +} + +describe('solid serializePageData (emitted)', () => { + const serialize = loadSerializePageData(); + + it('escapes so the inline script tag cannot be closed early', () => { + const payload = { html: '' }; + const out = serialize(payload); + + expect(out).not.toMatch(/<\/script/i); + expect(out).not.toContain('<'); + expect(out).not.toContain('>'); + expect(JSON.parse(out)).toEqual(payload); + }); + + it('escapes ampersands and U+2028 / U+2029 line separators', () => { + const payload = { amp: 'a & b', ls: `a${LS}b`, ps: `a${PS}b` }; + const out = serialize(payload); + + expect(out).not.toContain('&'); + expect(out).not.toContain(LS); + expect(out).not.toContain(PS); + expect(JSON.parse(out)).toEqual(payload); + }); +}); diff --git a/packages/svelte/src/plugin-pages.ts b/packages/svelte/src/plugin-pages.ts index 7b3a193c5..c3375d1ea 100644 --- a/packages/svelte/src/plugin-pages.ts +++ b/packages/svelte/src/plugin-pages.ts @@ -125,7 +125,11 @@ export function pagesPlugin(pagesDir: string, options?: PagesPluginOptions): Plu }; } -function generateRendererModule(scan: PageScanResult, pagesDir: string, _isDev: boolean): string { +export function generateRendererModule( + scan: PageScanResult, + pagesDir: string, + _isDev: boolean, +): string { return ` import { render } from "svelte/server"; import App from "@void/svelte/App.svelte"; diff --git a/packages/svelte/test/unit/serialize-page-data.test.ts b/packages/svelte/test/unit/serialize-page-data.test.ts new file mode 100644 index 000000000..7319335a5 --- /dev/null +++ b/packages/svelte/test/unit/serialize-page-data.test.ts @@ -0,0 +1,42 @@ +import { describe, expect, it } from 'vitest'; +import { generateRendererModule } from '../../src/plugin-pages.ts'; + +const LS = String.fromCharCode(0x2028); +const PS = String.fromCharCode(0x2029); + +function loadSerializePageData(): (pageObj: unknown) => string { + const emitted = generateRendererModule( + { pages: [], layouts: [], namedLayouts: [] }, + '/tmp/void-test-pages', + false, + ); + const match = emitted.match(/function serializePageData\(pageObj\) \{[\s\S]*?\n\}/); + if (!match) { + throw new Error('Could not locate serializePageData in generated renderer module'); + } + return new Function(`${match[0]}\nreturn serializePageData;`)() as (pageObj: unknown) => string; +} + +describe('svelte serializePageData (emitted)', () => { + const serialize = loadSerializePageData(); + + it('escapes so the inline script tag cannot be closed early', () => { + const payload = { html: '' }; + const out = serialize(payload); + + expect(out).not.toMatch(/<\/script/i); + expect(out).not.toContain('<'); + expect(out).not.toContain('>'); + expect(JSON.parse(out)).toEqual(payload); + }); + + it('escapes ampersands and U+2028 / U+2029 line separators', () => { + const payload = { amp: 'a & b', ls: `a${LS}b`, ps: `a${PS}b` }; + const out = serialize(payload); + + expect(out).not.toContain('&'); + expect(out).not.toContain(LS); + expect(out).not.toContain(PS); + expect(JSON.parse(out)).toEqual(payload); + }); +}); diff --git a/packages/vue/src/plugin-pages.ts b/packages/vue/src/plugin-pages.ts index 784e661bb..91002f3af 100644 --- a/packages/vue/src/plugin-pages.ts +++ b/packages/vue/src/plugin-pages.ts @@ -207,7 +207,7 @@ export function pagesPlugin(pagesDir: string, options?: PagesPluginOptions): Arr ]; } -function generateRendererModule(scan: PageScanResult, pagesDir: string): string { +export function generateRendererModule(scan: PageScanResult, pagesDir: string): string { return ` import { createSSRApp, h, reactive, provide } from "vue"; import { renderToString } from "vue/server-renderer"; diff --git a/packages/vue/test/unit/serialize-page-data.test.ts b/packages/vue/test/unit/serialize-page-data.test.ts new file mode 100644 index 000000000..f5fa2d48d --- /dev/null +++ b/packages/vue/test/unit/serialize-page-data.test.ts @@ -0,0 +1,45 @@ +import { describe, expect, it } from 'vitest'; +import { generateRendererModule } from '../../src/plugin-pages.ts'; + +const LS = String.fromCharCode(0x2028); +const PS = String.fromCharCode(0x2029); + +// The Vue renderer module is emitted as a template literal, so the +// `serializePageData` it ships only exists in the *generated* source. +// Extract that function from the generated output and exercise it directly, +// the same way it runs at SSR time. +function loadSerializePageData(): (pageObj: unknown) => string { + const emitted = generateRendererModule( + { pages: [], layouts: [], namedLayouts: [] }, + '/tmp/void-test-pages', + ); + const match = emitted.match(/function serializePageData\(pageObj\) \{[\s\S]*?\n\}/); + if (!match) { + throw new Error('Could not locate serializePageData in generated renderer module'); + } + return new Function(`${match[0]}\nreturn serializePageData;`)() as (pageObj: unknown) => string; +} + +describe('vue serializePageData (emitted)', () => { + const serialize = loadSerializePageData(); + + it('escapes so the inline script tag cannot be closed early', () => { + const payload = { html: '' }; + const out = serialize(payload); + + expect(out).not.toMatch(/<\/script/i); + expect(out).not.toContain('<'); + expect(out).not.toContain('>'); + expect(JSON.parse(out)).toEqual(payload); + }); + + it('escapes ampersands and U+2028 / U+2029 line separators', () => { + const payload = { amp: 'a & b', ls: `a${LS}b`, ps: `a${PS}b` }; + const out = serialize(payload); + + expect(out).not.toContain('&'); + expect(out).not.toContain(LS); + expect(out).not.toContain(PS); + expect(JSON.parse(out)).toEqual(payload); + }); +}); From 9968ee986913c08234735def5c4a717b515067d4 Mon Sep 17 00:00:00 2001 From: Alexander Lichter Date: Tue, 5 May 2026 13:46:17 +0200 Subject: [PATCH 3/4] refactor(pages): share serializePageData across adapters The Vue/Svelte/Solid pages plugins each emit their renderer module as a template literal and inlined an identical copy of serializePageData. That duplication is what made commit 3987931 a three-line fix in three different files, and it's also what made the bug possible in the first place: the function only existed inside string-in-template-in-string sandwiches, where one stray backslash collapses to a no-op replaceAll. Move the function to packages/void/src/pages/serialize.ts (using the single regex form React already had), expose it via the new "void/pages-server" subpath, and have all four adapters import it instead. The Vue/Svelte/Solid emitted modules now contain `import { serializePageData } from "void/pages-server"` instead of an inline copy, so the escaping logic lives in real source and can never collapse during template-literal evaluation again. Per-adapter regression tests added in the previous commit collapse to a single packages/void/test/unit/serialize-page-data.test.ts that exercises the real shared function. The temporary `export` on generateRendererModule in each adapter (only added to make the per-adapter tests reachable) goes away with them. --- packages/react/src/runtime/pages-server.ts | 7 +-- packages/solid/src/plugin-pages.ts | 12 +---- packages/svelte/src/plugin-pages.ts | 16 +------ .../test/unit/serialize-page-data.test.ts | 42 ----------------- packages/void/package.json | 5 +++ packages/void/src/pages/index.ts | 1 + packages/void/src/pages/serialize.ts | 11 +++++ .../test/unit/serialize-page-data.test.ts | 22 ++------- packages/void/tsdown.config.ts | 1 + packages/vue/src/plugin-pages.ts | 12 +---- .../vue/test/unit/serialize-page-data.test.ts | 45 ------------------- 11 files changed, 29 insertions(+), 145 deletions(-) delete mode 100644 packages/svelte/test/unit/serialize-page-data.test.ts create mode 100644 packages/void/src/pages/serialize.ts rename packages/{solid => void}/test/unit/serialize-page-data.test.ts (50%) delete mode 100644 packages/vue/test/unit/serialize-page-data.test.ts diff --git a/packages/react/src/runtime/pages-server.ts b/packages/react/src/runtime/pages-server.ts index c639d8f72..8036c7af1 100644 --- a/packages/react/src/runtime/pages-server.ts +++ b/packages/react/src/runtime/pages-server.ts @@ -3,6 +3,7 @@ import { renderToReadableStream } from 'react-dom/server'; import { createSsrRouter, idleNavigationState } from 'void/pages-client'; import { renderBodyAttrs, renderHeadToString, renderHtmlAttrs } from 'void/pages-head'; import type { PageObject } from 'void/pages-protocol'; +import { serializePageData } from 'void/pages-server'; import { ErrorsContext, NavigationContext, RouterContext, SharedContext } from './context.ts'; // oxlint-disable-next-line no-explicit-any -- component props are narrowed by generated route types @@ -19,12 +20,6 @@ export interface ReactPagesServerConfig { preambleScript?: string; } -function serializePageData(pageObj: PageObject): string { - return JSON.stringify(pageObj).replace(/[<>&\u2028\u2029]/g, (c) => { - return `\\u${c.charCodeAt(0).toString(16).padStart(4, '0')}`; - }); -} - function createPendingDeferred(): Promise { return new Promise(() => {}); } diff --git a/packages/solid/src/plugin-pages.ts b/packages/solid/src/plugin-pages.ts index e540e40fd..e722153bf 100644 --- a/packages/solid/src/plugin-pages.ts +++ b/packages/solid/src/plugin-pages.ts @@ -123,25 +123,17 @@ export function pagesPlugin(pagesDir: string, options?: PagesPluginOptions): Plu }; } -export function generateRendererModule(scan: PageScanResult, pagesDir: string): string { +function generateRendererModule(scan: PageScanResult, pagesDir: string): string { return ` import { renderToStringAsync, generateHydrationScript } from "solid-js/web"; import { createComponent } from "solid-js"; import { SharedContext, ErrorsContext, RouterContext, NavigationContext } from "@void/solid"; import { createSsrRouter, idleNavigationState } from "void/pages-client"; import { renderHeadToString, renderHtmlAttrs, renderBodyAttrs } from "void/pages-head"; +import { serializePageData } from "void/pages-server"; ${generateComponentManifest(scan, pagesDir)} -function serializePageData(pageObj) { - return JSON.stringify(pageObj) - .replaceAll("<", "\\\\u003c") - .replaceAll(">", "\\\\u003e") - .replaceAll("&", "\\\\u0026") - .replaceAll("\u2028", "\\\\u2028") - .replaceAll("\u2029", "\\\\u2029"); -} - export async function renderPage(pageObj, assetTags) { const PageComponent = (await components[pageObj.component]()).default; const layoutIds = layoutTree[pageObj.component] || []; diff --git a/packages/svelte/src/plugin-pages.ts b/packages/svelte/src/plugin-pages.ts index c3375d1ea..6b178ca95 100644 --- a/packages/svelte/src/plugin-pages.ts +++ b/packages/svelte/src/plugin-pages.ts @@ -125,28 +125,16 @@ export function pagesPlugin(pagesDir: string, options?: PagesPluginOptions): Plu }; } -export function generateRendererModule( - scan: PageScanResult, - pagesDir: string, - _isDev: boolean, -): string { +function generateRendererModule(scan: PageScanResult, pagesDir: string, _isDev: boolean): string { return ` import { render } from "svelte/server"; import App from "@void/svelte/App.svelte"; import { createSsrRouter, idleNavigationState } from "void/pages-client"; import { renderHeadToString, renderHtmlAttrs, renderBodyAttrs } from "void/pages-head"; +import { serializePageData } from "void/pages-server"; ${generateComponentManifest(scan, pagesDir)} -function serializePageData(pageObj) { - return JSON.stringify(pageObj) - .replaceAll("<", "\\\\u003c") - .replaceAll(">", "\\\\u003e") - .replaceAll("&", "\\\\u0026") - .replaceAll("\u2028", "\\\\u2028") - .replaceAll("\u2029", "\\\\u2029"); -} - export async function renderPage(pageObj, assetTags) { const PageComponent = (await components[pageObj.component]()).default; const layoutIds = layoutTree[pageObj.component] || []; diff --git a/packages/svelte/test/unit/serialize-page-data.test.ts b/packages/svelte/test/unit/serialize-page-data.test.ts deleted file mode 100644 index 7319335a5..000000000 --- a/packages/svelte/test/unit/serialize-page-data.test.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { describe, expect, it } from 'vitest'; -import { generateRendererModule } from '../../src/plugin-pages.ts'; - -const LS = String.fromCharCode(0x2028); -const PS = String.fromCharCode(0x2029); - -function loadSerializePageData(): (pageObj: unknown) => string { - const emitted = generateRendererModule( - { pages: [], layouts: [], namedLayouts: [] }, - '/tmp/void-test-pages', - false, - ); - const match = emitted.match(/function serializePageData\(pageObj\) \{[\s\S]*?\n\}/); - if (!match) { - throw new Error('Could not locate serializePageData in generated renderer module'); - } - return new Function(`${match[0]}\nreturn serializePageData;`)() as (pageObj: unknown) => string; -} - -describe('svelte serializePageData (emitted)', () => { - const serialize = loadSerializePageData(); - - it('escapes so the inline script tag cannot be closed early', () => { - const payload = { html: '' }; - const out = serialize(payload); - - expect(out).not.toMatch(/<\/script/i); - expect(out).not.toContain('<'); - expect(out).not.toContain('>'); - expect(JSON.parse(out)).toEqual(payload); - }); - - it('escapes ampersands and U+2028 / U+2029 line separators', () => { - const payload = { amp: 'a & b', ls: `a${LS}b`, ps: `a${PS}b` }; - const out = serialize(payload); - - expect(out).not.toContain('&'); - expect(out).not.toContain(LS); - expect(out).not.toContain(PS); - expect(JSON.parse(out)).toEqual(payload); - }); -}); diff --git a/packages/void/package.json b/packages/void/package.json index 38c5a4d16..294df4125 100644 --- a/packages/void/package.json +++ b/packages/void/package.json @@ -206,6 +206,11 @@ "import": "./dist/pages/client.mjs", "require": "./dist/pages/client.mjs" }, + "./pages-server": { + "types": "./dist/pages/serialize.d.mts", + "import": "./dist/pages/serialize.mjs", + "require": "./dist/pages/serialize.mjs" + }, "./pages-islands-plugin": { "types": "./dist/pages/islands-plugin.d.mts", "import": "./dist/pages/islands-plugin.mjs", diff --git a/packages/void/src/pages/index.ts b/packages/void/src/pages/index.ts index 526a52eef..7e464a316 100644 --- a/packages/void/src/pages/index.ts +++ b/packages/void/src/pages/index.ts @@ -15,3 +15,4 @@ export type { HeadDescriptor, HeadConfig } from './head.ts'; export { generateComponentManifest } from './manifest.ts'; export { resolveStaticPageDataBuildOptions } from './static-data.ts'; export type { StaticPageDataBuildOptions } from './static-data.ts'; +export { serializePageData } from './serialize.ts'; diff --git a/packages/void/src/pages/serialize.ts b/packages/void/src/pages/serialize.ts new file mode 100644 index 000000000..d490c38a3 --- /dev/null +++ b/packages/void/src/pages/serialize.ts @@ -0,0 +1,11 @@ +/** + * Escape JSON for embedding inside an inline ` so the inline script tag cannot be closed early', () => { const payload = { html: '' }; - const out = serialize(payload); + const out = serializePageData(payload); expect(out).not.toMatch(/<\/script/i); expect(out).not.toContain('<'); @@ -31,7 +17,7 @@ describe('solid serializePageData (emitted)', () => { it('escapes ampersands and U+2028 / U+2029 line separators', () => { const payload = { amp: 'a & b', ls: `a${LS}b`, ps: `a${PS}b` }; - const out = serialize(payload); + const out = serializePageData(payload); expect(out).not.toContain('&'); expect(out).not.toContain(LS); diff --git a/packages/void/tsdown.config.ts b/packages/void/tsdown.config.ts index 46843597e..2fa2af465 100644 --- a/packages/void/tsdown.config.ts +++ b/packages/void/tsdown.config.ts @@ -113,6 +113,7 @@ export default defineConfig({ 'src/pages/head-client.ts', 'src/pages/prefetch.ts', 'src/pages/client.ts', + 'src/pages/serialize.ts', 'src/pages/islands-plugin.ts', 'src/cli/cli.ts', ], diff --git a/packages/vue/src/plugin-pages.ts b/packages/vue/src/plugin-pages.ts index 91002f3af..446924479 100644 --- a/packages/vue/src/plugin-pages.ts +++ b/packages/vue/src/plugin-pages.ts @@ -207,24 +207,16 @@ export function pagesPlugin(pagesDir: string, options?: PagesPluginOptions): Arr ]; } -export function generateRendererModule(scan: PageScanResult, pagesDir: string): string { +function generateRendererModule(scan: PageScanResult, pagesDir: string): string { return ` import { createSSRApp, h, reactive, provide } from "vue"; import { renderToString } from "vue/server-renderer"; import { createSsrRouter, idleNavigationState } from "void/pages-client"; import { renderHeadToString, renderHtmlAttrs, renderBodyAttrs } from "void/pages-head"; +import { serializePageData } from "void/pages-server"; ${generateComponentManifest(scan, pagesDir)} -function serializePageData(pageObj) { - return JSON.stringify(pageObj) - .replaceAll("<", "\\\\u003c") - .replaceAll(">", "\\\\u003e") - .replaceAll("&", "\\\\u0026") - .replaceAll("\u2028", "\\\\u2028") - .replaceAll("\u2029", "\\\\u2029"); -} - function patchProps(comp, props) { if (comp.__voidPatched) return; comp.props = Object.keys(props); diff --git a/packages/vue/test/unit/serialize-page-data.test.ts b/packages/vue/test/unit/serialize-page-data.test.ts deleted file mode 100644 index f5fa2d48d..000000000 --- a/packages/vue/test/unit/serialize-page-data.test.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { describe, expect, it } from 'vitest'; -import { generateRendererModule } from '../../src/plugin-pages.ts'; - -const LS = String.fromCharCode(0x2028); -const PS = String.fromCharCode(0x2029); - -// The Vue renderer module is emitted as a template literal, so the -// `serializePageData` it ships only exists in the *generated* source. -// Extract that function from the generated output and exercise it directly, -// the same way it runs at SSR time. -function loadSerializePageData(): (pageObj: unknown) => string { - const emitted = generateRendererModule( - { pages: [], layouts: [], namedLayouts: [] }, - '/tmp/void-test-pages', - ); - const match = emitted.match(/function serializePageData\(pageObj\) \{[\s\S]*?\n\}/); - if (!match) { - throw new Error('Could not locate serializePageData in generated renderer module'); - } - return new Function(`${match[0]}\nreturn serializePageData;`)() as (pageObj: unknown) => string; -} - -describe('vue serializePageData (emitted)', () => { - const serialize = loadSerializePageData(); - - it('escapes so the inline script tag cannot be closed early', () => { - const payload = { html: '' }; - const out = serialize(payload); - - expect(out).not.toMatch(/<\/script/i); - expect(out).not.toContain('<'); - expect(out).not.toContain('>'); - expect(JSON.parse(out)).toEqual(payload); - }); - - it('escapes ampersands and U+2028 / U+2029 line separators', () => { - const payload = { amp: 'a & b', ls: `a${LS}b`, ps: `a${PS}b` }; - const out = serialize(payload); - - expect(out).not.toContain('&'); - expect(out).not.toContain(LS); - expect(out).not.toContain(PS); - expect(JSON.parse(out)).toEqual(payload); - }); -}); From d8b8ac51519c8a4598fde182cbbebe3124ce2c33 Mon Sep 17 00:00:00 2001 From: Alexander Lichter Date: Tue, 5 May 2026 15:14:30 +0200 Subject: [PATCH 4/4] test(pages): integration round-trip a -bearing payload MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The unit test in packages/void/test/unit/serialize-page-data.test.ts only proves the function is correct in isolation; nothing exercised the wiring (the `void/pages-server` subpath, the dist file, the literal string typed into each adapter's emitted SSR module). A typo or a future template-literal-collapse regression could slip past unit tests. Add a `scriptProbe` to each adapter playground's shared-state middleware holding the original attack payload (`` plus U+2028/U+2029 and `&`). Each adapter's existing "dev: SSR pages…" integration test now also extracts the `__VOID_PAGE_DATA__` script body via regex, asserts it contains no literal ``, and JSON-parses it to confirm the probe round-trips. If `serializePageData` ever regresses, the embedded ` in scriptProbe + // closes the inline tag early, the regex below captures truncated + // text, and JSON.parse throws. + const probe = '\u2028\u2029&'; + const probeMatch = rootHtml.match(/ in scriptProbe + // closes the inline tag early, the regex below captures truncated + // text, and JSON.parse throws. + const probe = '\u2028\u2029&'; + const probeMatch = rootHtml.match(/ in scriptProbe + // closes the inline tag early, the regex below captures truncated + // text, and JSON.parse throws. + const probe = '\u2028\u2029&'; + const probeMatch = rootHtml.match(/ in scriptProbe + // closes the inline tag early, the regex below captures truncated + // text, and JSON.parse throws. + const probe = '\u2028\u2029&'; + const probeMatch = rootHtml.match(/\u2028\u2029&'; + export default defineMiddleware(async (c, next) => { c.set('shared', { appName: 'React Pages Demo', + scriptProbe: SCRIPT_PROBE, }); await next(); }); diff --git a/playground/solid-pages/middleware/01.shared.ts b/playground/solid-pages/middleware/01.shared.ts index 16bc5bff5..0e31470a0 100644 --- a/playground/solid-pages/middleware/01.shared.ts +++ b/playground/solid-pages/middleware/01.shared.ts @@ -2,13 +2,19 @@ import { defineMiddleware } from 'void'; declare module 'void' { interface CloudContextVariables { - shared: { appName: string }; + shared: { appName: string; scriptProbe: string }; } } +// Regression payload for the inline page-data script-tag escape. If anything +// in the SSR pipeline forgets to escape, the embedded \u2028\u2029&'; + export default defineMiddleware(async (c, next) => { c.set('shared', { appName: 'Solid Pages Demo', + scriptProbe: SCRIPT_PROBE, }); await next(); }); diff --git a/playground/svelte-pages/middleware/01.shared.ts b/playground/svelte-pages/middleware/01.shared.ts index 8910612dc..05c64fa35 100644 --- a/playground/svelte-pages/middleware/01.shared.ts +++ b/playground/svelte-pages/middleware/01.shared.ts @@ -2,13 +2,19 @@ import { defineMiddleware } from 'void'; declare module 'void' { interface CloudContextVariables { - shared: { appName: string }; + shared: { appName: string; scriptProbe: string }; } } +// Regression payload for the inline page-data script-tag escape. If anything +// in the SSR pipeline forgets to escape, the embedded \u2028\u2029&'; + export default defineMiddleware(async (c, next) => { c.set('shared', { appName: 'Svelte Pages Demo', + scriptProbe: SCRIPT_PROBE, }); await next(); }); diff --git a/playground/vue-pages/middleware/01.shared.ts b/playground/vue-pages/middleware/01.shared.ts index 18899dc9d..5293c8183 100644 --- a/playground/vue-pages/middleware/01.shared.ts +++ b/playground/vue-pages/middleware/01.shared.ts @@ -2,13 +2,19 @@ import { defineMiddleware } from 'void'; declare module 'void' { interface CloudContextVariables { - shared: { appName: string }; + shared: { appName: string; scriptProbe: string }; } } +// Regression payload for the inline page-data script-tag escape. If anything +// in the SSR pipeline forgets to escape, the embedded \u2028\u2029&'; + export default defineMiddleware(async (c, next) => { c.set('shared', { appName: 'Vue Pages Demo', + scriptProbe: SCRIPT_PROBE, }); await next(); });