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): keep SSRF client cookies within one request (#44672)

-LAN-afc63e57b864
diff --git a/api/core/helper/ssrf_proxy.py b/api/core/helper/ssrf_proxy.py
index ab01db8ed2d..3af7c9d8a85 100644
--- a/api/core/helper/ssrf_proxy.py
+++ b/api/core/helper/ssrf_proxy.py
@@ -12,7 +12,13 @@ client.
import logging
import time
-from typing import Any
+import urllib.request
+from collections.abc import Generator, Iterator
+from contextlib import contextmanager
+from contextvars import ContextVar
+from http.client import HTTPResponse
+from http.cookiejar import Cookie, CookieJar
+from typing import Any, override
import httpx
from pydantic import TypeAdapter, ValidationError
@@ -68,6 +74,43 @@ class UnsupportedResponseEncodingError(ResponseLimitError):
request_error = httpx.RequestError
max_retries_exceeded_error = MaxRetriesExceededError
+_send_cookie_jar: ContextVar[CookieJar | None] = ContextVar("ssrf_send_cookie_jar", default=None)
+
+
+class _SendScopedCookieJar(CookieJar):
+ """Client cookie jar that keeps response cookies only inside ``_send_cookie_scope``.
+
+ The pooled SSRF clients are shared by every tenant in the process, and httpx stores each response's
+ ``Set-Cookie`` in the client jar and replays it on later requests. While sending, httpx writes the client
+ jar only through ``extract_cookies`` and reads it only by iterating it, so both go to the jar of the current
+ scope: redirect hops inside one scope still carry cookies set by earlier hops, and nothing survives the
+ scope. Outside a scope the jar is empty and stores nothing. Cookies passed explicitly on a request never
+ enter this jar.
+ """
+
+ @override
+ def extract_cookies(self, response: HTTPResponse, request: urllib.request.Request) -> None:
+ scope_jar = _send_cookie_jar.get()
+ if scope_jar is not None:
+ scope_jar.extract_cookies(response, request)
+
+ @override
+ def __iter__(self) -> Iterator[Cookie]:
+ scope_jar = _send_cookie_jar.get()
+ if scope_jar is None:
+ return iter(())
+ return iter(scope_jar)
+
+
+@contextmanager
+def _send_cookie_scope() -> Generator[None]:
+ """Give one request attempt, including its redirect hops, a fresh cookie jar on the SSRF clients."""
+ token = _send_cookie_jar.set(CookieJar())
+ try:
+ yield
+ finally:
+ _send_cookie_jar.reset(token)
+
def _create_proxy_mounts(verify: bool) -> dict[str, httpx.HTTPTransport]:
"""Build per-scheme proxy transports with the same TLS policy as the SSRF client."""
@@ -89,6 +132,7 @@ def _build_ssrf_client(verify: bool) -> httpx.Client:
proxy=dify_config.SSRF_PROXY_ALL_URL,
verify=verify,
limits=_SSRF_CLIENT_LIMITS,
+ cookies=_SendScopedCookieJar(),
)
if dify_config.SSRF_PROXY_HTTP_URL and dify_config.SSRF_PROXY_HTTPS_URL:
@@ -96,9 +140,10 @@ def _build_ssrf_client(verify: bool) -> httpx.Client:
mounts=_create_proxy_mounts(verify=verify),
verify=verify,
limits=_SSRF_CLIENT_LIMITS,
+ cookies=_SendScopedCookieJar(),
)
- return httpx.Client(verify=verify, limits=_SSRF_CLIENT_LIMITS)
+ return httpx.Client(verify=verify, limits=_SSRF_CLIENT_LIMITS, cookies=_SendScopedCookieJar())
def _get_ssrf_client(ssl_verify_enabled: bool) -> httpx.Client:
@@ -169,6 +214,9 @@ def make_request(
) -> httpx.Response:
"""Send one SSRF-protected request with optional streaming.
+ Cookies set by responses are sent only on the redirect hops of the same attempt, never on later requests;
+ callers that need a cookie pass it explicitly.
+
Args:
method: HTTP method sent through the configured SSRF client.
url: Absolute request URL.
@@ -233,11 +281,12 @@ def make_request(
if user_provided_host is not None:
headers["host"] = user_provided_host
kwargs["headers"] = headers
- request = client.build_request(method=method, url=url, **kwargs)
- if stream_response:
- response = client.send(request, stream=True, **send_kwargs)
- else:
- response = client.send(request, **send_kwargs)
+ with _send_cookie_scope():
+ request = client.build_request(method=method, url=url, **kwargs)
+ if stream_response:
+ response = client.send(request, stream=True, **send_kwargs)
+ else:
+ response = client.send(request, **send_kwargs)
last_request_error = None
# Check for SSRF protection by Squid proxy
diff --git a/api/tests/unit_tests/core/helper/test_ssrf_proxy.py b/api/tests/unit_tests/core/helper/test_ssrf_proxy.py
index f5fdec111bd..3755c3857a8 100644
--- a/api/tests/unit_tests/core/helper/test_ssrf_proxy.py
+++ b/api/tests/unit_tests/core/helper/test_ssrf_proxy.py
@@ -15,6 +15,7 @@ from core.helper.ssrf_proxy import (
UnsupportedResponseEncodingError,
_build_ssrf_client,
_get_user_provided_host_header,
+ _SendScopedCookieJar,
_to_graphon_http_response,
buffer_response,
graphon_ssrf_proxy,
@@ -221,9 +222,80 @@ def test_build_ssrf_client_passes_ssl_verify_to_proxy_mount_transports(
mounts={"http://": http_transport, "https://": https_transport},
verify=False,
limits=ANY,
+ cookies=ANY,
)
+ "proxy_config",
+ [
+ {"SSRF_PROXY_ALL_URL": "http://proxy.example.com:3128"},
+ {
+ "SSRF_PROXY_ALL_URL": None,
+ "SSRF_PROXY_HTTP_URL": "http://proxy.example.com:8080",
+ "SSRF_PROXY_HTTPS_URL": "http://proxy.example.com:8443",
+ },
+ {"SSRF_PROXY_ALL_URL": None, "SSRF_PROXY_HTTP_URL": None, "SSRF_PROXY_HTTPS_URL": None},
+ ],
+ ids=["all-proxy", "scheme-proxies", "direct"],
+)
+def test_build_ssrf_client_scopes_cookie_jar_to_one_send(
+ config_overrides: Callable[..., None],
+ proxy_config: dict[str, str | None],
+) -> None:
+ config_overrides(**proxy_config)
+
+ with _build_ssrf_client(verify=True) as client:
+ assert isinstance(client.cookies.jar, _SendScopedCookieJar)
+
+
+def vendor_requests(monkeypatch: pytest.MonkeyPatch) -> Iterator[list[httpx.Request]]:
+ """Route requests to a vendor whose ``/login`` sets a session cookie and redirects to ``/home``."""
+ received: list[httpx.Request] = []
+
+ def vendor(request: httpx.Request) -> httpx.Response:
+ received.append(request)
+ if request.url.path == "/login":
+ return httpx.Response(302, headers={"Set-Cookie": "session=tenant-a; Path=/", "Location": "/home"})
+ return httpx.Response(200)
+
+ with httpx.Client(transport=httpx.MockTransport(vendor), cookies=_SendScopedCookieJar()) as client:
+ monkeypatch.setattr("core.helper.ssrf_proxy._get_ssrf_client", lambda _verify: client)
+ yield received
+
+
+def test_response_cookies_are_not_sent_on_later_requests(vendor_requests: list[httpx.Request]) -> None:
+ login = make_request("GET", "https://vendor.example.com/login", max_retries=0)
+ make_request("GET", "https://vendor.example.com/data", max_retries=0)
+
+ assert login.cookies["session"] == "tenant-a"
+ assert "cookie" not in vendor_requests[-1].headers
+
+
+def test_redirect_hops_send_cookies_set_by_earlier_hops_of_the_same_request(
+ vendor_requests: list[httpx.Request],
+) -> None:
+ response = make_request("GET", "https://vendor.example.com/login", max_retries=0, follow_redirects=True)
+ make_request("GET", "https://vendor.example.com/data", max_retries=0)
+
+ assert response.status_code == 200
+ assert [request.url.path for request in vendor_requests] == ["/login", "/home", "/data"]
+ assert vendor_requests[1].headers["cookie"] == "session=tenant-a"
+ assert "cookie" not in vendor_requests[2].headers
+
+
+def test_explicitly_passed_cookies_are_sent(vendor_requests: list[httpx.Request]) -> None:
+ make_request("GET", "https://vendor.example.com/login", max_retries=0)
+ make_request("GET", "https://vendor.example.com/data", max_retries=0, headers={"Cookie": "session=from-header"})
+ make_request("GET", "https://vendor.example.com/data", max_retries=0, cookies={"session": "from-argument"})
+
+ assert [request.headers["cookie"] for request in vendor_requests[1:]] == [
+ "session=from-header",
+ "session=from-argument",
+ ]
+
+
class TestGetUserProvidedHostHeader:
"""Tests for _get_user_provided_host_header function."""