Files
AndreaSlopFotografie/web/src/test/htmlArbitraries.ts
T
SluijsensandClaude Opus 5.5 1574726c1d Adds the step that moves inline scripts to files
TanStack Start writes inline scripts into each page, which the strict CSP
blocks. This step moves them to hashed files and checks the output: every page
is present, no executable inline script remains and the e-mail address appears
nowhere. It is not part of the build yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 18:12:42 +01:00

313 lines
9.7 KiB
TypeScript

import fc from 'fast-check';
// Generators for HTML built from known pieces, each with a model of what it
// is. The script step (config/inlineScripts.ts) is tested against the model,
// never against its own scanner.
export type HtmlPieceKind =
| 'executable'
| 'external'
| 'empty'
| 'data'
| 'comment'
| 'rawText'
| 'attributeDecoy'
| 'markup';
export interface HtmlPiece {
kind: HtmlPieceKind;
html: string;
// Executable pieces only: the script body and the attribute text between
// the tag name and the closing '>'
body?: string;
attributes?: string;
}
export interface HtmlModel {
html: string;
pieces: HtmlPiece[];
// What sits between two pieces
separator: string;
}
// Bodies never contain a script end tag or a comment opener (the step
// refuses those on purpose). Several snippets contain '<' and '>' to show a
// body is not parsed as markup. A small pool makes equal bodies likely, so
// the "same body, same file" property gets exercised.
const snippet = fc.constantFrom(
'window.a=1;',
'var x = "<b>";',
'if (a<b && c>d) { f(); }',
'/* comment */ g();',
'document.currentScript.remove();/*$tsr-stream-boundary*/',
'$RB=[];$RV=function(a){return a};',
"const s = '</div>';",
'const é = "ünï";',
'requestAnimationFrame(function(){$RT=performance.now()});',
);
const padding = fc.constantFrom('', ' ', '\n', '\r\n ', '\t');
// Non-empty after trimming, with the padding a minifier would leave out
export const scriptBody: fc.Arbitrary<string> = fc
.tuple(padding, fc.array(snippet, { minLength: 1, maxLength: 3 }), padding)
.map(([before, core, after]) => before + core.join('\n') + after);
// Attribute text of an executable script, verbatim as it sits in the tag.
// Quoted values may contain '>' and the tag may end in a slash.
const executableAttributes = fc.constantFrom(
'',
' type="module"',
' type="text/javascript"',
' type=""',
' type = "text/ecmascript"',
" TYPE='Module'",
' type=application/javascript',
' data-tsr-stream-part=""',
' id="_R_"',
' class="a" nonce="x" data-n=1',
' data-x="a>b"',
" data-q='1 > 0'",
' language="javascript"',
' /',
);
const tagCase = fc.constantFrom(
{ open: 'script', close: 'script' },
{ open: 'SCRIPT', close: 'SCRIPT ' },
{ open: 'Script', close: 'sCrIpT\n' },
);
const canonicalCase = fc.constant({ open: 'script', close: 'script' });
function executableOf(cases: typeof tagCase): fc.Arbitrary<HtmlPiece> {
return fc
.tuple(cases, executableAttributes, scriptBody)
.map(([names, attributes, body]) => ({
kind: 'executable' as const,
html: `<${names.open}${attributes}>${body}</${names.close}>`,
body,
attributes,
}));
}
// Mixed tag-name case and end-tag spacing; the step turns these into
// lowercase references
export const executablePiece = executableOf(tagCase);
// Only `<script ...>body</script>`, so a moved script can be put back exactly
const canonicalExecutablePiece = executableOf(canonicalCase);
const externalPiece: fc.Arbitrary<HtmlPiece> = fc
.constantFrom(
'<script src="/a.js"></script>',
'<script type="module" async src="/assets/index-abc.js"></script>',
'<script src></script>',
'<script src=""></script>',
'<script defer src="/b.js">ignored();</script>',
'<script SRC=/c.js></script>',
)
.map((html) => ({ kind: 'external' as const, html }));
const emptyPiece: fc.Arbitrary<HtmlPiece> = fc
.constantFrom(
'<script></script>',
'<script> \n </script>',
'<script type="module"> </script>',
'<script type="text/javascript">\t</script>',
)
.map((html) => ({ kind: 'empty' as const, html }));
const dataPiece: fc.Arbitrary<HtmlPiece> = fc
.tuple(
fc.constantFrom(
'application/ld+json',
'application/json',
'importmap',
'speculationrules',
'text/plain',
'text/x-template',
' APPLICATION/LD+JSON ',
),
fc.constantFrom(
'{"a":1}',
'{"imports":{"x":"/x.js"}}',
'{"name":"a \\u003c/script\\u003e"}',
'if (a<b) { f(); }',
),
)
.map(([type, body]) => ({
kind: 'data' as const,
html: `<script type="${type}">${body}</script>`,
}));
// Text that looks like a script but sits where the scanner must skip it
const commentPiece: fc.Arbitrary<HtmlPiece> = fc
.constantFrom(
'<!-- <script>x()</script> -->',
'<!---->',
'<!-->',
'<!-- a -- b <script>y()</script> -->',
'<!-- <script type="module">z()</script>\n-->',
)
.map((html) => ({ kind: 'comment' as const, html }));
const rawTextPiece: fc.Arbitrary<HtmlPiece> = fc
.constantFrom(
'<style>.a::before{content:"<script>x()</script>"}</style>',
'<STYLE>a{}</script><script>b()</script></STYLE>',
'<textarea><script>x()</script></textarea>',
'<title>a <script>x()</script></title>',
'<noscript><script>x()</script></noscript>',
'<xmp><script>x()</script></xmp>',
'<iframe><script>x()</script></iframe>',
)
.map((html) => ({ kind: 'rawText' as const, html }));
const attributeDecoyPiece: fc.Arbitrary<HtmlPiece> = fc
.constantFrom(
'<div title="<script>x()</script>">t</div>',
"<a data-x='<script>y()</script>'>t</a>",
'<meta content="a>b<script>z()</script>">',
'<p data-code=<script>w()</script>>t</p>',
)
.map((html) => ({ kind: 'attributeDecoy' as const, html }));
// Complete markup with no script in it. Text pieces never end in '<', so a
// piece cannot glue onto the next one.
const markupPiece: fc.Arbitrary<HtmlPiece> = fc
.constantFrom(
'<!doctype html>',
'<?xml version="1.0"?>',
'<div class="a">text</div>',
'<p>1 < 2 and 3 > 2</p>',
'<br/>',
'<link rel="stylesheet" href="/assets/a.css">',
'<meta charSet="utf-8">',
'plain text',
'</p>',
)
.map((html) => ({ kind: 'markup' as const, html }));
// Everything the step must leave alone, whatever surrounds it
export const decoyPiece: fc.Arbitrary<HtmlPiece> = fc.oneof(
externalPiece,
emptyPiece,
dataPiece,
commentPiece,
rawTextPiece,
attributeDecoyPiece,
markupPiece,
);
export const htmlPiece: fc.Arbitrary<HtmlPiece> = fc.oneof(
{ arbitrary: executablePiece, weight: 3 },
{ arbitrary: decoyPiece, weight: 4 },
);
const canonicalHtmlPiece: fc.Arbitrary<HtmlPiece> = fc.oneof(
{ arbitrary: canonicalExecutablePiece, weight: 3 },
{ arbitrary: decoyPiece, weight: 4 },
);
const separator = fc.constantFrom('', '\n', ' ');
function modelOf(pieces: fc.Arbitrary<HtmlPiece>): fc.Arbitrary<HtmlModel> {
return fc
.tuple(fc.array(pieces, { maxLength: 12 }), separator)
.map(([list, join]) => ({
html: list.map((piece) => piece.html).join(join),
pieces: list,
separator: join,
}));
}
// A document and what each part of it is
export const htmlWithModel: fc.Arbitrary<HtmlModel> = modelOf(htmlPiece);
// The same, with executable scripts written as `<script ...>body</script>`
// only, so that putting the moved bodies back restores the input exactly
export const canonicalHtmlWithModel: fc.Arbitrary<HtmlModel> =
modelOf(canonicalHtmlPiece);
// Independent complete fragments, for split-and-join properties: each one
// parses the same alone as inside a larger document
export const htmlFragments: fc.Arbitrary<HtmlPiece[]> = fc.array(htmlPiece, {
maxLength: 10,
});
// A build output for the runner: which expected pages exist and what is wrong
// with them, plus files that do not belong. The test turns this into a file
// map and computes the expected violations from the same description.
export type BadScript = 'none' | 'unsupported' | 'unterminated';
export interface PageScenario {
present: boolean;
content: HtmlModel;
// Already processed: references a moved script without executable ones
reference: boolean;
bad: BadScript;
// The assembled address sits in the page text
leakAddress: boolean;
}
export interface ExtraFile {
name: string;
address: boolean;
rootPath: boolean;
}
export interface OutputScenario {
// index.html, over-mij.html, soorten-fotoshoots/newborn.html, 404.html
pages: PageScenario[];
unexpected: string[];
extras: ExtraFile[];
sourceMap: boolean;
sitemap: 'ok' | 'missing' | 'differs';
}
const pageScenario: fc.Arbitrary<PageScenario> = fc.record({
present: fc.boolean(),
content: htmlWithModel,
reference: fc.boolean(),
bad: fc.constantFrom<BadScript>(
'none',
'none',
'none',
'unsupported',
'unterminated',
),
leakAddress: fc.boolean(),
});
const extraFile: fc.Arbitrary<ExtraFile> = fc.record({
name: fc.constantFrom(
'assets/app.js',
'assets/app.css',
'robots.txt',
'data.json',
'manifest.webmanifest',
'feed.xml',
),
address: fc.boolean(),
rootPath: fc.boolean(),
});
export const outputScenario: fc.Arbitrary<OutputScenario> = fc.record({
pages: fc.tuple(pageScenario, pageScenario, pageScenario, pageScenario),
unexpected: fc.uniqueArray(
fc.constantFrom(
'extra.html',
'soorten-fotoshoots/index.html',
'a/b.html',
),
{ maxLength: 2 },
),
extras: fc.uniqueArray(extraFile, {
selector: (file) => file.name,
maxLength: 3,
}),
sourceMap: fc.boolean(),
sitemap: fc.constantFrom('ok', 'ok', 'missing', 'differs'),
});