|
| 1 | +import type { ProviderName } from "./_types.ts"; |
| 2 | + |
| 3 | +export type CspDirectiveKey = |
| 4 | + | "script-src" |
| 5 | + | "connect-src" |
| 6 | + | "frame-src" |
| 7 | + | "style-src" |
| 8 | + | "img-src" |
| 9 | + | "font-src" |
| 10 | + | "media-src"; |
| 11 | + |
| 12 | +export type CspDirectives = Record<CspDirectiveKey, readonly string[]>; |
| 13 | + |
| 14 | +const EMPTY: CspDirectives = { |
| 15 | + "script-src": [], |
| 16 | + "connect-src": [], |
| 17 | + "frame-src": [], |
| 18 | + "style-src": [], |
| 19 | + "img-src": [], |
| 20 | + "font-src": [], |
| 21 | + "media-src": [], |
| 22 | +}; |
| 23 | + |
| 24 | +const CATALOG: Partial<Record<ProviderName, CspDirectives>> = { |
| 25 | + intercom: { |
| 26 | + "script-src": ["https://widget.intercom.io", "https://js.intercomcdn.com"], |
| 27 | + "connect-src": [ |
| 28 | + "https://api-iam.intercom.io", |
| 29 | + "https://api-ping.intercom.io", |
| 30 | + "https://nexus-websocket-a.intercom.io", |
| 31 | + "https://nexus-websocket-b.intercom.io", |
| 32 | + "wss://nexus-websocket-a.intercom.io", |
| 33 | + "wss://nexus-websocket-b.intercom.io", |
| 34 | + "https://uploads.intercomcdn.com", |
| 35 | + "https://uploads.intercomusercontent.com", |
| 36 | + ], |
| 37 | + "frame-src": ["https://intercom-sheets.com", "https://www.intercom-reporting.com"], |
| 38 | + "style-src": ["https://fonts.intercomcdn.com"], |
| 39 | + "img-src": ["https://js.intercomcdn.com", "https://static.intercomassets.com"], |
| 40 | + "font-src": ["https://fonts.intercomcdn.com"], |
| 41 | + "media-src": ["https://js.intercomcdn.com"], |
| 42 | + }, |
| 43 | + crisp: { |
| 44 | + "script-src": ["https://client.crisp.chat"], |
| 45 | + "connect-src": [ |
| 46 | + "https://client.crisp.chat", |
| 47 | + "https://client.relay.crisp.chat", |
| 48 | + "wss://client.relay.crisp.chat", |
| 49 | + "https://storage.crisp.chat", |
| 50 | + ], |
| 51 | + "frame-src": ["https://game.crisp.chat"], |
| 52 | + "style-src": ["https://client.crisp.chat"], |
| 53 | + "img-src": [ |
| 54 | + "https://client.crisp.chat", |
| 55 | + "https://image.crisp.chat", |
| 56 | + "https://storage.crisp.chat", |
| 57 | + ], |
| 58 | + "font-src": ["https://client.crisp.chat"], |
| 59 | + "media-src": ["https://client.crisp.chat"], |
| 60 | + }, |
| 61 | + tawk: { |
| 62 | + "script-src": ["https://embed.tawk.to", "https://*.tawk.to"], |
| 63 | + "connect-src": ["https://*.tawk.to", "wss://*.tawk.to"], |
| 64 | + "frame-src": ["https://*.tawk.to"], |
| 65 | + "style-src": ["https://embed.tawk.to"], |
| 66 | + "img-src": ["https://*.tawk.to", "https://*.tawkcdn.com"], |
| 67 | + "font-src": ["https://embed.tawk.to"], |
| 68 | + "media-src": ["https://*.tawkcdn.com"], |
| 69 | + }, |
| 70 | + zendesk: { |
| 71 | + "script-src": ["https://static.zdassets.com", "https://ekr.zdassets.com"], |
| 72 | + "connect-src": [ |
| 73 | + "https://*.zendesk.com", |
| 74 | + "https://*.zopim.com", |
| 75 | + "https://ekr.zdassets.com", |
| 76 | + "https://static.zdassets.com", |
| 77 | + "wss://widget-mediator.zopim.com", |
| 78 | + "wss://*.zopim.com", |
| 79 | + ], |
| 80 | + "frame-src": ["https://*.zendesk.com", "https://*.zopim.com"], |
| 81 | + "style-src": ["https://static.zdassets.com"], |
| 82 | + "img-src": ["https://*.zdassets.com", "https://*.zendesk.com", "https://*.zopim.com"], |
| 83 | + "font-src": ["https://static.zdassets.com"], |
| 84 | + "media-src": ["https://*.zendesk.com"], |
| 85 | + }, |
| 86 | + hubspot: { |
| 87 | + "script-src": [ |
| 88 | + "https://js.hs-scripts.com", |
| 89 | + "https://js-eu1.hs-scripts.com", |
| 90 | + "https://js.hs-analytics.net", |
| 91 | + "https://js.hs-banner.com", |
| 92 | + "https://js.usemessages.com", |
| 93 | + "https://js.hsforms.net", |
| 94 | + "https://js.hs-scripts.com", |
| 95 | + "https://js.hubspot.com", |
| 96 | + ], |
| 97 | + "connect-src": [ |
| 98 | + "https://*.hubspot.com", |
| 99 | + "https://*.hubapi.com", |
| 100 | + "https://*.hs-analytics.net", |
| 101 | + "https://api.hubspot.com", |
| 102 | + "https://api.hubapi.com", |
| 103 | + "wss://*.hubspot.com", |
| 104 | + ], |
| 105 | + "frame-src": ["https://app.hubspot.com", "https://*.hubspot.com"], |
| 106 | + "style-src": ["https://*.hubspot.com", "https://*.hsforms.net"], |
| 107 | + "img-src": ["https://*.hubspot.com", "https://*.hs-analytics.net", "https://track.hubspot.com"], |
| 108 | + "font-src": ["https://*.hubspot.com", "https://fonts.hubspot.com"], |
| 109 | + "media-src": ["https://*.hubspot.com"], |
| 110 | + }, |
| 111 | + chatwoot: { |
| 112 | + "script-src": [], |
| 113 | + "connect-src": [], |
| 114 | + "frame-src": [], |
| 115 | + "style-src": [], |
| 116 | + "img-src": [], |
| 117 | + "font-src": [], |
| 118 | + "media-src": [], |
| 119 | + }, |
| 120 | +}; |
| 121 | + |
| 122 | +export interface CspOptions { |
| 123 | + /** Include 'self' in each directive. Default: true. */ |
| 124 | + includeSelf?: boolean; |
| 125 | + /** Override Chatwoot host for self-hosted directives. */ |
| 126 | + chatwootBaseUrl?: string; |
| 127 | +} |
| 128 | + |
| 129 | +export function cspDirectives(provider: ProviderName, options?: CspOptions): CspDirectives { |
| 130 | + const includeSelf = options?.includeSelf ?? true; |
| 131 | + const base = CATALOG[provider] ?? EMPTY; |
| 132 | + |
| 133 | + if (provider === "chatwoot") { |
| 134 | + const baseUrl = options?.chatwootBaseUrl?.replace(/\/+$/, "") ?? "https://app.chatwoot.com"; |
| 135 | + const host = new URL(baseUrl).host; |
| 136 | + const wss = `wss://${host}/cable`; |
| 137 | + const directives: CspDirectives = { |
| 138 | + "script-src": [baseUrl], |
| 139 | + "connect-src": [baseUrl, wss], |
| 140 | + "frame-src": [baseUrl], |
| 141 | + "style-src": [baseUrl], |
| 142 | + "img-src": [baseUrl], |
| 143 | + "font-src": [baseUrl], |
| 144 | + "media-src": [baseUrl], |
| 145 | + }; |
| 146 | + return withSelf(directives, includeSelf); |
| 147 | + } |
| 148 | + |
| 149 | + return withSelf(base, includeSelf); |
| 150 | +} |
| 151 | + |
| 152 | +function withSelf(directives: CspDirectives, includeSelf: boolean): CspDirectives { |
| 153 | + if (!includeSelf) return directives; |
| 154 | + const out = {} as CspDirectives; |
| 155 | + for (const key of Object.keys(directives) as CspDirectiveKey[]) { |
| 156 | + out[key] = ["'self'", ...directives[key]]; |
| 157 | + } |
| 158 | + return out; |
| 159 | +} |
| 160 | + |
| 161 | +export function toHeaderString(directives: CspDirectives): string { |
| 162 | + const parts: string[] = []; |
| 163 | + for (const key of Object.keys(directives) as CspDirectiveKey[]) { |
| 164 | + const values = directives[key]; |
| 165 | + if (values.length > 0) parts.push(`${key} ${values.join(" ")}`); |
| 166 | + } |
| 167 | + return parts.join("; "); |
| 168 | +} |
| 169 | + |
| 170 | +export function mergeCsp(...sets: CspDirectives[]): CspDirectives { |
| 171 | + const out = {} as CspDirectives; |
| 172 | + const allKeys: CspDirectiveKey[] = [ |
| 173 | + "script-src", |
| 174 | + "connect-src", |
| 175 | + "frame-src", |
| 176 | + "style-src", |
| 177 | + "img-src", |
| 178 | + "font-src", |
| 179 | + "media-src", |
| 180 | + ]; |
| 181 | + for (const key of allKeys) { |
| 182 | + const merged = new Set<string>(); |
| 183 | + for (const set of sets) for (const v of set[key]) merged.add(v); |
| 184 | + out[key] = [...merged]; |
| 185 | + } |
| 186 | + return out; |
| 187 | +} |
| 188 | + |
| 189 | +export function watchCspViolations( |
| 190 | + handler: (event: SecurityPolicyViolationEvent) => void, |
| 191 | +): () => void { |
| 192 | + if (typeof window === "undefined") return () => {}; |
| 193 | + const cast = handler as () => void; |
| 194 | + window.addEventListener?.("securitypolicyviolation", cast); |
| 195 | + return () => window?.removeEventListener?.("securitypolicyviolation", cast); |
| 196 | +} |
| 197 | + |
| 198 | +interface SecurityPolicyViolationEvent { |
| 199 | + blockedURI: string; |
| 200 | + violatedDirective: string; |
| 201 | + effectiveDirective: string; |
| 202 | + originalPolicy: string; |
| 203 | +} |
0 commit comments