import { describe, it, expect } from 'vitest' import { EMBED_FRAME_ORIGINS } from '$lib/app/util/embed-hosts' import { applySecurityHeaders, buildContentSecurityPolicy, DENY_ALL_CSP, parseOriginList, type SecurityHeaderOptions, } from './security-headers' const prod: SecurityHeaderOptions = { dev: false, secure: true, instanceOrigin: 'https://coves.social', videoOrigins: parseOriginList('https://pds.coves.me https://tdpl.io'), } /** Splits a serialized policy into a `directive → sources` map. */ function directives(policy: string): Record { return Object.fromEntries( policy .split(';') .map((d) => d.trim()) .filter(Boolean) .map((d) => { const [name, ...rest] = d.split(/\s+/) return [name, rest.join(' ')] }), ) } describe('parseOriginList', () => { it('returns an empty list for unset or blank input', () => { expect(parseOriginList(undefined)).toEqual([]) expect(parseOriginList('')).toEqual([]) expect(parseOriginList(' ')).toEqual([]) }) it('accepts whitespace- or comma-separated https/http origins', () => { expect( parseOriginList( 'https://img.coves.social, https://pds.coves.me http://a.test:8081', ), ).toEqual([ 'https://img.coves.social', 'https://pds.coves.me', 'http://a.test:8081', ]) }) it('normalises to the bare origin', () => { expect( parseOriginList( 'https://img.coves.social/img/?x=1 HTTPS://User:pw@IMG.COVES.SOCIAL:443/ https://[::1]:8080/x', ), ).toEqual([ 'https://img.coves.social', 'https://img.coves.social', 'https://[::1]:8080', ]) }) it('rejects non-http schemes and unparseable entries loudly', () => { expect(() => parseOriginList('data:')).toThrow(/data:/) expect(() => parseOriginList('javascript:alert(1)')).toThrow(/javascript/) expect(() => parseOriginList('not a url')).toThrow(/not/) }) it('rejects wildcards, with or without a scheme', () => { expect(() => parseOriginList('*.coves.social')).toThrow(/\*\.coves\.social/) expect(() => parseOriginList('https://*.coves.social')).toThrow( /single exact host/, ) }) it('rejects entries that WHATWG parsing would let serialise as extra directives', () => { // `;` and quotes are legal host code points, so new URL().origin keeps them. expect(() => parseOriginList("https://a.test;script-src'unsafe-inline'"), ).toThrow(/single exact host/) expect(() => parseOriginList("https://a.test'")).toThrow( /single exact host/, ) }) it('strips anything after the host when the URL does parse', () => { expect(parseOriginList('https://a.test/;script-src')).toEqual([ 'https://a.test', ]) }) }) describe('buildContentSecurityPolicy', () => { it('preserves the directives Kit already emitted (the nonce lives there)', () => { const existing = "script-src 'self' 'nonce-abc123'" const policy = directives(buildContentSecurityPolicy(existing, prod)) expect(policy['script-src']).toBe("'self' 'nonce-abc123'") }) it('emits a complete, fail-closed policy in production', () => { const policy = directives( buildContentSecurityPolicy("script-src 'self' 'nonce-x'", prod), ) expect(policy['default-src']).toBe("'self'") expect(policy['base-uri']).toBe("'self'") expect(policy['object-src']).toBe("'none'") expect(policy['frame-ancestors']).toBe("'none'") expect(policy['form-action']).toBe("'self'") expect(policy['worker-src']).toBe("'self'") expect(policy['manifest-src']).toBe("'self'") expect(policy['font-src']).toBe("'self'") expect(policy['upgrade-insecure-requests']).toBe('') }) it('does not add inline or eval to script-src', () => { const csp = buildContentSecurityPolicy("script-src 'self' 'nonce-x'", prod) expect(csp).not.toMatch(/script-src[^;]*'unsafe-inline'/) expect(csp).not.toMatch(/'unsafe-eval'/) }) it('keeps the SSR event-replay script-src-attr directive verbatim', () => { const policy = directives( buildContentSecurityPolicy( "script-src 'self' 'nonce-x'; script-src-attr 'unsafe-hashes' 'sha256-abc='", prod, ), ) expect(policy['script-src']).toBe("'self' 'nonce-x'") expect(policy['script-src-attr']).toBe("'unsafe-hashes' 'sha256-abc='") }) it('allows inline style attributes but not inline