requests icon indicating copy to clipboard operation
requests copied to clipboard

Fix: Respect no_proxy in proxies dictionary and NO_PROXY env var

Open cjac opened this issue 2 months ago • 0 comments

This commit addresses issue #5000 by ensuring that the no_proxy directive is respected, whether it's provided directly within the proxies dictionary passed to request functions or set as the NO_PROXY environment variable.

Previously, no_proxy in the proxies dictionary was not fully honored, and the NO_PROXY environment variable was not always checked when a proxies dictionary was provided.

This change includes:

  1. src/requests/sessions.py: Modified the Session.send method to check the no_proxy key within the kwargs['proxies'] dictionary. If the request URL matches any pattern in the no_proxy list, the proxies are cleared for that request.
  2. src/requests/utils.py: Updated the select_proxy function to check the NO_PROXY environment variable using should_bypass_proxies before selecting a proxy from the provided proxies dictionary.
  3. tests/test_requests.py: Added new test cases (test_no_proxy_in_proxies_dict, test_no_proxy_star_in_proxies_dict, test_no_proxy_not_matching_in_proxies_dict) to verify that no_proxy within the proxies dictionary works as expected, using mocks to check if the proxy is bypassed.
  4. tests/test_utils.py: Added new test cases (test_select_proxy_with_no_proxy) to ensure the NO_PROXY environment variable is correctly handled by select_proxy.

These changes ensure consistent behavior for proxy bypass logic, regardless of how the proxy settings are configured.

Closes #5000

cjac avatar Oct 24 '25 20:10 cjac