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:
+103
@@ -0,0 +1,103 @@
|
||||
package run.halo.operationaudit.audit;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.net.URI;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* 登录结果判定测试(LoginOutcome):Halo 2.26 登录端点表单提交的成功与失败均为
|
||||
* 302 重定向(成功 → /uc、2FA 页;失败 → /login?error=...&method=...),仅 XHR/JSON
|
||||
* 请求以 200/401 区分成败,成败判定必须结合重定向目标。
|
||||
*/
|
||||
class LoginOutcomeTest {
|
||||
|
||||
// ===== POST /login:状态码 + 重定向目标 =====
|
||||
|
||||
@Test
|
||||
void xhrSuccessIsNotFailure() {
|
||||
// XHR/JSON 登录成功:200 + 主体 JSON
|
||||
assertThat(LoginOutcome.isFailure(200, null)).isFalse();
|
||||
assertThat(LoginOutcome.isFailure(204, null)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void xhrFailureIsFailure() {
|
||||
// XHR/JSON 登录失败:401 + JSON 错误体
|
||||
assertThat(LoginOutcome.isFailure(401, null)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void formSuccessRedirectIsNotFailure() {
|
||||
// 表单登录成功:302 → /uc(无 error 参数)
|
||||
assertThat(LoginOutcome.isFailure(302, URI.create("/uc"))).isFalse();
|
||||
// 两步验证:302 → /challenges/two-factor/totp
|
||||
assertThat(LoginOutcome.isFailure(302,
|
||||
URI.create("/challenges/two-factor/totp"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void formFailureRedirectWithErrorParamIsFailure() {
|
||||
// 表单登录失败:302 → /login?error=invalid-credential&method=local
|
||||
assertThat(LoginOutcome.isFailure(302,
|
||||
URI.create("/login?error=invalid-credential&method=local"))).isTrue();
|
||||
// 账号禁用/限流等其他失败码同样含 error 参数
|
||||
assertThat(LoginOutcome.isFailure(302,
|
||||
URI.create("/login?error=account-disabled&method=local"))).isTrue();
|
||||
assertThat(LoginOutcome.isFailure(302,
|
||||
URI.create("/login?error=rate-limit-exceeded&method=local"))).isTrue();
|
||||
// 兜底失败分支:error 参数无值(/login?error&method=...)
|
||||
assertThat(LoginOutcome.isFailure(302, URI.create("/login?error&method=local"))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void redirectWithoutLocationIsNotFailure() {
|
||||
// 3xx 但缺失 Location(无法确认失败):按成功处理,不凭空判失败
|
||||
assertThat(LoginOutcome.isFailure(302, null)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingOrZeroStatusIsFailure() {
|
||||
// 状态码缺失(响应未写出)按失败处理,宁可多告警不漏报
|
||||
assertThat(LoginOutcome.isFailure(null, null)).isTrue();
|
||||
assertThat(LoginOutcome.isFailure(0, null)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void errorParamNameMustMatchExactly() {
|
||||
// 含 error 字样的其他参数(如 errors=1)不算失败标记
|
||||
assertThat(LoginOutcome.isFailure(302, URI.create("/uc?errors=1"))).isFalse();
|
||||
assertThat(LoginOutcome.isFailure(302, URI.create("/login?errors=1"))).isFalse();
|
||||
}
|
||||
|
||||
// ===== GET /login?error=...:失败回显 =====
|
||||
|
||||
@Test
|
||||
void loginPageWithErrorParamIsFailureEcho() {
|
||||
assertThat(LoginOutcome.isFailureEcho("/login?error=invalid-credential&method=local"))
|
||||
.isTrue();
|
||||
assertThat(LoginOutcome.isFailureEcho("/login?error&method=local")).isTrue();
|
||||
assertThat(LoginOutcome.isFailureEcho("/login?method=local&error=invalid-credential"))
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void plainLoginPageIsNotFailureEcho() {
|
||||
assertThat(LoginOutcome.isFailureEcho("/login")).isFalse();
|
||||
assertThat(LoginOutcome.isFailureEcho("/login?method=local")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void otherEndpointsAreNotFailureEcho() {
|
||||
assertThat(LoginOutcome.isFailureEcho("/logout?error=x")).isFalse();
|
||||
assertThat(LoginOutcome.isFailureEcho("/apis/core.halo.run/v1alpha1/login?error=x"))
|
||||
.isFalse();
|
||||
assertThat(LoginOutcome.isFailureEcho("/uc?error=x")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullOrQuerylessPathIsNotFailureEcho() {
|
||||
assertThat(LoginOutcome.isFailureEcho(null)).isFalse();
|
||||
assertThat(LoginOutcome.isFailureEcho("/apis/content.halo.run/v1alpha1/posts")).isFalse();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user