fix(鉴权): 任务归属身份从会话推导,user_id 与不符即 403
客户端任务接口原先把"这单是谁的"交给调用方自报:请求体/查询串带 user_id,服务端拿它当范围 条件。身份应只信会话(JWT),桌面执行器与内部代理通道也不例外: - AdminAuthSupport 增加 mayActForOtherUsers / resolveOwnershipUserId / requireOwnershipUserId / bindOwnershipUserId:会话身份与请求 user_id 不一致直接 403, 只有平台超管与被授权管理员能代他人; - 上架(PublishController/PublishTaskService)先接:user_id 缺失即 401, 仅两个桌面执行器端点允许留空;外观专利/相似 ASIN 的归属谓词补上 Objects.equals(userId, task.getUserId()),不再只比 moduleType; - AdminApiGuardFilter 把入口口径与新helper 对齐,护栏测试 AdminAuthSupportOwnershipTest + PublishOwnershipGuardrailTest 双向钉(放行面与拒绝面都有样本)。 同族的进度类模块(原先仍以请求体 user_id 为范围)在 cp-item7 一次接完;这一支只管会改状态/花钱的出口。 另外 resolveOwnershipUserId 的 [ownership-anonymous] 日志按 URI 限流也在这支——桌面执行器 4.0.44 不带凭据,只有分得清是哪个端点在打匿名请求,才敢逐支把它们从 resolve 换成 require。
This commit is contained in:
@@ -425,7 +425,9 @@ class AdminApiGuardFilterTest {
|
||||
void adminJwtMayActForAnotherUser() throws Exception {
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
when(authSupport.requireUserOrInternal(any())).thenReturn(user(1L, "admin"));
|
||||
when(authSupport.currentRole(any())).thenReturn("admin");
|
||||
// 豁免判定只有一个出口(AdminAuthSupport.mayActForOtherUsers),守卫不再自己抄一份角色名单:
|
||||
// 上一版这里 stub currentRole,改成委托后如果谁把两处又写回各判各的,这两支会一起红。
|
||||
when(authSupport.mayActForOtherUsers(any())).thenReturn(true);
|
||||
AdminApiGuardFilter filter = newFilter(authSupport, true, "", false);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/api/query-asin/tasks/detail");
|
||||
request.setParameter("user_id", "8");
|
||||
@@ -435,6 +437,7 @@ class AdminApiGuardFilterTest {
|
||||
filter.doFilter(request, response, chain);
|
||||
|
||||
assertThat(chain.getRequest()).isNotNull();
|
||||
verify(authSupport).mayActForOtherUsers(any());
|
||||
}
|
||||
|
||||
/** 桌面端不带 JWT、也就没有身份可绑:这条约束对存量匿名客户端零影响。 */
|
||||
@@ -556,7 +559,7 @@ class AdminApiGuardFilterTest {
|
||||
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");
|
||||
when(authSupport.mayActForOtherUsers(any())).thenReturn(true);
|
||||
AdminApiGuardFilter filter = newFilter(authSupport, true, "", false);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/api/query-asin/tasks/detail");
|
||||
request.setParameter("user_id", "8");
|
||||
@@ -568,6 +571,63 @@ class AdminApiGuardFilterTest {
|
||||
assertThat(chain.getRequest()).isNotNull();
|
||||
}
|
||||
|
||||
// ---- /api/publish 纳入绑定(2026-10-07)----
|
||||
// 取证:网页端调 publish 的 activate 三兄弟时**根本不传 user_id**
|
||||
// (frontend-vue/src/shared/api/types/modules/publish.ts:180,189),桌面执行器只调
|
||||
// items 与 result(app_client/amazon/add_product.py:973,1048)。此前 publish 一个都不在
|
||||
// 守卫名单里,控制器也不 require*,匿名请求即可激活/删除任意任务。
|
||||
|
||||
@Test
|
||||
void publishJwtCannotReadAnotherUsersItems() throws Exception {
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
when(authSupport.requireUserOrInternal(any())).thenReturn(user(7L, "normal"));
|
||||
when(authSupport.mayActForOtherUsers(any())).thenReturn(false);
|
||||
AdminApiGuardFilter filter = newFilter(authSupport, true, "", false);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/api/publish/tasks/9001/items");
|
||||
request.setParameter("user_id", "8");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockFilterChain chain = new MockFilterChain();
|
||||
|
||||
filter.doFilter(request, response, chain);
|
||||
|
||||
assertThat(chain.getRequest()).as("带 JWT 却填别人的 user_id 必须挡下").isNull();
|
||||
JsonNode body = objectMapper.readTree(response.getContentAsString());
|
||||
assertThat(body.path("code").asInt()).isEqualTo(403);
|
||||
}
|
||||
|
||||
/** 不带凭据的执行器(X-Internal-Token、无 operatorId、无 user_id)解析不出身份,绑定不触发。 */
|
||||
@Test
|
||||
void publishExecutorRequestWithoutIdentityIsNotBound() 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/publish/tasks/9001/items");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockFilterChain chain = new MockFilterChain();
|
||||
|
||||
filter.doFilter(request, response, chain);
|
||||
|
||||
assertThat(chain.getRequest()).isNotNull();
|
||||
assertThat(response.getContentAsString()).isEmpty();
|
||||
}
|
||||
|
||||
/** 前缀边界:/publishing-tools 这类同前缀名字不能被卷进来。 */
|
||||
@Test
|
||||
void publishLikeOtherPathsAreNotSweptIn() throws Exception {
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
AdminApiGuardFilter filter = newFilter(authSupport, true, "", false);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/api/publishing-tools/list");
|
||||
request.setParameter("user_id", "8");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockFilterChain chain = new MockFilterChain();
|
||||
|
||||
filter.doFilter(request, response, chain);
|
||||
|
||||
assertThat(chain.getRequest()).isNotNull();
|
||||
verify(authSupport, never()).requireUserOrInternal(any());
|
||||
}
|
||||
|
||||
private AdminUserEntity user(Long id, String role) {
|
||||
AdminUserEntity user = new AdminUserEntity();
|
||||
user.setId(id);
|
||||
|
||||
Reference in New Issue
Block a user