|
| 1 | +import nextConfig from "../next.config"; |
| 2 | + |
| 3 | +describe("Frontend Security Headers & Content Security Policy Configuration", () => { |
| 4 | + it("should export an async headers function", () => { |
| 5 | + expect(typeof nextConfig.headers).toBe("function"); |
| 6 | + }); |
| 7 | + |
| 8 | + it("should apply security headers to all routes (/:path*)", async () => { |
| 9 | + if (!nextConfig.headers) return; |
| 10 | + const headerConfigs = await nextConfig.headers(); |
| 11 | + expect(headerConfigs.length).toBeGreaterThan(0); |
| 12 | + expect(headerConfigs[0].source).toBe("/:path*"); |
| 13 | + }); |
| 14 | + |
| 15 | + it("should configure strict Content-Security-Policy (CSP)", async () => { |
| 16 | + if (!nextConfig.headers) return; |
| 17 | + const headerConfigs = await nextConfig.headers(); |
| 18 | + const headers = headerConfigs[0].headers; |
| 19 | + const csp = headers.find( |
| 20 | + (h: { key: string }) => h.key === "Content-Security-Policy", |
| 21 | + ); |
| 22 | + |
| 23 | + expect(csp).toBeDefined(); |
| 24 | + expect(csp?.value).toContain("default-src 'self'"); |
| 25 | + expect(csp?.value).toContain("script-src 'self'"); |
| 26 | + expect(csp?.value).toContain("connect-src 'self'"); |
| 27 | + expect(csp?.value).toContain("frame-ancestors 'none'"); |
| 28 | + expect(csp?.value).toContain("object-src 'none'"); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should configure X-Frame-Options DENY", async () => { |
| 32 | + if (!nextConfig.headers) return; |
| 33 | + const headerConfigs = await nextConfig.headers(); |
| 34 | + const headers = headerConfigs[0].headers; |
| 35 | + const xfo = headers.find( |
| 36 | + (h: { key: string }) => h.key === "X-Frame-Options", |
| 37 | + ); |
| 38 | + |
| 39 | + expect(xfo).toBeDefined(); |
| 40 | + expect(xfo?.value).toBe("DENY"); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should configure X-Content-Type-Options nosniff", async () => { |
| 44 | + if (!nextConfig.headers) return; |
| 45 | + const headerConfigs = await nextConfig.headers(); |
| 46 | + const headers = headerConfigs[0].headers; |
| 47 | + const xcto = headers.find( |
| 48 | + (h: { key: string }) => h.key === "X-Content-Type-Options", |
| 49 | + ); |
| 50 | + |
| 51 | + expect(xcto).toBeDefined(); |
| 52 | + expect(xcto?.value).toBe("nosniff"); |
| 53 | + }); |
| 54 | + |
| 55 | + it("should configure HSTS Strict-Transport-Security", async () => { |
| 56 | + if (!nextConfig.headers) return; |
| 57 | + const headerConfigs = await nextConfig.headers(); |
| 58 | + const headers = headerConfigs[0].headers; |
| 59 | + const hsts = headers.find( |
| 60 | + (h: { key: string }) => h.key === "Strict-Transport-Security", |
| 61 | + ); |
| 62 | + |
| 63 | + expect(hsts).toBeDefined(); |
| 64 | + expect(hsts?.value).toContain("max-age=63072000"); |
| 65 | + expect(hsts?.value).toContain("includeSubDomains"); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should configure Referrer-Policy strict-origin-when-cross-origin", async () => { |
| 69 | + if (!nextConfig.headers) return; |
| 70 | + const headerConfigs = await nextConfig.headers(); |
| 71 | + const headers = headerConfigs[0].headers; |
| 72 | + const rp = headers.find( |
| 73 | + (h: { key: string }) => h.key === "Referrer-Policy", |
| 74 | + ); |
| 75 | + |
| 76 | + expect(rp).toBeDefined(); |
| 77 | + expect(rp?.value).toBe("strict-origin-when-cross-origin"); |
| 78 | + }); |
| 79 | +}); |
0 commit comments