Skip to content

http_proxy bug with starlette.responses.RedirectResponse #6981

@wzh1994

Description

@wzh1994

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions