Fixes two nginx rule defects found by real nginx in CI

Plain map keys compare case-insensitively, so /Over-Mij was not redirected. A
path starting with an encoded backslash could become an open redirect through
the trailing-slash rule; it now gets the 404 page. Also makes the tests readable
for nginx's workers and less sensitive to the slow CI runner.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-01 04:47:52 +01:00
co-authored by Claude Opus 5.5
parent 95e3c61281
commit 8cb9185a69
7 changed files with 98 additions and 24 deletions
+26
View File
@@ -99,4 +99,30 @@ describe('serverconfiguratie', () => {
expect(block).toContain('Content-Security-Policy');
}
});
it('de nginx-map kent geen tekstsleutels: die negeren hoofdletters', () => {
// Een sleutel /over-mij zou ook /Over-Mij ongemoeid laten (echte nginx)
const map = nginxTestConf().match(
/map \$uri \$andreaslop_canonical \{([^}]*)\}/,
)?.[1];
const keys = (map ?? '')
.split('\n')
.map((line) => line.trim())
.filter((line) => line && line !== 'default "";')
.map((line) => line.split(' ')[0]);
expect(keys.length).toBeGreaterThan(0);
for (const key of keys) expect(key).toMatch(/^~\*?\^\//);
// De hoofdlettergevoelige canonieke vormen staan vóór de varianten
const lastCanonical = keys.findLastIndex((key) => key.startsWith('~^'));
const firstVariant = keys.findIndex((key) => key.startsWith('~*'));
expect(lastCanonical).toBeLessThan(firstVariant);
});
it('een backslash vooraan in het pad geeft 404 voor de doorverwijzing', () => {
const nginx = nginxTestConf();
const guard = nginx.indexOf('if ($uri ~ "^/\\x5c") {');
expect(guard).toBeGreaterThan(-1);
expect(guard).toBeLessThan(nginx.indexOf('if ($andreaslop_canonical)'));
expect(guard).toBeLessThan(nginx.indexOf('rewrite ^/(.+?)/+$'));
});
});
+14 -5
View File
@@ -115,11 +115,13 @@ export function nginxTestConf(
.map((line) => indent + line)
.join('\n');
// Exacte canonieke vormen winnen van de reguliere expressies eronder, dus
// het canonieke pad zelf wordt nooit doorverwezen. Geen regel mag /404 of
// /404.html raken: error_page draait de server-regels opnieuw met
// $uri=/404.html en zou van de 404-pagina een redirect maken.
const canonicalEntries = redirectNames.map((path) => ` ${path} "";`);
// De canonieke vormen komen eerst, als hoofdlettergevoelige regex: nginx
// vergelijkt gewone tekstsleutels in een map zonder onderscheid tussen
// hoofd- en kleine letters, dus een exacte sleutel /over-mij zou ook
// /Over-Mij ongemoeid laten. De eerste regex die past wint. Geen regel
// mag /404 of /404.html raken: error_page draait de server-regels
// opnieuw met $uri=/404.html en zou van de 404-pagina een redirect maken.
const canonicalEntries = redirectNames.map((path) => ` ~^${path}$ "";`);
const variantEntries = [
` ~*^/${variantPattern('/')}$ /;`,
...redirectNames.map(
@@ -174,6 +176,13 @@ ${block(' ', noCache)}
gzip_min_length 256;
gzip_types ${gzipMimeTypes().join(' ')};
# Een backslash vooraan zou na het weghalen van de slashes een Location
# /\\host geven, en browsers lezen dat als //host. Dat pad bestaat niet
# (BR-HST-06). \\x5c is de backslash; $uri is hier al gedecodeerd.
if ($uri ~ "^/\\x5c") {
return 404;
}
# Alleen return in een if is veilig. Het doel komt uit de map hierboven.
if ($andreaslop_canonical) {
return 301 $andreaslop_canonical$is_args$args;