Skip to content

Conversation

@tboy1337
Copy link

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.) via resolve_proxies() when trust_env=True, even when the user had explicitly passed proxy settings (including None to bypass proxies). This caused the following issues:

  1. Explicit proxy bypass ignored: Setting proxies={'http': None} to bypass environment proxies would work for the initial request but fail on redirects
  2. User intent overridden: Environment variables would take precedence over explicit user configuration during redirect resolution
  3. Unexpected failures: Requests would fail with proxy connection errors on redirects even when the user explicitly disabled proxy usage

Solution

This PR introduces a _user_explicitly_set_proxies flag that tracks whether the user has explicitly provided proxy configuration in the request call. The changes include:

  1. Flag propagation: The Session.send() method detects when proxies are explicitly set (including when set to None) and passes the flag through the redirect resolution chain
  2. Conditional trust_env: The rebuild_proxies() method now respects this flag and disables environment variable checking when the user has explicitly configured proxies
  3. Preserved user intent: User-defined proxy settings now take precedence over environment variables throughout the entire redirect chain

Changes

src/requests/sessions.py

  • Added _user_explicitly_set_proxies parameter to resolve_redirects() and rebuild_proxies() methods
  • Modified Session.send() to detect explicit proxy configuration and pass the flag
  • Updated rebuild_proxies() to conditionally disable trust_env when user explicitly set proxies

tests/test_requests.py

  • Added test_proxy_bypass_on_redirect_with_explicit_none() to verify that explicit proxies={'http': None} bypasses environment proxy settings even during redirects

Testing

The new test case verifies the fix by:

  1. Setting an invalid proxy in the environment (http_proxy=INVALID_PROXY)
  2. Making a request with explicit proxies={'http': None, 'https': None}
  3. Following a redirect and confirming the request succeeds (would fail if environment proxy was used)

Backward Compatibility

This change is fully backward compatible:

  • Existing behavior is preserved when proxies are not explicitly set
  • The trust_env flag continues to work as expected for environment-based proxy configuration
  • Only affects cases where users explicitly provide proxy configuration, ensuring their intent is respected

…d proxies during redirects. Added a flag to prevent environment proxy settings from overriding explicit user settings. Updated tests to verify correct behavior when proxies are explicitly set to None.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant