fix: 登录页访问不再记成「登录成功」——认证端点只记录 POST,登录成败以响应确认为准

现场反馈:审计列表「没有失败,全是成功,登录失败也显示成功」。在 Halo 2.26.1 上
复现(afda8d0 构建):GET /login、GET /login?authentication_required 只是打开登录页
(HTTP 200),却被记成「未知用户 · 登录 200」,列表里看起来全是登录成功;POST /login
本身的成败判定是对的,但被这些记录淹没。

- 捕获:AuthEndpointAuditWebFilter 只记录 POST /login(登录尝试)与 POST /logout
  (登出);登录/登出页面的 GET 访问(打开登录页、被弹回登录页、失败回显、登出
  确认页)原样透传不记录(CaptureRule.isAuthOperation)
- 判定:LoginOutcome 只有响应明确确认才算成功——302 跳回登录页(含 error 参数或
  路径即 /login)、缺失 Location、4xx/5xx 一律判失败;移除失败回显页判定
- 结果语义:新增 AuditResult(成功 / 失败 / 页面访问),结果筛选、CSV 导出与前端
  resultBadge 共用同一规则;登录成功显示「成功」,与「失败」对称
- 历史数据:升级前落库的登录页访问记录(GET,标注 login/logout)展示与导出为
  「访问登录页/登出页」且不计成败,结果筛选与「登录/登出」语义动作筛选均排除;
  落库标注不改写
- demo 同步同一套规则;README 与设计/技术文档同步

