requests icon indicating copy to clipboard operation
requests copied to clipboard

`http_proxy` bug with `starlette.responses.RedirectResponse`

Open wzh1994 opened this issue 6 months ago • 1 comments

When using requests with the http_proxy environment variable set, and explicitly passing proxies={'http': None, 'https': None} to bypass the proxy, the first request correctly avoids using the proxy. However, if the request returns a redirect (e.g. 3xx), the redirected request incorrectly picks up the proxy settings from the environment, causing the request to fail.

When passing proxies={'http': None, 'https': None} to a request, both the original and any redirected requests should bypass the proxy and not use the http_proxy environment variable.

Only the original request respects the proxies={'http': None, 'https': None} setting. The redirected request ignores it and uses the proxy from the environment variable, leading to connection errors if the target is not proxy-accessible.

Reproduction Steps

import os
import requests

# Set environment variable proxy (simulating a typical corporate environment)
os.environ['http_proxy'] = 'http://some.invalid.proxy:8080'

# Target URL that redirects to another domain
url = "http://httpbin.org/redirect-to?url=http://example.com"

# Explicitly disable proxy
resp = requests.get(url, proxies={'http': None, 'https': None})
print(resp.status_code)

Suggested Fix

In sessions.py line 316:

new_proxies = resolve_proxies(prepared_request, proxies, self.trust_env) should be updated to: new_proxies = resolve_proxies(prepared_request, proxies, self.trust_env if proxies is None else False)

This ensures that when proxies is explicitly passed in by the user (even as None), environment proxies are not used for redirects.

wzh1994 avatar Jul 01 '25 03:07 wzh1994

For the time being, I solved the issue by manually setting the trust_env attribute of the Session object to False in the modified request function.

Refer: https://github.com/LazyAGI/LazyLLM/pull/548/files#diff-b02a502a018fbdf49a0af37c6f8c2e971bbd850f4cad1ca6cca54da63cf1ca1cR26

wzh1994 avatar Jul 01 '25 03:07 wzh1994