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
+129 -29
View File
@@ -45,17 +45,27 @@ test('shouldCapture: /apis/** 下匿名请求不记录(401/403 场景)', ()
assert.match(r.reason, /匿名/);
});
test('shouldCapture: 认证端点始终记录(含查询串与匿名登录尝试)', () => {
test('shouldCapture: 认证操作 POST /login、POST /logout 始终记录(含匿名登录尝试)', () => {
assert.deepEqual(
C.shouldCapture({ method: 'POST', path: '/login', authenticated: false }),
{
captured: true,
channel: 'auth',
reason: '认证端点始终记录(登录/登出,含登录失败与失败回显)'
reason: '认证操作始终记录(POST /login 登录尝试、POST /logout 登出,含登录失败)'
}
);
assert.equal(C.shouldCapture({ method: 'POST', path: '/logout', authenticated: true }).channel, 'auth');
assert.equal(C.shouldCapture({ method: 'GET', path: '/login?error=invalid-credential', authenticated: false }).captured, true);
});
test('shouldCapture: 登录/登出页面的 GET 访问不是操作,不记录', () => {
// 打开登录页、被弹回登录页、失败回显、登出确认页都只是渲染页面(HTTP 200);
// 原先一律落库成「未知用户 · 登录 · 200」,在列表里与登录成功无法区分
for (const path of ['/login', '/login?authentication_required', '/login?error=invalid-credential', '/logout']) {
const r = C.shouldCapture({ method: 'GET', path, authenticated: false });
assert.equal(r.captured, false, path);
assert.equal(r.channel, 'none', path);
assert.match(r.reason, /GET 访问不是操作/, path);
}
});
test('shouldCapture: /apis/** 之外的普通路径不记录', () => {
@@ -105,11 +115,17 @@ test('resolveResource: 非 /apis 路径与畸形路径 → other', () => {
});
// ---------------- deriveAction / describeOperation ----------------
test('deriveAction: 登录/登出/失败回显', () => {
test('deriveAction: 登录/登出按方法区分,失败回显由 error 参数识别', () => {
assert.equal(C.deriveAction('POST', 'auth', '/login'), 'login');
assert.equal(C.deriveAction('GET', 'auth', '/login?error=invalid-credential'), 'login-failure');
assert.equal(C.deriveAction('POST', 'auth', '/logout'), 'logout');
assert.equal(C.deriveAction('POST', 'auth', '/login?continue=/uc'), 'login');
assert.equal(C.deriveAction('POST', 'auth', '/logout'), 'logout');
// GET 是页面访问(打开登录页、被弹回登录页、登出确认页)
assert.equal(C.deriveAction('GET', 'auth', '/login'), 'login-page');
assert.equal(C.deriveAction('GET', 'auth', '/login?authentication_required'), 'login-page');
assert.equal(C.deriveAction('GET', 'auth', '/logout'), 'logout-page');
// 升级前落库的失败回显(GET /login?error=...)无标注时现场推导
assert.equal(C.deriveAction('GET', 'auth', '/login?error=invalid-credential'), 'login-failure');
assert.equal(C.deriveAction('GET', 'auth', '/login?errors=1'), 'login-page');
});
test('deriveAction: 子动作段优先于 HTTP 方法', () => {
@@ -143,32 +159,70 @@ test('describeOperation: 历史记录无 action 时现场推导;未识别回
assert.equal(C.describeOperation({ method: 'POST', resourceType: 'unknowns', path: '/apis/x/v1alpha1/unknowns' }), '新建unknowns');
});
test('describeOperation: 升级前落库的登录页访问记录(GET + login 标注)纠正为页面访问', () => {
// 旧版本把登录页的 GET 访问记成「未知用户 · 登录 · 200」,看起来像登录成功
assert.equal(C.describeOperation({ method: 'GET', resourceType: 'auth', path: '/login?authentication_required', action: 'login' }), '访问登录页');
assert.equal(C.describeOperation({ method: 'GET', resourceType: 'auth', path: '/login', action: 'login' }), '访问登录页');
assert.equal(C.describeOperation({ method: 'GET', resourceType: 'auth', path: '/login' }), '访问登录页');
assert.equal(C.describeOperation({ method: 'GET', resourceType: 'auth', path: '/logout', action: 'logout' }), '访问登出页');
// POST 才是登录/登出操作,落库标注照常生效
assert.equal(C.describeOperation({ method: 'POST', resourceType: 'auth', path: '/login', action: 'login' }), '登录');
assert.equal(C.describeOperation({ method: 'POST', resourceType: 'auth', path: '/logout', action: 'logout' }), '登出');
});
// ---------------- classifyLogin(登录结果判定) ----------------
test('classifyLogin: POST /login 的 2xx/302/401 判定', () => {
assert.deepEqual(C.classifyLogin({ method: 'POST', path: '/login', statusCode: 200 }), { attempt: true, failure: false, echo: false });
assert.deepEqual(C.classifyLogin({ method: 'POST', path: '/login', statusCode: 200 }), { attempt: true, failure: false });
assert.equal(C.classifyLogin({ method: 'POST', path: '/login', statusCode: 302, location: '/uc' }).failure, false);
assert.equal(C.classifyLogin({ method: 'POST', path: '/login', statusCode: 302, location: '/challenges/two-factor/totp' }).failure, false);
assert.equal(C.classifyLogin({ method: 'POST', path: '/login', statusCode: 302, location: '/login?error=invalid-credential&method=form' }).failure, true);
assert.equal(C.classifyLogin({ method: 'POST', path: '/login', statusCode: 302, location: '/login?error&method=form' }).failure, true);
assert.equal(C.classifyLogin({ method: 'POST', path: '/login', statusCode: 302, location: '/login?error=rate-limit-exceeded&method=form' }).failure, true);
assert.equal(C.classifyLogin({ method: 'POST', path: '/login', statusCode: 401 }).failure, true);
assert.equal(C.classifyLogin({ method: 'POST', path: '/login', statusCode: 403 }).failure, true);
assert.equal(C.classifyLogin({ method: 'POST', path: '/login', statusCode: null }).failure, true);
});
test('classifyLogin: 失败回显页(GET /login?error=...,HTTP 200)判为失败', () => {
const echo = C.classifyLogin({ method: 'GET', path: '/login?error=invalid-credential&method=form', statusCode: 200 });
assert.deepEqual(echo, { attempt: false, failure: true, echo: true });
const page = C.classifyLogin({ method: 'GET', path: '/login', statusCode: 200 });
assert.deepEqual(page, { attempt: false, failure: false, echo: false });
test('classifyLogin: 只有响应明确确认才算成功——跳回登录页或缺失 Location 均判失败', () => {
// 不依赖 error 参数:宿主换了失败重定向格式也不会把失败记成成功
assert.equal(C.classifyLogin({ method: 'POST', path: '/login', statusCode: 302, location: '/login' }).failure, true);
assert.equal(C.classifyLogin({ method: 'POST', path: '/login', statusCode: 302, location: '/login?authentication_required' }).failure, true);
assert.equal(C.classifyLogin({ method: 'POST', path: '/login', statusCode: 302 }).failure, true, '缺失 Location 无法确认成功');
// 含 error 字样的其他参数、路径前缀相同都不算跳回登录页
assert.equal(C.classifyLogin({ method: 'POST', path: '/login', statusCode: 302, location: '/uc?errors=1' }).failure, false);
assert.equal(C.classifyLogin({ method: 'POST', path: '/login', statusCode: 302, location: '/login-callback' }).failure, false);
});
test('classifyLogin: 登录页 GET 与登出不是登录尝试', () => {
for (const input of [
{ method: 'GET', path: '/login', statusCode: 200 },
{ method: 'GET', path: '/login?error=invalid-credential', statusCode: 200 },
{ method: 'GET', path: '/login?authentication_required', statusCode: 200 },
{ method: 'POST', path: '/logout', statusCode: 302 }
]) {
assert.deepEqual(C.classifyLogin(input), { attempt: false, failure: false }, JSON.stringify(input));
}
});
// ---------------- 结果徽标 / 登录失败标注 ----------------
test('resultBadge: 登录失败即使 200/302 也显示「失败」', () => {
test('resultBadge: 登录失败即使 200/302 也显示「失败」,登录成功显示「成功」', () => {
assert.deepEqual(
C.resultBadge({ method: 'POST', resourceType: 'auth', path: '/login', action: 'login-failure', statusCode: 302 }),
{ text: '失败', class: 'status-fail' }
);
// 升级前落库的失败回显页(GET,HTTP 200)
assert.deepEqual(
C.resultBadge({ method: 'GET', resourceType: 'auth', path: '/login?error=x', action: 'login-failure', statusCode: 200 }),
{ text: '失败', class: 'status-fail' }
);
// 表单登录成功同样是 302:裸状态码读不出成败,写「成功」与「失败」对称
assert.deepEqual(
C.resultBadge({ method: 'POST', resourceType: 'auth', path: '/login', action: 'login', statusCode: 302 }),
{ text: '成功', class: 'status-ok' }
);
// 其余记录保持数字状态码徽标
assert.deepEqual(
C.resultBadge({ method: 'POST', resourceType: 'auth', path: '/logout', action: 'logout', statusCode: 302 }),
{ text: '302', class: 'status-ok' }
);
assert.deepEqual(
@@ -177,11 +231,30 @@ test('resultBadge: 登录失败即使 200/302 也显示「失败」', () => {
);
});
test('resultBadge / resultOutcome: 登录页访问记录显示灰色「—」,既不算成功也不算失败', () => {
// 升级前落库的 GET /login(HTTP 200、带 login 标注)原先显示绿色「200」,
// 看起来像未知用户登录成功
const visit = { method: 'GET', resourceType: 'auth', path: '/login?authentication_required', action: 'login', statusCode: 200 };
assert.equal(C.isAuthPageVisit(visit), true);
assert.equal(C.isLoginFailure(visit), false);
assert.deepEqual(C.resultBadge(visit), { text: '—', class: 'status-na' });
assert.equal(C.resultOutcome(visit), 'page-visit');
assert.equal(C.isSuccessRecord(visit), false);
assert.equal(C.isFailureRecord(visit), false);
assert.equal(C.resultOutcome({ method: 'GET', resourceType: 'auth', path: '/logout', action: 'logout', statusCode: 200 }), 'page-visit');
// 无标注的历史记录、页面自身出错(5xx)同样不计成败
assert.equal(C.resultOutcome({ method: 'GET', resourceType: 'auth', path: '/login', statusCode: 500 }), 'page-visit');
// POST 是认证操作
assert.equal(C.isAuthPageVisit({ method: 'POST', resourceType: 'auth', path: '/login' }), false);
assert.equal(C.isAuthPageVisit({ method: 'GET', resourceType: 'posts', path: '/apis/x/v1alpha1/login' }), false);
});
test('isLoginFailure / isSuccessRecord: 兼容历史记录(无 action 时按路径推导)', () => {
assert.equal(C.isLoginFailure({ method: 'GET', resourceType: 'auth', path: '/login?error=x', statusCode: 200 }), true);
assert.equal(C.isLoginFailure({ method: 'POST', resourceType: 'auth', path: '/login', statusCode: 302 }), false);
assert.equal(C.isSuccessRecord({ action: 'login-failure', statusCode: 200 }), false);
assert.equal(C.isSuccessRecord({ action: 'login', statusCode: 302 }), true);
assert.equal(C.isSuccessRecord({ method: 'POST', resourceType: 'auth', path: '/login', action: 'login-failure', statusCode: 200 }), false);
assert.equal(C.isSuccessRecord({ method: 'POST', resourceType: 'auth', path: '/login', action: 'login', statusCode: 302 }), true);
assert.equal(C.isFailureRecord({ method: 'POST', resourceType: 'posts', path: '/apis/x/v1alpha1/posts', statusCode: 403 }), true);
});
test('displayUsername: unknown → 未知用户,system → 系统', () => {
@@ -367,7 +440,8 @@ test('buildRecord: 登录请求操作者记「未知用户」,失败标注 log
timestamp: '2026-11-21T10:00:00Z'
});
assert.equal(success.action, 'login');
assert.equal(C.resultBadge(success).text, '302');
assert.equal(C.resultBadge(success).text, '成功');
assert.equal(C.resultBadge(failure).text, '失败');
});
test('buildRecord: 仅 2xx 且 recordChanges 开启时计算 diff', () => {
@@ -456,7 +530,10 @@ function fixture() {
{ id: 'a2', timestamp: '2026-11-21T09:00:00Z', username: 'editor.zhang', method: 'POST', path: '/apis/api.content.halo.run/v1alpha1/posts/hello/publish', resourceType: 'posts', resourceName: 'hello', statusCode: 500, action: 'publish' },
{ id: 'a3', timestamp: '2026-11-20T08:00:00Z', username: 'unknown', method: 'GET', path: '/login?error=x', resourceType: 'auth', resourceName: null, statusCode: 200, action: 'login-failure' },
{ id: 'a4', timestamp: '2026-11-21T11:30:00Z', username: 'guichumo', method: 'POST', path: '/login', resourceType: 'auth', resourceName: null, statusCode: 302, action: 'login' },
{ id: 'a5', timestamp: '2026-11-10T08:00:00Z', username: 'writer.li', method: 'PUT', path: '/apis/v1alpha1/configmaps/system', resourceType: 'configmaps', resourceName: 'system', statusCode: 403, action: 'update' }
{ id: 'a5', timestamp: '2026-11-10T08:00:00Z', username: 'writer.li', method: 'PUT', path: '/apis/v1alpha1/configmaps/system', resourceType: 'configmaps', resourceName: 'system', statusCode: 403, action: 'update' },
// 升级前落库的登录页访问记录(GET、HTTP 200,带旧版本的 login 标注)
{ id: 'a6', timestamp: '2026-11-20T07:00:00Z', username: 'unknown', method: 'GET', path: '/login?authentication_required', resourceType: 'auth', resourceName: null, statusCode: 200, action: 'login' },
{ id: 'a7', timestamp: '2026-11-09T08:00:00Z', username: 'unknown', method: 'GET', path: '/logout', resourceType: 'auth', resourceName: null, statusCode: 200, action: 'logout' }
];
}
@@ -467,7 +544,12 @@ test('applyFilters: 操作者 / 资源类型 / 动作 / 语义动作', () => {
assert.deepEqual(C.applyFilters(fixture(), { action: 'login-failure' }).map(l => l.id), ['a3']);
});
test('applyFilters: 结果语义(登录失败 200 计失败;登录成功 302 计成功)', () => {
test('applyFilters: 语义动作筛「登录/登出」不混入登录页访问记录(它们展示为「访问登录页/登出页」)', () => {
assert.deepEqual(C.applyFilters(fixture(), { action: 'login' }).map(l => l.id), ['a4']);
assert.deepEqual(C.applyFilters(fixture(), { action: 'logout' }).map(l => l.id), []);
});
test('applyFilters: 结果语义(登录失败 200 计失败;登录成功 302 计成功;页面访问不计成败)', () => {
const success = C.applyFilters(fixture(), { result: 'success' }).map(l => l.id);
assert.deepEqual(success, ['a4', 'a1']);
const failure = C.applyFilters(fixture(), { result: 'failure' }).map(l => l.id);
@@ -477,7 +559,7 @@ test('applyFilters: 结果语义(登录失败 200 计失败;登录成功 302
test('applyFilters: 关键字匹配资源名称与路径', () => {
assert.deepEqual(C.applyFilters(fixture(), { keyword: 'hello' }).map(l => l.id), ['a1', 'a2']);
assert.deepEqual(C.applyFilters(fixture(), { keyword: 'configmaps' }).map(l => l.id), ['a5']);
assert.deepEqual(C.applyFilters(fixture(), { keyword: 'LOGIN' }).map(l => l.id), ['a4', 'a3']);
assert.deepEqual(C.applyFilters(fixture(), { keyword: 'LOGIN' }).map(l => l.id), ['a4', 'a3', 'a6']);
});
test('applyFilters: 起止时间闭区间', () => {
@@ -493,8 +575,8 @@ test('applyFilters: 组合条件、时间倒序、不修改入参', () => {
assert.deepEqual(out.map(l => l.id), ['a2']);
const logs = fixture();
C.applyFilters(logs, {});
assert.equal(logs.length, 5);
assert.deepEqual(C.applyFilters(fixture(), {}).map(l => l.id), ['a4', 'a1', 'a2', 'a3', 'a5']);
assert.equal(logs.length, 7);
assert.deepEqual(C.applyFilters(fixture(), {}).map(l => l.id), ['a4', 'a1', 'a2', 'a3', 'a6', 'a5', 'a7']);
});
// ---------------- paginate / countExpired ----------------
@@ -511,7 +593,7 @@ test('paginate: 分页与越界收敛', () => {
});
test('countExpired: 统计超过保留期的记录', () => {
assert.equal(C.countExpired(fixture(), 5, NOW), 1);
assert.equal(C.countExpired(fixture(), 5, NOW), 2);
assert.equal(C.countExpired(fixture(), 90, NOW), 0);
});
@@ -525,13 +607,18 @@ test('toCsv: UTF-8 BOM + CRLF + 表头列顺序', () => {
test('toCsv: 记录按时间升序输出,结果/描述/变更列与界面一致', () => {
const logs = [
{ id: 'b', timestamp: '2026-11-21T10:00:00Z', username: 'guichumo', method: 'PUT', path: '/apis/x/v1alpha1/posts/b', resourceType: 'posts', resourceName: 'b', statusCode: 200, action: 'publish', attemptedUsername: null, remoteIp: '1.1.1.1', userAgent: 'UA' },
{ id: 'a', timestamp: '2026-11-20T10:00:00Z', username: 'unknown', method: 'GET', path: '/login?error=x', resourceType: 'auth', resourceName: null, statusCode: 200, action: 'login-failure', attemptedUsername: 'admin', remoteIp: '2.2.2.2', userAgent: 'bot' }
{ id: 'a', timestamp: '2026-11-20T10:00:00Z', username: 'unknown', method: 'GET', path: '/login?error=x', resourceType: 'auth', resourceName: null, statusCode: 200, action: 'login-failure', attemptedUsername: 'admin', remoteIp: '2.2.2.2', userAgent: 'bot' },
{ id: 'c', timestamp: '2026-11-22T10:00:00Z', username: 'unknown', method: 'POST', path: '/login', resourceType: 'auth', resourceName: null, statusCode: 302, action: 'login', attemptedUsername: 'guichumo', remoteIp: '3.3.3.3', userAgent: 'chrome' },
// 升级前落库的登录页访问记录:原先导出为「登录,成功」,看起来像未知用户登录成功
{ id: 'd', timestamp: '2026-11-19T10:00:00Z', username: 'unknown', method: 'GET', path: '/login?authentication_required', resourceType: 'auth', resourceName: null, statusCode: 200, action: 'login', attemptedUsername: null, remoteIp: '43.228.157.244', userAgent: 'bot' }
];
const lines = C.toCsv(logs).split('\r\n');
assert.equal(lines.length, 4, '表头 + 2 行 + 末尾空行');
assert.match(lines[1], /^2026-11-20 18:00:00,未知用户,admin,GET,auth,,/);
assert.match(lines[1], /登录失败,.*失败,200,2\.2\.2\.2,bot$/);
assert.match(lines[2], /发布文章,.*成功,200,1\.1\.1\.1,UA$/);
assert.equal(lines.length, 6, '表头 + 4 行 + 末尾空行');
assert.match(lines[1], /^2026-11-19 18:00:00,未知用户,,GET,auth,,\/login\?authentication_required,访问登录页,,页面访问,200,43\.228\.157\.244,bot$/);
assert.match(lines[2], /^2026-11-20 18:00:00,未知用户,admin,GET,auth,,/);
assert.match(lines[2], /登录失败,.*失败,200,2\.2\.2\.2,bot$/);
assert.match(lines[3], /发布文章,.*成功,200,1\.1\.1\.1,UA$/);
assert.match(lines[4], /登录,,成功,302,3\.3\.3\.3,chrome$/);
});
test('toCsv: 引号/逗号/换行转义与公式注入防护', () => {
@@ -655,13 +742,26 @@ test('MockData.generateLogs: 资源解析与语义动作与捕获管线一致',
}
});
test('MockData.generateLogs: 覆盖登录失败(302 重定向与 GET 回显 200)', () => {
test('MockData.generateLogs: 覆盖登录失败(302)、登录成功与升级前的登录页访问记录', () => {
const logs = MockData.generateLogs(FIXED_NOW);
assert.ok(logs.some(l => l.action === 'login-failure' && l.method === 'POST' && l.statusCode === 302));
assert.ok(logs.some(l => l.action === 'login-failure' && l.method === 'GET' && l.statusCode === 200));
assert.ok(logs.some(l => l.action === 'login' && l.method === 'POST' && l.statusCode === 302));
assert.ok(logs.some(l => l.method === 'POST' && l.path === '/login' && l.username === 'unknown'));
assert.ok(logs.some(l => l.attemptedUsername === 'admin'));
// 模拟数据不产生「登录页 GET 访问」的新记录:除升级前落库的历史记录外,
// 认证端点只有 POST
const visits = logs.filter(l => l.method !== 'POST' && ['/login', '/logout'].includes(C.pathWithoutQuery(l.path)));
assert.ok(visits.length >= 2, '应含升级前的登录页/登出页访问记录');
for (const visit of visits) {
if (C.isLoginFailure(visit)) {
// 升级前落库的失败回显页:当时标注 login-failure,仍按登录失败展示
assert.equal(C.resultOutcome(visit), 'failure');
assert.equal(C.describeOperation(visit), '登录失败');
} else {
assert.equal(C.resultOutcome(visit), 'page-visit');
assert.match(C.describeOperation(visit), /^访问(登录|登出)页$/);
}
}
});
test('MockData.generateLogs: 覆盖系统内部变更(执行者「系统」、无 IP/UA)', () => {