|
| 1 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 2 | +import nextConfig from '../next.config'; |
| 3 | + |
| 4 | +type Header = { key: string; value: string }; |
| 5 | +type HeaderRule = { source: string; headers: Header[] }; |
| 6 | + |
| 7 | +const ALL_ROUTES_SOURCE = '/(.*)'; |
| 8 | + |
| 9 | +async function getRule(source: string): Promise<HeaderRule> { |
| 10 | + if (typeof nextConfig.headers !== 'function') { |
| 11 | + throw new Error('next.config does not export a headers() function'); |
| 12 | + } |
| 13 | + const rules = (await nextConfig.headers()) as HeaderRule[]; |
| 14 | + const rule = rules.find((r) => r.source === source); |
| 15 | + if (!rule) throw new Error(`Expected a header rule for source "${source}"`); |
| 16 | + return rule; |
| 17 | +} |
| 18 | + |
| 19 | +async function getSecurityHeaders(): Promise<Header[]> { |
| 20 | + return (await getRule(ALL_ROUTES_SOURCE)).headers; |
| 21 | +} |
| 22 | + |
| 23 | +function headerValue(headers: Header[], key: string): string | undefined { |
| 24 | + return headers.find((h) => h.key === key)?.value; |
| 25 | +} |
| 26 | + |
| 27 | +function cspDirective(csp: string, name: string): string { |
| 28 | + const match = csp.match(new RegExp(`${name}\\s+([^;]+)`)); |
| 29 | + if (!match) throw new Error(`Missing CSP directive: ${name}`); |
| 30 | + return match[1].trim(); |
| 31 | +} |
| 32 | + |
| 33 | +describe('next.config security headers', () => { |
| 34 | + afterEach(() => { |
| 35 | + vi.unstubAllEnvs(); |
| 36 | + delete process.env.NEXT_PUBLIC_API_BASE_URL; |
| 37 | + }); |
| 38 | + |
| 39 | + it('applies a baseline security header set to all routes', async () => { |
| 40 | + const headers = await getSecurityHeaders(); |
| 41 | + |
| 42 | + expect(headerValue(headers, 'X-Frame-Options')).toBe('DENY'); |
| 43 | + expect(headerValue(headers, 'X-Content-Type-Options')).toBe('nosniff'); |
| 44 | + expect(headerValue(headers, 'Referrer-Policy')).toBe( |
| 45 | + 'strict-origin-when-cross-origin', |
| 46 | + ); |
| 47 | + expect(headerValue(headers, 'Permissions-Policy')).toBe( |
| 48 | + 'camera=(), microphone=(), geolocation=()', |
| 49 | + ); |
| 50 | + expect(headerValue(headers, 'Content-Security-Policy')).toBeDefined(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('only emits Strict-Transport-Security in production', async () => { |
| 54 | + vi.stubEnv('NODE_ENV', 'development'); |
| 55 | + expect( |
| 56 | + headerValue(await getSecurityHeaders(), 'Strict-Transport-Security'), |
| 57 | + ).toBeUndefined(); |
| 58 | + |
| 59 | + vi.stubEnv('NODE_ENV', 'production'); |
| 60 | + expect( |
| 61 | + headerValue(await getSecurityHeaders(), 'Strict-Transport-Security'), |
| 62 | + ).toBe('max-age=63072000; includeSubDomains; preload'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('CSP blocks framing via frame-ancestors and allows required script sources', async () => { |
| 66 | + const csp = headerValue(await getSecurityHeaders(), 'Content-Security-Policy'); |
| 67 | + expect(csp).toBeDefined(); |
| 68 | + if (!csp) return; |
| 69 | + |
| 70 | + expect(cspDirective(csp, 'frame-ancestors')).toBe("'none'"); |
| 71 | + const scriptSrc = cspDirective(csp, 'script-src'); |
| 72 | + expect(scriptSrc).toContain("'self'"); |
| 73 | + expect(scriptSrc).toContain("'unsafe-inline'"); |
| 74 | + }); |
| 75 | + |
| 76 | + it('CSP connect-src allows the default API base URL', async () => { |
| 77 | + const csp = headerValue(await getSecurityHeaders(), 'Content-Security-Policy'); |
| 78 | + expect(csp).toBeDefined(); |
| 79 | + if (!csp) return; |
| 80 | + |
| 81 | + const connectSrc = cspDirective(csp, 'connect-src'); |
| 82 | + expect(connectSrc).toContain("'self'"); |
| 83 | + expect(connectSrc).toContain('http://localhost:4000'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('CSP connect-src allows a custom NEXT_PUBLIC_API_BASE_URL', async () => { |
| 87 | + vi.stubEnv('NEXT_PUBLIC_API_BASE_URL', 'https://api.example.com'); |
| 88 | + const csp = headerValue(await getSecurityHeaders(), 'Content-Security-Policy'); |
| 89 | + expect(csp).toBeDefined(); |
| 90 | + if (!csp) return; |
| 91 | + |
| 92 | + expect(cspDirective(csp, 'connect-src')).toContain('https://api.example.com'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('CSP img-src covers every images.remotePatterns host', async () => { |
| 96 | + const csp = headerValue(await getSecurityHeaders(), 'Content-Security-Policy'); |
| 97 | + expect(csp).toBeDefined(); |
| 98 | + if (!csp) return; |
| 99 | + |
| 100 | + const imgSrc = cspDirective(csp, 'img-src'); |
| 101 | + for (const host of [ |
| 102 | + 'https://images.unsplash.com', |
| 103 | + 'https://*.pinata.cloud', |
| 104 | + 'https://cdn.jsdelivr.net', |
| 105 | + 'https://*.stellar.org', |
| 106 | + ]) { |
| 107 | + expect(imgSrc).toContain(host); |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + it('dev CSP keeps hot reload working (unsafe-eval + WebSocket)', async () => { |
| 112 | + vi.stubEnv('NODE_ENV', 'development'); |
| 113 | + const csp = headerValue(await getSecurityHeaders(), 'Content-Security-Policy'); |
| 114 | + expect(csp).toBeDefined(); |
| 115 | + if (!csp) return; |
| 116 | + |
| 117 | + expect(cspDirective(csp, 'script-src')).toContain("'unsafe-eval'"); |
| 118 | + const connectSrc = cspDirective(csp, 'connect-src'); |
| 119 | + expect(connectSrc).toContain('ws:'); |
| 120 | + expect(connectSrc).toContain('wss:'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('production CSP is stricter (no unsafe-eval, no WebSocket)', async () => { |
| 124 | + vi.stubEnv('NODE_ENV', 'production'); |
| 125 | + const csp = headerValue(await getSecurityHeaders(), 'Content-Security-Policy'); |
| 126 | + expect(csp).toBeDefined(); |
| 127 | + if (!csp) return; |
| 128 | + |
| 129 | + expect(cspDirective(csp, 'script-src')).not.toContain("'unsafe-eval'"); |
| 130 | + const connectSrc = cspDirective(csp, 'connect-src'); |
| 131 | + expect(connectSrc).not.toContain('ws:'); |
| 132 | + expect(connectSrc).not.toContain('wss:'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('blocks plugins and confines the document base', async () => { |
| 136 | + const csp = headerValue(await getSecurityHeaders(), 'Content-Security-Policy'); |
| 137 | + expect(csp).toBeDefined(); |
| 138 | + if (!csp) return; |
| 139 | + |
| 140 | + expect(cspDirective(csp, 'object-src')).toBe("'none'"); |
| 141 | + expect(cspDirective(csp, 'base-uri')).toBe("'self'"); |
| 142 | + expect(cspDirective(csp, 'form-action')).toBe("'self'"); |
| 143 | + }); |
| 144 | + |
| 145 | + it('preserves the existing caching header rules', async () => { |
| 146 | + const staticRule = await getRule('/_next/static/:path*'); |
| 147 | + expect(staticRule.headers).toEqual( |
| 148 | + expect.arrayContaining([ |
| 149 | + { key: 'Cache-Control', value: 'public, max-age=31536000, immutable' }, |
| 150 | + { key: 'Vary', value: 'Accept-Encoding' }, |
| 151 | + ]), |
| 152 | + ); |
| 153 | + |
| 154 | + const imageRule = await getRule('/_next/image/:path*'); |
| 155 | + expect(headerValue(imageRule.headers, 'Cache-Control')).toBe( |
| 156 | + 'public, max-age=31536000, immutable', |
| 157 | + ); |
| 158 | + }); |
| 159 | + |
| 160 | + it('does not add security headers to the caching rules (no accidental overrides)', async () => { |
| 161 | + const staticRule = await getRule('/_next/static/:path*'); |
| 162 | + expect(headerValue(staticRule.headers, 'X-Frame-Options')).toBeUndefined(); |
| 163 | + expect(headerValue(staticRule.headers, 'Content-Security-Policy')).toBeUndefined(); |
| 164 | + }); |
| 165 | +}); |
0 commit comments