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/react/test/integration/react-pages-client.test.ts b/packages/react/test/integration/react-pages-client.test.ts index 64b8090ce..2cf8a767b 100644 --- a/packages/react/test/integration/react-pages-client.test.ts +++ b/packages/react/test/integration/react-pages-client.test.ts @@ -795,6 +795,18 @@ describe('react-pages client (dev)', () => { expect(rootHtml).toContain('Welcome'); expect(rootHtml).toContain('React Pages Demo'); // shared data from middleware + // Inline page-data must round-trip through the 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(/ so the inline script tag cannot be closed early', () => { + const payload = { html: '' }; + const out = serializePageData(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 = serializePageData(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/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 bd8797c8e..446924479 100644 --- a/packages/vue/src/plugin-pages.ts +++ b/packages/vue/src/plugin-pages.ts @@ -213,18 +213,10 @@ 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/integration/vue-pages-client.test.ts b/packages/vue/test/integration/vue-pages-client.test.ts index ccde466e8..6171f7801 100644 --- a/packages/vue/test/integration/vue-pages-client.test.ts +++ b/packages/vue/test/integration/vue-pages-client.test.ts @@ -640,6 +640,18 @@ describe('vue-pages client (dev)', () => { expect(rootHtml).toContain('Welcome'); expect(rootHtml).toContain('Vue Pages Demo'); // shared data from middleware + // Inline page-data must round-trip through the 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(); });