fix: 登录失败按 Halo 登录端点契约判定,不再被 200/302 掩盖

Halo 2.26 登录端点表单提交的成功与失败均为 302 重定向(成功 → /uc,
失败 → /login?error=...&method=...),失败回显页 GET /login?error=...
渲染 HTTP 200;旧逻辑按「2xx 即成功」判定导致:

- 审计列表中失败登录显示绿色 200/302,误读为登录成功
- 登录失败告警把表单成功登录也计为失败,成功清零失效、可误报警

修复:
- 新增 LoginOutcome 纯函数:2xx 成功;3xx 以重定向目标查询串是否含
  error 参数判定(含无值形式);其余(401、状态码缺失)失败
- 失败的登录尝试与失败回显(GET /login?error=...)落库标注语义动作
  login-failure(spec.action),HTTP 状态码仍如实记录
- Console 列表/详情与 CSV:登录失败记录显示红色「失败」而非裸状态码,
  语义动作筛选与「结果=失败」筛选均命中登录失败记录
- LoginFailureAlertService 改由过滤器传入 LoginOutcome 判定结果,
  成功清零、失败累计恢复正常
- 历史局限:升级前落库的表单登录失败(302)无法由路径重新推导,
  仍按登录展示;升级后新记录捕获时标注,不再有此局限

