export type AllowedOriginsValidationError = | { code: 'required' } | { code: 'wildcard_prod' } | { code: 'invalid'; origin: string } /** Mirrors backend validateAllowedOrigins in internal/handler/embed_channel.go */ export function parseAllowedOrigins(text: string): string[] { return text .split('\n') .map((line) => line.trim()) .filter(Boolean) } export function validateAllowedOrigins( origins: string[], prod = import.meta.env?.PROD ?? false, ): { ok: true; origins: string[] } | { ok: false; error: AllowedOriginsValidationError } { const cleaned = parseAllowedOrigins(origins.join('\n')) if (cleaned.length === 0) { return { ok: false, error: { code: 'required' } } } for (const origin of cleaned) { if (origin === '*') { if (prod) { return { ok: false, error: { code: 'wildcard_prod' } } } continue } let host = origin if (origin.startsWith('*.')) { host = `https://${origin.slice(2)}` } try { if (!/^https?:\/\/[^/?#\\\s]+\/?$/i.test(host)) { return { ok: false, error: { code: 'invalid', origin } } } const url = new URL(host) if ((url.protocol !== 'http:' && url.protocol !== 'https:') || !url.host || url.username || url.password || (url.pathname !== '' && url.pathname !== '/') || host.includes('?') || host.includes('#') || /[\s'";,*\\]/.test(url.host)) { return { ok: false, error: { code: 'invalid', origin } } } } catch { return { ok: false, error: { code: 'invalid', origin } } } } return { ok: true, origins: cleaned } }