fix: audit target is the record acted on, never a typed username; guard retention

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-11-30 17:16:29 +08:00
1 parent 81ef54f996
commit 7aa5c4b921
10 files changed
+61 -7

No files matched your search

@@ -3,6 +3,7 @@ package com.solvway.mobile.api.controller.admin;
import com.solvway.core.annotation.AppAuth;
import com.solvway.mobile.api.service.AdminLoginSecurityService;
import com.solvway.mobile.api.useraccess.AdminModule;
import com.solvway.mobile.api.useraccess.Audited;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
@@ -44,6 +45,7 @@ public class AdminLoginSecurityController {
return ResponseEntity.ok(loginSecurity.locked());
}
@Audited(target = false) // the username is whatever was typed at sign-in, possibly a password
@AppAuth
@PostMapping("/locked/{username}/unlock")
public ResponseEntity<Void> unlock(@PathVariable String username) {
@@ -181,9 +181,14 @@ public class AppAccessAspect {
event.setHttpMethod(request.getMethod());
Object pattern = request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
event.setPathTemplate(pattern == null ? null : pattern.toString());
if (request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE) instanceof Map<?, ?> vars
// The last path variable is the record acted on (/events/{eventUuid}/leads/{eventLeadUuid}); Spring
// fills the map in template order.
if ((audited == null || audited.target())
&& request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE) instanceof Map<?, ?> vars
&& !vars.isEmpty()) {
event.setTargetId(String.valueOf(vars.values().iterator().next()));
Object last = null;
for (Object v : vars.values()) last = v;
event.setTargetId(String.valueOf(last));
}
event.setHttpStatus(status);
event.setOutcome(status >= 200 && status < 300 ? AuditEvent.OUTCOME_SUCCESS : AuditEvent.OUTCOME_FAILURE);
@@ -19,4 +19,7 @@ public @interface Audited {
/** Never audit this handler (read-only POST). */
boolean skip() default false;
/** False when the path variable is free text a person typed (e.g. a username from a failed sign-in): no target is recorded. */
boolean target() default true;
}
@@ -93,6 +93,10 @@ class AppAccessAspectAuditTest {
@Audited(skip = true)
public void match() {
}
@Audited(target = false)
public void unlock() {
}
}
@BeforeEach
@@ -180,6 +184,27 @@ class AppAccessAspectAuditTest {
assertThat(e.getChanges()).isNull();
}
@Test
void target_isTheLastPathVariable() throws Throwable {
Map<String, String> vars = new LinkedHashMap<>();
vars.put("eventUuid", "E1");
vars.put("eventLeadUuid", "L9");
req.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, vars);
handler(AdminUserController.class, "update");
admin(1L);
call();
assertThat(published().getTargetId()).isEqualTo("L9");
}
@Test
void target_isNotRecordedWhenTheHandlerOptsOut() throws Throwable {
req.setMethod("POST");
handler(AdminUserController.class, "unlock");
admin(1L);
call();
assertThat(published().getTargetId()).isNull();
}
@Test
void module_fallsBackToClassLevel() throws Throwable {
handler(AdminUserController.class, "classModule");