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:
+1
-1
@@ -13,7 +13,7 @@ The final commands with full paths go into the draft in the `server-ops` reposit
|
||||
| gzip and headers from other vhosts | gzip is on nowhere, and `nginx.conf` has no http-level `add_header`. gzip in this server block changes no other site |
|
||||
| Default block | Since 2026-10-21, `/etc/nginx/conf.d/00-default.conf` answers an unknown host with 444. The current comment that nginx would fall back to WordPress is out of date; correct it in the generator |
|
||||
| nginx 1.30.6 | Every directive in the sketch is standard there. `if` with only `return` at server level is safe |
|
||||
| `map` matching | Exact strings win over regexes, so `/over-mij ""` beats the variant regex and no loop arises |
|
||||
| `map` matching | Plain string keys in `map` compare case-insensitively, so `/over-mij ""` also matched `/Over-Mij` and blocked its redirect (found by the real nginx in CI). Canonical forms are therefore case-sensitive regex keys placed before the variant regexes; the first regex that matches wins and no loop arises. The draft must use the regenerated config |
|
||||
| `error_page` | Its internal redirect runs server-level rewrite and `if` again with `$uri=/404.html`. That matches no map rule. **Keep it so: no map rule may match `/404` or `/404.html`** |
|
||||
| Directories | `try_files $uri` matches files only, so `/assets` gives 404 |
|
||||
|
||||
|
||||
@@ -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 ^/(.+?)/+$'));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -17,16 +17,16 @@
|
||||
# zijn constanten uit de paginalijst, nooit afgeleid van het verzoek.
|
||||
map $uri $andreaslop_canonical {
|
||||
default "";
|
||||
/over-mij "";
|
||||
/portfolio "";
|
||||
/soorten-fotoshoots "";
|
||||
/soorten-fotoshoots/newborn "";
|
||||
/soorten-fotoshoots/zwangerschap "";
|
||||
/soorten-fotoshoots/gezin "";
|
||||
/soorten-fotoshoots/loveshoot "";
|
||||
/soorten-fotoshoots/portret "";
|
||||
/contact "";
|
||||
/privacyverklaring "";
|
||||
~^/over-mij$ "";
|
||||
~^/portfolio$ "";
|
||||
~^/soorten-fotoshoots$ "";
|
||||
~^/soorten-fotoshoots/newborn$ "";
|
||||
~^/soorten-fotoshoots/zwangerschap$ "";
|
||||
~^/soorten-fotoshoots/gezin$ "";
|
||||
~^/soorten-fotoshoots/loveshoot$ "";
|
||||
~^/soorten-fotoshoots/portret$ "";
|
||||
~^/contact$ "";
|
||||
~^/privacyverklaring$ "";
|
||||
~*^/index(?:\.html)?/*$ /;
|
||||
~*^/over-mij(?:\.html)?/*$ /over-mij;
|
||||
~*^/portfolio(?:\.html)?/*$ /portfolio;
|
||||
@@ -70,6 +70,13 @@ server {
|
||||
gzip_min_length 256;
|
||||
gzip_types text/javascript application/javascript text/css image/svg+xml application/json text/xml application/xml text/plain;
|
||||
|
||||
# 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;
|
||||
|
||||
+25
-7
@@ -1,7 +1,7 @@
|
||||
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
||||
import type { Page } from 'playwright-core';
|
||||
import sitePages from '../src/sitePages.ts';
|
||||
import { openPage, waitForHydration } from './support/browser.ts';
|
||||
import { eventually, openPage, waitForHydration } from './support/browser.ts';
|
||||
import { startServer } from './support/server.ts';
|
||||
|
||||
// FR-PR-11, FR-PR-21, V-D13, V-D15: welke foto's de browser opvraagt, wanneer
|
||||
@@ -44,8 +44,19 @@ const counts = (log: Logged[]) => {
|
||||
return map;
|
||||
};
|
||||
|
||||
// Elke uitgestelde foto is gehydrateerd: React hangt dan een fiber aan het
|
||||
// element. Scrollen voor die tijd (traag apparaat) laat de bovenste foto's
|
||||
// nooit in de buurt komen, want ze krijgen pas een src na de hydratatie.
|
||||
const deferredImagesHydrated = (page: Page) =>
|
||||
page.waitForFunction(() =>
|
||||
[...document.querySelectorAll('img[data-deferred]')].every((image) =>
|
||||
Object.keys(image).some((key) => key.startsWith('__reactFiber')),
|
||||
),
|
||||
);
|
||||
|
||||
// Scrolt de hele pagina door, zodat elke uitgestelde foto in de buurt komt
|
||||
async function scrollThrough(page: Page) {
|
||||
await deferredImagesHydrated(page);
|
||||
const height = await page.evaluate(() => document.body.scrollHeight);
|
||||
const step = await page.evaluate(() => Math.floor(window.innerHeight / 2));
|
||||
for (let y = 0; y <= height; y += step) {
|
||||
@@ -218,12 +229,16 @@ describe("logo's", () => {
|
||||
height: 940,
|
||||
prepare: requests.prepare,
|
||||
});
|
||||
const light = () =>
|
||||
requests.log.filter((entry) =>
|
||||
/\/logo-light-/.test(entry.url),
|
||||
);
|
||||
// Het verzoek komt pas na de load én de hydratatie; op een
|
||||
// traag apparaat is het netwerk dan al even stil geweest
|
||||
await eventually(async () => light().length).toBeGreaterThan(0);
|
||||
await page.waitForLoadState('networkidle');
|
||||
const light = requests.log.filter((entry) =>
|
||||
/\/logo-light-/.test(entry.url),
|
||||
);
|
||||
expect(light.length, path).toBe(1);
|
||||
expect(light[0].beforeLoad, path).toBe(path === '/');
|
||||
expect(light().length, path).toBe(1);
|
||||
expect(light()[0].beforeLoad, path).toBe(path === '/');
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -303,7 +318,10 @@ describe('de eerste rij van /portfolio (V-D13)', () => {
|
||||
.locator('img')
|
||||
.evaluateAll((images) =>
|
||||
images
|
||||
.map((image) => (image as HTMLImageElement).src)
|
||||
.map(
|
||||
(image) =>
|
||||
(image as HTMLImageElement).currentSrc,
|
||||
)
|
||||
.filter(Boolean),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -256,6 +256,8 @@ describe('Reveal bij een navigatie in de browser (O1)', () => {
|
||||
|
||||
// BR-RVL-13: een verborgen Reveal hoger dan (schermhoogte - 60) / 0,12 haalt
|
||||
// de drempel van 12 % nooit. Per pagina en breedte wordt de hoogste genoteerd.
|
||||
const PER_PAGE_TIMEOUT = 20_000;
|
||||
|
||||
describe('hoogte van de verborgen Reveal (V-D6)', () => {
|
||||
const viewports = [
|
||||
{ width: 320, height: 568 },
|
||||
@@ -292,5 +294,9 @@ describe('hoogte van de verborgen Reveal (V-D6)', () => {
|
||||
tallest.join('; '),
|
||||
);
|
||||
},
|
||||
// Elke pagina kost minstens de 2 s van settle plus laden en hydrateren;
|
||||
// op de Pi (CI) duurde dat bij 11 pagina's langer dan de standaard 60 s.
|
||||
// De wachttijd per pagina blijft gelijk, zodat de meting niet verzwakt.
|
||||
PER_PAGE_TIMEOUT * Object.keys(sitePages).length,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -65,6 +65,14 @@ process.on('exit', () => {
|
||||
}
|
||||
});
|
||||
|
||||
// Als root (de CI-container) draait de master als root en laat nginx de workers
|
||||
// onder een andere gebruiker (www-data of nobody) draaien. Die kan de
|
||||
// tijdelijke map (mode 700 van mkdtemp) niet openen en geeft overal 403. De
|
||||
// workers draaien daarom ook als root. Zonder root mag de user-directive niet:
|
||||
// nginx waarschuwt dan dat hij alleen voor het hoofdproces als root zin heeft.
|
||||
export const userDirective = (uid: number | undefined) =>
|
||||
uid === 0 ? 'user root;\n' : '';
|
||||
|
||||
// nginx onder Windows wil voorwaartse slashes in de configuratie
|
||||
const slashed = (path: string) => path.replace(/\\/g, '/');
|
||||
|
||||
@@ -113,7 +121,7 @@ export async function startNginx(options: {
|
||||
);
|
||||
await writeFile(
|
||||
confFile,
|
||||
`daemon off;
|
||||
`${userDirective(process.getuid?.())}daemon off;
|
||||
pid "${slashed(join(tmp, 'nginx.pid'))}";
|
||||
error_log "${slashed(errorLog)}";
|
||||
events {}
|
||||
|
||||
Reference in New Issue
Block a user