测试:后端 265 例、前端 60 例全部通过;文档与 README 同步更新。
This commit is contained in:
2026-11-22 15:47:47 +08:00
parent 9865149ab3
commit 9e89ed75ed
22 changed files with 743 additions and 100 deletions
@@ -6,6 +6,8 @@ import {
deriveAction,
describeOperation,
displayUsername,
isLoginFailure,
resultBadge,
SEMANTIC_ACTIONS,
SYSTEM_USERNAME,
UNKNOWN_USERNAME,
@@ -153,6 +155,19 @@ describe('deriveAction 语义动作推导', () => {
expect(deriveAction('POST', 'auth', '/auth/other')).toBe('auth')
})
it('登录失败回显(GET /login?error=...)推导为 login-failure', () => {
// 登录失败后 Halo 重定向到带 error 参数的登录页,页面渲染 HTTP 200;
// 历史记录(无落库标注)由路径中的 error 参数现场推导
expect(deriveAction('GET', 'auth', '/login?error=invalid-credential&method=local'))
.toBe('login-failure')
// 兜底失败分支:error 参数无值
expect(deriveAction('GET', 'auth', '/login?error&method=local')).toBe('login-failure')
// 首次打开登录页(无 error 参数)不是失败回显
expect(deriveAction('GET', 'auth', '/login?method=local')).toBe('login')
// 含 error 字样的其他参数不算失败标记
expect(deriveAction('GET', 'auth', '/login?errors=1')).toBe('login')
})
it('子动作段优先于 HTTP 方法', () => {
expect(deriveAction('PUT', 'posts',
'/apis/api.console.halo.run/v1alpha1/posts/post-abc/publish')).toBe('publish')
@@ -219,6 +234,101 @@ describe('describeOperation 落库标注优先', () => {
action: 'bogus',
}))).toBe('修改文章')
})
it('登录失败标注(login-failure)描述为 登录失败', () => {
// 表单登录失败(POST /login,302)与失败回显页(GET /login?error=...,200)
// 均落库标注,描述为 登录失败 而非按路径推导出的 登录
expect(describeOperation(spec({
method: 'POST',
resourceType: 'auth',
path: '/login',
action: 'login-failure',
}))).toBe('登录失败')
expect(describeOperation(spec({
method: 'GET',
resourceType: 'auth',
path: '/login?error=invalid-credential&method=local',
action: 'login-failure',
}))).toBe('登录失败')
// 历史回显记录(无落库标注)由路径现场推导,同样得到 登录失败
expect(describeOperation(spec({
method: 'GET',
resourceType: 'auth',
path: '/login?error=invalid-credential&method=local',
}))).toBe('登录失败')
})
})
describe('isLoginFailure / resultBadge 登录失败结果展示', () => {
it('落库标注 login-failure 判定为登录失败(HTTP 200/302 也可能是失败)', () => {
expect(isLoginFailure(spec({
method: 'POST',
resourceType: 'auth',
path: '/login',
statusCode: 302,
action: 'login-failure',
}))).toBe(true)
expect(isLoginFailure(spec({
method: 'GET',
resourceType: 'auth',
path: '/login?error=invalid-credential&method=local',
statusCode: 200,
action: 'login-failure',
}))).toBe(true)
})
it('历史回显记录(无落库标注)由路径现场推导', () => {
expect(isLoginFailure(spec({
method: 'GET',
resourceType: 'auth',
path: '/login?error=invalid-credential&method=local',
statusCode: 200,
}))).toBe(true)
// 表单登录失败的历史记录(POST /login,302)无法由路径推导,仍按登录展示
// (升级后新记录落库标注,不再有此局限)
expect(isLoginFailure(spec({
method: 'POST',
resourceType: 'auth',
path: '/login',
statusCode: 302,
}))).toBe(false)
// 成功登录(POST /login,302 → /uc)不是登录失败
expect(isLoginFailure(spec({
method: 'POST',
resourceType: 'auth',
path: '/login',
statusCode: 302,
action: 'login',
}))).toBe(false)
})
it('登录失败记录结果徽标为红色 失败(不显示裸状态码)', () => {
// 失败回显页 HTTP 200:修复前显示绿色 200,误读为登录成功
expect(resultBadge(spec({
method: 'GET',
resourceType: 'auth',
path: '/login?error=invalid-credential&method=local',
statusCode: 200,
action: 'login-failure',
}))).toEqual({ text: '失败', class: 'status-fail' })
// 表单登录失败 302 重定向:同理
expect(resultBadge(spec({
method: 'POST',
resourceType: 'auth',
path: '/login',
statusCode: 302,
action: 'login-failure',
}))).toEqual({ text: '失败', class: 'status-fail' })
})
it('非登录记录保持原状:数字状态码徽标(2xx/3xx 绿、其余红)', () => {
expect(resultBadge(spec({ statusCode: 200 }))).toEqual({ text: '200', class: 'status-ok' })
expect(resultBadge(spec({ statusCode: 302 }))).toEqual({ text: '302', class: 'status-ok' })
expect(resultBadge(spec({ statusCode: 403 }))).toEqual({ text: '403', class: 'status-fail' })
expect(resultBadge(spec({ statusCode: 500 }))).toEqual({ text: '500', class: 'status-fail' })
expect(resultBadge(spec({ statusCode: undefined })))
.toEqual({ text: 'undefined', class: 'status-fail' })
})
})
describe('SEMANTIC_ACTIONS 筛选选项', () => {
@@ -231,9 +341,9 @@ describe('SEMANTIC_ACTIONS 筛选选项', () => {
})
it('覆盖全部可落库的语义动作键(view 不会被捕获规则记录,除外)', () => {
// 全量枚举:登录/登出/认证 + 方法动作 + 全部子动作段(含 activation 归并)
// 全量枚举:登录/登录失败/登出/认证 + 方法动作 + 全部子动作段(含 activation 归并)
const storable = new Set([
'login', 'logout', 'auth',
'login', 'login-failure', 'logout', 'auth',
'create', 'update', 'delete',
'publish', 'unpublish', 'recycle', 'restore', 'enable', 'disable',
'upgrade', 'uninstall', 'reload', 'reply', 'recalculate',
+63 -3
View File
@@ -104,9 +104,13 @@ const SEGMENT_ACTIONS: Record<string, string> = {
resetpassword: 'resetpassword',
}
/** 登录失败语义动作键(与后端 OperationDescription.LOGIN_FAILURE_ACTION 同步)。 */
const LOGIN_FAILURE_ACTION = 'login-failure'
/** 语义动作键 → 完整短语(不拼资源名)。 */
const ACTION_PHRASES: Record<string, string> = {
login: '登录',
[LOGIN_FAILURE_ACTION]: '登录失败',
logout: '登出',
auth: '认证操作',
password: '修改密码',
@@ -138,6 +142,7 @@ const ACTION_VERBS: Record<string, string> = {
*/
export const SEMANTIC_ACTIONS: Array<{ value: string; label: string }> = [
{ value: 'login', label: '登录' },
{ value: 'login-failure', label: '登录失败' },
{ value: 'logout', label: '登出' },
{ value: 'auth', label: '认证操作' },
{ value: 'create', label: '新建' },
@@ -167,10 +172,32 @@ function pathWithoutQuery(path?: string | null): string {
return queryStart >= 0 ? path.slice(0, queryStart) : path
}
/**
* 查询串是否含 error 参数(可为无值形式 error)。
* 登录失败回显页(GET /login?error=invalid-credential&method=...)由该参数识别。
*/
function hasErrorParam(path?: string | null): boolean {
if (!path) {
return false
}
const queryStart = path.indexOf('?')
if (queryStart < 0) {
return false
}
return path
.slice(queryStart + 1)
.split('&')
.some((pair) => (pair.indexOf('=') < 0 ? pair : pair.slice(0, pair.indexOf('='))) === 'error')
}
/**
* 推导语义动作键(语义动作标注,与后端 OperationDescription.actionOf 保持一致):
* 登录/登出/认证操作由路径判定,子动作段优先于 HTTP 方法,其余方法归为
* create/update/delete/view;无法识别返回 null。
* 登录/登录失败/登出/认证操作由路径判定(GET /login?error=... 为登录失败回显),
* 子动作段优先于 HTTP 方法,其余方法归为 create/update/delete/view;
* 无法识别返回 null。
*
* <p>历史局限:升级前落库的表单登录失败记录(POST /login,302)无法由路径重新
* 推导出失败,仍推导为 login;仅回显页(GET,error 参数在路径中)可现场推导。
*/
export function deriveAction(
method: string,
@@ -179,7 +206,7 @@ export function deriveAction(
): string | null {
const cleanPath = pathWithoutQuery(path)
if (cleanPath === '/login') {
return 'login'
return hasErrorParam(path) ? LOGIN_FAILURE_ACTION : 'login'
}
if (cleanPath === '/logout') {
return 'logout'
@@ -250,6 +277,39 @@ export function isSuccessful(statusCode?: number | null): boolean {
return statusCode != null && statusCode >= 200 && statusCode < 400
}
/** 结果徽标展示形态:文本与配色 class。 */
export interface ResultBadge {
text: string
class: 'status-ok' | 'status-fail'
}
/**
* 结果列徽标(列表/详情共用):登录失败记录(语义动作 login-failure,HTTP 状态码可能
* 为 200/302——失败回显页渲染/表单登录失败重定向)显示「失败」而非裸状态码,避免
* 绿色 200/302 误读为登录成功;其余记录保持原状——按 HTTP 状态码展示
* (2xx/3xx 绿、其余红),与既有行为一致。
*/
export function resultBadge(spec: Pick<AuditLogSpec, 'method' | 'resourceType' | 'path'
| 'action' | 'statusCode'>): ResultBadge {
if (isLoginFailure(spec)) {
return { text: '失败', class: 'status-fail' }
}
return {
text: String(spec.statusCode),
class: isSuccessful(spec.statusCode) ? 'status-ok' : 'status-fail',
}
}
/**
* 是否为登录失败记录:优先落库语义动作标注(spec.action,POST /login 失败与
* 失败回显均落库),历史记录回退由路径现场推导(仅回显页可推导)。
*/
export function isLoginFailure(spec: Pick<AuditLogSpec, 'method' | 'resourceType' | 'path'
| 'action'>): boolean {
const action = spec.action ?? deriveAction(spec.method, spec.resourceType, spec.path)
return action === LOGIN_FAILURE_ACTION
}
/** 变更类型 → 中文标签(未知类型回退原始值)。 */
const CHANGE_TYPE_LABELS: Record<string, string> = {
ADDED: '新增',