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: ssrf, add internal ip filter when parse tool schema (#29548)

zyssyz1232bf44057e95e
diff --git a/api/core/helper/ssrf_proxy.py b/api/core/helper/ssrf_proxy.py
index 0de026f3c78..6c98aea1bef 100644
--- a/api/core/helper/ssrf_proxy.py
+++ b/api/core/helper/ssrf_proxy.py
@@ -9,6 +9,7 @@ import httpx
from configs import dify_config
from core.helper.http_client_pooling import get_pooled_http_client
+from core.tools.errors import ToolSSRFError
logger = logging.getLogger(__name__)
@@ -93,6 +94,18 @@ def make_request(method, url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
while retries <= max_retries:
try:
response = client.request(method=method, url=url, **kwargs)
+ # Check for SSRF protection by Squid proxy
+ if response.status_code in (401, 403):
+ # Check if this is a Squid SSRF rejection
+ server_header = response.headers.get("server", "").lower()
+ via_header = response.headers.get("via", "").lower()
+
+ # Squid typically identifies itself in Server or Via headers
+ if "squid" in server_header or "squid" in via_header:
+ raise ToolSSRFError(
+ f"Access to '{url}' was blocked by SSRF protection. "
+ f"The URL may point to a private or local network address. "
+ )
if response.status_code not in STATUS_FORCELIST:
return response
diff --git a/api/core/tools/errors.py b/api/core/tools/errors.py
index b0c2232857a..e4afe244262 100644
--- a/api/core/tools/errors.py
+++ b/api/core/tools/errors.py
@@ -29,6 +29,10 @@ class ToolApiSchemaError(ValueError):
pass
+class ToolSSRFError(ValueError):
+ pass
+
+
class ToolCredentialPolicyViolationError(ValueError):
pass
diff --git a/api/core/tools/utils/parser.py b/api/core/tools/utils/parser.py
index 6eabde3991b..34861821923 100644
--- a/api/core/tools/utils/parser.py
+++ b/api/core/tools/utils/parser.py
@@ -425,7 +425,7 @@ class ApiBasedToolSchemaParser:
except ToolApiSchemaError as e:
openapi_error = e
- # openai parse error, fallback to swagger
+ # openapi parse error, fallback to swagger
try:
converted_swagger = ApiBasedToolSchemaParser.parse_swagger_to_openapi(
loaded_content, extra_info=extra_info, warning=warning
@@ -436,7 +436,6 @@ class ApiBasedToolSchemaParser:
), schema_type
except ToolApiSchemaError as e:
swagger_error = e
-
# swagger parse error, fallback to openai plugin
try:
openapi_plugin = ApiBasedToolSchemaParser.parse_openai_plugin_json_to_tool_bundle(