|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +const { useNuxt } = vi.hoisted(() => ({ |
| 4 | + useNuxt: vi.fn(), |
| 5 | +})) |
| 6 | + |
| 7 | +vi.mock('nuxt/kit', () => ({ |
| 8 | + defineNuxtModule: <T>(module: T) => module, |
| 9 | + useNuxt, |
| 10 | +})) |
| 11 | + |
| 12 | +import securityHeadersModule from '../../../modules/security-headers' |
| 13 | + |
| 14 | +type RouteRule = { |
| 15 | + headers?: Record<string, string> |
| 16 | + redirect?: string |
| 17 | +} |
| 18 | + |
| 19 | +type MockNuxt = { |
| 20 | + options: { |
| 21 | + app: { |
| 22 | + head?: { |
| 23 | + meta?: Array<Record<string, string>> |
| 24 | + } |
| 25 | + } |
| 26 | + dev: boolean |
| 27 | + devtools?: boolean | { enabled?: boolean } |
| 28 | + routeRules: Record<string, RouteRule> |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +function createNuxt(options: Partial<MockNuxt['options']> = {}): MockNuxt { |
| 33 | + return { |
| 34 | + options: { |
| 35 | + app: {}, |
| 36 | + dev: false, |
| 37 | + devtools: false, |
| 38 | + routeRules: {}, |
| 39 | + ...options, |
| 40 | + }, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +function getCsp(nuxt: MockNuxt) { |
| 45 | + return nuxt.options.app.head?.meta?.find(meta => meta['http-equiv'] === 'Content-Security-Policy') |
| 46 | + ?.content |
| 47 | +} |
| 48 | + |
| 49 | +describe('security headers module', () => { |
| 50 | + beforeEach(() => { |
| 51 | + delete process.env.TEST |
| 52 | + useNuxt.mockReset() |
| 53 | + }) |
| 54 | + |
| 55 | + it('keeps security headers and only relaxes devtools-specific bits in dev', () => { |
| 56 | + const nuxt = createNuxt({ |
| 57 | + dev: true, |
| 58 | + devtools: { enabled: true }, |
| 59 | + routeRules: { |
| 60 | + '/**': { |
| 61 | + headers: { |
| 62 | + 'Permissions-Policy': 'camera=()', |
| 63 | + }, |
| 64 | + }, |
| 65 | + '/__nuxt_devtools__/**': { |
| 66 | + headers: { |
| 67 | + 'Cache-Control': 'no-store', |
| 68 | + }, |
| 69 | + redirect: '/devtools', |
| 70 | + }, |
| 71 | + }, |
| 72 | + }) |
| 73 | + |
| 74 | + useNuxt.mockReturnValue(nuxt) |
| 75 | + securityHeadersModule.setup() |
| 76 | + |
| 77 | + const csp = getCsp(nuxt) |
| 78 | + |
| 79 | + expect(csp).toContain('ws://localhost:*') |
| 80 | + expect(csp).toContain("frame-src https://bsky.app https://pdsmoover.com 'self'") |
| 81 | + expect(nuxt.options.routeRules['/**']?.headers).toEqual( |
| 82 | + expect.objectContaining({ |
| 83 | + 'Permissions-Policy': 'camera=()', |
| 84 | + 'Referrer-Policy': 'strict-origin-when-cross-origin', |
| 85 | + 'X-Content-Type-Options': 'nosniff', |
| 86 | + 'X-Frame-Options': 'DENY', |
| 87 | + }), |
| 88 | + ) |
| 89 | + expect(nuxt.options.routeRules['/__nuxt_devtools__/**']).toEqual({ |
| 90 | + headers: { |
| 91 | + 'Cache-Control': 'no-store', |
| 92 | + 'Permissions-Policy': 'camera=()', |
| 93 | + 'Referrer-Policy': 'strict-origin-when-cross-origin', |
| 94 | + 'X-Content-Type-Options': 'nosniff', |
| 95 | + 'X-Frame-Options': 'SAMEORIGIN', |
| 96 | + }, |
| 97 | + redirect: '/devtools', |
| 98 | + }) |
| 99 | + }) |
| 100 | + |
| 101 | + it('does not apply devtools relaxations when devtools are disabled via object config', () => { |
| 102 | + const nuxt = createNuxt({ |
| 103 | + dev: true, |
| 104 | + devtools: { enabled: false }, |
| 105 | + }) |
| 106 | + |
| 107 | + useNuxt.mockReturnValue(nuxt) |
| 108 | + securityHeadersModule.setup() |
| 109 | + |
| 110 | + const csp = getCsp(nuxt) |
| 111 | + |
| 112 | + expect(csp).not.toContain('ws://localhost:*') |
| 113 | + expect(csp).not.toContain("frame-src https://bsky.app https://pdsmoover.com 'self'") |
| 114 | + expect(nuxt.options.routeRules['/__nuxt_devtools__/**']).toBeUndefined() |
| 115 | + }) |
| 116 | +}) |
0 commit comments