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:
+49
-3
@@ -116,9 +116,9 @@ class AuditExportEndpointTest {
|
||||
|
||||
@Test
|
||||
void shouldExportLoginFailureRowsAsFailureDespiteHttp2xx3xx() {
|
||||
// 登录失败标注(语义动作 login-failure):表单登录失败重定向(302)与失败回显页
|
||||
// (GET /login?error=...,200)状态码落在 2xx/3xx,结果列仍应为「失败」;
|
||||
// 描述列优先落库语义动作(登录失败),而非按路径重新推导出的「登录」
|
||||
// 登录失败标注(语义动作 login-failure):表单登录失败重定向(302)状态码落在
|
||||
// 2xx/3xx,结果列仍应为「失败」;描述列优先落库语义动作(登录失败)。
|
||||
// 升级前落库的失败回显页(GET /login?error=...,200,带同一标注)同样导出失败
|
||||
var time = Instant.parse("2026-11-01T00:00:00Z");
|
||||
var failedFormLogin = auditLog("auditlog-1", time, "unknown", "admin", "POST", "auth",
|
||||
null, "/login", 302, "119.53.161.18", "Mozilla/5.0");
|
||||
@@ -140,6 +140,52 @@ class AuditExportEndpointTest {
|
||||
+ ",登录失败,,失败,200,119.53.161.18,Mozilla/5.0");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldExportSuccessfulLoginAsSuccessInsteadOfBareRedirectCode() {
|
||||
// 登录成功(表单提交为 302):结果列写「成功」,与失败的「失败」对称
|
||||
var time = Instant.parse("2026-11-01T00:00:00Z");
|
||||
var success = auditLog("auditlog-1", time, "unknown", "admin", "POST", "auth", null,
|
||||
"/login", 302, "116.179.32.88", "Mozilla/5.0");
|
||||
success.getSpec().setAction("login");
|
||||
stubSinglePage(List.of(success));
|
||||
|
||||
var body = exportAndGetBody(EXPORT_URI);
|
||||
|
||||
var lines = body.split("\r\n");
|
||||
assertThat(lines[1]).isEqualTo(expectedTimeColumn(time)
|
||||
+ ",unknown,admin,POST,auth,,/login,登录,,成功,302,116.179.32.88,Mozilla/5.0");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldExportAuthPageVisitsAsPageVisitInsteadOfLoginSuccess() {
|
||||
// 升级前落库的页面访问记录(GET /login、GET /logout,带着 login/logout 标注、
|
||||
// HTTP 200):原先导出为「登录,成功」,看起来像未知用户登录成功;
|
||||
// 现在描述列写「访问登录页/访问登出页」,结果列写「页面访问」(不计成败)
|
||||
var time = Instant.parse("2026-12-04T14:16:36Z");
|
||||
var bounced = auditLog("auditlog-1", time, "unknown", null, "GET", "auth", null,
|
||||
"/login?authentication_required", 200, "43.228.157.244", "Mozilla/5.0");
|
||||
bounced.getSpec().setAction("login");
|
||||
var opened = auditLog("auditlog-2", time.plusSeconds(9), "unknown", null, "GET", "auth",
|
||||
null, "/login", 200, "43.228.157.244", "Mozilla/5.0");
|
||||
opened.getSpec().setAction("login");
|
||||
var logoutConfirm = auditLog("auditlog-3", time.plusSeconds(60), "unknown", null,
|
||||
"GET", "auth", null, "/logout", 200, "127.0.0.1", "Mozilla/5.0");
|
||||
logoutConfirm.getSpec().setAction("logout");
|
||||
stubSinglePage(List.of(bounced, opened, logoutConfirm));
|
||||
|
||||
var body = exportAndGetBody(EXPORT_URI);
|
||||
|
||||
var lines = body.split("\r\n");
|
||||
assertThat(lines).hasSize(4);
|
||||
assertThat(lines[1]).isEqualTo(expectedTimeColumn(time)
|
||||
+ ",unknown,,GET,auth,,/login?authentication_required,访问登录页,,页面访问,200"
|
||||
+ ",43.228.157.244,Mozilla/5.0");
|
||||
assertThat(lines[2]).isEqualTo(expectedTimeColumn(time.plusSeconds(9))
|
||||
+ ",unknown,,GET,auth,,/login,访问登录页,,页面访问,200,43.228.157.244,Mozilla/5.0");
|
||||
assertThat(lines[3]).isEqualTo(expectedTimeColumn(time.plusSeconds(60))
|
||||
+ ",unknown,,GET,auth,,/logout,访问登出页,,页面访问,200,127.0.0.1,Mozilla/5.0");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldExportChangesColumnWhenRecordHasChanges() {
|
||||
var time = Instant.parse("2026-11-04T00:00:00Z");
|
||||
|
||||
+29
-5
@@ -5,6 +5,7 @@ import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.after;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.timeout;
|
||||
@@ -69,6 +70,9 @@ class AuditFilterChainIntegrationTest {
|
||||
/** doFinally 回调为响应式异步执行,断言等待上限(毫秒)。 */
|
||||
private static final long CALLBACK_TIMEOUT_MS = 5_000;
|
||||
|
||||
/** 「不记录」断言的等待时长(毫秒):确认透传请求在全链上始终没有触发记录。 */
|
||||
private static final long PASS_THROUGH_WAIT_MS = 500;
|
||||
|
||||
private static final String LOGIN_FORM = "username=admin&password=right-password";
|
||||
|
||||
private AuditRecorder recorder;
|
||||
@@ -212,16 +216,36 @@ class AuditFilterChainIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRecordLoginPageVisitExactlyOnceThroughRealChain() {
|
||||
// GET /login 会流经全链(无终止过滤器命中):必须仅由链首过滤器记录一次,
|
||||
// 链尾过滤器不得重复记录(两个过滤器按 CaptureRule 分治、互不重叠)
|
||||
void shouldNotRecordLoginPageVisitThroughRealChain() {
|
||||
// 登录页的 GET 访问不是操作:打开登录页、被弹回登录页(?authentication_required)、
|
||||
// 登录失败后的回显(?error=...)都只渲染页面,全链任何位置都不得落库——
|
||||
// 否则列表里全是「未知用户 · 登录 · 200」,真正的登录成败被淹没
|
||||
for (var path : List.of("/login", "/login?authentication_required",
|
||||
"/login?error=invalid-credential&method=local")) {
|
||||
var exchange = MockServerWebExchange.from(MockServerHttpRequest
|
||||
.get(path)
|
||||
.build());
|
||||
|
||||
proxy.filter(exchange, OUTER_CHAIN).block();
|
||||
}
|
||||
|
||||
// 真实链的记录回调可能异步晚于 block() 返回:等待一段时间后仍为零调用才算数
|
||||
verify(recorder, after(PASS_THROUGH_WAIT_MS).never())
|
||||
.record(any(RequestSnapshot.class), any(), any(), any(), any(), anyBoolean());
|
||||
verify(loginFailureAlertService, never())
|
||||
.onLoginResult(any(RequestSnapshot.class), anyBoolean(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotRecordLogoutConfirmPageThroughRealChain() {
|
||||
// 登出确认页(GET /logout)同样只是页面访问
|
||||
var exchange = MockServerWebExchange.from(MockServerHttpRequest
|
||||
.get("/login")
|
||||
.get("/logout")
|
||||
.build());
|
||||
|
||||
proxy.filter(exchange, OUTER_CHAIN).block();
|
||||
|
||||
verify(recorder, timeout(CALLBACK_TIMEOUT_MS).times(1))
|
||||
verify(recorder, after(PASS_THROUGH_WAIT_MS).never())
|
||||
.record(any(RequestSnapshot.class), any(), any(), any(), any(), anyBoolean());
|
||||
}
|
||||
|
||||
|
||||
+46
-4
@@ -106,13 +106,16 @@ class AuditQueryEndpointTest {
|
||||
var captor = ArgumentCaptor.forClass(ListOptions.class);
|
||||
verify(client).listBy(eq(AuditLog.class), captor.capture(), any(PageRequest.class));
|
||||
var condition = captor.getValue().toCondition().toString();
|
||||
// failure = statusCode >= 400(无上界)或语义动作标注 login-failure
|
||||
// (登录失败的 HTTP 状态码可能为 200/302)
|
||||
// failure = 登录失败标注(表单登录失败的 HTTP 状态码是 302,仅凭状态码识别不出)
|
||||
// 或 4xx/5xx;页面访问记录(非 POST 的 /login、/logout)不计成败,予以排除
|
||||
assertThat(condition)
|
||||
.contains("spec.statusCode")
|
||||
.contains("400")
|
||||
.contains("spec.action")
|
||||
.contains("login-failure");
|
||||
.contains("login-failure")
|
||||
.contains("spec.method")
|
||||
.contains("/login")
|
||||
.contains("/logout");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -126,10 +129,49 @@ class AuditQueryEndpointTest {
|
||||
verify(client).listBy(eq(AuditLog.class), captor.capture(), any(PageRequest.class));
|
||||
var condition = captor.getValue().toCondition().toString();
|
||||
// success = 2xx/3xx 且排除登录失败标注(其状态码可能落在 2xx/3xx 区间)
|
||||
// 与页面访问记录(升级前落库的 GET /login、GET /logout,HTTP 200)
|
||||
assertThat(condition)
|
||||
.contains("spec.statusCode")
|
||||
.contains("spec.action")
|
||||
.contains("login-failure");
|
||||
.contains("login-failure")
|
||||
.contains("spec.method")
|
||||
.contains("/login")
|
||||
.contains("/logout");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldExcludePageVisitsWhenFilteringByLoginOrLogoutAction() {
|
||||
// 升级前落库的页面访问记录带着 login/logout 标注,但展示为「访问登录页/登出页」:
|
||||
// 按语义动作筛登录/登出操作时不混入
|
||||
webClient.get()
|
||||
.uri("/apis/audit.guichumo.com/v1alpha1/auditlogs/-/query"
|
||||
+ "?fieldSelector=spec.action=login")
|
||||
.exchange()
|
||||
.expectStatus().isOk();
|
||||
|
||||
var captor = ArgumentCaptor.forClass(ListOptions.class);
|
||||
verify(client).listBy(eq(AuditLog.class), captor.capture(), any(PageRequest.class));
|
||||
var condition = captor.getValue().toCondition().toString();
|
||||
assertThat(condition)
|
||||
.contains("spec.action")
|
||||
.contains("login")
|
||||
.contains("spec.method")
|
||||
.contains("/login");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldKeepPlainEqualityForOtherSemanticActions() {
|
||||
// 其余语义动作的等值筛选不带页面访问排除条件
|
||||
webClient.get()
|
||||
.uri("/apis/audit.guichumo.com/v1alpha1/auditlogs/-/query"
|
||||
+ "?fieldSelector=spec.action=login-failure")
|
||||
.exchange()
|
||||
.expectStatus().isOk();
|
||||
|
||||
var captor = ArgumentCaptor.forClass(ListOptions.class);
|
||||
verify(client).listBy(eq(AuditLog.class), captor.capture(), any(PageRequest.class));
|
||||
var condition = captor.getValue().toCondition().toString();
|
||||
assertThat(condition).doesNotContain("spec.method");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+7
-13
@@ -120,7 +120,7 @@ class AuditRecorderTest {
|
||||
|
||||
@Test
|
||||
void shouldAnnotateLoginFailureActionOnFailedLoginAttempt() {
|
||||
// 表单登录失败(302 重定向目标含 error 参数):过滤器传入 loginFailed=true,
|
||||
// 表单登录失败(302 重定向回跳登录页):过滤器传入 loginFailed=true,
|
||||
// 语义动作标注 login-failure;状态码仍如实记录 302
|
||||
recorder.record(snapshotOf("/login"), new ChangeCapture(), 302,
|
||||
AuditLogSpec.UNKNOWN_USERNAME, null, true);
|
||||
@@ -133,22 +133,16 @@ class AuditRecorderTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAnnotateLoginFailureActionOnFailureEcho() {
|
||||
// 登录失败回显(GET /login?error=... 页面渲染 HTTP 200):同样标注登录失败
|
||||
var snapshot = RequestSnapshot.of(MockServerHttpRequest
|
||||
.get("http://localhost/login?error=invalid-credential&method=local")
|
||||
.remoteAddress(new InetSocketAddress(InetAddress.getLoopbackAddress(), 45678))
|
||||
.build());
|
||||
|
||||
recorder.record(snapshot, new ChangeCapture(), 200,
|
||||
AuditLogSpec.UNKNOWN_USERNAME, null, true);
|
||||
void shouldAnnotateLogoutWithoutFailureMark() {
|
||||
// 登出(POST /logout)是认证操作但不是登录尝试:语义动作 logout,不标注登录失败
|
||||
recorder.record(snapshotOf("/logout"), new ChangeCapture(), 302,
|
||||
AuditLogSpec.UNKNOWN_USERNAME, null, false);
|
||||
|
||||
var captor = ArgumentCaptor.forClass(AuditLog.class);
|
||||
verify(client).create(captor.capture());
|
||||
var spec = captor.getValue().getSpec();
|
||||
assertThat(spec.getAction()).isEqualTo("login-failure");
|
||||
assertThat(spec.getStatusCode()).isEqualTo(200);
|
||||
assertThat(spec.getPath()).isEqualTo("/login?error=invalid-credential&method=local");
|
||||
assertThat(spec.getAction()).isEqualTo("logout");
|
||||
assertThat(spec.getStatusCode()).isEqualTo(302);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
package run.halo.operationaudit.audit;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* 结果语义测试(AuditResult):结果筛选与 CSV 导出共用的成败判定。
|
||||
* 登录失败不能只看状态码(表单登录失败的 HTTP 状态码与成功相同,均为 302);
|
||||
* 认证端点的页面访问(升级前落库的 GET 记录)不是操作,不计成败。
|
||||
*/
|
||||
class AuditResultTest {
|
||||
|
||||
private static AuditLogSpec spec(String method, String path, Integer statusCode,
|
||||
String action) {
|
||||
var spec = new AuditLogSpec();
|
||||
spec.setMethod(method);
|
||||
spec.setResourceType("auth");
|
||||
spec.setPath(path);
|
||||
spec.setStatusCode(statusCode);
|
||||
spec.setAction(action);
|
||||
return spec;
|
||||
}
|
||||
|
||||
@Test
|
||||
void loginFailureIsFailureRegardlessOfStatusCode() {
|
||||
// 表单登录失败:302(与成功相同的状态码),只能靠落库标注识别
|
||||
assertThat(AuditResult.of(spec("POST", "/login", 302, "login-failure")))
|
||||
.isEqualTo(AuditResult.FAILURE);
|
||||
// XHR 登录失败、CSRF 拦截
|
||||
assertThat(AuditResult.of(spec("POST", "/login", 401, "login-failure")))
|
||||
.isEqualTo(AuditResult.FAILURE);
|
||||
assertThat(AuditResult.of(spec("POST", "/login", 403, "login-failure")))
|
||||
.isEqualTo(AuditResult.FAILURE);
|
||||
// 升级前落库的失败回显(GET /login?error=...,HTTP 200):带标注或由路径推导
|
||||
assertThat(AuditResult.of(spec("GET", "/login?error=invalid-credential", 200,
|
||||
"login-failure"))).isEqualTo(AuditResult.FAILURE);
|
||||
assertThat(AuditResult.of(spec("GET", "/login?error=invalid-credential", 200, null)))
|
||||
.isEqualTo(AuditResult.FAILURE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void successfulLoginIsSuccess() {
|
||||
assertThat(AuditResult.of(spec("POST", "/login", 302, "login")))
|
||||
.isEqualTo(AuditResult.SUCCESS);
|
||||
assertThat(AuditResult.of(spec("POST", "/login", 200, "login")))
|
||||
.isEqualTo(AuditResult.SUCCESS);
|
||||
}
|
||||
|
||||
@Test
|
||||
void authPageVisitsAreNeitherSuccessNorFailure() {
|
||||
// 升级前落库的页面访问记录:当时带着 login/logout 标注、HTTP 200——
|
||||
// 原先显示为绿色「登录 200」,看起来像未知用户登录成功
|
||||
assertThat(AuditResult.of(spec("GET", "/login", 200, "login")))
|
||||
.isEqualTo(AuditResult.PAGE_VISIT);
|
||||
assertThat(AuditResult.of(spec("GET", "/login?authentication_required", 200, "login")))
|
||||
.isEqualTo(AuditResult.PAGE_VISIT);
|
||||
assertThat(AuditResult.of(spec("GET", "/logout", 200, "logout")))
|
||||
.isEqualTo(AuditResult.PAGE_VISIT);
|
||||
// 无标注的历史记录、页面自身出错(5xx)同样不计成败
|
||||
assertThat(AuditResult.of(spec("GET", "/login", 200, null)))
|
||||
.isEqualTo(AuditResult.PAGE_VISIT);
|
||||
assertThat(AuditResult.of(spec("GET", "/login", 500, "login")))
|
||||
.isEqualTo(AuditResult.PAGE_VISIT);
|
||||
}
|
||||
|
||||
@Test
|
||||
void ordinaryRecordsFollowStatusCode() {
|
||||
assertThat(AuditResult.of(spec("POST", "/apis/x/v1/users", 201, "create")))
|
||||
.isEqualTo(AuditResult.SUCCESS);
|
||||
assertThat(AuditResult.of(spec("POST", "/apis/x/v1/users", 302, "create")))
|
||||
.isEqualTo(AuditResult.SUCCESS);
|
||||
assertThat(AuditResult.of(spec("POST", "/apis/x/v1/users", 403, "create")))
|
||||
.isEqualTo(AuditResult.FAILURE);
|
||||
assertThat(AuditResult.of(spec("POST", "/apis/x/v1/users", 500, "create")))
|
||||
.isEqualTo(AuditResult.FAILURE);
|
||||
assertThat(AuditResult.of(spec("POST", "/apis/x/v1/users", null, "create")))
|
||||
.isEqualTo(AuditResult.FAILURE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void labelsAreUsedForCsvResultColumn() {
|
||||
assertThat(AuditResult.SUCCESS.label()).isEqualTo("成功");
|
||||
assertThat(AuditResult.FAILURE.label()).isEqualTo("失败");
|
||||
assertThat(AuditResult.PAGE_VISIT.label()).isEqualTo("页面访问");
|
||||
}
|
||||
}
|
||||
+61
-18
@@ -11,6 +11,7 @@ import static org.mockito.Mockito.verify;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
@@ -27,8 +28,9 @@ import run.halo.operationaudit.alert.LoginFailureAlertService;
|
||||
/**
|
||||
* 认证端点捕获过滤器行为测试(P0 修复):POST /login、POST /logout 必须产生审计记录
|
||||
* (真实环境中表单登录/登出过滤器在链尾过滤器之前直接完成响应,因此由本过滤器在
|
||||
* Security 链之前承接)。操作者统一为未知用户,登录尝试账号由请求体白名单提取;
|
||||
* 登录失败告警回调仅发生在 POST /login;非认证端点请求一律透传、零副作用。
|
||||
* Security 链之前承接)。登录/登出页面的 GET 访问不是操作,一律透传不记录。
|
||||
* 操作者统一为未知用户,登录尝试账号由请求体白名单提取;登录失败告警回调仅发生在
|
||||
* POST /login;非认证端点请求一律透传、零副作用。
|
||||
*/
|
||||
class AuthEndpointAuditWebFilterTest {
|
||||
|
||||
@@ -130,34 +132,75 @@ class AuthEndpointAuditWebFilterTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRecordLoginPageVisitWithoutFailureMark() {
|
||||
// 首次打开登录页(无 error 参数):记录登录页访问,不标注登录失败
|
||||
var exchange = MockServerWebExchange.from(MockServerHttpRequest
|
||||
.get("/login")
|
||||
.build());
|
||||
void shouldPassThroughLoginPageVisitsWithoutRecording() {
|
||||
// 登录页的 GET 访问不是登录操作:打开登录页、匿名访问被弹回登录页
|
||||
// (/login?authentication_required)、登录失败后的回显(/login?error=...)
|
||||
// 都只渲染页面(HTTP 200)。原先一律落库为「未知用户 · 登录 · 200」,在列表里
|
||||
// 与登录成功无法区分(现场反馈:全是成功、登录失败也显示成功)——现在原样透传
|
||||
for (var path : List.of("/login", "/login?authentication_required",
|
||||
"/login?error=invalid-credential&method=local", "/login?logout")) {
|
||||
var exchange = MockServerWebExchange.from(MockServerHttpRequest.get(path).build());
|
||||
|
||||
StepVerifier.create(filter.filter(exchange, CHAIN)).verifyComplete();
|
||||
StepVerifier.create(filter.filter(exchange, CHAIN)).verifyComplete();
|
||||
}
|
||||
|
||||
verify(recorder).record(any(RequestSnapshot.class), isNull(), eq(200),
|
||||
eq(AuditLogSpec.UNKNOWN_USERNAME), isNull(), eq(false));
|
||||
verify(recorder, never()).record(any(), any(), any(), any(), any(), anyBoolean());
|
||||
verify(writeMarker, never()).onRequestStart(any());
|
||||
verify(writeMarker, never()).onRequestComplete(any());
|
||||
verify(loginFailureAlertService, never())
|
||||
.onLoginResult(any(RequestSnapshot.class), anyBoolean(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRecordLoginPageErrorEchoAsLoginFailure() {
|
||||
// 登录失败回显:GET /login?error=... 页面渲染 HTTP 200,标注登录失败,
|
||||
// 但不是登录尝试、不触发告警
|
||||
void shouldPassThroughLogoutConfirmPageWithoutRecording() {
|
||||
// 登出确认页(GET /logout)同样只是页面访问,不是登出操作
|
||||
var exchange = MockServerWebExchange.from(MockServerHttpRequest
|
||||
.get("/login?error=invalid-credential&method=local")
|
||||
.get("/logout")
|
||||
.build());
|
||||
|
||||
StepVerifier.create(filter.filter(exchange, CHAIN)).verifyComplete();
|
||||
|
||||
verify(recorder).record(any(RequestSnapshot.class), isNull(), eq(200),
|
||||
eq(AuditLogSpec.UNKNOWN_USERNAME), isNull(), eq(true));
|
||||
verify(loginFailureAlertService, never())
|
||||
.onLoginResult(any(RequestSnapshot.class), anyBoolean(), any());
|
||||
verify(recorder, never()).record(any(), any(), any(), any(), any(), anyBoolean());
|
||||
verify(writeMarker, never()).onRequestStart(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRecordLoginFailureWhenRedirectedBackToLoginWithoutErrorParam() {
|
||||
// 302 跳回登录页且不带 error 参数:不可能是登录成功(成功只会去 /uc、已保存的
|
||||
// 原请求或 2FA 页),同样判失败——只有响应明确确认才算成功
|
||||
var exchange = loginExchange("bob", "wrong");
|
||||
|
||||
StepVerifier.create(filter.filter(exchange, new RedirectWebFilterChainStub(
|
||||
URI.create("/login")))).verifyComplete();
|
||||
|
||||
verify(recorder).record(any(RequestSnapshot.class), isNull(), eq(302),
|
||||
eq(AuditLogSpec.UNKNOWN_USERNAME), eq("bob"), eq(true));
|
||||
verify(loginFailureAlertService).onLoginResult(any(RequestSnapshot.class), eq(false),
|
||||
eq("bob"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRecordLoginFailureWhenRedirectHasNoLocation() {
|
||||
// 3xx 但缺失 Location:响应异常,无法确认登录成功,不记成功
|
||||
var exchange = loginExchange("bob", "wrong");
|
||||
WebFilterChain foundWithoutLocation = ex -> {
|
||||
ex.getResponse().setStatusCode(HttpStatus.FOUND);
|
||||
return Mono.empty();
|
||||
};
|
||||
|
||||
StepVerifier.create(filter.filter(exchange, foundWithoutLocation)).verifyComplete();
|
||||
|
||||
verify(recorder).record(any(RequestSnapshot.class), isNull(), eq(302),
|
||||
eq(AuditLogSpec.UNKNOWN_USERNAME), eq("bob"), eq(true));
|
||||
}
|
||||
|
||||
private static MockServerWebExchange loginExchange(String username, String password) {
|
||||
var body = "username=" + username + "&password=" + password;
|
||||
return MockServerWebExchange.from(MockServerHttpRequest
|
||||
.post("/login")
|
||||
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
||||
.contentLength(body.getBytes(StandardCharsets.UTF_8).length)
|
||||
.body(body));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+18
-1
@@ -29,7 +29,7 @@ class CaptureRuleTest {
|
||||
|
||||
@Test
|
||||
void shouldNotCaptureAuthEndpointsInApisRule() {
|
||||
// P0 修复后认证端点由 AuthEndpointAuditWebFilter 独立捕获(isAuthEndpoint),
|
||||
// 认证操作由 AuthEndpointAuditWebFilter 独立捕获(isAuthOperation),
|
||||
// 通用规则不再包含,两个过滤器按规则分治、互不重叠
|
||||
assertThat(shouldCapture("/login", "POST")).isFalse();
|
||||
assertThat(shouldCapture("/login", "GET")).isFalse();
|
||||
@@ -71,4 +71,21 @@ class CaptureRuleTest {
|
||||
assertThat(CaptureRule.isLoginAttempt(null, "POST")).isFalse();
|
||||
assertThat(CaptureRule.isLoginAttempt("/login", null)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldTreatOnlyPostAsAuthOperation() {
|
||||
// 认证操作 = POST /login(登录尝试)+ POST /logout(登出)
|
||||
assertThat(CaptureRule.isAuthOperation("/login", "POST")).isTrue();
|
||||
assertThat(CaptureRule.isAuthOperation("/logout", "POST")).isTrue();
|
||||
// 登录/登出页面的 GET 访问不是操作:打开登录页、被弹回登录页、失败回显、登出确认页
|
||||
assertThat(CaptureRule.isAuthOperation("/login", "GET")).isFalse();
|
||||
assertThat(CaptureRule.isAuthOperation("/logout", "GET")).isFalse();
|
||||
// 其余方法与路径一律不算
|
||||
assertThat(CaptureRule.isAuthOperation("/login", "HEAD")).isFalse();
|
||||
assertThat(CaptureRule.isAuthOperation("/login", "PUT")).isFalse();
|
||||
assertThat(CaptureRule.isAuthOperation("/loginx", "POST")).isFalse();
|
||||
assertThat(CaptureRule.isAuthOperation("/console/login", "POST")).isFalse();
|
||||
assertThat(CaptureRule.isAuthOperation(null, "POST")).isFalse();
|
||||
assertThat(CaptureRule.isAuthOperation("/login", null)).isFalse();
|
||||
}
|
||||
}
|
||||
|
||||
+34
-34
@@ -9,6 +9,9 @@ import org.junit.jupiter.api.Test;
|
||||
* 登录结果判定测试(LoginOutcome):Halo 2.26 登录端点表单提交的成功与失败均为
|
||||
* 302 重定向(成功 → /uc、2FA 页;失败 → /login?error=...&method=...),仅 XHR/JSON
|
||||
* 请求以 200/401 区分成败,成败判定必须结合重定向目标。
|
||||
*
|
||||
* <p>判定原则:只有响应明确确认登录完成才算成功,其余一律按失败记录——审计里把
|
||||
* 失败登录记成成功,远比把异常响应记成失败更糟。
|
||||
*/
|
||||
class LoginOutcomeTest {
|
||||
|
||||
@@ -34,26 +37,42 @@ class LoginOutcomeTest {
|
||||
// 两步验证: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();
|
||||
// 账号禁用/限流等其他失败码同样含 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();
|
||||
assertThat(LoginOutcome.isFailure(302,
|
||||
URI.create("http://blog.guichumo.com/login?error=invalid-credential"))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void redirectWithoutLocationIsNotFailure() {
|
||||
// 3xx 但缺失 Location(无法确认失败):按成功处理,不凭空判失败
|
||||
assertThat(LoginOutcome.isFailure(302, null)).isFalse();
|
||||
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
|
||||
@@ -63,41 +82,22 @@ class LoginOutcomeTest {
|
||||
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();
|
||||
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();
|
||||
void loginPagePathMustMatchExactly() {
|
||||
// 仅路径恰为 /login 才算跳回登录页;/login-xxx 等前缀相同的路径不算
|
||||
assertThat(LoginOutcome.isFailure(302, URI.create("/login-callback"))).isFalse();
|
||||
}
|
||||
}
|
||||
|
||||
+105
-6
@@ -12,15 +12,24 @@ class OperationDescriptionTest {
|
||||
|
||||
@Test
|
||||
void shouldDescribeLoginEndpointIncludingQuery() {
|
||||
// POST /login 是登录操作;GET /login(含任意查询串)只是打开登录页
|
||||
assertThat(OperationDescription.describe("POST", "auth", "/login")).isEqualTo("登录");
|
||||
// GET /login?redirect_uri=... 也应识别为登录(path 含查询串)
|
||||
assertThat(OperationDescription.describe("GET", "auth",
|
||||
"/login?redirect_uri=https%3A%2F%2Fwww.guichumo.com%2Fconsole")).isEqualTo("登录");
|
||||
"/login?redirect_uri=https%3A%2F%2Fwww.guichumo.com%2Fconsole"))
|
||||
.isEqualTo("访问登录页");
|
||||
assertThat(OperationDescription.describe("GET", "auth", "/login?authentication_required"))
|
||||
.isEqualTo("访问登录页");
|
||||
// 升级前落库的失败回显(GET /login?error=...)无标注时由 error 参数现场推导
|
||||
assertThat(OperationDescription.describe("GET", "auth",
|
||||
"/login?error=invalid-credential&method=local")).isEqualTo("登录失败");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDescribeLogoutEndpoint() {
|
||||
assertThat(OperationDescription.describe("POST", "auth", "/logout")).isEqualTo("登出");
|
||||
// GET /logout 是登出确认页
|
||||
assertThat(OperationDescription.describe("GET", "auth", "/logout"))
|
||||
.isEqualTo("访问登出页");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -104,14 +113,46 @@ class OperationDescriptionTest {
|
||||
@Test
|
||||
void shouldDeriveLoginLogoutAuthActions() {
|
||||
assertThat(OperationDescription.actionOf("POST", "auth", "/login")).isEqualTo("login");
|
||||
// GET /login?redirect_uri=... 也应识别为登录(path 含查询串)
|
||||
assertThat(OperationDescription.actionOf("GET", "auth",
|
||||
"/login?redirect_uri=https%3A%2F%2Fwww.guichumo.com%2Fconsole")).isEqualTo("login");
|
||||
assertThat(OperationDescription.actionOf("POST", "auth", "/logout")).isEqualTo("logout");
|
||||
assertThat(OperationDescription.actionOf("POST", "auth", "/auth/other"))
|
||||
.isEqualTo("auth");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDeriveAuthPageVisitsByMethod() {
|
||||
// 认证端点的页面访问(非 POST):打开登录页、被弹回登录页、登出确认页
|
||||
assertThat(OperationDescription.actionOf("GET", "auth", "/login"))
|
||||
.isEqualTo("login-page");
|
||||
assertThat(OperationDescription.actionOf("GET", "auth",
|
||||
"/login?authentication_required")).isEqualTo("login-page");
|
||||
assertThat(OperationDescription.actionOf("GET", "auth", "/logout"))
|
||||
.isEqualTo("logout-page");
|
||||
// 失败回显(路径含 error 参数)推导为登录失败
|
||||
assertThat(OperationDescription.actionOf("GET", "auth",
|
||||
"/login?error=invalid-credential&method=local"))
|
||||
.isEqualTo(OperationDescription.LOGIN_FAILURE_ACTION);
|
||||
assertThat(OperationDescription.actionOf("GET", "auth", "/login?error&method=local"))
|
||||
.isEqualTo(OperationDescription.LOGIN_FAILURE_ACTION);
|
||||
// 含 error 字样的其他参数不算失败标记
|
||||
assertThat(OperationDescription.actionOf("GET", "auth", "/login?errors=1"))
|
||||
.isEqualTo("login-page");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDetectAuthPageVisits() {
|
||||
assertThat(OperationDescription.isAuthPageVisit("GET", "/login")).isTrue();
|
||||
assertThat(OperationDescription.isAuthPageVisit("GET", "/login?error=x")).isTrue();
|
||||
assertThat(OperationDescription.isAuthPageVisit("GET", "/logout")).isTrue();
|
||||
// POST 是真正的登录/登出操作
|
||||
assertThat(OperationDescription.isAuthPageVisit("POST", "/login")).isFalse();
|
||||
assertThat(OperationDescription.isAuthPageVisit("POST", "/logout")).isFalse();
|
||||
// 其余路径一律不算
|
||||
assertThat(OperationDescription.isAuthPageVisit("GET", "/apis/core.halo.run/v1alpha1/posts"))
|
||||
.isFalse();
|
||||
assertThat(OperationDescription.isAuthPageVisit("GET", "/loginx")).isFalse();
|
||||
assertThat(OperationDescription.isAuthPageVisit("GET", null)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDeriveMethodActions() {
|
||||
assertThat(OperationDescription.actionOf("POST", "users",
|
||||
@@ -197,10 +238,15 @@ class OperationDescriptionTest {
|
||||
@Test
|
||||
void shouldDescribeFromActionKey() {
|
||||
assertThat(OperationDescription.describeAction("login", "auth")).isEqualTo("登录");
|
||||
// 登录失败语义动作(POST /login 失败或 GET /login?error=... 失败回显)
|
||||
// 登录失败语义动作(POST /login 失败,或升级前落库的 GET /login?error=... 失败回显)
|
||||
assertThat(OperationDescription.describeAction("login-failure", "auth"))
|
||||
.isEqualTo("登录失败");
|
||||
assertThat(OperationDescription.describeAction("logout", "auth")).isEqualTo("登出");
|
||||
// 认证端点的页面访问(升级前落库的历史 GET 记录)
|
||||
assertThat(OperationDescription.describeAction("login-page", "auth"))
|
||||
.isEqualTo("访问登录页");
|
||||
assertThat(OperationDescription.describeAction("logout-page", "auth"))
|
||||
.isEqualTo("访问登出页");
|
||||
assertThat(OperationDescription.describeAction("auth", "auth")).isEqualTo("认证操作");
|
||||
assertThat(OperationDescription.describeAction("create", "users")).isEqualTo("新建用户");
|
||||
assertThat(OperationDescription.describeAction("update", "posts")).isEqualTo("修改文章");
|
||||
@@ -244,4 +290,57 @@ class OperationDescriptionTest {
|
||||
bogus.setAction("bogus");
|
||||
assertThat(OperationDescription.describe(bogus)).isEqualTo("修改文章");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCorrectAuthPageVisitsRegardlessOfStoredAction() {
|
||||
// 升级前落库的页面访问记录带着 login/logout 标注(当时 GET 也被当成登录/登出记录):
|
||||
// 展示必须按方法纠正为页面访问,否则这些记录继续显示成「未知用户 · 登录」
|
||||
var loginPage = new AuditLogSpec();
|
||||
loginPage.setMethod("GET");
|
||||
loginPage.setResourceType("auth");
|
||||
loginPage.setPath("/login?authentication_required");
|
||||
loginPage.setAction("login");
|
||||
assertThat(OperationDescription.semanticAction(loginPage)).isEqualTo("login-page");
|
||||
assertThat(OperationDescription.describe(loginPage)).isEqualTo("访问登录页");
|
||||
|
||||
var logoutPage = new AuditLogSpec();
|
||||
logoutPage.setMethod("GET");
|
||||
logoutPage.setResourceType("auth");
|
||||
logoutPage.setPath("/logout");
|
||||
logoutPage.setAction("logout");
|
||||
assertThat(OperationDescription.semanticAction(logoutPage)).isEqualTo("logout-page");
|
||||
assertThat(OperationDescription.describe(logoutPage)).isEqualTo("访问登出页");
|
||||
|
||||
// 无标注的页面访问记录同样推导为页面访问
|
||||
var unannotated = new AuditLogSpec();
|
||||
unannotated.setMethod("GET");
|
||||
unannotated.setResourceType("auth");
|
||||
unannotated.setPath("/login");
|
||||
assertThat(OperationDescription.describe(unannotated)).isEqualTo("访问登录页");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldKeepStoredActionForAuthOperations() {
|
||||
// POST 是真正的认证操作:落库标注优先(成功 login / 失败 login-failure / 登出 logout)
|
||||
var success = new AuditLogSpec();
|
||||
success.setMethod("POST");
|
||||
success.setResourceType("auth");
|
||||
success.setPath("/login");
|
||||
success.setAction("login");
|
||||
assertThat(OperationDescription.semanticAction(success)).isEqualTo("login");
|
||||
|
||||
var failure = new AuditLogSpec();
|
||||
failure.setMethod("POST");
|
||||
failure.setResourceType("auth");
|
||||
failure.setPath("/login");
|
||||
failure.setAction("login-failure");
|
||||
assertThat(OperationDescription.semanticAction(failure)).isEqualTo("login-failure");
|
||||
|
||||
var logout = new AuditLogSpec();
|
||||
logout.setMethod("POST");
|
||||
logout.setResourceType("auth");
|
||||
logout.setPath("/logout");
|
||||
logout.setAction("logout");
|
||||
assertThat(OperationDescription.semanticAction(logout)).isEqualTo("logout");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user