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>
730 lines
27 KiB
TypeScript
730 lines
27 KiB
TypeScript
import { spawnSync } from 'node:child_process';
|
||
import {
|
||
existsSync,
|
||
mkdirSync,
|
||
mkdtempSync,
|
||
readFileSync,
|
||
readdirSync,
|
||
rmSync,
|
||
writeFileSync,
|
||
} from 'node:fs';
|
||
import { tmpdir } from 'node:os';
|
||
import { dirname, join } from 'node:path';
|
||
import { fileURLToPath } from 'node:url';
|
||
import fc from 'fast-check';
|
||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||
import type { EmailAddress } from '../src/content/types';
|
||
import { siteUrl } from '../src/seo';
|
||
import sitePages, { pageList } from '../src/sitePages';
|
||
import { sitemapXml } from '../src/sitemap';
|
||
import { validEmail } from '../src/test/arbitraries';
|
||
import {
|
||
htmlWithModel,
|
||
outputScenario,
|
||
type OutputScenario,
|
||
} from '../src/test/htmlArbitraries';
|
||
import {
|
||
containsAddress,
|
||
externalizeOutput,
|
||
formatViolation,
|
||
planExternalization,
|
||
type PlanContext,
|
||
type ScriptStep,
|
||
} from './externalizeInlineScripts';
|
||
import { pageFile } from './pageFiles';
|
||
|
||
const SITEMAP = '<urlset>sitemap</urlset>\n';
|
||
const ROOT_PATHS = ['/work/repo', 'C:\\work\\repo'];
|
||
const ADDRESS = 'info@example.nl';
|
||
|
||
const context = (override: Partial<PlanContext> = {}): PlanContext => ({
|
||
expected: {
|
||
pages: new Map([
|
||
['/', 'index.html'],
|
||
['/over-mij', 'over-mij.html'],
|
||
['/soorten-fotoshoots/newborn', 'soorten-fotoshoots/newborn.html'],
|
||
]),
|
||
notFound: '404.html',
|
||
},
|
||
address: ADDRESS,
|
||
sitemap: SITEMAP,
|
||
rootPaths: ROOT_PATHS,
|
||
...override,
|
||
});
|
||
|
||
const page = (script = 'boot()') =>
|
||
`<html><body><p>tekst</p><script>${script}</script></body></html>`;
|
||
|
||
const validFiles = (): Map<string, string> =>
|
||
new Map([
|
||
['index.html', page('boot("index")')],
|
||
['over-mij.html', page('boot("over-mij")')],
|
||
['soorten-fotoshoots/newborn.html', page('boot("newborn")')],
|
||
['404.html', page('boot("404")')],
|
||
['sitemap.xml', SITEMAP],
|
||
['assets/index-abc.js', 'console.log(1);'],
|
||
['assets/index-abc.css', 'a{color:red}'],
|
||
]);
|
||
|
||
const found = (plan: { violations: { file: string; code: string }[] }) =>
|
||
plan.violations.map((v) => `${v.file}: ${v.code}`).sort();
|
||
|
||
describe('planExternalization: pad zonder overtredingen', () => {
|
||
it('verplaatst de scripts van elke pagina, ook van 404.html', () => {
|
||
const plan = planExternalization(validFiles(), context());
|
||
expect(plan.violations).toEqual([]);
|
||
const scripts = [...plan.writes.keys()].filter((f) =>
|
||
f.startsWith('assets/inline-'),
|
||
);
|
||
expect(scripts).toHaveLength(4);
|
||
for (const file of [
|
||
'index.html',
|
||
'over-mij.html',
|
||
'soorten-fotoshoots/newborn.html',
|
||
'404.html',
|
||
]) {
|
||
expect(plan.writes.get(file)).toMatch(
|
||
/src="\/assets\/inline-[0-9a-f]{12}\.js"/,
|
||
);
|
||
}
|
||
});
|
||
|
||
it('is een no-op bij een tweede keer (al verwerkte pagina telt mee)', () => {
|
||
const files = validFiles();
|
||
const first = planExternalization(files, context());
|
||
for (const [file, content] of first.writes) files.set(file, content);
|
||
const second = planExternalization(files, context());
|
||
expect(second.writes.size).toBe(0);
|
||
expect(second.violations).toEqual([]);
|
||
});
|
||
|
||
it('schrijft een scriptbestand dat al met dezelfde inhoud bestaat niet opnieuw', () => {
|
||
const files = validFiles();
|
||
const first = planExternalization(files, context());
|
||
const name = [...first.writes.keys()].find((f) =>
|
||
f.startsWith('assets/inline-'),
|
||
)!;
|
||
files.set(name, first.writes.get(name)!);
|
||
const plan = planExternalization(files, context());
|
||
expect(plan.writes.has(name)).toBe(false);
|
||
expect(plan.violations).toEqual([]);
|
||
});
|
||
|
||
it('schrijft een gedeeld script één keer', () => {
|
||
const files = validFiles();
|
||
for (const file of ['index.html', 'over-mij.html']) {
|
||
files.set(file, page('gedeeld()'));
|
||
}
|
||
const plan = planExternalization(files, context());
|
||
const shared = [...plan.writes.values()].filter(
|
||
(c) => c === 'gedeeld()',
|
||
);
|
||
expect(shared).toHaveLength(1);
|
||
});
|
||
});
|
||
|
||
describe('planExternalization: overtredingen', () => {
|
||
it('NO_HTML: geen enkel html-bestand, zonder de pagina’s afzonderlijk te melden', () => {
|
||
const files = new Map([['sitemap.xml', SITEMAP]]);
|
||
expect(found(planExternalization(files, context()))).toEqual([
|
||
'.: NO_HTML',
|
||
]);
|
||
});
|
||
|
||
it('PAGE_MISSING: meldt alle ontbrekende pagina’s', () => {
|
||
const files = validFiles();
|
||
files.delete('over-mij.html');
|
||
files.delete('soorten-fotoshoots/newborn.html');
|
||
const plan = planExternalization(files, context());
|
||
expect(found(plan)).toEqual([
|
||
'over-mij.html: PAGE_MISSING',
|
||
'soorten-fotoshoots/newborn.html: PAGE_MISSING',
|
||
]);
|
||
expect(plan.violations[0].detail).toContain('/over-mij');
|
||
});
|
||
|
||
it('NOT_FOUND_PAGE_MISSING: 404.html ontbreekt', () => {
|
||
const files = validFiles();
|
||
files.delete('404.html');
|
||
expect(found(planExternalization(files, context()))).toEqual([
|
||
'404.html: NOT_FOUND_PAGE_MISSING',
|
||
]);
|
||
});
|
||
|
||
it('UNEXPECTED_HTML: een html-bestand buiten het register', () => {
|
||
const files = validFiles();
|
||
files.set('soorten-fotoshoots/index.html', page());
|
||
files.set('extra.html', page());
|
||
expect(found(planExternalization(files, context()))).toEqual([
|
||
'extra.html: UNEXPECTED_HTML',
|
||
'soorten-fotoshoots/index.html: UNEXPECTED_HTML',
|
||
]);
|
||
});
|
||
|
||
it('NO_SCRIPTS_MOVED: een pagina zonder uitvoerbaar of verplaatst script', () => {
|
||
const files = validFiles();
|
||
files.set(
|
||
'index.html',
|
||
'<p>geen script</p><script type="application/ld+json">{}</script><script src="/assets/index-abc.js"></script>',
|
||
);
|
||
expect(found(planExternalization(files, context()))).toEqual([
|
||
'index.html: NO_SCRIPTS_MOVED',
|
||
]);
|
||
});
|
||
|
||
it('EXECUTABLE_REMAINS: de verdediging als het herschrijven iets overlaat', () => {
|
||
const scripts: ScriptStep = {
|
||
externalize: (html) => ({ html, moved: [] }),
|
||
find: () => ['geheim()'],
|
||
};
|
||
const plan = planExternalization(validFiles(), context(), scripts);
|
||
expect(found(plan)).toContain('index.html: EXECUTABLE_REMAINS');
|
||
expect(plan.violations.map(formatViolation).join('\n')).not.toContain(
|
||
'geheim',
|
||
);
|
||
});
|
||
|
||
it('UNSUPPORTED_SCRIPT en UNTERMINATED_SCRIPT: meldt de code en gooit niet', () => {
|
||
const files = validFiles();
|
||
files.set('index.html', '<script async>geheim()</script>');
|
||
files.set('over-mij.html', '<script>geheim()');
|
||
const plan = planExternalization(files, context());
|
||
expect(found(plan)).toEqual([
|
||
'index.html: UNSUPPORTED_SCRIPT',
|
||
'over-mij.html: UNTERMINATED_SCRIPT',
|
||
]);
|
||
const text = plan.violations.map(formatViolation).join('\n');
|
||
expect(text).toContain('index.html: UNSUPPORTED_SCRIPT: ');
|
||
expect(text).toContain('regel 1');
|
||
expect(text).not.toContain('geheim');
|
||
});
|
||
|
||
it('geeft een andere fout dan een ScriptError door', () => {
|
||
const scripts: ScriptStep = {
|
||
externalize: () => {
|
||
throw new TypeError('kapot');
|
||
},
|
||
find: () => [],
|
||
};
|
||
expect(() =>
|
||
planExternalization(validFiles(), context(), scripts),
|
||
).toThrow('kapot');
|
||
});
|
||
|
||
it('COLLISION: een scriptbestand met dezelfde naam en andere inhoud', () => {
|
||
const files = validFiles();
|
||
const first = planExternalization(files, context());
|
||
const name = [...first.writes.keys()].find((f) =>
|
||
f.startsWith('assets/inline-'),
|
||
)!;
|
||
files.set(name, 'iets anders');
|
||
const plan = planExternalization(files, context());
|
||
expect(found(plan)).toEqual([`${name}: COLLISION`]);
|
||
});
|
||
|
||
it('COLLISION: twee verschillende scripts met dezelfde bestandsnaam in één run', () => {
|
||
const scripts: ScriptStep = {
|
||
externalize: (html) => ({
|
||
html: `<script src="/assets/inline-000000000000.js"></script>`,
|
||
moved: [{ fileName: 'inline-000000000000.js', body: html }],
|
||
}),
|
||
find: () => [],
|
||
};
|
||
const plan = planExternalization(validFiles(), context(), scripts);
|
||
expect(found(plan)).toContain(
|
||
'assets/inline-000000000000.js: COLLISION',
|
||
);
|
||
});
|
||
|
||
it('SITEMAP_MISSING en SITEMAP_DIFFERS', () => {
|
||
const missing = validFiles();
|
||
missing.delete('sitemap.xml');
|
||
expect(found(planExternalization(missing, context()))).toEqual([
|
||
'sitemap.xml: SITEMAP_MISSING',
|
||
]);
|
||
const differs = validFiles();
|
||
differs.set('sitemap.xml', SITEMAP + ' ');
|
||
expect(found(planExternalization(differs, context()))).toEqual([
|
||
'sitemap.xml: SITEMAP_DIFFERS',
|
||
]);
|
||
});
|
||
|
||
it('SOURCE_MAP_PRESENT: een .map-bestand', () => {
|
||
const files = validFiles();
|
||
files.set('assets/index-abc.js.map', '{}');
|
||
expect(found(planExternalization(files, context()))).toEqual([
|
||
'assets/index-abc.js.map: SOURCE_MAP_PRESENT',
|
||
]);
|
||
});
|
||
|
||
it.each(ROOT_PATHS)('BUILD_PATH_LEAK: %s in een tekstbestand', (root) => {
|
||
const files = validFiles();
|
||
files.set('assets/index-abc.js', `var p = "${root}/src/x.ts";`);
|
||
expect(found(planExternalization(files, context()))).toEqual([
|
||
'assets/index-abc.js: BUILD_PATH_LEAK',
|
||
]);
|
||
});
|
||
|
||
it('EMAIL_IN_OUTPUT: in html, js, css en in de inhoud van een verplaatst script', () => {
|
||
const files = validFiles();
|
||
files.set('assets/index-abc.js', `var a = "${ADDRESS}";`);
|
||
files.set('assets/index-abc.css', `a::after{content:"${ADDRESS}"}`);
|
||
files.set('over-mij.html', `<p>${ADDRESS}</p>${page('boot("o")')}`);
|
||
files.set('index.html', page(`mail("${ADDRESS}")`));
|
||
const plan = planExternalization(files, context());
|
||
const inline = [...plan.writes.entries()].find(([, c]) =>
|
||
c.includes(ADDRESS),
|
||
)![0];
|
||
expect(found(plan)).toEqual(
|
||
[
|
||
'assets/index-abc.js: EMAIL_IN_OUTPUT',
|
||
'assets/index-abc.css: EMAIL_IN_OUTPUT',
|
||
'over-mij.html: EMAIL_IN_OUTPUT',
|
||
`${inline}: EMAIL_IN_OUTPUT`,
|
||
].sort(),
|
||
);
|
||
});
|
||
|
||
it('slaat de adrescontrole over zonder adres (ongeldige delen)', () => {
|
||
const files = validFiles();
|
||
files.set('assets/index-abc.js', `var a = "${ADDRESS}";`);
|
||
const plan = planExternalization(files, context({ address: null }));
|
||
expect(plan.violations).toEqual([]);
|
||
});
|
||
|
||
it('meldt alle overtredingen tegelijk, in een vast formaat zonder adres', () => {
|
||
const files = validFiles();
|
||
files.delete('404.html');
|
||
files.set('assets/index-abc.js', `"${ADDRESS}"`);
|
||
files.set('x.html', page());
|
||
const lines = planExternalization(files, context()).violations.map(
|
||
formatViolation,
|
||
);
|
||
expect(lines).toEqual(
|
||
expect.arrayContaining([
|
||
'404.html: NOT_FOUND_PAGE_MISSING: de 404-pagina ontbreekt',
|
||
'x.html: UNEXPECTED_HTML: html-bestand dat niet bij het register hoort',
|
||
'assets/index-abc.js: EMAIL_IN_OUTPUT: e-mailadres in de uitvoer',
|
||
]),
|
||
);
|
||
expect(lines.join('\n')).not.toContain(ADDRESS);
|
||
});
|
||
});
|
||
|
||
describe('containsAddress', () => {
|
||
const address = 'info@andreaslop.nl';
|
||
|
||
it.each([
|
||
['platte tekst', 'info@andreaslop.nl'],
|
||
['hoofdletters', 'INFO@AndreaSlop.NL'],
|
||
['URL-codering', 'mailto:info%40andreaslop.nl'],
|
||
['JavaScript-escape', 'info\\u0040andreaslop.nl'],
|
||
['JavaScript-escape met accolades', 'info\\u{40}andreaslop.nl'],
|
||
['hexadecimale escape', 'info\\x40andreaslop.nl'],
|
||
['decimale entiteit', 'info@andreaslop.nl'],
|
||
['hexadecimale entiteit', 'info@andreaslop.nl'],
|
||
['hexadecimale entiteit in hoofdletters', 'info@andreaslop.nl'],
|
||
['benoemde entiteit', 'info@andreaslop.nl'],
|
||
['midden in tekst', 'Mail <a href="x">info@andreaslop.nl</a>!'],
|
||
])('vindt het adres als %s', (_, text) => {
|
||
expect(containsAddress(text, address)).toBe(true);
|
||
});
|
||
|
||
it.each([
|
||
['alleen de gebruiker', 'info'],
|
||
['alleen het domein', 'andreaslop.nl'],
|
||
['met een spatie ertussen', 'info @ andreaslop.nl'],
|
||
['een ander domein', 'info@andreaslop.com'],
|
||
['een andere gebruiker', 'post@andreaslop.nl'],
|
||
['een onbekende codering', 'infoAandreaslop.nl'],
|
||
['lege tekst', ''],
|
||
])('vindt het adres niet bij %s', (_, text) => {
|
||
expect(containsAddress(text, address)).toBe(false);
|
||
});
|
||
|
||
it('vindt niets bij een adres zonder @', () => {
|
||
expect(containsAddress('infoandreaslop.nl', 'infoandreaslop.nl')).toBe(
|
||
false,
|
||
);
|
||
});
|
||
});
|
||
|
||
describe('containsAddress: eigenschappen', () => {
|
||
// Onafhankelijke lijst van BR-MAIL-10
|
||
const encodings = [
|
||
'@',
|
||
'%40',
|
||
'\\u0040',
|
||
'\\u{40}',
|
||
'\\x40',
|
||
'@',
|
||
'@',
|
||
'@',
|
||
];
|
||
// Geen @, %, & of \ : een schrijfwijze kan er dus nooit in ontstaan
|
||
const safeText = fc.stringMatching(/^[a-z0-9 .<>/="'-]{0,30}$/);
|
||
const randomCase = (text: string, flips: boolean[]) =>
|
||
[...text]
|
||
.map((char, i) =>
|
||
flips[i % flips.length] ? char.toUpperCase() : char,
|
||
)
|
||
.join('');
|
||
|
||
it('vindt het adres in elke schrijfwijze op elke plaats, in elke hoofdletterstand (oracle)', () => {
|
||
fc.assert(
|
||
fc.property(
|
||
validEmail,
|
||
fc.constantFrom(...encodings),
|
||
safeText,
|
||
safeText,
|
||
fc.array(fc.boolean(), { minLength: 1, maxLength: 8 }),
|
||
(email, encoding, before, after, flips) => {
|
||
const written = randomCase(
|
||
`${email.user}${encoding}${email.domain}`,
|
||
flips,
|
||
);
|
||
expect(
|
||
containsAddress(
|
||
`${before}${written}${after}`,
|
||
`${email.user}@${email.domain}`,
|
||
),
|
||
).toBe(true);
|
||
},
|
||
),
|
||
);
|
||
});
|
||
|
||
it('markeert tekst zonder het adres nooit (oracle)', () => {
|
||
fc.assert(
|
||
fc.property(
|
||
validEmail,
|
||
safeText,
|
||
fc.constantFrom(...encodings),
|
||
(email, text, encoding) => {
|
||
const address = `${email.user}@${email.domain}`;
|
||
expect(containsAddress(text, address)).toBe(false);
|
||
// Een deel van het adres is niet genoeg
|
||
expect(
|
||
containsAddress(
|
||
`${text}${email.user}${encoding}`,
|
||
address,
|
||
),
|
||
).toBe(false);
|
||
expect(
|
||
containsAddress(
|
||
`${encoding}${email.domain}${text}`,
|
||
address,
|
||
),
|
||
).toBe(false);
|
||
},
|
||
),
|
||
);
|
||
});
|
||
});
|
||
|
||
// De drie pagina's van `context()` plus 404.html, in de volgorde van het scenario
|
||
const SCENARIO_FILES = [
|
||
'index.html',
|
||
'over-mij.html',
|
||
'soorten-fotoshoots/newborn.html',
|
||
'404.html',
|
||
];
|
||
const EXTRA_REFERENCE =
|
||
'<script src="/assets/inline-0123456789ab.js"></script>';
|
||
|
||
function filesOf(scenario: OutputScenario, address: string) {
|
||
const files = new Map<string, string>();
|
||
scenario.pages.forEach((p, i) => {
|
||
if (!p.present) return;
|
||
files.set(
|
||
SCENARIO_FILES[i],
|
||
p.content.html +
|
||
(p.reference ? EXTRA_REFERENCE : '') +
|
||
(p.leakAddress ? `<p>${address}</p>` : '') +
|
||
(p.bad === 'unsupported'
|
||
? '<script async>bad()</script>'
|
||
: '') +
|
||
(p.bad === 'unterminated' ? '<script>bad()' : ''),
|
||
);
|
||
});
|
||
for (const name of scenario.unexpected) files.set(name, '<p>x</p>');
|
||
for (const extra of scenario.extras) {
|
||
files.set(
|
||
extra.name,
|
||
`run();${extra.address ? address : ''}${extra.rootPath ? ' /work/repo/x' : ''}`,
|
||
);
|
||
}
|
||
if (scenario.sourceMap) files.set('assets/app.js.map', '{}');
|
||
if (scenario.sitemap === 'ok') files.set('sitemap.xml', SITEMAP);
|
||
if (scenario.sitemap === 'differs') files.set('sitemap.xml', 'anders');
|
||
return files;
|
||
}
|
||
|
||
// Het eenvoudige model: wat er mis is, rechtstreeks uit het scenario
|
||
function expectedViolations(scenario: OutputScenario): string[] {
|
||
const expected: string[] = [];
|
||
const anyHtml =
|
||
scenario.pages.some((p) => p.present) || scenario.unexpected.length > 0;
|
||
if (!anyHtml) expected.push('.: NO_HTML');
|
||
|
||
scenario.pages.forEach((p, i) => {
|
||
const file = SCENARIO_FILES[i];
|
||
if (!p.present) {
|
||
if (anyHtml) {
|
||
expected.push(
|
||
`${file}: ${i === 3 ? 'NOT_FOUND_PAGE_MISSING' : 'PAGE_MISSING'}`,
|
||
);
|
||
}
|
||
return;
|
||
}
|
||
if (p.bad === 'unsupported')
|
||
expected.push(`${file}: UNSUPPORTED_SCRIPT`);
|
||
else if (p.bad === 'unterminated') {
|
||
expected.push(`${file}: UNTERMINATED_SCRIPT`);
|
||
} else if (
|
||
!p.reference &&
|
||
!p.content.pieces.some((piece) => piece.kind === 'executable')
|
||
) {
|
||
expected.push(`${file}: NO_SCRIPTS_MOVED`);
|
||
}
|
||
if (p.leakAddress) expected.push(`${file}: EMAIL_IN_OUTPUT`);
|
||
});
|
||
for (const name of scenario.unexpected) {
|
||
expected.push(`${name}: UNEXPECTED_HTML`);
|
||
}
|
||
for (const extra of scenario.extras) {
|
||
if (extra.address) expected.push(`${extra.name}: EMAIL_IN_OUTPUT`);
|
||
if (extra.rootPath) expected.push(`${extra.name}: BUILD_PATH_LEAK`);
|
||
}
|
||
if (scenario.sourceMap) {
|
||
expected.push('assets/app.js.map: SOURCE_MAP_PRESENT');
|
||
}
|
||
if (scenario.sitemap === 'missing') {
|
||
expected.push('sitemap.xml: SITEMAP_MISSING');
|
||
}
|
||
if (scenario.sitemap === 'differs') {
|
||
expected.push('sitemap.xml: SITEMAP_DIFFERS');
|
||
}
|
||
return expected.sort();
|
||
}
|
||
|
||
const assemble = ({ user, domain }: EmailAddress) => `${user}@${domain}`;
|
||
|
||
describe('planExternalization: eigenschappen', () => {
|
||
it('meldt precies de overtredingen van een eenvoudig model (oracle)', () => {
|
||
fc.assert(
|
||
fc.property(outputScenario, validEmail, (scenario, email) => {
|
||
const address = assemble(email);
|
||
const plan = planExternalization(
|
||
filesOf(scenario, address),
|
||
context({ address }),
|
||
);
|
||
expect(found(plan)).toEqual(expectedViolations(scenario));
|
||
// Geen bericht noemt het adres (BR-SCR-21)
|
||
expect(
|
||
plan.violations.map(formatViolation).join('\n'),
|
||
).not.toContain(address);
|
||
}),
|
||
);
|
||
});
|
||
|
||
it('geeft na het toepassen van de writes geen writes en geen overtredingen meer (idempotent)', () => {
|
||
const validOutput = fc.tuple(
|
||
fc.array(htmlWithModel, { minLength: 4, maxLength: 4 }),
|
||
fc.array(fc.constantFrom('a', 'b', 'c'), { maxLength: 3 }),
|
||
);
|
||
fc.assert(
|
||
fc.property(validOutput, ([models, suffixes]) => {
|
||
// Elke pagina krijgt een eigen, uitvoerbaar script, zodat het
|
||
// geldig is
|
||
const files = validFiles();
|
||
SCENARIO_FILES.forEach((file, i) => {
|
||
files.set(
|
||
file,
|
||
`${models[i].html}<script>boot("${i}${suffixes.join('')}")</script>`,
|
||
);
|
||
});
|
||
const first = planExternalization(files, context());
|
||
expect(first.violations).toEqual([]);
|
||
for (const [file, content] of first.writes) {
|
||
files.set(file, content);
|
||
}
|
||
const second = planExternalization(files, context());
|
||
expect(second.writes.size).toBe(0);
|
||
expect(second.violations).toEqual([]);
|
||
}),
|
||
);
|
||
});
|
||
});
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// externalizeOutput met een echte map
|
||
|
||
const temporary: string[] = [];
|
||
afterEach(() => {
|
||
for (const dir of temporary.splice(0)) {
|
||
rmSync(dir, { recursive: true, force: true });
|
||
}
|
||
});
|
||
|
||
// <tmp>/web/dist/client: de repositorywortel voor de lekcontrole is <tmp>/web
|
||
function outputDir() {
|
||
const root = mkdtempSync(join(tmpdir(), 'externalize-'));
|
||
temporary.push(root);
|
||
const clientDir = join(root, 'web', 'dist', 'client');
|
||
const put = (file: string, content: string) => {
|
||
mkdirSync(dirname(join(clientDir, file)), { recursive: true });
|
||
writeFileSync(join(clientDir, file), content, 'utf8');
|
||
};
|
||
for (const path of Object.keys(sitePages)) {
|
||
put(pageFile(path), page(`boot("${path}")`));
|
||
}
|
||
put('404.html', page('boot("404")'));
|
||
put('sitemap.xml', sitemapXml(pageList(sitePages), siteUrl));
|
||
put('assets/index-abc.js', 'console.log(1);');
|
||
return { root: join(root, 'web'), clientDir, put };
|
||
}
|
||
|
||
const listing = (dir: string) =>
|
||
readdirSync(dir, { recursive: true, encoding: 'utf8' }).sort();
|
||
|
||
const silent = () => ({ log: vi.fn(), error: vi.fn() });
|
||
|
||
describe('externalizeOutput', () => {
|
||
it('schrijft eerst de scriptbestanden, dan de html, en meldt één regel', () => {
|
||
const { clientDir } = outputDir();
|
||
const out = silent();
|
||
const result = externalizeOutput(clientDir, out);
|
||
|
||
expect(result.exitCode).toBe(0);
|
||
expect(result.violations).toEqual([]);
|
||
const firstHtml = result.written.findIndex((f) => f.endsWith('.html'));
|
||
expect(firstHtml).toBeGreaterThan(0);
|
||
expect(
|
||
result.written
|
||
.slice(0, firstHtml)
|
||
.every((f) => !f.endsWith('.html')),
|
||
).toBe(true);
|
||
expect(
|
||
result.written.slice(firstHtml).every((f) => f.endsWith('.html')),
|
||
).toBe(true);
|
||
|
||
const index = readFileSync(join(clientDir, 'index.html'), 'utf8');
|
||
const reference = /src="\/(assets\/inline-[0-9a-f]{12}\.js)"/.exec(
|
||
index,
|
||
)!;
|
||
expect(readFileSync(join(clientDir, reference[1]), 'utf8')).toBe(
|
||
'boot("/")',
|
||
);
|
||
expect(index).not.toContain('boot(');
|
||
expect(out.error).not.toHaveBeenCalled();
|
||
expect(out.log).toHaveBeenCalledTimes(1);
|
||
expect(out.log.mock.calls[0][0]).toContain(
|
||
`${Object.keys(sitePages).length + 1} pagina's gecontroleerd`,
|
||
);
|
||
expect(out.log.mock.calls[0][0]).toContain(
|
||
`${Object.keys(sitePages).length + 1} scripts verplaatst`,
|
||
);
|
||
});
|
||
|
||
it('doet bij een tweede keer niets', () => {
|
||
const { clientDir } = outputDir();
|
||
externalizeOutput(clientDir, silent());
|
||
const before = listing(clientDir);
|
||
const out = silent();
|
||
const result = externalizeOutput(clientDir, out);
|
||
expect(result).toEqual({ exitCode: 0, violations: [], written: [] });
|
||
expect(listing(clientDir)).toEqual(before);
|
||
expect(out.log.mock.calls[0][0]).toContain('0 scripts verplaatst');
|
||
});
|
||
|
||
it('schrijft niets en meldt alles bij een overtreding', () => {
|
||
const { clientDir, put } = outputDir();
|
||
rmSync(join(clientDir, 'over-mij.html'));
|
||
put('assets/index-abc.js.map', '{}');
|
||
const before = listing(clientDir);
|
||
const out = silent();
|
||
const result = externalizeOutput(clientDir, out);
|
||
|
||
expect(result.exitCode).toBe(1);
|
||
expect(result.written).toEqual([]);
|
||
expect(listing(clientDir)).toEqual(before);
|
||
expect(out.log).not.toHaveBeenCalled();
|
||
expect(out.error.mock.calls.map(([line]) => line)).toEqual(
|
||
result.violations.map(formatViolation),
|
||
);
|
||
expect(result.violations.map((v) => v.code).sort()).toEqual([
|
||
'PAGE_MISSING',
|
||
'SOURCE_MAP_PRESENT',
|
||
]);
|
||
});
|
||
|
||
it('vindt het e-mailadres van de site in een uitvoerbestand', () => {
|
||
const { clientDir, put } = outputDir();
|
||
put('assets/index-abc.js', 'var a = "info@andreaslop.nl";');
|
||
const result = externalizeOutput(clientDir, silent());
|
||
expect(result.violations.map(formatViolation)).toEqual([
|
||
'assets/index-abc.js: EMAIL_IN_OUTPUT: e-mailadres in de uitvoer',
|
||
]);
|
||
});
|
||
|
||
it.each([
|
||
[
|
||
'met voorwaartse slashes',
|
||
(root: string) => root.replaceAll('\\', '/'),
|
||
],
|
||
['met backslashes', (root: string) => root.replaceAll('/', '\\')],
|
||
[
|
||
'met verdubbelde backslashes',
|
||
(root: string) =>
|
||
root.replaceAll('/', '\\').replaceAll('\\', '\\\\'),
|
||
],
|
||
])('vindt het pad van de repository %s', (_, style) => {
|
||
const { clientDir, put, root } = outputDir();
|
||
put('assets/index-abc.js', `var p = "${style(root)}";`);
|
||
const result = externalizeOutput(clientDir, silent());
|
||
expect(result.violations.map((v) => v.code)).toEqual([
|
||
'BUILD_PATH_LEAK',
|
||
]);
|
||
});
|
||
|
||
it('leest alleen tekstbestanden en gaat niet af op een bestandssysteemwortel', () => {
|
||
// clientDir direct onder de wortel: '../..' is de wortel zelf en mag
|
||
// niet elk bestand met een slash als lek aanmerken
|
||
const { clientDir, put } = outputDir();
|
||
put('logo.png', 'binair');
|
||
expect(externalizeOutput(clientDir, silent()).exitCode).toBe(0);
|
||
const rootResult = externalizeOutput(join(clientDir, '..'), silent());
|
||
expect(
|
||
rootResult.violations.every((v) => v.code !== 'BUILD_PATH_LEAK'),
|
||
).toBe(true);
|
||
});
|
||
});
|
||
|
||
describe('externalizeOutput als opdrachtregel', () => {
|
||
const script = join(
|
||
dirname(fileURLToPath(import.meta.url)),
|
||
'externalizeInlineScripts.ts',
|
||
);
|
||
const run = (dir: string) =>
|
||
spawnSync(process.execPath, [script, dir], { encoding: 'utf8' });
|
||
|
||
it('geeft exitcode 0 en één regel bij een geldige uitvoer', () => {
|
||
const { clientDir } = outputDir();
|
||
const result = run(clientDir);
|
||
expect(result.status).toBe(0);
|
||
expect(result.stdout.trim().split('\n')).toHaveLength(1);
|
||
expect(existsSync(join(clientDir, 'assets'))).toBe(true);
|
||
});
|
||
|
||
it('geeft exitcode 1 en schrijft niets bij een overtreding', () => {
|
||
const { clientDir } = outputDir();
|
||
rmSync(join(clientDir, '404.html'));
|
||
const before = listing(clientDir);
|
||
const result = run(clientDir);
|
||
expect(result.status).toBe(1);
|
||
expect(result.stderr).toContain('404.html: NOT_FOUND_PAGE_MISSING');
|
||
expect(listing(clientDir)).toEqual(before);
|
||
});
|
||
});
|