|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | +import { fileURLToPath } from 'node:url' |
| 3 | +import { setup, fetch } from '@nuxt/test-utils' |
| 4 | + |
| 5 | +describe('[nuxt-security] Public Assets', async () => { |
| 6 | + await setup({ |
| 7 | + rootDir: fileURLToPath(new URL('./fixtures/publicAssets', import.meta.url)), |
| 8 | + }) |
| 9 | + |
| 10 | + it('does not set all-resources security headers when disabled in config', async () => { |
| 11 | + const { headers } = await fetch('/icon.png') |
| 12 | + expect(headers).toBeDefined() |
| 13 | + |
| 14 | + // Security headers that are always set on all resources |
| 15 | + const rp = headers.get('referrer-policy') |
| 16 | + const sts = headers.get('strict-transport-security') |
| 17 | + const xcto = headers.get('x-content-type-options') |
| 18 | + const xdo = headers.get('x-download-options') |
| 19 | + const xfo = headers.get('x-frame-options') |
| 20 | + const xpcdp = headers.get('x-permitted-cross-domain-policies') |
| 21 | + const xxp = headers.get('x-xss-protection') |
| 22 | + |
| 23 | + expect(rp).toBeNull() |
| 24 | + expect(sts).toBeNull() |
| 25 | + expect(xcto).toBeNull() |
| 26 | + expect(xdo).toBeNull() |
| 27 | + expect(xfo).toBeNull() |
| 28 | + expect(xpcdp).toBeNull() |
| 29 | + expect(xxp).toBeNull() |
| 30 | + }) |
| 31 | + |
| 32 | + it('sets security headers on routes when specified in routeRules', async () => { |
| 33 | + const { headers } = await fetch('/test') |
| 34 | + expect(headers).toBeDefined() |
| 35 | + |
| 36 | + // Security headers that are always set on all resources |
| 37 | + const rp = headers.get('referrer-policy') |
| 38 | + const sts = headers.get('strict-transport-security') |
| 39 | + const xcto = headers.get('x-content-type-options') |
| 40 | + const xdo = headers.get('x-download-options') |
| 41 | + const xfo = headers.get('x-frame-options') |
| 42 | + const xpcdp = headers.get('x-permitted-cross-domain-policies') |
| 43 | + const xxp = headers.get('x-xss-protection') |
| 44 | + |
| 45 | + expect(rp).toBe('no-referrer') |
| 46 | + expect(sts).toBe('max-age=15552000; includeSubDomains;') |
| 47 | + expect(xcto).toBe('nosniff') |
| 48 | + expect(xdo).toBe('noopen') |
| 49 | + expect(xfo).toBe('SAMEORIGIN') |
| 50 | + expect(xpcdp).toBe('none') |
| 51 | + expect(xxp).toBe('0') |
| 52 | + }) |
| 53 | +}) |
0 commit comments