Skip to content
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
7 changes: 1 addition & 6 deletions packages/react/src/runtime/pages-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<never> {
return new Promise(() => {});
}
Expand Down
12 changes: 12 additions & 0 deletions packages/react/test/integration/react-pages-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <script> tag without
// truncation. If serializePageData ever regresses (e.g. via another
// template-literal backslash-collapse), the </script> in scriptProbe
// closes the inline tag early, the regex below captures truncated
// text, and JSON.parse throws.
const probe = '</script><script>alert(1)</script>\u2028\u2029&';
const probeMatch = rootHtml.match(/<script id="__VOID_PAGE_DATA__"[^>]*>([\s\S]*?)<\/script>/);
expect(probeMatch).not.toBeNull();
const probeJson = probeMatch![1];
expect(probeJson).not.toMatch(/<\/script/i);
expect(JSON.parse(probeJson).shared.scriptProbe).toBe(probe);

// 1b. Head management — SSR HTML contains head tags from void.json + page head()
expect(rootHtml).toContain('<title>Home | React Pages Demo</title>');
expect(rootHtml).toContain('<meta charset="utf-8">');
Expand Down
10 changes: 1 addition & 9 deletions packages/solid/src/plugin-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,10 @@ 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] || [];
Expand Down
12 changes: 12 additions & 0 deletions packages/solid/test/integration/solid-pages-playground.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,18 @@ describe('solid-pages client (dev)', () => {
expect(rootHtml).toContain('Welcome');
expect(rootHtml).toContain('Solid Pages Demo'); // shared data from middleware

// Inline page-data must round-trip through the <script> tag without
// truncation. If serializePageData ever regresses (e.g. via another
// template-literal backslash-collapse), the </script> in scriptProbe
// closes the inline tag early, the regex below captures truncated
// text, and JSON.parse throws.
const probe = '</script><script>alert(1)</script>\u2028\u2029&';
const probeMatch = rootHtml.match(/<script id="__VOID_PAGE_DATA__"[^>]*>([\s\S]*?)<\/script>/);
expect(probeMatch).not.toBeNull();
const probeJson = probeMatch![1];
expect(probeJson).not.toMatch(/<\/script/i);
expect(JSON.parse(probeJson).shared.scriptProbe).toBe(probe);

// 2. SSR content contains server-rendered props
expect(rootHtml).toContain('This is the Solid Pages playground.');

Expand Down
10 changes: 1 addition & 9 deletions packages/svelte/src/plugin-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,10 @@ 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] || [];
Expand Down
12 changes: 12 additions & 0 deletions packages/svelte/test/integration/svelte-pages-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,18 @@ describe('svelte-pages client (dev)', () => {
expect(rootHtml).toContain('Welcome');
expect(rootHtml).toContain('Svelte Pages Demo'); // shared data from middleware

// Inline page-data must round-trip through the <script> tag without
// truncation. If serializePageData ever regresses (e.g. via another
// template-literal backslash-collapse), the </script> in scriptProbe
// closes the inline tag early, the regex below captures truncated
// text, and JSON.parse throws.
const probe = '</script><script>alert(1)</script>\u2028\u2029&';
const probeMatch = rootHtml.match(/<script id="__VOID_PAGE_DATA__"[^>]*>([\s\S]*?)<\/script>/);
expect(probeMatch).not.toBeNull();
const probeJson = probeMatch![1];
expect(probeJson).not.toMatch(/<\/script/i);
expect(JSON.parse(probeJson).shared.scriptProbe).toBe(probe);

// 2. SSR content contains server-rendered props
expect(rootHtml).toContain('This is the Svelte Pages playground.');

Expand Down
5 changes: 5 additions & 0 deletions packages/void/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions packages/void/src/pages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
11 changes: 11 additions & 0 deletions packages/void/src/pages/serialize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Escape JSON for embedding inside an inline `<script type="application/json">`
* tag. Replaces `<`, `>`, `&`, U+2028, U+2029 with their `\uXXXX` JSON escape
* sequences so the script tag cannot be closed early and the payload survives
* `JSON.parse` on hydration.
*/
export function serializePageData(pageObj: unknown): string {
return JSON.stringify(pageObj).replace(/[<>&\u2028\u2029]/g, (c) => {
return `\\u${c.charCodeAt(0).toString(16).padStart(4, '0')}`;
});
}
27 changes: 27 additions & 0 deletions packages/void/test/unit/serialize-page-data.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, expect, it } from 'vitest';
import { serializePageData } from '../../src/pages/serialize.ts';

const LS = String.fromCharCode(0x2028);
const PS = String.fromCharCode(0x2029);

describe('serializePageData', () => {
it('escapes </script> so the inline script tag cannot be closed early', () => {
const payload = { html: '</script><script>alert(1)</script>' };
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);
});
});
1 change: 1 addition & 0 deletions packages/void/tsdown.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
Expand Down
10 changes: 1 addition & 9 deletions packages/vue/src/plugin-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions packages/vue/test/integration/vue-pages-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <script> tag without
// truncation. If serializePageData ever regresses (e.g. via another
// template-literal backslash-collapse), the </script> in scriptProbe
// closes the inline tag early, the regex below captures truncated
// text, and JSON.parse throws.
const probe = '</script><script>alert(1)</script>\u2028\u2029&';
const probeMatch = rootHtml.match(/<script id="__VOID_PAGE_DATA__"[^>]*>([\s\S]*?)<\/script>/);
expect(probeMatch).not.toBeNull();
const probeJson = probeMatch![1];
expect(probeJson).not.toMatch(/<\/script/i);
expect(JSON.parse(probeJson).shared.scriptProbe).toBe(probe);

// 2. SSR content contains server-rendered props
expect(rootHtml).toContain('This is the Vue Pages playground.');

Expand Down
8 changes: 7 additions & 1 deletion playground/react-pages/middleware/01.shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <script> closes early
// and JSON.parse fails on hydration.
const SCRIPT_PROBE = '</script><script>alert(1)</script>\u2028\u2029&';

export default defineMiddleware(async (c, next) => {
c.set('shared', {
appName: 'React Pages Demo',
scriptProbe: SCRIPT_PROBE,
});
await next();
});
8 changes: 7 additions & 1 deletion playground/solid-pages/middleware/01.shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <script> closes early
// and JSON.parse fails on hydration.
const SCRIPT_PROBE = '</script><script>alert(1)</script>\u2028\u2029&';

export default defineMiddleware(async (c, next) => {
c.set('shared', {
appName: 'Solid Pages Demo',
scriptProbe: SCRIPT_PROBE,
});
await next();
});
8 changes: 7 additions & 1 deletion playground/svelte-pages/middleware/01.shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <script> closes early
// and JSON.parse fails on hydration.
const SCRIPT_PROBE = '</script><script>alert(1)</script>\u2028\u2029&';

export default defineMiddleware(async (c, next) => {
c.set('shared', {
appName: 'Svelte Pages Demo',
scriptProbe: SCRIPT_PROBE,
});
await next();
});
8 changes: 7 additions & 1 deletion playground/vue-pages/middleware/01.shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <script> closes early
// and JSON.parse fails on hydration.
const SCRIPT_PROBE = '</script><script>alert(1)</script>\u2028\u2029&';

export default defineMiddleware(async (c, next) => {
c.set('shared', {
appName: 'Vue Pages Demo',
scriptProbe: SCRIPT_PROBE,
});
await next();
});