From 00b25d6ca4f55890cc1fb07484c8c37205677bb1 Mon Sep 17 00:00:00 2001 From: user <70670632+stuxf@users.noreply.github.com> Date: Thu, 16 Apr 2026 04:55:56 +0000 Subject: [PATCH] fix: sync redirect bypass, Host header port, redirect loop dead code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pass follow_redirects through in HTTPHandler.get() — previously the parameter was accepted but never forwarded to the underlying httpx client, making sync redirect protection ineffective. Include port in Host header when non-default (e.g. example.com:8080). Fix redirect loop to read Location header directly instead of response.next_request (which is None when follow_redirects=False). --- litellm/llms/custom_httpx/http_handler.py | 1 + litellm/proxy/common_utils/url_utils.py | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/litellm/llms/custom_httpx/http_handler.py b/litellm/llms/custom_httpx/http_handler.py index 489a56daf8e..03d2af72329 100644 --- a/litellm/llms/custom_httpx/http_handler.py +++ b/litellm/llms/custom_httpx/http_handler.py @@ -1019,6 +1019,7 @@ class HTTPHandler: url, params=params, headers=headers, + follow_redirects=_follow_redirects, ) return response diff --git a/litellm/proxy/common_utils/url_utils.py b/litellm/proxy/common_utils/url_utils.py index f18378f9007..a47d7ae2ca4 100644 --- a/litellm/proxy/common_utils/url_utils.py +++ b/litellm/proxy/common_utils/url_utils.py @@ -87,6 +87,11 @@ def validate_url(url: str) -> Tuple[str, str]: port = parsed.port default_port = 443 if parsed.scheme == "https" else 80 + # Build the Host header value — include port when non-default + host_header = ( + hostname if (port is None or port == default_port) else f"{hostname}:{port}" + ) + # Resolve hostname and validate ALL addresses try: addrinfo = socket.getaddrinfo( @@ -113,7 +118,7 @@ def validate_url(url: str) -> Tuple[str, str]: # we rewrite to the validated IP like HTTP. ssl_verify = getattr(litellm, "ssl_verify", True) if parsed.scheme == "https" and ssl_verify is not False: - return url, hostname + return url, host_header # For HTTP, rewrite URL to connect to the validated IP directly # to prevent DNS rebinding (no TLS to bind the connection). @@ -130,7 +135,7 @@ def validate_url(url: str) -> Tuple[str, str]: (parsed.scheme, new_netloc, parsed.path, parsed.params, parsed.query, "") ) - return rewritten, hostname + return rewritten, host_header _MAX_REDIRECTS = 10