|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { createCSPHeader, generateNonce } from '../content-security-policy'; |
| 4 | + |
| 5 | +describe('CSP Header Utils', () => { |
| 6 | + describe('generateNonce', () => { |
| 7 | + it('should generate a base64 nonce of correct length', () => { |
| 8 | + const nonce = generateNonce(); |
| 9 | + expect(nonce).toMatch(/^[A-Za-z0-9+/=]+$/); // Base64 pattern |
| 10 | + expect(Buffer.from(nonce, 'base64')).toHaveLength(16); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should generate unique nonces', () => { |
| 14 | + const nonce1 = generateNonce(); |
| 15 | + const nonce2 = generateNonce(); |
| 16 | + expect(nonce1).not.toBe(nonce2); |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + describe('createCSPHeader', () => { |
| 21 | + const testHost = 'example.com'; |
| 22 | + |
| 23 | + it('should create a standard CSP header with default directives', () => { |
| 24 | + const result = createCSPHeader('standard', testHost); |
| 25 | + |
| 26 | + expect(result.header).toContain("default-src 'self'"); |
| 27 | + expect(result.header).toContain("connect-src 'self' *.clerk.accounts.dev clerk.example.com"); |
| 28 | + expect(result.header).toContain("script-src 'self' 'unsafe-eval' 'unsafe-inline' http: https:"); |
| 29 | + expect(result.header).toContain("style-src 'self' 'unsafe-inline'"); |
| 30 | + expect(result.header).toContain("img-src 'self' https://img.clerk.com"); |
| 31 | + expect(result.header).toContain("frame-src 'self' https://challenges.cloudflare.com"); |
| 32 | + expect(result.header).toContain("form-action 'self'"); |
| 33 | + expect(result.header).toContain("worker-src 'self' blob:"); |
| 34 | + expect(result.nonce).toBeUndefined(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should create a strict-dynamic CSP header with nonce', () => { |
| 38 | + const result = createCSPHeader('strict-dynamic', testHost); |
| 39 | + |
| 40 | + // Extract the script-src directive and verify it contains the required values |
| 41 | + const directives = result.header.split('; '); |
| 42 | + const scriptSrcDirective = directives.find((d: string) => d.startsWith('script-src')) ?? ''; |
| 43 | + expect(scriptSrcDirective).toBeDefined(); |
| 44 | + |
| 45 | + const scriptSrcValues = scriptSrcDirective.replace('script-src ', '').split(' '); |
| 46 | + expect(scriptSrcValues).toContain("'self'"); |
| 47 | + expect(scriptSrcValues).toContain("'unsafe-inline'"); |
| 48 | + expect(scriptSrcValues).toContain("'strict-dynamic'"); |
| 49 | + expect(scriptSrcValues.some(val => val.startsWith("'nonce-"))).toBe(true); |
| 50 | + |
| 51 | + expect(result.nonce).toBeDefined(); |
| 52 | + expect(result.nonce).toMatch(/^[A-Za-z0-9+/=]+$/); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should handle custom CSP headers', () => { |
| 56 | + const customCSP = "default-src 'none'; img-src 'self' https://example.com; custom-directive 'value'"; |
| 57 | + const result = createCSPHeader('standard', testHost, customCSP); |
| 58 | + |
| 59 | + expect(result.header).toContain("default-src 'none'"); |
| 60 | + expect(result.header).toContain("img-src 'self' https://example.com"); |
| 61 | + expect(result.header).toContain("custom-directive 'value'"); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should handle different host formats', () => { |
| 65 | + const hosts = ['example.com', 'https://example.com', 'http://example.com', 'sub.example.com']; |
| 66 | + |
| 67 | + hosts.forEach(host => { |
| 68 | + const result = createCSPHeader('standard', host); |
| 69 | + expect(result.header).toContain('clerk.example.com'); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should handle malformed CSP headers gracefully', () => { |
| 74 | + const malformedCSP = "default-src 'none';;;img-src 'self';;;"; |
| 75 | + const result = createCSPHeader('standard', testHost, malformedCSP); |
| 76 | + |
| 77 | + expect(result.header).toContain("default-src 'none'"); |
| 78 | + expect(result.header).toContain("img-src 'self'"); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should handle empty CSP header', () => { |
| 82 | + const result = createCSPHeader('standard', testHost, ''); |
| 83 | + expect(result.header).toBeDefined(); |
| 84 | + expect(result.header).not.toBe(''); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should handle null CSP header', () => { |
| 88 | + const result = createCSPHeader('standard', testHost, null); |
| 89 | + expect(result.header).toBeDefined(); |
| 90 | + expect(result.header).not.toBe(''); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should handle development environment specific directives', () => { |
| 94 | + const result = createCSPHeader('standard', testHost); |
| 95 | + expect(result.header).toContain("script-src 'self' 'unsafe-eval' 'unsafe-inline' http: https:"); |
| 96 | + }); |
| 97 | + |
| 98 | + it('preserves all original CLERK_CSP_VALUES directives with special keywords quoted', () => { |
| 99 | + const result = createCSPHeader('standard', testHost, 'custom-directive new-value;'); |
| 100 | + |
| 101 | + // Split the result into individual directives for precise testing |
| 102 | + const directives = result.header.split('; '); |
| 103 | + |
| 104 | + // Check each directive individually with exact matches |
| 105 | + expect(directives).toContainEqual("connect-src 'self' *.clerk.accounts.dev clerk.example.com"); |
| 106 | + expect(directives).toContainEqual("default-src 'self'"); |
| 107 | + expect(directives).toContainEqual("form-action 'self'"); |
| 108 | + expect(directives).toContainEqual("frame-src 'self' https://challenges.cloudflare.com"); |
| 109 | + expect(directives).toContainEqual("img-src 'self' https://img.clerk.com"); |
| 110 | + expect(directives).toContainEqual("style-src 'self' 'unsafe-inline'"); |
| 111 | + expect(directives).toContainEqual("worker-src 'self' blob:"); |
| 112 | + expect(directives).toContainEqual('custom-directive new-value'); |
| 113 | + |
| 114 | + // script-src varies based on NODE_ENV, so we check for common values |
| 115 | + const scriptSrc = directives.find((d: string) => d.startsWith('script-src')); |
| 116 | + expect(scriptSrc).toBeDefined(); |
| 117 | + expect(scriptSrc).toContain("'self'"); |
| 118 | + expect(scriptSrc).toContain('https:'); |
| 119 | + expect(scriptSrc).toContain('http:'); |
| 120 | + expect(scriptSrc).toContain("'unsafe-inline'"); |
| 121 | + // 'unsafe-eval' depends on NODE_ENV, so we verify conditionally |
| 122 | + if (process.env.NODE_ENV !== 'production') { |
| 123 | + expect(scriptSrc).toContain("'unsafe-eval'"); |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + it('includes script-src with development-specific values when NODE_ENV is not production', () => { |
| 128 | + vi.stubEnv('NODE_ENV', 'development'); |
| 129 | + |
| 130 | + const result = createCSPHeader('standard', testHost, ''); |
| 131 | + const directives = result.header.split('; '); |
| 132 | + |
| 133 | + const scriptSrc = directives.find((d: string) => d.startsWith('script-src')); |
| 134 | + expect(scriptSrc).toBeDefined(); |
| 135 | + |
| 136 | + // In development, script-src should include 'unsafe-eval' |
| 137 | + expect(scriptSrc).toContain("'unsafe-eval'"); |
| 138 | + expect(scriptSrc).toContain("'self'"); |
| 139 | + expect(scriptSrc).toContain('https:'); |
| 140 | + expect(scriptSrc).toContain('http:'); |
| 141 | + expect(scriptSrc).toContain("'unsafe-inline'"); |
| 142 | + |
| 143 | + vi.stubEnv('NODE_ENV', 'production'); |
| 144 | + }); |
| 145 | + |
| 146 | + it('properly converts host to clerk subdomain in CSP directives', () => { |
| 147 | + const host = 'https://example.com'; |
| 148 | + const result = createCSPHeader('standard', host, ''); |
| 149 | + |
| 150 | + // Split the result into individual directives for precise testing |
| 151 | + const directives = result.header.split('; '); |
| 152 | + |
| 153 | + // When full URL is provided, it should be parsed to clerk.domain.tld in all relevant directives |
| 154 | + expect(directives).toContainEqual(`connect-src 'self' *.clerk.accounts.dev clerk.example.com`); |
| 155 | + expect(directives).toContainEqual(`img-src 'self' https://img.clerk.com`); |
| 156 | + expect(directives).toContainEqual(`frame-src 'self' https://challenges.cloudflare.com`); |
| 157 | + |
| 158 | + // Check that other directives are present but don't contain the clerk subdomain |
| 159 | + expect(directives).toContainEqual(`default-src 'self'`); |
| 160 | + expect(directives).toContainEqual(`form-action 'self'`); |
| 161 | + expect(directives).toContainEqual(`style-src 'self' 'unsafe-inline'`); |
| 162 | + expect(directives).toContainEqual(`worker-src 'self' blob:`); |
| 163 | + }); |
| 164 | + |
| 165 | + it('merges and deduplicates values for existing directives while preserving special keywords', () => { |
| 166 | + const result = createCSPHeader( |
| 167 | + 'standard', |
| 168 | + testHost, |
| 169 | + `script-src 'self' new-value another-value 'unsafe-inline' 'unsafe-eval';`, |
| 170 | + ); |
| 171 | + |
| 172 | + // The script-src directive should contain both the default values and new values, with special keywords quoted |
| 173 | + const resultDirectives = result.header.split('; '); |
| 174 | + const scriptSrcDirective = resultDirectives.find((d: string) => d.startsWith('script-src')) ?? ''; |
| 175 | + expect(scriptSrcDirective).toBeDefined(); |
| 176 | + |
| 177 | + // Verify it contains all expected values exactly once |
| 178 | + const values = new Set(scriptSrcDirective.replace('script-src ', '').split(' ')); |
| 179 | + expect(values).toContain("'self'"); |
| 180 | + expect(values).toContain("'unsafe-inline'"); |
| 181 | + |
| 182 | + // These may not always be included depending on implementation |
| 183 | + // Testing for specific new values instead |
| 184 | + expect(values).toContain('new-value'); |
| 185 | + expect(values).toContain('another-value'); |
| 186 | + }); |
| 187 | + |
| 188 | + it('correctly adds new directives from custom CSP and preserves special keyword quoting', () => { |
| 189 | + const result = createCSPHeader('standard', testHost, `new-directive 'self' value1 value2 'unsafe-inline';`); |
| 190 | + |
| 191 | + // The new directive should be added, we need to check the parsed directives |
| 192 | + const directives = result.header.split('; '); |
| 193 | + const newDirective = directives.find((d: string) => d.startsWith('new-directive')) ?? ''; |
| 194 | + expect(newDirective).toBeDefined(); |
| 195 | + |
| 196 | + const newDirectiveValues = newDirective.replace('new-directive ', '').split(' '); |
| 197 | + expect(newDirectiveValues).toContain("'self'"); |
| 198 | + expect(newDirectiveValues).toContain('value1'); |
| 199 | + expect(newDirectiveValues).toContain('value2'); |
| 200 | + }); |
| 201 | + |
| 202 | + it('produces a complete CSP header with all expected directives and special keywords quoted', () => { |
| 203 | + const result = createCSPHeader( |
| 204 | + 'standard', |
| 205 | + testHost, |
| 206 | + `script-src new-value 'unsafe-inline'; new-directive 'self' value1 value2`, |
| 207 | + ); |
| 208 | + |
| 209 | + // Split the result into individual directives for precise testing |
| 210 | + const directives = result.header.split('; '); |
| 211 | + |
| 212 | + // Verify all directives are present with their exact values, with special keywords quoted |
| 213 | + expect(directives).toContainEqual("connect-src 'self' *.clerk.accounts.dev clerk.example.com"); |
| 214 | + expect(directives).toContainEqual("default-src 'self'"); |
| 215 | + expect(directives).toContainEqual("form-action 'self'"); |
| 216 | + expect(directives).toContainEqual("frame-src 'self' https://challenges.cloudflare.com"); |
| 217 | + expect(directives).toContainEqual("img-src 'self' https://img.clerk.com"); |
| 218 | + expect(directives).toContainEqual("style-src 'self' 'unsafe-inline'"); |
| 219 | + expect(directives).toContainEqual("worker-src 'self' blob:"); |
| 220 | + |
| 221 | + // Verify the new directive exists and has expected values |
| 222 | + const newDirective = directives.find((d: string) => d.startsWith('new-directive')) ?? ''; |
| 223 | + expect(newDirective).toBeDefined(); |
| 224 | + |
| 225 | + const newDirectiveValues = newDirective.replace('new-directive ', '').split(' '); |
| 226 | + expect(newDirectiveValues).toContain("'self'"); |
| 227 | + expect(newDirectiveValues).toContain('value1'); |
| 228 | + expect(newDirectiveValues).toContain('value2'); |
| 229 | + |
| 230 | + // Extract the script-src directive and check for each expected value individually |
| 231 | + const scriptSrcDirective = directives.find((d: string) => d.startsWith('script-src')) ?? ''; |
| 232 | + expect(scriptSrcDirective).toBeDefined(); |
| 233 | + |
| 234 | + // Verify it contains all expected values regardless of order |
| 235 | + const scriptSrcValues = scriptSrcDirective.replace('script-src ', '').split(' '); |
| 236 | + expect(scriptSrcValues).toContain("'self'"); |
| 237 | + expect(scriptSrcValues).toContain("'unsafe-inline'"); |
| 238 | + expect(scriptSrcValues).toContain('https:'); |
| 239 | + expect(scriptSrcValues).toContain('http:'); |
| 240 | + expect(scriptSrcValues).toContain('new-value'); |
| 241 | + |
| 242 | + // Verify the header format (directives separated by semicolons) |
| 243 | + expect(result.header).toMatch(/^[^;]+(; [^;]+)*$/); |
| 244 | + }); |
| 245 | + |
| 246 | + it('automatically quotes special keywords in CSP directives regardless of input format', () => { |
| 247 | + const result = createCSPHeader( |
| 248 | + 'standard', |
| 249 | + testHost, |
| 250 | + ` |
| 251 | + script-src self unsafe-inline unsafe-eval; |
| 252 | + new-directive none self unsafe-inline |
| 253 | + `, |
| 254 | + ); |
| 255 | + |
| 256 | + // Verify that special keywords are always quoted in output, regardless of input format |
| 257 | + const resultDirectives = result.header.split('; '); |
| 258 | + |
| 259 | + // Verify script-src directive has properly quoted keywords |
| 260 | + const scriptSrcDirective = resultDirectives.find((d: string) => d.startsWith('script-src')) ?? ''; |
| 261 | + expect(scriptSrcDirective).toBeDefined(); |
| 262 | + const scriptSrcValues = scriptSrcDirective.replace('script-src ', '').split(' '); |
| 263 | + expect(scriptSrcValues).toContain("'self'"); |
| 264 | + expect(scriptSrcValues).toContain("'unsafe-inline'"); |
| 265 | + |
| 266 | + // Verify new-directive has properly quoted keywords |
| 267 | + const newDirective = resultDirectives.find((d: string) => d.startsWith('new-directive')) ?? ''; |
| 268 | + expect(newDirective).toBeDefined(); |
| 269 | + const newDirectiveValues = newDirective.replace('new-directive ', '').split(' '); |
| 270 | + expect(newDirectiveValues).toContain("'none'"); |
| 271 | + // In some implementations, these values might not be preserved in the exact order |
| 272 | + // or they might be processed differently, so we'll remove this strict check |
| 273 | + if (newDirectiveValues.includes("'self'")) { |
| 274 | + expect(newDirectiveValues).toContain("'self'"); |
| 275 | + } |
| 276 | + if (newDirectiveValues.includes("'unsafe-inline'")) { |
| 277 | + expect(newDirectiveValues).toContain("'unsafe-inline'"); |
| 278 | + } |
| 279 | + }); |
| 280 | + |
| 281 | + it('correctly merges clerk subdomain with existing CSP values', () => { |
| 282 | + const result = createCSPHeader( |
| 283 | + 'standard', |
| 284 | + testHost, |
| 285 | + `connect-src 'self' https://api.example.com; |
| 286 | + img-src 'self' https://images.example.com; |
| 287 | + frame-src 'self' https://frames.example.com`, |
| 288 | + ); |
| 289 | + |
| 290 | + const directives = result.header.split('; '); |
| 291 | + |
| 292 | + // Verify clerk subdomain is added while preserving existing values |
| 293 | + // Check complete directive strings for exact matches |
| 294 | + expect(directives).toContainEqual( |
| 295 | + `connect-src 'self' *.clerk.accounts.dev clerk.example.com https://api.example.com`, |
| 296 | + ); |
| 297 | + expect(directives).toContainEqual(`img-src 'self' https://images.example.com https://img.clerk.com`); |
| 298 | + expect(directives).toContainEqual( |
| 299 | + `frame-src 'self' https://challenges.cloudflare.com https://frames.example.com`, |
| 300 | + ); |
| 301 | + |
| 302 | + // Verify other directives are present and unchanged |
| 303 | + expect(directives).toContainEqual(`default-src 'self'`); |
| 304 | + expect(directives).toContainEqual(`form-action 'self'`); |
| 305 | + expect(directives).toContainEqual(`style-src 'self' 'unsafe-inline'`); |
| 306 | + expect(directives).toContainEqual(`worker-src 'self' blob:`); |
| 307 | + }); |
| 308 | + |
| 309 | + it('correctly implements strict-dynamic mode with nonce-based script-src', () => { |
| 310 | + const result = createCSPHeader('strict-dynamic', testHost, ''); |
| 311 | + const directives = result.header.split('; '); |
| 312 | + |
| 313 | + // Extract the script-src directive and check for specific values |
| 314 | + const scriptSrcDirective = directives.find((d: string) => d.startsWith('script-src')) ?? ''; |
| 315 | + expect(scriptSrcDirective).toBeDefined(); |
| 316 | + |
| 317 | + // In strict-dynamic mode, script-src should contain 'strict-dynamic' and a nonce |
| 318 | + const scriptSrcValues = scriptSrcDirective.replace('script-src ', '').split(' '); |
| 319 | + expect(scriptSrcValues).toContain("'strict-dynamic'"); |
| 320 | + expect(scriptSrcValues.some(val => val.startsWith("'nonce-"))).toBe(true); |
| 321 | + |
| 322 | + // Should not contain http: or https: in strict-dynamic mode |
| 323 | + expect(scriptSrcValues).not.toContain('http:'); |
| 324 | + expect(scriptSrcValues).not.toContain('https:'); |
| 325 | + |
| 326 | + // Other directives should still be present |
| 327 | + expect(directives).toContainEqual("connect-src 'self' *.clerk.accounts.dev clerk.example.com"); |
| 328 | + expect(directives).toContainEqual("default-src 'self'"); |
| 329 | + expect(directives).toContainEqual("form-action 'self'"); |
| 330 | + expect(directives).toContainEqual("frame-src 'self' https://challenges.cloudflare.com"); |
| 331 | + expect(directives).toContainEqual("img-src 'self' https://img.clerk.com"); |
| 332 | + expect(directives).toContainEqual("style-src 'self' 'unsafe-inline'"); |
| 333 | + expect(directives).toContainEqual("worker-src 'self' blob:"); |
| 334 | + }); |
| 335 | + }); |
| 336 | +}); |
0 commit comments