|
| 1 | +import { env } from '../env' |
| 2 | + |
| 3 | +/** |
| 4 | + * Content Security Policy (CSP) configuration builder |
| 5 | + * This creates a more maintainable and readable CSP configuration |
| 6 | + */ |
| 7 | + |
| 8 | +export interface CSPDirectives { |
| 9 | + 'default-src'?: string[] |
| 10 | + 'script-src'?: string[] |
| 11 | + 'style-src'?: string[] |
| 12 | + 'img-src'?: string[] |
| 13 | + 'media-src'?: string[] |
| 14 | + 'font-src'?: string[] |
| 15 | + 'connect-src'?: string[] |
| 16 | + 'frame-src'?: string[] |
| 17 | + 'frame-ancestors'?: string[] |
| 18 | + 'form-action'?: string[] |
| 19 | + 'base-uri'?: string[] |
| 20 | + 'object-src'?: string[] |
| 21 | +} |
| 22 | + |
| 23 | +export const cspDirectives: CSPDirectives = { |
| 24 | + 'default-src': ["'self'"], |
| 25 | + |
| 26 | + 'script-src': [ |
| 27 | + "'self'", |
| 28 | + "'unsafe-inline'", |
| 29 | + "'unsafe-eval'", |
| 30 | + 'https://*.google.com', |
| 31 | + 'https://apis.google.com', |
| 32 | + 'https://*.vercel-scripts.com', |
| 33 | + 'https://*.vercel-insights.com', |
| 34 | + 'https://vercel.live', |
| 35 | + 'https://*.vercel.live', |
| 36 | + 'https://vercel.com', |
| 37 | + 'https://*.vercel.app', |
| 38 | + 'https://vitals.vercel-insights.com', |
| 39 | + ], |
| 40 | + |
| 41 | + 'style-src': ["'self'", "'unsafe-inline'", 'https://fonts.googleapis.com'], |
| 42 | + |
| 43 | + 'img-src': [ |
| 44 | + "'self'", |
| 45 | + 'data:', |
| 46 | + 'blob:', |
| 47 | + 'https://*.googleusercontent.com', |
| 48 | + 'https://*.google.com', |
| 49 | + 'https://*.atlassian.com', |
| 50 | + 'https://cdn.discordapp.com', |
| 51 | + 'https://*.githubusercontent.com', |
| 52 | + ], |
| 53 | + |
| 54 | + 'media-src': ["'self'", 'blob:'], |
| 55 | + |
| 56 | + 'font-src': ["'self'", 'https://fonts.gstatic.com'], |
| 57 | + |
| 58 | + 'connect-src': [ |
| 59 | + "'self'", |
| 60 | + env.NEXT_PUBLIC_APP_URL || '', |
| 61 | + env.OLLAMA_URL || 'http://localhost:11434', |
| 62 | + env.NEXT_PUBLIC_SOCKET_URL || 'http://localhost:3002', |
| 63 | + env.NEXT_PUBLIC_SOCKET_URL?.replace('http://', 'ws://').replace('https://', 'wss://') || |
| 64 | + 'ws://localhost:3002', |
| 65 | + 'https://*.up.railway.app', |
| 66 | + 'wss://*.up.railway.app', |
| 67 | + 'https://api.browser-use.com', |
| 68 | + 'https://api.exa.ai', |
| 69 | + 'https://api.firecrawl.dev', |
| 70 | + 'https://*.googleapis.com', |
| 71 | + 'https://*.amazonaws.com', |
| 72 | + 'https://*.s3.amazonaws.com', |
| 73 | + 'https://*.blob.core.windows.net', |
| 74 | + 'https://*.vercel-insights.com', |
| 75 | + 'https://vitals.vercel-insights.com', |
| 76 | + 'https://*.atlassian.com', |
| 77 | + 'https://*.supabase.co', |
| 78 | + 'https://vercel.live', |
| 79 | + 'https://*.vercel.live', |
| 80 | + 'https://vercel.com', |
| 81 | + 'https://*.vercel.app', |
| 82 | + 'wss://*.vercel.app', |
| 83 | + ], |
| 84 | + |
| 85 | + // Google Picker and Drive integration |
| 86 | + 'frame-src': [ |
| 87 | + 'https://drive.google.com', |
| 88 | + 'https://docs.google.com', // Required for Google Picker |
| 89 | + 'https://*.google.com', |
| 90 | + ], |
| 91 | + |
| 92 | + 'frame-ancestors': ["'self'"], |
| 93 | + 'form-action': ["'self'"], |
| 94 | + 'base-uri': ["'self'"], |
| 95 | + 'object-src': ["'none'"], |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Build CSP string from directives object |
| 100 | + */ |
| 101 | +export function buildCSPString(directives: CSPDirectives): string { |
| 102 | + return Object.entries(directives) |
| 103 | + .map(([directive, sources]) => { |
| 104 | + if (!sources || sources.length === 0) return '' |
| 105 | + // Filter out empty strings |
| 106 | + const validSources = sources.filter((source: string) => source && source.trim() !== '') |
| 107 | + if (validSources.length === 0) return '' |
| 108 | + return `${directive} ${validSources.join(' ')}` |
| 109 | + }) |
| 110 | + .filter(Boolean) |
| 111 | + .join('; ') |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Get the main CSP policy string |
| 116 | + */ |
| 117 | +export function getMainCSPPolicy(): string { |
| 118 | + return buildCSPString(cspDirectives) |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * Permissive CSP for workflow execution endpoints |
| 123 | + */ |
| 124 | +export function getWorkflowExecutionCSPPolicy(): string { |
| 125 | + return "default-src * 'unsafe-inline' 'unsafe-eval'; connect-src *;" |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Add a source to a specific directive |
| 130 | + */ |
| 131 | +export function addCSPSource(directive: keyof CSPDirectives, source: string): void { |
| 132 | + if (!cspDirectives[directive]) { |
| 133 | + cspDirectives[directive] = [] |
| 134 | + } |
| 135 | + if (!cspDirectives[directive]!.includes(source)) { |
| 136 | + cspDirectives[directive]!.push(source) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * Remove a source from a specific directive |
| 142 | + */ |
| 143 | +export function removeCSPSource(directive: keyof CSPDirectives, source: string): void { |
| 144 | + if (cspDirectives[directive]) { |
| 145 | + cspDirectives[directive] = cspDirectives[directive]!.filter((s: string) => s !== source) |
| 146 | + } |
| 147 | +} |
0 commit comments