Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(dify): search the dataset owner's index, not the caller's (#18987)

MarMar Labs5e1898e29369
diff --git a/api/apps/restful_apis/dify_retrieval_api.py b/api/apps/restful_apis/dify_retrieval_api.py
index d271d9cdb6..0640ff799c 100644
--- a/api/apps/restful_apis/dify_retrieval_api.py
+++ b/api/apps/restful_apis/dify_retrieval_api.py
@@ -280,11 +280,11 @@ async def retrieval(tenant_id):
doc_ids=doc_ids,
rank_feature=label_question(question, [kb]),
)
- ranks["chunks"] = settings.retriever.retrieval_by_children(ranks["chunks"], [tenant_id])
+ ranks["chunks"] = settings.retriever.retrieval_by_children(ranks["chunks"], [kb.tenant_id])
if use_kg:
model_config = get_tenant_default_model_by_type(kb.tenant_id, LLMType.CHAT)
- ck = await settings.kg_retriever.retrieval(question, [tenant_id], [kb_id], embd_mdl, LLMBundle(kb.tenant_id, model_config))
+ ck = await settings.kg_retriever.retrieval(question, [kb.tenant_id], [kb_id], embd_mdl, LLMBundle(kb.tenant_id, model_config))
if ck["content_with_weight"]:
ranks["chunks"].insert(0, ck)
diff --git a/test/unit_test/api/apps/sdk/test_dify_retrieval.py b/test/unit_test/api/apps/sdk/test_dify_retrieval.py
index d6453407f9..f394a45ea4 100644
--- a/test/unit_test/api/apps/sdk/test_dify_retrieval.py
+++ b/test/unit_test/api/apps/sdk/test_dify_retrieval.py
@@ -70,6 +70,7 @@ class _FakeRetriever:
self._raise_exc = raise_exc
self.retrieval_calls = []
self.last_kwargs = None
+ self.by_children_tenant_ids = None
async def retrieval(self, question, embd_mdl, tenant_id, kb_ids, **kwargs):
if self._raise_exc is not None:
@@ -78,15 +79,18 @@ class _FakeRetriever:
self.last_kwargs = kwargs
return {"chunks": list(self._chunks)}
- def retrieval_by_children(self, chunks, _tenant_ids):
+ def retrieval_by_children(self, chunks, tenant_ids):
+ self.by_children_tenant_ids = list(tenant_ids)
return chunks
class _FakeKGRetriever:
def __init__(self, content=""):
self._content = content
+ self.tenant_ids = None
- async def retrieval(self, *_a, **_k):
+ async def retrieval(self, _question, tenant_ids, *_a, **_k):
+ self.tenant_ids = list(tenant_ids)
if not self._content:
return {"content_with_weight": ""}
return {
@@ -180,11 +184,13 @@ def _load_dify_retrieval(
_stub(monkeypatch, "rag.app.tag", label_question=lambda *_a, **_k: {})
fake_retriever = _FakeRetriever(chunks=chunks)
+
+ fake_kg = _FakeKGRetriever(kg_content)
_stub(
monkeypatch,
"common.settings",
retriever=fake_retriever,
- kg_retriever=_FakeKGRetriever(kg_content),
+ kg_retriever=fake_kg,
)
quart_stub = ModuleType("quart")
@@ -200,6 +206,7 @@ def _load_dify_retrieval(
monkeypatch.setitem(sys.modules, "test_dify_retrieval_module", module)
spec.loader.exec_module(module)
module._fake_retriever = fake_retriever
+ module._fake_kg = fake_kg
return module
@@ -277,6 +284,37 @@ class TestDifyRetrievalTenantCheck:
assert result["records"][0]["content"] == "hello world"
assert module._fake_retriever.retrieval_calls, "retriever was not called on legitimate request"
+ @pytest.mark.p1
+ def test_team_member_searches_the_owner_index_not_their_own(self, monkeypatch):
+ """A permitted cross-tenant caller must search the KB owner's index.
+
+ ``KnowledgebaseService.accessible`` admits the owning tenant and, for a
+ ``TEAM`` KB, a member of that tenant. In the second case the caller's tenant
+ is not the KB's, so an index named after the caller holds none of its chunks.
+ """
+ team_kb = SimpleNamespace(id="kb-shared", tenant_id="tenant-owner", tenant_embd_id="", embd_id="bge")
+ module = _load_dify_retrieval(
+ monkeypatch,
+ kb=(True, team_kb),
+ accessible=lambda _id, _u: True,
+ request_body={
+ "knowledge_id": "kb-shared",
+ "query": "shared question",
+ "use_kg": True,
+ "retrieval_setting": {"top_k": 5, "score_threshold": 0.0},
+ },
+ tenant_id="tenant-member",
+ chunks=[{"doc_id": "d1", "content_with_weight": "chunk", "similarity": 0.9, "docnm_kwd": "doc.txt"}],
+ kg_content="graph answer",
+ )
+
+ asyncio.run(module.retrieval())
+
+ assert module._fake_retriever.retrieval_calls, "retriever was not called"
+ assert module._fake_retriever.retrieval_calls[0]["tenant_id"] == "tenant-owner"
+ assert module._fake_retriever.by_children_tenant_ids == ["tenant-owner"], "retrieval_by_children searched the caller's tenant instead of the KB owner's"
+ assert module._fake_kg.tenant_ids == ["tenant-owner"], "kg_retriever searched the caller's tenant instead of the KB owner's"
+
@pytest.mark.p1
def test_missing_knowledge_base_returns_not_found(self, monkeypatch):
"""KB id that does not exist returns 404 before the access check fires."""