Skip to content

Commit 5e5d9c9

Browse files
committed
fix: Knowledge base retrieval nodes are authorized based on authentication
1 parent 1786e2d commit 5e5d9c9

3 files changed

Lines changed: 22 additions & 15 deletions

File tree

‎apps/knowledge/EXTERNAL_RETRIEVAL.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ MCP 沿用智能体的 Django HTTP 入口与 ToolHandler 结构,
3030
Key 创建、查询、删除仍用原有 `/chat/api/v3/api_key` 接口。检索期间再次检查撤权及服务关闭。
3131

3232
对话用户授权开关同样覆盖应用内知识库检索、文档检索和工具工作流。身份取自后端认证结果,
33-
不使用 `form_data.asker`,子工具参数不能覆盖它。已通过智能体访问校验的匿名会话沿用智能体关联知识库的访问权限,
34-
不要求对话用户授权;匿名身份只能由后端认证结果确定,并传递到子工具。已登录的对话用户仍按知识库开关检查授权,
35-
管理员调试仍检查后台资源权限。匿名会话不能用于外部 REST/MCP 检索,外部入口始终要求有效的对话用户 API Key。
33+
不使用 `form_data.asker`,子工具参数不能覆盖它。匿名会话没有对话用户授权,只能检索未开启对话用户授权的知识库,
34+
该限制同样传递到子工具。已登录的对话用户按知识库开关检查授权,管理员调试仍检查后台资源权限。
35+
匿名会话不能用于外部 REST/MCP 检索,外部入口始终要求有效的对话用户 API Key。
3636
外部服务开关不影响内部检索。
3737

3838
## REST

‎apps/knowledge/services/retrieval_access.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class RetrievalIdentity:
2222
user_id: str | None = None
2323
api_key_id: str | None = None
2424
admin_user_id: str | None = None
25-
is_anonymous: bool = False
2625

2726

2827
def key_identity(key):
@@ -77,7 +76,6 @@ def identity_from_server(user_id, user_type, debug=False):
7776
return RetrievalIdentity(
7877
user_id=str(user_id) if user_id and user_type == "CHAT_USER" else None,
7978
admin_user_id=str(user_id) if user_id and debug and user_type in {"SYSTEM_USER", "ADMIN"} else None,
80-
is_anonymous=bool(user_id and user_type == "ANONYMOUS_USER"),
8179
)
8280

8381

@@ -124,8 +122,8 @@ def filter_workflow_knowledge(knowledge_ids, parameters):
124122
return filter_admin_knowledge(knowledge, identity.admin_user_id)
125123
allowed, restricted = set(), []
126124
for item in knowledge:
127-
# Anonymous sessions have already passed application access checks and have no chat-user grants.
128-
if (item.meta or {}).get("authentication", False) and not identity.is_anonymous:
125+
# Anonymous sessions carry no chat-user grants, so they only reach knowledge without authentication.
126+
if (item.meta or {}).get("authentication", False):
129127
restricted.append(str(item.id))
130128
else:
131129
allowed.add(str(item.id))

‎apps/knowledge/tests/retrieval/test_external_retrieval.py‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -278,21 +278,27 @@ def test_workflow_checks_grants_only_when_enabled_in_metadata(self, queryset, au
278278
queryset.return_value.filter.return_value = [knowledge(authentication=False)]
279279
self.assertEqual(access.filter_workflow_knowledge([KID], {}), [KID])
280280

281-
@patch.object(access, "authorized_ids", return_value=set())
281+
@patch.object(access, "authorized_ids", return_value={KID})
282282
@patch.object(access, "QuerySet")
283-
def test_authenticated_anonymous_session_keeps_application_knowledge_access(self, queryset, authorized):
283+
def test_anonymous_session_only_reaches_knowledge_without_authentication(self, queryset, authorized):
284284
identity = access.identity_from_server(UID, "ANONYMOUS_USER")
285+
self.assertIsNone(identity.user_id)
285286
for enabled in (False, True):
286287
for authentication in (False, True):
287288
with self.subTest(enabled=enabled, authentication=authentication):
289+
authorized.reset_mock()
290+
authorized.return_value = set()
288291
queryset.return_value.filter.return_value = [
289292
knowledge(enabled=enabled, authentication=authentication)
290293
]
291294
self.assertEqual(
292295
access.filter_workflow_knowledge([KID, "missing-knowledge"], {"retrieval_identity": identity}),
293-
[KID],
296+
[] if authentication else [KID],
294297
)
295-
authorized.assert_not_called()
298+
if authentication:
299+
authorized.assert_called_once_with(None, [KID])
300+
else:
301+
authorized.assert_not_called()
296302

297303
@patch.object(access, "authorized_ids", return_value=set())
298304
@patch.object(access, "QuerySet")
@@ -333,12 +339,15 @@ def test_child_tools_cannot_override_trusted_identity(self):
333339
self.assertFalse(child["debug"])
334340
self.assertIsNone(access.identity_from_server(UID, "APPLICATION_API_KEY", True).admin_user_id)
335341

342+
@patch.object(access, "authorized_ids", return_value=set())
336343
@patch.object(access, "QuerySet")
337-
def test_anonymous_identity_is_inherited_by_nested_tools(self, queryset):
338-
queryset.return_value.filter.return_value = [knowledge(authentication=True)]
344+
def test_anonymous_restriction_is_inherited_by_nested_tools(self, queryset, authorized):
345+
open_item = SimpleNamespace(**{**vars(knowledge(authentication=False)), "id": DID})
346+
queryset.return_value.filter.return_value = [knowledge(authentication=True), open_item]
339347
parent = {"retrieval_identity": access.identity_from_server(UID, "ANONYMOUS_USER")}
340-
child = {"retrieval_identity": {}, **access.inherited_retrieval_context(parent)}
341-
self.assertEqual(access.filter_workflow_knowledge([KID], child), [KID])
348+
child = {"retrieval_identity": {"user_id": UID}, **access.inherited_retrieval_context(parent)}
349+
self.assertEqual(access.filter_workflow_knowledge([KID, DID], child), [DID])
350+
authorized.assert_called_once_with(None, [KID])
342351

343352
@patch("oss.serializers.file._check_workspace_resource_permission")
344353
@patch("common.auth.handle.impl.user_token.get_auth")

0 commit comments

Comments
 (0)