未认证访问登录后误进首页:302 /login 带 next=originalUrl,登录成功/token静默恢复/bfcache僵尸页踢回均按 next 回跳原页(仅认同源/路径,拦//与反斜杠防开放重定向);前端 core/files 的 401 跳登录同样带上当前页

This commit is contained in:
2026-09-26 10:13:45 +08:00
parent 6ff1646c14
commit 9892e48cbe
4 changed files with 25 additions and 8 deletions
+11 -2
View File
@@ -79,6 +79,13 @@ function isPublicPath(p) {
return false;
}
// 登录成功后的回跳目标:只允许本站绝对路径,拦掉 //evil.com(协议相对)与反斜杠混淆,防开放重定向。
function safeNextUrl(u) {
const s = String(u || '');
if (!s.startsWith('/') || s.startsWith('//') || s.includes('\\')) return '/';
return s;
}
// 带签名 token 的临时下载链接(files.js 自己验签),此处放行由路由层拒绝伪造请求。
// URL 形态都要认:/files/download?path=…、/files/download-name/<名字>?path=…,
// 及打包下载 /files/zip?paths=…、/files/zip-name/<名字>?paths=…;
@@ -203,7 +210,8 @@ function createAuth(cfg) {
// ----- 登录页 / 登出跳转 -----
router.get('/login', (req, res) => {
if (sessionUser(req)) return res.redirect('/');
// 已登录直接放行:优先回跳 next 指定的原页面(中间件在 302 来 /login 时带上的)
if (sessionUser(req)) return res.redirect(safeNextUrl(req.query.next));
res.sendFile(path.join(__dirname, 'public', 'login.html'));
});
@@ -219,7 +227,8 @@ function createAuth(cfg) {
if (user) { req.user = user; return next(); }
if (isPublicPath(req.path) || isSignedDownload(req)) return next();
if (req.path.startsWith('/api/')) return res.status(401).json({ error: '未登录' });
return res.redirect('/login');
// 带上原始 URL(含 query),登录页成功后回跳,不再一律进首页
return res.redirect('/login?next=' + encodeURIComponent(req.originalUrl));
};
return { enabled: true, middleware, router };
+2 -1
View File
@@ -129,7 +129,8 @@
}
throw e;
}
if (res.status === 401) { location.href = '/login'; throw new Error('未登录'); }
// 运行中登录态失效:带上当前页 URL,重新登录后原页返回
if (res.status === 401) { location.href = '/login?next=' + encodeURIComponent(location.pathname + location.search); throw new Error('未登录'); }
if (!res.ok) {
let msg = 'HTTP ' + res.status;
try { msg = (await res.json()).error || msg; } catch (_) {}
+1 -1
View File
@@ -496,7 +496,7 @@
async function previewInFs(name) {
try {
const r = await fetch('/api/sessions/' + state.sessionId + '/files/download?path=' + encodeURIComponent(name));
if (r.status === 401) { location.href = '/login'; return; }
if (r.status === 401) { location.href = '/login?next=' + encodeURIComponent(location.pathname + location.search); return; }
if (!r.ok) { let msg = 'HTTP ' + r.status; try { msg = (await r.json()).error || msg; } catch (_) {} throw new Error(msg); }
if (/\.html?$/i.test(name)) {
const html = await r.text();
+11 -4
View File
@@ -42,11 +42,18 @@
<script>
'use strict';
const TOKEN_KEY = 'agent_token';
// 未认证访问页面时,服务端 302 到 /login?next=<原始 URL>;登录/恢复成功后回跳该页。
// 只认本站绝对路径,拦掉 //evil.com 与反斜杠混淆,防开放重定向;无/非法则回首页。
function redirectTarget() {
const n = new URLSearchParams(location.search).get('next') || '';
if (!n.startsWith('/') || n.startsWith('//') || n.indexOf('\\') !== -1) return '/';
return n;
}
// bfcache(前进/后退缓存)恢复会绕过服务端“已登录访问 /login → 302 回主页”的判定,
// 侧滑返回可能直接复活旧登录表单。能走到这里说明 /login 是历史残留条目,立刻踢回主页;
// 侧滑返回可能直接复活旧登录表单。能走到这里说明 /login 是历史残留条目,立刻踢回目标页;
// 若其实已登出,服务端会再 302 回 /login,拿到的也是新鲜表单而非僵尸页。
window.addEventListener('pageshow', e => {
if (e.persisted) location.replace('/');
if (e.persisted) location.replace(redirectTarget());
});
// WebView 封装 APK 杀进程后 HttpOnly cookie 可能没落盘,但 localStorage 还在:
// 登录成功后把 token 另存一份,下次进来先尝试用旧 token 静默恢复登录态。
@@ -65,7 +72,7 @@
if (res.ok) {
const j = await res.json().catch(() => ({}));
if (j && j.token) localStorage.setItem(TOKEN_KEY, j.token); // 续期 token
location.replace('/');
location.replace(redirectTarget());
return;
}
} catch (_) { /* 网络错误交给表单正常登录 */ }
@@ -100,7 +107,7 @@
// 不再往 sessionStorage 写明文口令——Basic 头与 cookie 作用域不同,会引发多标签页身份分裂
localStorage.setItem(TOKEN_KEY, j.token);
}
location.replace('/'); // 替换而非压栈:历史里不留 /login 条目,登录后侧滑不会退回登录页
location.replace(redirectTarget()); // 替换而非压栈:历史里不留 /login 条目,登录后侧滑不会退回登录页
} catch (e2) {
errEl.textContent = e2.message || '登录失败';
btn.disabled = false;