fix(mobile-api): stop admins widening their own access

- A non-super admin cannot change the roles on their own account (403 OWN_ROLES).
- ROLE_ADMIN and ROLE_MEMBER cannot be assigned (400 UNKNOWN_ROLE, as if absent).
- A non-super admin cannot change the grants of a role they hold (403 OWN_ROLE);
  this replaces the BO_ROLES lock-out check, which it makes unreachable.
- POST /api/admin/users/{id}/changePassword is removed; the emailed reset stays.

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-26 22:30:11 +08:00
1 parent 816c8bcb5e
commit 4c48ebea2e
9 files changed
+114 -70

No files matched your search

@@ -129,9 +129,9 @@ class AdminRoleControllerTest {
}
@Test
void putModules_lockoutAnswers400() throws Exception {
when(acl.updateGrants(eq(4L), any())).thenThrow(new CodedResponseStatusException(HttpStatus.BAD_REQUEST, "lockout", "ACL_LOCKOUT"));
void putModules_ownRoleAnswers403() throws Exception {
when(acl.updateGrants(eq(4L), any())).thenThrow(new CodedResponseStatusException(HttpStatus.FORBIDDEN, "own role", "OWN_ROLE"));
mvc.perform(put("/api/admin/roles/4/modules").contentType(MediaType.APPLICATION_JSON).content("{\"moduleIds\":[]}"))
.andExpect(status().isBadRequest());
.andExpect(status().isForbidden());
}
}
@@ -99,10 +99,10 @@ class AdminUserControllerTest {
}
@Test
void changePassword_returnsUpdated() throws Exception {
when(users.changePassword(5L, "Str0ng#Pass")).thenReturn(1);
void changePassword_endpointNoLongerExists() throws Exception {
mvc.perform(post("/api/admin/users/5/changePassword").contentType(MediaType.APPLICATION_JSON)
.content("{\"password\":\"Str0ng#Pass\"}"))
.andExpect(status().isOk()).andExpect(jsonPath("$.updated").value(1));
.andExpect(status().is4xxClientError());
org.mockito.Mockito.verifyNoInteractions(users);
}
}
@@ -196,28 +196,23 @@ class AdminAclServiceTest {
}
@Test
void update_removingBoRolesFromTheCallersOnlyGrantingRoleIsALockout() {
caller(5, 2); // role 2 is attached to every admin and does not count
assertCoded(() -> service.updateGrants(5L, List.of()), HttpStatus.BAD_REQUEST, "ACL_LOCKOUT");
void update_nonSuperCallerEditingARoleTheyHoldIsRefused() {
caller(5, 2); // role 2 is attached to every admin and is ignored
assertCoded(() -> service.updateGrants(5L, List.of()), HttpStatus.FORBIDDEN, "OWN_ROLE");
verifyNothingChanged();
}
@Test
void update_removingBoRolesIsFineWhenAnotherCallerRoleStillGrantsIt() {
stubModules(
module(ROOT, "BO_ROOT", null, 1),
module(USERS, "BO_USERS", ROOT, 1, 4),
module(ROLES, "BO_ROLES", ROOT, 1, 5, 6));
void update_ownRoleIsRefusedEvenWhenAnotherCallerRoleGrantsBoRoles() {
caller(5, 6, 2);
service.updateGrants(5L, List.of());
verify(rest).getForEntity(eq(DEL), eq(Integer.class), eq(ROLES), eq(5L));
assertCoded(() -> service.updateGrants(5L, List.of()), HttpStatus.FORBIDDEN, "OWN_ROLE");
verifyNothingChanged();
}
@Test
void update_keepingBoRolesOnTheCallersRoleIsFine() {
service.updateGrants(5L, List.of(ROLES, USERS));
verify(rest).getForEntity(eq(ADD), eq(Integer.class), eq(USERS), eq(5L));
verify(rest, never()).getForEntity(eq(DEL), eq(Integer.class), any(), any());
void update_ownRoleIsRefusedEvenWhenTheChangeKeepsBoRoles() {
assertCoded(() -> service.updateGrants(5L, List.of(ROLES, USERS)), HttpStatus.FORBIDDEN, "OWN_ROLE");
verifyNothingChanged();
}
@Test
@@ -227,8 +222,8 @@ class AdminAclServiceTest {
}
@Test
void update_superAdminCallerIsNotSubjectToLockout() {
caller(1);
void update_superAdminCallerMayEditARoleTheyHold() {
caller(1, 5);
service.updateGrants(5L, List.of());
verify(rest).getForEntity(eq(DEL), eq(Integer.class), eq(ROLES), eq(5L));
}
@@ -43,7 +43,6 @@ class AdminUserServiceTest {
private static final String BY_USERNAME = "/user/findByUsername/{username}";
private static final String BY_EMAIL = "/user/findByEmail/{email}";
private static final String BY_EMAIL_NOT = "/user/findByEmailAndIdNot/{email}/{id}";
private static final String UPDATE_PWD = "/user/updateUserPassword";
private RestTemplateCustomUtil rest;
private AdminAuthenticationService auth;
@@ -497,9 +496,7 @@ class AdminUserServiceTest {
assertCoded(() -> service.update(6L, new AdminUserRequest("X", null, null, null, null, null)),
HttpStatus.FORBIDDEN, "SUPER_ADMIN_REQUIRED");
assertCoded(() -> service.resetPassword(6L), HttpStatus.FORBIDDEN, "SUPER_ADMIN_REQUIRED");
assertCoded(() -> service.changePassword(6L, "Str0ng#Pass"), HttpStatus.FORBIDDEN, "SUPER_ADMIN_REQUIRED");
verify(rest, never()).postForEntity(eq(SAVE), any(ParameterizedTypeReference.class), any());
verify(rest, never()).postForEntity(eq(UPDATE_PWD), eq(Integer.class), any());
verify(auth, never()).resetAdminPassword(anyString(), anyString());
}
@@ -544,20 +541,81 @@ class AdminUserServiceTest {
verify(auth).resetAdminPassword("stored", "stored@x.com");
}
@Test
void changePassword_rulesRejected_is400() {
stubFind(5, row(5, "A", "a", "a@x.com", true, "R"));
when(rest.postForEntity(eq(UPDATE_PWD), eq(Integer.class), any())).thenReturn(ResponseEntity.ok(0));
assertCoded(() -> service.changePassword(5L, "weak"), HttpStatus.BAD_REQUEST, "PASSWORD_RULES");
// ---- own roles and hidden roles ----
private static final List<AdminRoleRef> HIDDEN_ADMIN = List.of(new AdminRoleRef(2L, "ROLE_ADMIN"));
private static final List<AdminRoleRef> HIDDEN_MEMBER = List.of(new AdminRoleRef(3L, "ROLE_MEMBER"));
private void storedRolesInclude(Map<String, Object>... extra) {
List<Map<String, Object>> all = new ArrayList<>(List.of(fullRole(1, "ROLE_SUPER_ADMIN"), fullRole(4, "ROLE_SALES"), fullRole(5, "ROLE_OPS")));
all.addAll(List.of(extra));
when(rest.getForEntity(eq("/role/findAll"), any(ParameterizedTypeReference.class))).thenReturn(ResponseEntity.ok(all));
}
private Map<String, Object> ownRow() {
Map<String, Object> m = row(1, "Boss", "boss", "b@x.com", true);
m.put("roles", List.of(fullRole(2, "ROLE_ADMIN"), fullRole(4, "ROLE_SALES")));
return m;
}
@Test
void changePassword_ok_returnsRowsUpdated() {
void update_nonSuperCallerCannotChangeTheirOwnRoles() {
storedRolesInclude(fullRole(2, "ROLE_ADMIN"));
stubNoDuplicates();
stubFind(1, ownRow());
assertCoded(() -> service.update(1L, new AdminUserRequest(null, null, null, null, null, List.of(new AdminRoleRef(5L, "ROLE_OPS")))),
HttpStatus.FORBIDDEN, "OWN_ROLES");
verify(rest, never()).postForEntity(eq(SAVE), any(ParameterizedTypeReference.class), any());
}
@Test
void update_nonSuperCallerCannotAddARoleToThemself() {
storedRolesInclude(fullRole(2, "ROLE_ADMIN"));
stubNoDuplicates();
stubFind(1, ownRow());
assertCoded(() -> service.update(1L, new AdminUserRequest(null, null, null, null, null,
List.of(new AdminRoleRef(4L, "ROLE_SALES"), new AdminRoleRef(5L, "ROLE_OPS")))), HttpStatus.FORBIDDEN, "OWN_ROLES");
verify(rest, never()).postForEntity(eq(SAVE), any(ParameterizedTypeReference.class), any());
}
@Test
void update_nonSuperCallerSavingTheirOwnSameRolesPasses() {
storedRolesInclude(fullRole(2, "ROLE_ADMIN"));
stubNoDuplicates();
stubFind(1, ownRow());
stubSave(ownRow());
service.update(1L, new AdminUserRequest("Boss2", null, null, null, null, ROLES));
verify(rest).postForEntity(eq(SAVE), any(ParameterizedTypeReference.class), any());
}
@Test
void update_nonSuperCallerSavingWithoutRolesPasses() {
stubNoDuplicates();
stubFind(1, ownRow());
stubSave(ownRow());
service.update(1L, new AdminUserRequest("Boss2", null, null, null, null, null));
verify(rest).postForEntity(eq(SAVE), any(ParameterizedTypeReference.class), any());
}
@Test
void update_superCallerMayChangeTheirOwnRoles() {
callerIsSuper();
stubNoDuplicates();
stubFind(1, ownRow());
stubSave(ownRow());
service.update(1L, new AdminUserRequest(null, null, null, null, null, List.of(new AdminRoleRef(5L, "ROLE_OPS"))));
verify(rest).postForEntity(eq(SAVE), any(ParameterizedTypeReference.class), any());
}
@Test
void createAndUpdate_hiddenRolesLookUnknown() {
storedRolesInclude(fullRole(2, "ROLE_ADMIN"), fullRole(3, "ROLE_MEMBER"));
stubNoDuplicates();
stubFind(5, row(5, "A", "a", "a@x.com", true, "R"));
when(rest.postForEntity(eq(UPDATE_PWD), eq(Integer.class), any())).thenReturn(ResponseEntity.ok(1));
assertThat(service.changePassword(5L, "Str0ng#Pass")).isEqualTo(1);
ArgumentCaptor<Object> body = ArgumentCaptor.forClass(Object.class);
verify(rest).postForEntity(eq(UPDATE_PWD), eq(Integer.class), body.capture());
assertThat((Map<String, Object>) body.getValue()).containsEntry("userId", 5L).containsEntry("password", "Str0ng#Pass");
for (List<AdminRoleRef> hidden : List.of(HIDDEN_ADMIN, HIDDEN_MEMBER)) {
assertCoded(() -> service.create(req("N", "u", "e@x.com", hidden)), HttpStatus.BAD_REQUEST, "UNKNOWN_ROLE");
assertCoded(() -> service.update(5L, new AdminUserRequest(null, null, null, null, null, hidden)), HttpStatus.BAD_REQUEST, "UNKNOWN_ROLE");
}
verify(rest, never()).postForEntity(eq(SAVE), any(ParameterizedTypeReference.class), any());
}
}