// post-login targets are attacker-controlled (login links, stored pending // state), so resolve against an opaque origin and only accept paths that // stay on it. also covers network-path forms like "/\evil.com" that the url // parser turns into a foreign host const SAFE_BASE = "https://return-to.invalid"; export const safeReturnTo = (value: unknown): string => { if (typeof value !== "string" || value.trim() === "") return "/"; let resolved: URL; try { resolved = new URL(value, SAFE_BASE); } catch { return "/"; } if (resolved.origin !== SAFE_BASE) return "/"; return `${resolved.pathname}${resolved.search}${resolved.hash}`; };