Files
AndreaSlopFotografie/web/config/securityHeaders.test.ts
SluijsensandClaude Sonnet 5.5 49b1d5f0ba Monitoring settings (Sentry, Umami) and the CSP origins come from the build environment
One build per environment: PROD_* variables for the production artifact,
TEST_* for the test build and the CSP check. The build fails when a variable
is empty. Tests use the production values from config/monitoringTestEnv.ts.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-18 19:29:51 +01:00

88 lines
3.4 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest';
import { contentSecurityPolicy, securityHeaders } from './securityHeaders';
const umami = 'https://analytics.slpsoftware.nl';
const sentry = 'https://o4511795618185216.ingest.de.sentry.io';
const captchaFrames = [
'https://global.frcapi.com',
'https://global0.frcapi.com',
'https://global1.frcapi.com',
];
const directive = (csp: string, name: string) =>
csp.split('; ').find((d) => d.startsWith(name + ' ')) ?? '';
afterEach(() => vi.unstubAllEnvs());
describe('securityHeaders', () => {
it('noemt Umami en Sentry niet zonder monitoringinstellingen in de build', () => {
vi.stubEnv('VITE_SENTRY_DSN', '');
const csp = contentSecurityPolicy('production');
expect(csp).not.toContain(umami);
expect(csp).not.toContain(sentry);
expect(directive(csp, 'script-src')).toBe("script-src 'self'");
});
it('productie-CSP heeft geen onveilige uitzonderingen voor scripts', () => {
const csp = contentSecurityPolicy('production');
expect(csp).not.toContain('unsafe-eval');
expect(directive(csp, 'script-src')).toBe(`script-src 'self' ${umami}`);
expect(csp).not.toContain('ws:');
expect(directive(csp, 'frame-ancestors')).toBe(
"frame-ancestors 'none'",
);
});
it('productie-CSP opent alleen Umami en Sentry, op de juiste plekken (M7)', () => {
const csp = contentSecurityPolicy('production');
const externalHosts = new Set(csp.match(/https:\/\/[^\s;]+/g));
expect([...externalHosts].sort()).toEqual(
[sentry, umami, ...captchaFrames].sort(),
);
expect(directive(csp, 'connect-src')).toBe(
`connect-src 'self' ${umami} ${sentry}`,
);
// Alleen script-src, connect-src en frame-src noemen een externe host
for (const d of csp.split('; ')) {
if (!/^(script|connect|frame)-src /.test(d)) {
expect(d).not.toContain('https://');
}
}
expect(csp).not.toContain('*');
});
it('productie-CSP laat het captcha-venster alleen van de drie vaste hosts toe', () => {
const csp = contentSecurityPolicy('production');
expect(directive(csp, 'frame-src')).toBe(
`frame-src ${captchaFrames.join(' ')}`,
);
// Geen wildcard en niets extra's totdat een echte browser het eist
expect(csp).not.toContain('*');
expect(directive(csp, 'script-src')).toBe(`script-src 'self' ${umami}`);
expect(directive(csp, 'worker-src')).toBe('');
expect(directive(csp, 'connect-src')).toBe(
`connect-src 'self' ${umami} ${sentry}`,
);
});
it('dev-CSP voegt alleen toe wat Vite nodig heeft', () => {
const csp = contentSecurityPolicy('development');
expect(directive(csp, 'script-src')).toBe(
`script-src 'self' ${umami} 'unsafe-inline'`,
);
expect(directive(csp, 'connect-src')).toBe(
`connect-src 'self' ${umami} ${sentry} ws:`,
);
expect(csp).not.toContain('unsafe-eval');
});
it('zet alle verplichte headers', () => {
expect(securityHeaders('production')).toMatchObject({
'Strict-Transport-Security': 'max-age=31536000; includeSubDomains',
'X-Content-Type-Options': 'nosniff',
'X-Frame-Options': 'DENY',
'Referrer-Policy': 'strict-origin-when-cross-origin',
});
});
});