feat(安全): 用户态接口的 user_id 归属改由令牌身份判定
带 JWT 的请求若 user_id 参数与登录身份不符直接 403,管理员/超管豁免(后台代客查任务是本职), 匿名与 X-Internal-Token 调用不参与绑定——按 2026-09-28 取证,桌面端 15 个调用文件只有 4 个带凭据 且全走内部令牌,所以现网客户端零影响;网页端 uid 与令牌主体同源于 AuthService.buildResult。 "要求凭据"仍留在 user-tool-guard-enabled 开关后面,新增 user-tool-guard-prefixes 支持按前缀分批收。
This commit is contained in:
@@ -34,10 +34,16 @@ class AdminApiGuardFilterTest {
|
||||
|
||||
private AdminApiGuardFilter newFilter(AdminAuthSupport adminAuthSupport, boolean enabled, String exemptPrefixes,
|
||||
boolean userToolGuardEnabled) {
|
||||
return newFilter(adminAuthSupport, enabled, exemptPrefixes, userToolGuardEnabled, "");
|
||||
}
|
||||
|
||||
private AdminApiGuardFilter newFilter(AdminAuthSupport adminAuthSupport, boolean enabled, String exemptPrefixes,
|
||||
boolean userToolGuardEnabled, String userToolGuardPrefixes) {
|
||||
AdminApiGuardFilter filter = new AdminApiGuardFilter(adminAuthSupport, objectMapper);
|
||||
ReflectionTestUtils.setField(filter, "enabled", enabled);
|
||||
ReflectionTestUtils.setField(filter, "exemptPrefixes", exemptPrefixes);
|
||||
ReflectionTestUtils.setField(filter, "userToolGuardEnabled", userToolGuardEnabled);
|
||||
ReflectionTestUtils.setField(filter, "userToolGuardPrefixes", userToolGuardPrefixes);
|
||||
return filter;
|
||||
}
|
||||
|
||||
@@ -371,4 +377,121 @@ class AdminApiGuardFilterTest {
|
||||
assertThat(chain.getRequest()).isNull();
|
||||
assertThat(response.getContentAsString()).contains("401");
|
||||
}
|
||||
|
||||
// ---- 登录身份与 user_id 参数绑定:归属不再"由请求参数说了算" ----
|
||||
// 背景:用户态工具接口的数据归属全部来自请求参数 user_id,109 处调用点无一校验。
|
||||
// 桌面客户端今天仍不带任何凭据(15 个调用模块里只有 2 个带 X-Internal-Token),
|
||||
// 所以"要求凭据"这一步做不了;但**已经带 JWT 的请求**必须先不能冒读他人数据。
|
||||
|
||||
@Test
|
||||
void jwtUserCannotReadAnotherUsersData() throws Exception {
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
when(authSupport.requireUserOrInternal(any())).thenReturn(user(7L, null));
|
||||
when(authSupport.currentRole(any())).thenReturn(null);
|
||||
AdminApiGuardFilter filter = newFilter(authSupport, true, "", false);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/api/query-asin/tasks/detail");
|
||||
request.setParameter("user_id", "8");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockFilterChain chain = new MockFilterChain();
|
||||
|
||||
filter.doFilter(request, response, chain);
|
||||
|
||||
assertThat(chain.getRequest()).isNull();
|
||||
JsonNode body = objectMapper.readTree(response.getContentAsString());
|
||||
assertThat(body.path("success").asBoolean()).isFalse();
|
||||
assertThat(body.path("code").asInt()).isEqualTo(403);
|
||||
assertThat(body.path("message").asText()).contains("user_id");
|
||||
}
|
||||
|
||||
@Test
|
||||
void jwtUserReadingOwnDataStillPasses() throws Exception {
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
when(authSupport.requireUserOrInternal(any())).thenReturn(user(7L, "user"));
|
||||
when(authSupport.currentRole(any())).thenReturn(null);
|
||||
AdminApiGuardFilter filter = newFilter(authSupport, true, "", false);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/api/query-asin/tasks/detail");
|
||||
request.setParameter("user_id", "7");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockFilterChain chain = new MockFilterChain();
|
||||
|
||||
filter.doFilter(request, response, chain);
|
||||
|
||||
assertThat(chain.getRequest()).isNotNull();
|
||||
assertThat(response.getContentAsString()).isEmpty();
|
||||
}
|
||||
|
||||
/** 管理端(admin-frontend-vue)本来就是替客户查任务的,绑定必须给它留口子。 */
|
||||
@Test
|
||||
void adminJwtMayActForAnotherUser() throws Exception {
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
when(authSupport.requireUserOrInternal(any())).thenReturn(user(1L, "admin"));
|
||||
when(authSupport.currentRole(any())).thenReturn("admin");
|
||||
AdminApiGuardFilter filter = newFilter(authSupport, true, "", false);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/api/query-asin/tasks/detail");
|
||||
request.setParameter("user_id", "8");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockFilterChain chain = new MockFilterChain();
|
||||
|
||||
filter.doFilter(request, response, chain);
|
||||
|
||||
assertThat(chain.getRequest()).isNotNull();
|
||||
}
|
||||
|
||||
/** 桌面端不带 JWT、也就没有身份可绑:这条约束对存量匿名客户端零影响。 */
|
||||
@Test
|
||||
void anonymousRequestIsNotBoundButStillNeedsNoToken() throws Exception {
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
AdminApiGuardFilter filter = newFilter(authSupport, true, "", false);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/api/patrol-delete/tasks/submit");
|
||||
request.setParameter("user_id", "8");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockFilterChain chain = new MockFilterChain();
|
||||
|
||||
filter.doFilter(request, response, chain);
|
||||
|
||||
assertThat(chain.getRequest()).isNotNull();
|
||||
}
|
||||
|
||||
/** 参数不是数字时交回 controller 报它自己的错,守卫不抢着当格式校验器。 */
|
||||
@Test
|
||||
void nonNumericUserIdParameterIsLeftToController() throws Exception {
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
when(authSupport.requireUserOrInternal(any())).thenReturn(user(7L, null));
|
||||
when(authSupport.currentRole(any())).thenReturn(null);
|
||||
AdminApiGuardFilter filter = newFilter(authSupport, true, "", false);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/api/query-asin/tasks/detail");
|
||||
request.setParameter("user_id", "abc");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockFilterChain chain = new MockFilterChain();
|
||||
|
||||
filter.doFilter(request, response, chain);
|
||||
|
||||
assertThat(chain.getRequest()).isNotNull();
|
||||
}
|
||||
|
||||
// ---- 用户态守卫按前缀分批开启:全开会让存量匿名客户端整条产品线 401,全关又等于没有 ----
|
||||
|
||||
@Test
|
||||
void userToolGuardCanCoverOnlyListedPrefixes() throws Exception {
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
when(authSupport.requireUserOrInternal(any())).thenThrow(new BusinessException(401, "未登录"));
|
||||
AdminApiGuardFilter filter = newFilter(authSupport, true, "", true, "/api/query-asin");
|
||||
MockHttpServletResponse rejected = new MockHttpServletResponse();
|
||||
MockFilterChain rejectedChain = new MockFilterChain();
|
||||
filter.doFilter(new MockHttpServletRequest("GET", "/api/query-asin/tasks/detail"), rejected, rejectedChain);
|
||||
assertThat(rejectedChain.getRequest()).isNull();
|
||||
|
||||
MockFilterChain otherChain = new MockFilterChain();
|
||||
newFilter(authSupport, true, "", true, "/api/query-asin")
|
||||
.doFilter(new MockHttpServletRequest("POST", "/api/patrol-delete/tasks/submit"),
|
||||
new MockHttpServletResponse(), otherChain);
|
||||
assertThat(otherChain.getRequest()).as("未列入清单的前缀本轮不动,留给客户端铺开后再收").isNotNull();
|
||||
}
|
||||
|
||||
private AdminUserEntity user(Long id, String role) {
|
||||
AdminUserEntity user = new AdminUserEntity();
|
||||
user.setId(id);
|
||||
user.setRole(role);
|
||||
return user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user