(origin: string | undefined, port: number)
| 174 | * the literal `null` origin (sandboxed iframe, `file://` page) is refused. |
| 175 | */ |
| 176 | export function isAllowedOrigin(origin: string | undefined, port: number): boolean { |
| 177 | if (origin === undefined) return true; |
| 178 | const trimmed = origin.trim(); |
| 179 | if (trimmed === '') return true; |
| 180 | if (trimmed === 'null') return false; |
| 181 | |
| 182 | let url: URL; |
| 183 | try { |
| 184 | url = new URL(trimmed); |
| 185 | } catch { |
| 186 | return false; |
| 187 | } |
| 188 | if (url.protocol !== 'http:' && url.protocol !== 'https:') return false; |
| 189 | // WHATWG keeps IPv6 hostnames bracketed; the allowlist stores them bare. |
| 190 | const hostname = url.hostname.replace(/^\[/, '').replace(/\]$/, '').toLowerCase(); |
| 191 | if (!LOOPBACK_HOSTNAMES.has(hostname)) return false; |
| 192 | return url.port === '' || Number(url.port) === port; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Whether a raw request path is worth resolving at all. |
no test coverage detected