Fix: Respect user-defined proxy settings during redirects #7070
+24
−5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR potentially fixes issue #6981 where it was reported that explicitly set proxy configurations were being overridden by environment proxy settings during HTTP redirects. When users intentionally set
proxies={'http': None}to bypass environment-configured proxies, the redirect logic would ignore this and reapply environment proxy settings, causing unexpected behavior.Problem
Previously, when following redirects, the
rebuild_proxies()method would always check environment variables (HTTP_PROXY,HTTPS_PROXY, etc.) viaresolve_proxies()whentrust_env=True, even when the user had explicitly passed proxy settings (includingNoneto bypass proxies). This caused the following issues:proxies={'http': None}to bypass environment proxies would work for the initial request but fail on redirectsSolution
This PR introduces a
_user_explicitly_set_proxiesflag that tracks whether the user has explicitly provided proxy configuration in the request call. The changes include:Session.send()method detects when proxies are explicitly set (including when set toNone) and passes the flag through the redirect resolution chainrebuild_proxies()method now respects this flag and disables environment variable checking when the user has explicitly configured proxiesChanges
src/requests/sessions.py_user_explicitly_set_proxiesparameter toresolve_redirects()andrebuild_proxies()methodsSession.send()to detect explicit proxy configuration and pass the flagrebuild_proxies()to conditionally disabletrust_envwhen user explicitly set proxiestests/test_requests.pytest_proxy_bypass_on_redirect_with_explicit_none()to verify that explicitproxies={'http': None}bypasses environment proxy settings even during redirectsTesting
The new test case verifies the fix by:
http_proxy=INVALID_PROXY)proxies={'http': None, 'https': None}Backward Compatibility
This change is fully backward compatible:
trust_envflag continues to work as expected for environment-based proxy configuration