fix(安全): X-Internal-Token 不再绕开用户态接口的 user_id 归属绑定

守卫里"令牌可信即在 shouldNotFilter 整体放行"把刚上线的绑定又开了一个洞:这把
X-Internal-Token 是硬编码在分发出去的桌面客户端里的静态共享密钥(amazon/tool.py:20、
crawler_core/log_uploader.py:38),它能证明"走了内部通道",证明不了"这个 user_id 是谁"。
带 JWT + 这把钥匙的请求因此完全不参与绑定,等于给别人令牌就能读任意 user_id。

改法只拆语义不加开关:令牌继续豁免"要不要凭据"(否则仅凭令牌即鉴权的
/api/admin/shop-manages/credential 会被误拦 401),不再豁免"归属归谁"。
管理员/超管代客操作豁免照旧。

对现网零影响的取证:全工作区 .py 里 operatorId/operator_id 出现 0 处,所以纯令牌请求
进 requireUserOrInternal 后无 JWT→resolveToken 即抛→identity=null→绑定不触发;
不带 user_id 的按 task_id 回调仍然整体跳过守卫(新用例用 verify(never()) 守住)。

AdminApiGuardFilterTest 30 个用例全绿;新增 4 个,其中 internalTokenDoesNotBypassOwnershipBinding
在改代码前是红的,就是那个洞的可执行证明。
This commit is contained in:
2026-09-28 18:45:46 +08:00
parent 812ac31a48
commit 31d3c0857d
2 changed files with 96 additions and 5 deletions
@@ -488,6 +488,86 @@ class AdminApiGuardFilterTest {
assertThat(otherChain.getRequest()).as("未列入清单的前缀本轮不动,留给客户端铺开后再收").isNotNull();
}
// ---- X-Internal-Token 的豁免不能把"归属绑定"一起绕开(2026-09-28 发现) ----
// 令牌是硬编码在桌面客户端里的静态共享密钥(amazon/tool.py:20,32 字符字面量,随客户端分发给每个用户),
// 而旧逻辑在 shouldNotFilter 里对"令牌可信"的请求**整体跳过**过滤器,包括带 user_id 的用户态前缀。
// 于是刚上线的 user_id↔JWT 绑定对"同时带 JWT 与内部令牌"的请求完全不生效 —— 拿别人 JWT + 自己客户端里的
// 这把钥匙,就能读任意 user_id 的数据。
@Test
void internalTokenDoesNotBypassOwnershipBinding() throws Exception {
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
when(authSupport.isTrustedInternalToken(any())).thenReturn(true);
when(authSupport.requireUserOrInternal(any())).thenReturn(user(7L, "user"));
when(authSupport.currentRole(any())).thenReturn("user");
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()).as("内部令牌不能成为冒读他人数据的通行证").isNull();
JsonNode body = objectMapper.readTree(response.getContentAsString());
assertThat(body.path("code").asInt()).isEqualTo(403);
}
/**
* 真实客户端形状必须不受影响:`price_match_new.py:1292` 就是"内部令牌 + user_id、无 JWT、无 operatorId"
* 往 /api/shop-match/** 回调。这类请求解析不出登录身份,绑定不该拦它(也拦不了——没有身份可比)。
*/
@Test
void internalTokenWithoutLoginIdentityIsNotBound() throws Exception {
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
when(authSupport.isTrustedInternalToken(any())).thenReturn(true);
when(authSupport.requireUserOrInternal(any())).thenThrow(new BusinessException(401, "未登录"));
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();
assertThat(response.getContentAsString()).isEmpty();
}
/** 按 task_id 回调、不带 user_id 的内部令牌请求:仍整体豁免,不因为它多跑一遍身份解析。 */
@Test
void internalTokenOnTaskScopedCallbackStaysExempt() throws Exception {
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
when(authSupport.isTrustedInternalToken(any())).thenReturn(true);
AdminApiGuardFilter filter = newFilter(authSupport, true, "", false);
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/api/shop-match/tasks/7123/stage-finished");
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain();
filter.doFilter(request, response, chain);
assertThat(chain.getRequest()).isNotNull();
verify(authSupport, never()).requireUserOrInternal(any());
}
/** 管理员用内部令牌代客操作是本职,豁免照旧。 */
@Test
void adminInternalOperatorIsStillExemptFromBinding() throws Exception {
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
when(authSupport.isTrustedInternalToken(any())).thenReturn(true);
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();
}
private AdminUserEntity user(Long id, String role) {
AdminUserEntity user = new AdminUserEntity();
user.setId(id);