Skip to content

Commit d8b8ac5

Browse files
test(pages): integration round-trip a </script>-bearing payload
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 (`</script><script>alert(1)</script>` 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 `</script>`, and JSON-parses it to confirm the probe round-trips. If `serializePageData` ever regresses, the embedded `<script>` closes early, the regex captures truncated content, and JSON.parse throws — catching the original bug end-to-end on all four adapters.
1 parent 9968ee9 commit d8b8ac5

8 files changed

Lines changed: 76 additions & 4 deletions

File tree

‎packages/react/test/integration/react-pages-client.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,18 @@ describe('react-pages client (dev)', () => {
795795
expect(rootHtml).toContain('Welcome');
796796
expect(rootHtml).toContain('React Pages Demo'); // shared data from middleware
797797

798+
// Inline page-data must round-trip through the <script> tag without
799+
// truncation. If serializePageData ever regresses (e.g. via another
800+
// template-literal backslash-collapse), the </script> in scriptProbe
801+
// closes the inline tag early, the regex below captures truncated
802+
// text, and JSON.parse throws.
803+
const probe = '</script><script>alert(1)</script>\u2028\u2029&';
804+
const probeMatch = rootHtml.match(/<script id="__VOID_PAGE_DATA__"[^>]*>([\s\S]*?)<\/script>/);
805+
expect(probeMatch).not.toBeNull();
806+
const probeJson = probeMatch![1];
807+
expect(probeJson).not.toMatch(/<\/script/i);
808+
expect(JSON.parse(probeJson).shared.scriptProbe).toBe(probe);
809+
798810
// 1b. Head management — SSR HTML contains head tags from void.json + page head()
799811
expect(rootHtml).toContain('<title>Home | React Pages Demo</title>');
800812
expect(rootHtml).toContain('<meta charset="utf-8">');

‎packages/solid/test/integration/solid-pages-playground.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,18 @@ describe('solid-pages client (dev)', () => {
636636
expect(rootHtml).toContain('Welcome');
637637
expect(rootHtml).toContain('Solid Pages Demo'); // shared data from middleware
638638

639+
// Inline page-data must round-trip through the <script> tag without
640+
// truncation. If serializePageData ever regresses (e.g. via another
641+
// template-literal backslash-collapse), the </script> in scriptProbe
642+
// closes the inline tag early, the regex below captures truncated
643+
// text, and JSON.parse throws.
644+
const probe = '</script><script>alert(1)</script>\u2028\u2029&';
645+
const probeMatch = rootHtml.match(/<script id="__VOID_PAGE_DATA__"[^>]*>([\s\S]*?)<\/script>/);
646+
expect(probeMatch).not.toBeNull();
647+
const probeJson = probeMatch![1];
648+
expect(probeJson).not.toMatch(/<\/script/i);
649+
expect(JSON.parse(probeJson).shared.scriptProbe).toBe(probe);
650+
639651
// 2. SSR content contains server-rendered props
640652
expect(rootHtml).toContain('This is the Solid Pages playground.');
641653

‎packages/svelte/test/integration/svelte-pages-client.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,18 @@ describe('svelte-pages client (dev)', () => {
602602
expect(rootHtml).toContain('Welcome');
603603
expect(rootHtml).toContain('Svelte Pages Demo'); // shared data from middleware
604604

605+
// Inline page-data must round-trip through the <script> tag without
606+
// truncation. If serializePageData ever regresses (e.g. via another
607+
// template-literal backslash-collapse), the </script> in scriptProbe
608+
// closes the inline tag early, the regex below captures truncated
609+
// text, and JSON.parse throws.
610+
const probe = '</script><script>alert(1)</script>\u2028\u2029&';
611+
const probeMatch = rootHtml.match(/<script id="__VOID_PAGE_DATA__"[^>]*>([\s\S]*?)<\/script>/);
612+
expect(probeMatch).not.toBeNull();
613+
const probeJson = probeMatch![1];
614+
expect(probeJson).not.toMatch(/<\/script/i);
615+
expect(JSON.parse(probeJson).shared.scriptProbe).toBe(probe);
616+
605617
// 2. SSR content contains server-rendered props
606618
expect(rootHtml).toContain('This is the Svelte Pages playground.');
607619

‎packages/vue/test/integration/vue-pages-client.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,18 @@ describe('vue-pages client (dev)', () => {
640640
expect(rootHtml).toContain('Welcome');
641641
expect(rootHtml).toContain('Vue Pages Demo'); // shared data from middleware
642642

643+
// Inline page-data must round-trip through the <script> tag without
644+
// truncation. If serializePageData ever regresses (e.g. via another
645+
// template-literal backslash-collapse), the </script> in scriptProbe
646+
// closes the inline tag early, the regex below captures truncated
647+
// text, and JSON.parse throws.
648+
const probe = '</script><script>alert(1)</script>\u2028\u2029&';
649+
const probeMatch = rootHtml.match(/<script id="__VOID_PAGE_DATA__"[^>]*>([\s\S]*?)<\/script>/);
650+
expect(probeMatch).not.toBeNull();
651+
const probeJson = probeMatch![1];
652+
expect(probeJson).not.toMatch(/<\/script/i);
653+
expect(JSON.parse(probeJson).shared.scriptProbe).toBe(probe);
654+
643655
// 2. SSR content contains server-rendered props
644656
expect(rootHtml).toContain('This is the Vue Pages playground.');
645657

‎playground/react-pages/middleware/01.shared.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ import { defineMiddleware } from 'void';
22

33
declare module 'void' {
44
interface CloudContextVariables {
5-
shared: { appName: string };
5+
shared: { appName: string; scriptProbe: string };
66
}
77
}
88

9+
// Regression payload for the inline page-data script-tag escape. If anything
10+
// in the SSR pipeline forgets to escape, the embedded <script> closes early
11+
// and JSON.parse fails on hydration.
12+
const SCRIPT_PROBE = '</script><script>alert(1)</script>\u2028\u2029&';
13+
914
export default defineMiddleware(async (c, next) => {
1015
c.set('shared', {
1116
appName: 'React Pages Demo',
17+
scriptProbe: SCRIPT_PROBE,
1218
});
1319
await next();
1420
});

‎playground/solid-pages/middleware/01.shared.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ import { defineMiddleware } from 'void';
22

33
declare module 'void' {
44
interface CloudContextVariables {
5-
shared: { appName: string };
5+
shared: { appName: string; scriptProbe: string };
66
}
77
}
88

9+
// Regression payload for the inline page-data script-tag escape. If anything
10+
// in the SSR pipeline forgets to escape, the embedded <script> closes early
11+
// and JSON.parse fails on hydration.
12+
const SCRIPT_PROBE = '</script><script>alert(1)</script>\u2028\u2029&';
13+
914
export default defineMiddleware(async (c, next) => {
1015
c.set('shared', {
1116
appName: 'Solid Pages Demo',
17+
scriptProbe: SCRIPT_PROBE,
1218
});
1319
await next();
1420
});

‎playground/svelte-pages/middleware/01.shared.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ import { defineMiddleware } from 'void';
22

33
declare module 'void' {
44
interface CloudContextVariables {
5-
shared: { appName: string };
5+
shared: { appName: string; scriptProbe: string };
66
}
77
}
88

9+
// Regression payload for the inline page-data script-tag escape. If anything
10+
// in the SSR pipeline forgets to escape, the embedded <script> closes early
11+
// and JSON.parse fails on hydration.
12+
const SCRIPT_PROBE = '</script><script>alert(1)</script>\u2028\u2029&';
13+
914
export default defineMiddleware(async (c, next) => {
1015
c.set('shared', {
1116
appName: 'Svelte Pages Demo',
17+
scriptProbe: SCRIPT_PROBE,
1218
});
1319
await next();
1420
});

‎playground/vue-pages/middleware/01.shared.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ import { defineMiddleware } from 'void';
22

33
declare module 'void' {
44
interface CloudContextVariables {
5-
shared: { appName: string };
5+
shared: { appName: string; scriptProbe: string };
66
}
77
}
88

9+
// Regression payload for the inline page-data script-tag escape. If anything
10+
// in the SSR pipeline forgets to escape, the embedded <script> closes early
11+
// and JSON.parse fails on hydration.
12+
const SCRIPT_PROBE = '</script><script>alert(1)</script>\u2028\u2029&';
13+
914
export default defineMiddleware(async (c, next) => {
1015
c.set('shared', {
1116
appName: 'Vue Pages Demo',
17+
scriptProbe: SCRIPT_PROBE,
1218
});
1319
await next();
1420
});

0 commit comments

Comments
 (0)