Generates the nginx rules for prerendered pages
Pages now map to their html file, other forms get one redirect, unknown URLs get a real 404 and responses are compressed. A browser-free test suite runs the rules against a real nginx, in CI only so far. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
import fc from 'fast-check';
|
||||
import { pagePaths } from '../../config/canonicalUrls.ts';
|
||||
import { pathSegment } from './arbitraries';
|
||||
|
||||
// Generatoren voor de URL-regels van bekende en onbekende pagina's (BR-HST).
|
||||
// Los van arbitraries.ts en requestArbitraries.ts, zodat die klein blijven
|
||||
// (PBT-07).
|
||||
|
||||
// Een pad uit het paginaregister, '/' inbegrepen
|
||||
export const knownPage = fc.constantFrom(...pagePaths);
|
||||
|
||||
// Hetzelfde pad met willekeurig hoofd- of kleine letters per teken; gelijk aan
|
||||
// het origineel als je het weer kleine letters maakt
|
||||
export const caseVariant = (path: string) =>
|
||||
fc
|
||||
.array(fc.boolean(), { minLength: path.length, maxLength: path.length })
|
||||
.map((flags) =>
|
||||
[...path]
|
||||
.map((char, i) => (flags[i] ? char.toUpperCase() : char))
|
||||
.join(''),
|
||||
);
|
||||
|
||||
// De naam zonder voorste slash; voor '/' de naam 'index' (BR-HST-04)
|
||||
const nameOf = (path: string) =>
|
||||
path === '/' ? 'index' : path.replace(/^\//, '');
|
||||
|
||||
// Een variant van een bekende pagina: hoofdletters, optioneel .html en 0 tot 3
|
||||
// slashes aan het eind. Voor '/' is dat de index-familie (/Index.html//). Kan
|
||||
// ook de canonieke vorm zelf zijn.
|
||||
export const pageVariant = knownPage.chain((page) =>
|
||||
fc
|
||||
.tuple(
|
||||
caseVariant(nameOf(page)),
|
||||
fc.boolean(),
|
||||
fc.integer({ min: 0, max: 3 }),
|
||||
)
|
||||
.map(
|
||||
([name, html, slashes]) =>
|
||||
`/${name}${html ? '.html' : ''}${'/'.repeat(slashes)}`,
|
||||
),
|
||||
);
|
||||
|
||||
// Pad met 1 tot 3 veilige segmenten dat in geen enkele variant (hoofdletters,
|
||||
// .html, slashes, index-familie) een bekende pagina is en niet 404 heet
|
||||
const reserved = new Set([
|
||||
...pagePaths.map((path) => `/${nameOf(path)}`),
|
||||
'/404',
|
||||
]);
|
||||
export const unknownPath = fc
|
||||
.array(pathSegment, { minLength: 1, maxLength: 3 })
|
||||
.map((segments) => '/' + segments.join('/'))
|
||||
.filter((path) => !reserved.has(path));
|
||||
|
||||
// Wat een aanvaller zou proberen: dubbele slash en backslash voor een ander
|
||||
// domein, puntsegmenten (ook gecodeerd), NUL, regeleinden, stuurcodes en heel
|
||||
// lange paden. Begint altijd met een slash.
|
||||
export const hostilePath = fc
|
||||
.oneof(
|
||||
fc.constantFrom(
|
||||
'//evil.example/',
|
||||
'//evil.example',
|
||||
'/\\evil.example/',
|
||||
'/\\evil.example',
|
||||
'/%2e%2e/',
|
||||
'/%2e%2e/etc/passwd/',
|
||||
'/over-mij\0/',
|
||||
'/over-mij\r\nSet-Cookie: x=1/',
|
||||
'/over-mij\n/',
|
||||
'/\u0007bel/',
|
||||
),
|
||||
fc.string({ maxLength: 40 }),
|
||||
fc.string({ minLength: 2000, maxLength: 3000 }),
|
||||
)
|
||||
.map((text) => (text.startsWith('/') ? text : `/${text}`));
|
||||
|
||||
// Query zoals een browser hem stuurt, zonder het vraagteken; kan leeg zijn
|
||||
export const queryString = fc.oneof(
|
||||
fc.constant(''),
|
||||
fc.stringMatching(/^[a-z0-9=&%_.-]{1,30}$/),
|
||||
);
|
||||
Reference in New Issue
Block a user