Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(api): send valid body in external knowledge validation probe (#39410)

David Parkda2177c7b2b3
diff --git a/api/services/external_knowledge_service.py b/api/services/external_knowledge_service.py
index d069c259165..1a15fee1e97 100644
--- a/api/services/external_knowledge_service.py
+++ b/api/services/external_knowledge_service.py
@@ -93,8 +93,20 @@ class ExternalDatasetService:
raise ValueError(f"invalid endpoint: {endpoint} must start with http:// or https://")
else:
raise ValueError(f"invalid endpoint: {endpoint}")
+ # Send a minimal body shaped like the External Knowledge API retrieval contract so providers
+ # that require a JSON payload (e.g. RAGFlow) accept the validation probe instead of rejecting
+ # a body-less POST. Mirrors the request built in fetch_external_knowledge_retrieval.
+ validation_payload = {
+ "knowledge_id": "",
+ "query": "",
+ "retrieval_setting": {"top_k": 1, "score_threshold": 0.0},
+ }
try:
- response = ssrf_proxy.post(endpoint, headers={"Authorization": f"Bearer {api_key}"})
+ response = ssrf_proxy.post(
+ endpoint,
+ headers={"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"},
+ data=json.dumps(validation_payload),
+ )
except Exception as e:
raise ValueError(f"failed to connect to the endpoint: {endpoint}") from e
if response.status_code == 502:
diff --git a/api/tests/unit_tests/services/test_external_dataset_service.py b/api/tests/unit_tests/services/test_external_dataset_service.py
index ffc52b5c369..c2b8b392573 100644
--- a/api/tests/unit_tests/services/test_external_dataset_service.py
+++ b/api/tests/unit_tests/services/test_external_dataset_service.py
@@ -613,6 +613,31 @@ class TestExternalDatasetServiceCheckEndpoint:
# Act & Assert - should not raise
ExternalDatasetService.check_endpoint_and_api_key(settings)
+ @patch("services.external_knowledge_service.ssrf_proxy")
+ def test_check_endpoint_sends_json_body(self, mock_proxy, factory: ExternalDatasetServiceTestDataFactory):
+ """Regression for #39402: the validation probe must POST a JSON body matching the
+ External Knowledge API retrieval contract, not a body-less request that providers
+ such as RAGFlow reject (empty POST -> 502 ERR_ZERO_SIZE_OBJECT)."""
+ # Arrange
+ settings = {"endpoint": "https://api.example.com", "api_key": "test-key"}
+
+ mock_response = MagicMock()
+ mock_response.status_code = 200
+ mock_proxy.post.return_value = mock_response
+
+ # Act
+ ExternalDatasetService.check_endpoint_and_api_key(settings)
+
+ # Assert - a non-empty JSON body is sent with the JSON content type
+ mock_proxy.post.assert_called_once()
+ _, call_kwargs = mock_proxy.post.call_args
+ assert call_kwargs["headers"]["Content-Type"] == "application/json"
+ assert call_kwargs["headers"]["Authorization"] == "Bearer test-key"
+ sent_body = json.loads(call_kwargs["data"])
+ assert "knowledge_id" in sent_body
+ assert "query" in sent_body
+ assert sent_body["retrieval_setting"] == {"top_k": 1, "score_threshold": 0.0}
+
def test_check_endpoint_missing_endpoint_key(self, factory: ExternalDatasetServiceTestDataFactory):
"""Test validation fails when endpoint key is missing."""
# Arrange