测试:后端 286、UI 74、demo 61 全过;真实 Halo 2.26.1 升级场景联调记录见测试报告
This commit is contained in:
2026-12-05 13:24:26 +08:00
parent afda8d04fd
commit fc7984291f
35 changed files with 1307 additions and 437 deletions
+5 -3
View File
@@ -423,15 +423,17 @@
if (!log) { return; }
state.detailId = id;
var changes = log.changes || [];
var badge = C.resultBadge(log);
// 结果与列表徽标、筛选、CSV 共用同一语义(AuditResult):登录失败靠标注识别,
// 页面访问不计成败——详情写结果语义文字,原始状态码另列
var outcome = C.resultOutcome(log);
var details =
detailRow('操作者', avatar(log.username) + ' ' + esc(C.displayUsername(log.username))) +
(log.attemptedUsername ? detailRow('尝试用户', '<span class="mono">' + esc(log.attemptedUsername) + '</span>') : '') +
detailRow('描述', esc(C.describeOperation(log))) +
detailRow('动作', methodBadge(log.method)) +
detailRow('资源', esc(log.resourceType) + (log.resourceName ? ' / ' + esc(log.resourceName) : ' <span class="muted">(集合级操作)</span>')) +
detailRow('结果', '<span class="status-badge ' + badge.class + '">' + esc(badge.text) + '</span>' +
detailRow('结果', '<span class="status-badge ' + C.resultBadge(log).class + '">' +
esc(C.RESULT_LABELS[outcome]) + '</span>' +
' <span class="muted">状态码 ' + esc(log.statusCode) + '</span>') +
detailRow('路径', '<code class="path-code">' + esc(log.path) + '</code>') +
detailRow('时间', esc(C.formatTime(log.timestamp))) +
+160 -57
View File
@@ -2,10 +2,13 @@
* 操作审计 · 核心纯函数(浏览器与 Node 测试共用)
*
* 与插件当前实现保持一致(单一事实来源:后端 Java 与 Console UI 的同名逻辑):
* - 捕获规则(CaptureRule / AuthEndpointAuditWebFilter)
* - 捕获规则(CaptureRule / AuthEndpointAuditWebFilter:认证端点只记录 POST,
* 登录/登出页面的 GET 访问不是操作)
* - 资源解析(ResourceResolver)
* - 语义动作标注与中文描述(OperationDescription / ui/src/utils/audit.ts)
* - 登录结果判定(LoginOutcome:表单登录成败均为 302,以重定向目标 error 参数区分)
* - 登录结果判定(LoginOutcome:表单登录成败均为 302,以重定向目标是否跳回登录页区分;
* 只有响应明确确认才算成功)
* - 结果语义(AuditResult:登录失败 / 页面访问 / 按状态码)
* - 字段级变更 diff(ChangeDiffEngine:PUT/PATCH/POST/DELETE 语义、脱敏、截断、封顶)
* - 查询筛选(AuditLogFilters:八维筛选 + 登录失败结果语义)
* - CSV 导出(AuditLogCsvExporter:BOM、列顺序、公式注入防护)
@@ -25,6 +28,9 @@
var WRITE_METHODS = ['POST', 'PUT', 'DELETE', 'PATCH'];
var AUTH_ENDPOINTS = ['/login', '/logout'];
var LOGIN_FAILURE_ACTION = 'login-failure';
/** 登录页 / 登出页访问(GET,仅升级前落库的历史记录会出现)。 */
var LOGIN_PAGE_ACTION = 'login-page';
var LOGOUT_PAGE_ACTION = 'logout-page';
/** 资源类型(与 AuditLogList.vue 的筛选下拉一致) */
var RESOURCE_TYPES = [
@@ -80,6 +86,8 @@
login: '登录',
'login-failure': '登录失败',
logout: '登出',
'login-page': '访问登录页',
'logout-page': '访问登出页',
auth: '认证操作',
password: '修改密码',
resetpassword: '重置密码'
@@ -204,7 +212,8 @@
/**
* 捕获规则(CaptureRule + AuthEndpointAuditWebFilter):
* - /apis/** 下已登录用户的写操作(POST/PUT/DELETE/PATCH);
* - /login、/logout 认证端点始终记录(链首过滤器捕获,含登录失败与失败回显);
* - 认证操作 POST /login(登录尝试)、POST /logout(登出)始终记录
* (链首过滤器捕获,含登录失败);登录/登出页面的 GET 访问不是操作,不记录;
* - /apis/** 下认证后仍为匿名主体的请求不记录(401/403 场景)。
*
* @param {{method: string, path: string, authenticated: boolean}} options
@@ -217,10 +226,17 @@
var authenticated = !!(options && options.authenticated);
if (AUTH_ENDPOINTS.indexOf(clean) !== -1) {
if (method === 'POST') {
return {
captured: true,
channel: 'auth',
reason: '认证操作始终记录(POST /login 登录尝试、POST /logout 登出,含登录失败)'
};
}
return {
captured: true,
channel: 'auth',
reason: '认证端点始终记录(登录/登出,含登录失败与失败回显)'
captured: false,
channel: 'none',
reason: '登录/登出页面的 GET 访问不是操作(打开登录页、被弹回登录页、失败回显、登出确认页),不记录'
};
}
if (clean.indexOf('/apis/') !== 0) {
@@ -272,14 +288,21 @@
return { resourceType: segs[3], resourceName: segs.length >= 5 && segs[4] ? segs[4] : null };
}
/** 推导语义动作键(与 OperationDescription.actionOf / deriveAction 一致)。 */
/**
* 推导语义动作键(与 OperationDescription.actionOf / deriveAction 一致):
* 认证端点按方法区分——POST 是登录/登出操作,其余方法是页面访问,路径带 error 参数的
* 登录页是升级前落库的失败回显;子动作段优先于 HTTP 方法。
*/
function deriveAction(method, resourceType, path) {
var clean = pathWithoutQuery(path);
if (clean === '/login') {
return hasErrorParam(path) ? LOGIN_FAILURE_ACTION : 'login';
if (hasErrorParam(path)) {
return LOGIN_FAILURE_ACTION;
}
return normalizeMethod(method) === 'POST' ? 'login' : LOGIN_PAGE_ACTION;
}
if (clean === '/logout') {
return 'logout';
return normalizeMethod(method) === 'POST' ? 'logout' : LOGOUT_PAGE_ACTION;
}
if (resourceType === 'auth') {
return 'auth';
@@ -294,6 +317,35 @@
return METHOD_ACTIONS[normalizeMethod(method)] || null;
}
/**
* 是否为认证端点的页面访问(GET /login、GET /logout,含查询串):不是登录/登出操作,
* 结果不计成败;现已不再记录,只有升级前落库的历史记录会出现——当时它们被记成
* 「未知用户 · 登录 · 200」,看起来像登录成功。
*/
function isAuthPageVisit(log) {
log = log || {};
if (normalizeMethod(log.method) === 'POST') {
return false;
}
var clean = pathWithoutQuery(log.path);
return clean === '/login' || clean === '/logout';
}
/**
* 记录的语义动作键(展示与结果判定共用):优先落库标注;认证端点的失败回显与页面访问
* (升级前落库为 login/logout 或无标注的历史记录)按方法与路径纠正;落库标注未识别时
* 回退 method/path 现场推导。
*/
function semanticAction(log) {
log = log || {};
var derived = deriveAction(log.method, log.resourceType, log.path);
if (derived === LOGIN_FAILURE_ACTION || derived === LOGIN_PAGE_ACTION ||
derived === LOGOUT_PAGE_ACTION) {
return derived;
}
return log.action && describeFromAction(log.action, log.resourceType) ? log.action : derived;
}
/** 由语义动作键推导中文短语;未识别返回 null(调用方回退 method 推导)。 */
function describeFromAction(action, resourceType) {
if (!action) {
@@ -307,16 +359,15 @@
return verb ? verb + (RESOURCE_LABELS[resourceType] || resourceType) : null;
}
/** 操作中文描述(优先落库语义动作,历史记录现场推导)。 */
function describeOperation(spec) {
spec = spec || {};
var action = spec.action || deriveAction(spec.method, spec.resourceType, spec.path);
var fromAction = describeFromAction(action, spec.resourceType);
/** 操作中文描述(以记录的语义动作键为准,含失败回显与页面访问纠正)。 */
function describeOperation(log) {
log = log || {};
var fromAction = describeFromAction(semanticAction(log), log.resourceType);
if (fromAction) {
return fromAction;
}
var label = RESOURCE_LABELS[spec.resourceType] || spec.resourceType;
var verb = METHOD_VERBS[normalizeMethod(spec.method)] || spec.method;
var label = RESOURCE_LABELS[log.resourceType] || log.resourceType;
var verb = METHOD_VERBS[normalizeMethod(log.method)] || log.method;
return verb + label;
}
@@ -325,24 +376,53 @@
return statusCode != null && statusCode >= 200 && statusCode < 400;
}
/** 是否为登录失败记录(优先落库标注,回退路径推导)。 */
function isLoginFailure(spec) {
spec = spec || {};
var action = spec.action || deriveAction(spec.method, spec.resourceType, spec.path);
return action === LOGIN_FAILURE_ACTION;
/** 是否为登录失败记录(以记录的语义动作键为准)。 */
function isLoginFailure(log) {
return semanticAction(log) === LOGIN_FAILURE_ACTION;
}
/** 结果语义标签(与后端 AuditResult.label 一致,CSV「结果」列用)。 */
var RESULT_LABELS = {
success: '成功',
failure: '失败',
'page-visit': '页面访问'
};
/**
* 记录的结果语义(与后端 AuditResult 同一规则,筛选 / CSV / 徽标共用):
* - failure:登录失败(表单登录失败的 HTTP 状态码与成功相同、均为 302,只能靠标注识别)
* 或状态码 4xx/5xx;
* - page-visit:认证端点的页面访问(升级前落库的 GET 记录),不是操作,不计成败;
* - success:其余状态码 2xx/3xx。
*/
function resultOutcome(log) {
if (isLoginFailure(log)) {
return 'failure';
}
if (isAuthPageVisit(log)) {
return 'page-visit';
}
return isSuccessful(log && log.statusCode) ? 'success' : 'failure';
}
/**
* 结果徽标:登录失败(HTTP 可能为 200/302)显示「失败」,
* 结果徽标:登录失败显示红色「失败」(表单失败是 302、失败回显是 200,裸状态码是绿的);
* 登录成功显示绿色「成功」(表单成功同样是 302);页面访问显示灰色「—」;
* 其余按状态码(2xx/3xx 绿、其余红)。
*/
function resultBadge(spec) {
if (isLoginFailure(spec)) {
function resultBadge(log) {
if (isLoginFailure(log)) {
return { text: '失败', class: 'status-fail' };
}
if (isAuthPageVisit(log)) {
return { text: '—', class: 'status-na' };
}
if (semanticAction(log) === 'login' && isSuccessful(log && log.statusCode)) {
return { text: '成功', class: 'status-ok' };
}
return {
text: String(spec && spec.statusCode),
class: isSuccessful(spec && spec.statusCode) ? 'status-ok' : 'status-fail'
text: String(log && log.statusCode),
class: isSuccessful(log && log.statusCode) ? 'status-ok' : 'status-fail'
};
}
@@ -376,37 +456,46 @@
// ---------------------------------------------------------------- 登录结果判定
/**
* 重定向是否跳回登录页:查询串含 error 参数(Halo 2.26 的四种失败重定向),或路径
* 本身就是 /login——成功只会去 /uc、已保存的原请求或 2FA 页。缺失 Location 的重定向
* 同样无法确认成功。
*/
function returnsToLoginPage(location) {
var target = String(location || '');
if (!target.trim()) {
return true;
}
return pathWithoutQuery(target) === '/login' || hasErrorParam(target);
}
/**
* 登录结果判定(LoginOutcome):
* - POST /login:2xx 成功;3xx 看重定向目标是否含 error 参数;其余失败;
* - GET /login?error=...:登录失败回显(HTTP 200 但语义为失败);
* - 其他请求不算登录尝试。
* - POST /login:2xx 成功;3xx 看重定向目标是否跳回登录页;其余状态码失败。
* 只有响应明确确认才算成功——把失败登录记成成功,远比把异常响应记成失败更糟;
* - 其他请求不算登录尝试(登录页 GET 不是操作,不记录)。
*
* @returns {{attempt: boolean, failure: boolean, echo: boolean}}
* @returns {{attempt: boolean, failure: boolean}}
*/
function classifyLogin(input) {
input = input || {};
var method = normalizeMethod(input.method);
var fullPath = String(input.path || '');
var clean = pathWithoutQuery(fullPath);
var attempt = method === 'POST' && clean === '/login';
var echo = method === 'GET' && clean === '/login' && hasErrorParam(fullPath);
if (attempt) {
var status = input.statusCode;
var failure;
if (status == null) {
failure = true;
} else if (status >= 200 && status < 300) {
failure = false;
} else if (status >= 300 && status < 400) {
failure = hasErrorParam(input.location);
} else {
failure = true;
}
return { attempt: true, failure: failure, echo: false };
var clean = pathWithoutQuery(String(input.path || ''));
if (method !== 'POST' || clean !== '/login') {
return { attempt: false, failure: false };
}
return { attempt: false, failure: echo, echo: echo };
var status = input.statusCode;
var failure;
if (status == null) {
failure = true;
} else if (status >= 200 && status < 300) {
failure = false;
} else if (status >= 300 && status < 400) {
failure = returnsToLoginPage(input.location);
} else {
failure = true;
}
return { attempt: true, failure: failure };
}
// ---------------------------------------------------------------- 字段级变更 diff
@@ -774,8 +863,9 @@
// 认证端点由 Security 链首过滤器捕获,无认证上下文:操作者统一记「未知用户」,
// 登录尝试的账号由请求体白名单提取(attemptedUsername)。
var username = authEndpoint ? 'unknown' : (input.username || 'unknown');
var action = login.failure ? LOGIN_FAILURE_ACTION
: deriveAction(method, resolved.resourceType, fullPath);
// 登录失败(POST /login)标注 login-failure;其余按 method/path 推导
var action = login.failure ? LOGIN_FAILURE_ACTION : deriveAction(
method, resolved.resourceType, fullPath);
var changes = null;
if (!authEndpoint && input.recordChanges !== false &&
@@ -878,9 +968,13 @@
return isNaN(ms) ? null : ms;
}
/** 结果判定:success = 2xx/3xx 且非登录失败;failure = 其余。 */
/** 结果判定(与 resultOutcome 一致):页面访问记录(历史 GET)既非成功也非失败。 */
function isSuccessRecord(log) {
return !isLoginFailure(log) && isSuccessful(log && log.statusCode);
return resultOutcome(log) === 'success';
}
function isFailureRecord(log) {
return resultOutcome(log) === 'failure';
}
/**
@@ -905,13 +999,15 @@
if (method && normalizeMethod(log.method) !== method) {
return false;
}
if (filters.action && log.action !== filters.action) {
// 语义动作按记录的语义动作键比对:升级前落库的页面访问记录(GET,标注 login/logout)
// 展示为「访问登录页/登出页」,筛登录/登出操作时不混入
if (filters.action && semanticAction(log) !== filters.action) {
return false;
}
if (filters.result === 'success' && !isSuccessRecord(log)) {
return false;
}
if (filters.result === 'failure' && isSuccessRecord(log)) {
if (filters.result === 'failure' && !isFailureRecord(log)) {
return false;
}
var time = new Date(log.timestamp).getTime();
@@ -995,7 +1091,6 @@
});
var lines = [CSV_HEADER.map(csvEscape).join(',')];
sorted.forEach(function (log) {
var badge = resultBadge(log);
lines.push([
csvEscape(formatTime(log.timestamp)),
csvEscape(displayUsername(log.username)),
@@ -1006,7 +1101,8 @@
csvEscape(log.path),
csvEscape(describeOperation(log)),
csvEscape(formatChanges(log.changes)),
csvEscape(badge.text === '失败' ? '失败' : (isSuccessRecord(log) ? '成功' : '失败')),
// 结果语义与界面一致(AuditResult):登录失败靠标注识别,页面访问不计成败
csvEscape(RESULT_LABELS[resultOutcome(log)]),
csvEscape(log.statusCode == null ? null : String(log.statusCode)),
csvEscape(log.remoteIp),
csvEscape(log.userAgent)
@@ -1111,6 +1207,9 @@
WRITE_METHODS: WRITE_METHODS,
AUTH_ENDPOINTS: AUTH_ENDPOINTS,
LOGIN_FAILURE_ACTION: LOGIN_FAILURE_ACTION,
LOGIN_PAGE_ACTION: LOGIN_PAGE_ACTION,
LOGOUT_PAGE_ACTION: LOGOUT_PAGE_ACTION,
RESULT_LABELS: RESULT_LABELS,
RESOURCE_TYPES: RESOURCE_TYPES,
RESOURCE_LABELS: RESOURCE_LABELS,
SEMANTIC_ACTIONS: SEMANTIC_ACTIONS,
@@ -1122,10 +1221,14 @@
shouldCapture: shouldCapture,
resolveResource: resolveResource,
deriveAction: deriveAction,
isAuthPageVisit: isAuthPageVisit,
semanticAction: semanticAction,
describeOperation: describeOperation,
isSuccessful: isSuccessful,
isLoginFailure: isLoginFailure,
isSuccessRecord: isSuccessRecord,
isFailureRecord: isFailureRecord,
resultOutcome: resultOutcome,
resultBadge: resultBadge,
displayUsername: displayUsername,
formatTime: formatTime,
+2
View File
@@ -529,6 +529,8 @@ tr.row-new td { animation: rowFlash 2.8s ease-out; }
}
.status-ok { color: #0f766e; background: rgba(76, 203, 160, 0.15); }
.status-fail { color: var(--danger); background: rgba(215, 29, 29, 0.12); }
/* 页面访问(登录页/登出页的 GET,不计成败):中性灰 */
.status-na { color: #6b7280; background: rgba(107, 114, 128, 0.12); }
/* 空状态 */
.empty-state {
+33 -11
View File
@@ -8,7 +8,7 @@
*
* 内置故事线(固定时间锚点,可直接按演示页提示复现):
* 1. 排查「hello-halo 这篇文章谁改过」(多用户多次编辑,含字段 diff)
* 2. 登录爆破回溯(同一 IP、无差别撞库,含失败回显页)
* 2. 登录爆破回溯(同一 IP、无差别撞库;另含升级前落库的登录页访问记录)
* 3. 忘记密码后登录成功(失败 → 成功清零)
* 4. 定时发布(系统内部变更,执行者「系统」、无 IP/UA)
* 5. 插件生命周期(升级 / 停用 / 卸载,含字段 diff)
@@ -492,7 +492,7 @@
body: postDoc(hello, { visible: 'PUBLIC', pinned: true })
}));
// ---- 故事线 2:登录爆破回溯(同一 IP 无差别撞库 + 失败回显) ----
// ---- 故事线 2:登录爆破回溯(同一 IP、无差别撞库 + 升级前的登录页访问记录) ----
var burstBase = now - 20 * DAY - 3 * 3600 * 1000;
var attackAccounts = ['admin', 'root', 'oracle', 'test', 'admin', 'postgres'];
attackAccounts.forEach(function (account, idx) {
@@ -507,15 +507,25 @@
timestamp: new Date(burstBase + [0, 41, 88, 135, 210, 400][idx] * 1000).toISOString()
}));
});
// 浏览器随后渲染失败回显页(GET,HTTP 200):审计标注 login-failure,红色「失败」
add(make({
method: 'GET',
path: '/login?error=invalid-credential&method=form',
statusCode: 200,
remoteIp: '203.0.113.66',
userAgent: UAS.bot,
timestamp: new Date(burstBase + 401 * 1000).toISOString()
}));
// 升级前落库的登录页访问记录:旧版本把登录/登出页的 GET 访问也记成「登录/登出」
// (HTTP 200,标注 login/logout),现已不再记录;历史记录展示为「访问登录页/登出页」,
// 结果不计成败。失败回显页当时标注 login-failure,仍按登录失败展示。
[
{ at: 30, path: '/login', action: 'login' },
{ at: 70, path: '/login?authentication_required', action: 'login' },
{ at: 401, path: '/login?error=invalid-credential&method=form', action: 'login-failure' }
].forEach(function (visit) {
var legacy = make({
method: 'GET',
path: visit.path,
statusCode: 200,
remoteIp: '203.0.113.66',
userAgent: UAS.bot,
timestamp: new Date(burstBase + visit.at * 1000).toISOString()
});
legacy.action = visit.action;
add(legacy);
});
// ---- 故事线 3:忘记密码(失败两次后成功) ----
var retryBase = now - 12 * DAY - 10 * 3600 * 1000;
@@ -557,6 +567,18 @@
userAgent: UAS.chrome,
timestamp: new Date(now - 4 * DAY - 11 * 3600 * 1000).toISOString()
}));
// 升级前落库的登出确认页访问(GET,旧版本记成「登出」,现展示为「访问登出页」)
var legacyLogoutPage = make({
username: 'guichumo',
method: 'GET',
path: '/logout',
statusCode: 200,
remoteIp: '192.168.1.23',
userAgent: UAS.chrome,
timestamp: new Date(now - 4 * DAY - 11 * 3600 * 1000 - 2000).toISOString()
});
legacyLogoutPage.action = 'logout';
add(legacyLogoutPage);
// ---- 故事线 4:定时发布(系统内部变更:无 IP/UA,执行者「系统」) ----
add(AuditCore.buildSystemRecord({