Files
AndreaSlopFotografie/web/config/securityHeaders.test.ts
T
SluijsensandClaude Opus 5.5 6d3a1566c2
Continuous Integration / lighthouse (pull_request) Canceled after 0s
Continuous Integration / deploy-test (pull_request) Canceled after 0s
Continuous Integration / deploy-production (pull_request) Canceled after 0s
Continuous Integration / check (pull_request) Canceled after 2m22s
Adds Sentry and Umami monitoring to the site
Sentry loads after the page and reports errors only, without personal data;
Umami counts visits per environment. Both only run on andreaslop.nl and
test.andreaslop.nl. The CSP allows just these two hosts, and builds carry the
commit SHA as release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 12:08:47 +02:00

57 lines
2.2 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { contentSecurityPolicy, securityHeaders } from './securityHeaders';
const umami = 'https://analytics.slpsoftware.nl';
const sentry = 'https://o4511795618185216.ingest.de.sentry.io';
const directive = (csp: string, name: string) =>
csp.split('; ').find((d) => d.startsWith(name + ' ')) ?? '';
describe('securityHeaders', () => {
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].sort());
expect(directive(csp, 'connect-src')).toBe(
`connect-src 'self' ${umami} ${sentry}`,
);
// Alleen script-src en connect-src noemen een externe host
for (const d of csp.split('; ')) {
if (!/^(script|connect)-src /.test(d)) {
expect(d).not.toContain('https://');
}
}
expect(csp).not.toContain('*');
});
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',
});
});
});