f75a9abecc
- logout 使用站内相对 Location,避免跳到 0.0.0.0 监听地址 - 校验站内返回路径并覆盖正式域名、本地地址及外部跳转变体
20 lines
913 B
TypeScript
20 lines
913 B
TypeScript
/** 登出后的站内跳转,避免反向代理后的内部监听地址泄漏到浏览器。 */
|
|
|
|
/** 使用相对 Location 留在当前站点,并拒绝外部地址和 URL 控制字符。 */
|
|
export function logoutRedirect(next: string | null) {
|
|
let destination = "/login";
|
|
if (
|
|
next?.startsWith("/") &&
|
|
!/^\/[/\\]/.test(next) &&
|
|
!/[\\\u0000-\u0020\u007f]/.test(next)
|
|
) {
|
|
const local = new URL(next, "https://same-origin.invalid");
|
|
// 消除点路径后仍须禁止双斜杠,避免 /.//host 被规范化为外站地址。
|
|
if (local.origin === "https://same-origin.invalid" && !local.pathname.startsWith("//")) {
|
|
destination = local.pathname + local.search + local.hash;
|
|
}
|
|
}
|
|
// Route Handler 可直接返回相对 Location;不使用 request.url(生产中可能是 0.0.0.0)。
|
|
return new Response(null, { status: 307, headers: { Location: destination } });
|
|
}
|