现场反馈:审计列表「没有失败,全是成功,登录失败也显示成功」。在 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 升级场景联调记录见测试报告
104 lines
4.5 KiB
Java
104 lines
4.5 KiB
Java
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 区分成败,成败判定必须结合重定向目标。
|
||
*
|
||
* <p>判定原则:只有响应明确确认登录完成才算成功,其余一律按失败记录——审计里把
|
||
* 失败登录记成成功,远比把异常响应记成失败更糟。
|
||
*/
|
||
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();
|
||
// 登录前被弹回的原请求:302 → 已保存的受保护页面
|
||
assertThat(LoginOutcome.isFailure(302, URI.create("/console/posts"))).isFalse();
|
||
assertThat(LoginOutcome.isFailure(302,
|
||
URI.create("http://blog.guichumo.com/console/posts?page=1"))).isFalse();
|
||
}
|
||
|
||
@Test
|
||
void formFailureRedirectWithErrorParamIsFailure() {
|
||
// 表单登录失败:302 → /login?error=invalid-credential&method=local
|
||
// (Halo 2.26.1 LoginFailureHandler 的四种失败重定向:invalid-credential /
|
||
// account-disabled / rate-limit-exceeded / 无值 error)
|
||
assertThat(LoginOutcome.isFailure(302,
|
||
URI.create("/login?error=invalid-credential&method=local"))).isTrue();
|
||
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();
|
||
assertThat(LoginOutcome.isFailure(302, URI.create("/login?error&method=local"))).isTrue();
|
||
assertThat(LoginOutcome.isFailure(302,
|
||
URI.create("http://blog.guichumo.com/login?error=invalid-credential"))).isTrue();
|
||
}
|
||
|
||
@Test
|
||
void redirectBackToLoginPageWithoutErrorParamIsFailure() {
|
||
// 跳回登录页必然是失败:成功只会去 /uc、已保存的原请求或 2FA 页。
|
||
// 不依赖 error 参数——宿主或第三方登录扩展换了失败重定向格式也不会把失败记成成功
|
||
assertThat(LoginOutcome.isFailure(302, URI.create("/login"))).isTrue();
|
||
assertThat(LoginOutcome.isFailure(302, URI.create("/login?authentication_required")))
|
||
.isTrue();
|
||
assertThat(LoginOutcome.isFailure(307, URI.create("/login?logout"))).isTrue();
|
||
}
|
||
|
||
@Test
|
||
void redirectWithoutLocationIsFailure() {
|
||
// 3xx 但缺失 Location:响应异常,无法确认登录成功,不记成功
|
||
assertThat(LoginOutcome.isFailure(302, null)).isTrue();
|
||
}
|
||
|
||
@Test
|
||
void missingOrZeroStatusIsFailure() {
|
||
// 状态码缺失(响应未写出)按失败处理,宁可多告警不漏报
|
||
assertThat(LoginOutcome.isFailure(null, null)).isTrue();
|
||
assertThat(LoginOutcome.isFailure(0, null)).isTrue();
|
||
}
|
||
|
||
@Test
|
||
void clientAndServerErrorsAreFailure() {
|
||
// CSRF 拦截(403)、服务端异常(500)同为失败
|
||
assertThat(LoginOutcome.isFailure(403, URI.create("/login"))).isTrue();
|
||
assertThat(LoginOutcome.isFailure(500, null)).isTrue();
|
||
}
|
||
|
||
@Test
|
||
void errorParamNameMustMatchExactly() {
|
||
// 含 error 字样的其他参数(如 errors=1)不算失败标记
|
||
assertThat(LoginOutcome.isFailure(302, URI.create("/uc?errors=1"))).isFalse();
|
||
}
|
||
|
||
@Test
|
||
void loginPagePathMustMatchExactly() {
|
||
// 仅路径恰为 /login 才算跳回登录页;/login-xxx 等前缀相同的路径不算
|
||
assertThat(LoginOutcome.isFailure(302, URI.create("/login-callback"))).isFalse();
|
||
}
|
||
}
|