|
| 1 | +/** |
| 2 | + * @jest-environment node |
| 3 | + */ |
| 4 | +const { getSecurityHeaders } = require('../security-headers'); |
| 5 | + |
| 6 | +describe('security headers', () => { |
| 7 | + const originalHorizon = process.env.NEXT_PUBLIC_HORIZON_URL; |
| 8 | + const originalSoroban = process.env.NEXT_PUBLIC_SOROBAN_RPC_URL; |
| 9 | + |
| 10 | + afterEach(() => { |
| 11 | + process.env.NEXT_PUBLIC_HORIZON_URL = originalHorizon; |
| 12 | + process.env.NEXT_PUBLIC_SOROBAN_RPC_URL = originalSoroban; |
| 13 | + }); |
| 14 | + |
| 15 | + it('applies CSP and hardening headers to every route by default', () => { |
| 16 | + delete process.env.NEXT_PUBLIC_HORIZON_URL; |
| 17 | + delete process.env.NEXT_PUBLIC_SOROBAN_RPC_URL; |
| 18 | + |
| 19 | + const byKey = Object.fromEntries(getSecurityHeaders().map((h) => [h.key, h.value])); |
| 20 | + |
| 21 | + expect(byKey['Content-Security-Policy']).toContain("default-src 'self'"); |
| 22 | + expect(byKey['Content-Security-Policy']).toContain('connect-src'); |
| 23 | + expect(byKey['Content-Security-Policy']).toContain('https://horizon-testnet.stellar.org'); |
| 24 | + expect(byKey['Content-Security-Policy']).toContain('https://soroban-testnet.stellar.org'); |
| 25 | + expect(byKey['Content-Security-Policy']).toContain("frame-ancestors 'none'"); |
| 26 | + expect(byKey['X-Content-Type-Options']).toBe('nosniff'); |
| 27 | + expect(byKey['X-Frame-Options']).toBe('DENY'); |
| 28 | + expect(byKey['Referrer-Policy']).toBe('strict-origin-when-cross-origin'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('derives connect-src from NEXT_PUBLIC_HORIZON_URL / NEXT_PUBLIC_SOROBAN_RPC_URL for self-hosted deployments', () => { |
| 32 | + process.env.NEXT_PUBLIC_HORIZON_URL = 'https://horizon.example.com'; |
| 33 | + process.env.NEXT_PUBLIC_SOROBAN_RPC_URL = 'https://soroban.example.com'; |
| 34 | + |
| 35 | + const csp = getSecurityHeaders().find((h) => h.key === 'Content-Security-Policy').value; |
| 36 | + |
| 37 | + expect(csp).toContain('https://horizon.example.com'); |
| 38 | + expect(csp).toContain('https://soroban.example.com'); |
| 39 | + }); |
| 40 | +}); |
0 commit comments