Merge pull request 'fix(auth): 修复会话过期跳转内部地址' (#11) from feature/env-pipeline-split into master
This commit is contained in:
@@ -138,3 +138,9 @@ pnpm start:prod
|
|||||||
内部滚动区域统一隐藏原生轨道,保留触摸/键盘滚动,设定表单关闭横向溢出。风格控制提示使用只读 `GET /api/digital/conversations/:id/style-policy`,打开设定不会写入实验分组、决策或曝光。
|
内部滚动区域统一隐藏原生轨道,保留触摸/键盘滚动,设定表单关闭横向溢出。风格控制提示使用只读 `GET /api/digital/conversations/:id/style-policy`,打开设定不会写入实验分组、决策或曝光。
|
||||||
|
|
||||||
实验决策支持 A~Z 动态组;未入组时组别为 null。界面消费 `decision.value`(`new` / `true` 为新版),组名仅用于归因。决策过期使用服务端的 `fallbackValue`,不将无实验用户伪装成 A 组;旧服务未返回value时保留既有兼容路径。
|
实验决策支持 A~Z 动态组;未入组时组别为 null。界面消费 `decision.value`(`new` / `true` 为新版),组名仅用于归因。决策过期使用服务端的 `fallbackValue`,不将无实验用户伪装成 A 组;旧服务未返回value时保留既有兼容路径。
|
||||||
|
|
||||||
|
## 登录跳转回归
|
||||||
|
|
||||||
|
- `src/lib/auth-redirect.ts`:登出与会话过期后使用站内相对 `Location`,避免跳到 Next.js 内部监听地址 `0.0.0.0:3210`;仅允许站内返回路径。
|
||||||
|
- `tests/auth-redirect.test.mjs`:覆盖正式域名/本地地址的登录页跳转、站内参数和外部地址拦截。
|
||||||
|
- 执行:`node --experimental-strip-types --test tests/auth-redirect.test.mjs`(Node.js 22)。
|
||||||
|
|||||||
@@ -1,15 +1,17 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
/** 清理会话后返回当前站点,保持反向代理域名与本地访问地址。 */
|
||||||
|
import { NextRequest } from "next/server";
|
||||||
|
import { logoutRedirect } from "@/lib/auth-redirect";
|
||||||
|
|
||||||
import { clearSessionToken } from "@/lib/session";
|
import { clearSessionToken } from "@/lib/session";
|
||||||
|
|
||||||
|
/** 客户端主动退出时仅清理 Cookie,由页面处理导航。 */
|
||||||
export async function POST() {
|
export async function POST() {
|
||||||
await clearSessionToken();
|
await clearSessionToken();
|
||||||
return Response.json({ success: true, data: null });
|
return Response.json({ success: true, data: null });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 会话过期或链接退出后跳转站内页面,不使用服务端监听地址。 */
|
||||||
export async function GET(request: NextRequest) {
|
export async function GET(request: NextRequest) {
|
||||||
await clearSessionToken();
|
await clearSessionToken();
|
||||||
const next = request.nextUrl.searchParams.get("next") || "/login";
|
return logoutRedirect(request.nextUrl.searchParams.get("next"));
|
||||||
|
|
||||||
return NextResponse.redirect(new URL(next, request.url));
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
/** 登出后的站内跳转,避免反向代理后的内部监听地址泄漏到浏览器。 */
|
||||||
|
|
||||||
|
/** 使用相对 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 } });
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
/** 回归登录过期后的跳转:仅返回站内路径,不把服务监听地址发送给浏览器。 */
|
||||||
|
import test from 'node:test';
|
||||||
|
import assert from 'node:assert/strict';
|
||||||
|
import { logoutRedirect } from '../src/lib/auth-redirect.ts';
|
||||||
|
|
||||||
|
test('登录过期后跳回当前站点的登录页,不依赖服务内部地址', () => {
|
||||||
|
const response = logoutRedirect(null);
|
||||||
|
assert.equal(response.status, 307);
|
||||||
|
assert.equal(response.headers.get('location'), '/login');
|
||||||
|
assert.equal(new URL(response.headers.get('location'), 'https://mrzhan.top/api/auth/logout').href, 'https://mrzhan.top/login');
|
||||||
|
assert.equal(new URL(response.headers.get('location'), 'http://localhost:3210/api/auth/logout').href, 'http://localhost:3210/login');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('保留站内路径和查询参数', () => {
|
||||||
|
assert.equal(logoutRedirect('/login?reason=expired').headers.get('location'), '/login?reason=expired');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('拒绝外部链接、协议相对地址及反斜杠变体', () => {
|
||||||
|
for (const next of ['https://example.com', '//example.com', '/\\example.com', 'javascript:alert(1)', '\n//example.com', '/\n/example.com', '/.//example.com', '/%2e//example.com']) {
|
||||||
|
assert.equal(logoutRedirect(next).headers.get('location'), '/login', next);
|
||||||
|
}
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user