// Sanitizes a user-supplied `next` redirect target so it can only ever // point at a path on our own origin. Without this, `new URL(next, base)` // lets an absolute ("https://evil.com") or protocol-relative ("//evil.com") // value override the base — an open-redirect / phishing vector on the // post-auth redirect routes. export function safeNext(raw: string | null | undefined, fallback = '/orders'): string { if (!raw) return fallback; // Must be a same-origin absolute path: a single leading slash, and not // a scheme-relative ("//host") or backslash ("/\host") form that a // browser would resolve to a different origin. if (!raw.startsWith('/')) return fallback; if (raw.startsWith('//') || raw.includes('\\') || /[\u0000-\u001f\u007f]/.test(raw)) return fallback; return raw; }