/** 回归登录过期后的跳转:仅返回站内路径,不把服务监听地址发送给浏览器。 */ 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); } });