gongxiangp2/lib/safe-redirect.ts
2026-07-06 13:22:04 +08:00

26 lines
801 B
TypeScript

export function normalizeInternalRedirectPath(value: unknown, fallback = '/') {
const raw = String(value ?? '').trim()
if (!raw) return fallback
if (!raw.startsWith('/') || raw.startsWith('//')) {
return fallback
}
try {
const parsed = new URL(raw, 'https://internal.local')
return `${parsed.pathname}${parsed.search}${parsed.hash}`
} catch {
return fallback
}
}
export function buildDingtalkAutoLoginPath(redirectPath: unknown) {
const target = normalizeInternalRedirectPath(redirectPath)
return `/login?dingtalk_redirect=true&redirect=${encodeURIComponent(target)}`
}
export function buildDingtalkEntryPath(redirectPath: unknown) {
const target = normalizeInternalRedirectPath(redirectPath)
return `/dingtalk-entry?redirect=${encodeURIComponent(target)}`
}