Skip to content

Fix: Respect no_proxy in proxies dictionary and NO_PROXY env var - #7068

Open
cjac wants to merge 1 commit into
psf:mainfrom
LLC-Technologies-Collier:fix-no-proxy-2025-10-24
Open

cjac wants to merge 1 commit into
psf:mainfrom
LLC-Technologies-Collier:fix-no-proxy-2025-10-24

Conversation

@cjac

@cjac cjac commented Oct 24, 2025 •

Copy link
Copy Markdown

Closes #5000

Problem

When an explicit proxies mapping is passed to a request or configured on Session.proxies, merge_environment_settings and resolve_proxies only use no_proxy to decide whether to merge missing schemes from os.environ, leaving explicit entries in proxies untouched. Consequently:

  • Explicit 'no_proxy' or 'no' keys in proxies (including mappings returned directly by urllib.request.getproxies()) did not bypass explicit proxies.
  • no_proxy / NO_PROXY environment variables were ignored whenever an explicit proxy was configured for that scheme.

Approach

  • Explicit bypass keys & override semantics: Extract 'no_proxy' and 'no' by key presence (_extract_no_proxy) rather than truthiness. Non-empty rules always apply; an explicit empty string ('no_proxy': '' or 'no': '') disables bypass so callers can force an explicit proxy without disabling trust_env for .netrc or CA bundles.
  • trust_env isolation: Fall back to environment/OS bypass rules only when trust_env=True (_should_bypass_proxies_with_trust_env). select_proxy accepts trust_env: bool = True to match resolve_proxies, while HTTPAdapter passes trust_env=False since Session evaluates self.trust_env upstream.
  • Redirect & hook preservation: In Session.send, clear proxies on a shallow copy of kwargs passed to adapter.send so explicit proxies survive for response hooks and subsequent redirects to non-bypassed hosts.

@cjac
cjac force-pushed the fix-no-proxy-2025-10-24 branch from ed6f3a0 to a784de5 Compare October 24, 2025 20:58
@cjac
cjac marked this pull request as ready for review October 24, 2025 20:59

@hajo-m hajo-m left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding #5000 (comment)

Comment thread src/requests/sessions.py Resolved
Comment thread src/requests/utils.py Resolved
@cjac

cjac commented Sep 19, 2026

Copy link
Copy Markdown
Author

Thank you for the review. I have addressed the issues raised:

  • Missing test_rewind_body_no_seek: This test has been restored to its original implementation with BadFileObj and the UnrewindableBodyError assertion. The new tests have been moved out of this function.

  • Handling of None from select_proxy: Callers in HTTPAdapter have been checked. They use truthiness evaluations (if proxy:) and treat None safely as bypassing the proxy setup, proceeding to standard connection Pool Manager invocation.

  • Dictionary Key and Environment Precedence: Addressed concerns regarding key naming ('no' vs 'no_proxy') by checking both in the proxies dictionary. Refined select_proxy to explicitly respect both no_proxy and NO_PROXY environment variables, prioritizing the lowercase version to maintain consistency with urllib and other standard HTTP clients.

@cjac
cjac requested a review from hajo-m September 19, 2026 18:11

@cjac cjac left a comment •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now possible to accurately specify using environment variables which hosts should skip proxy and go direct

@cjac
cjac force-pushed the fix-no-proxy-2025-10-24 branch from 7524091 to 508449a Compare September 19, 2026 19:56
@cjac

cjac commented Sep 19, 2026

Copy link
Copy Markdown
Author

rebased onto orgin's main

@cjac

cjac commented Oct 6, 2026

Copy link
Copy Markdown
Author

@hajo-m ready for another round

@cjac

cjac commented Oct 6, 2026

Copy link
Copy Markdown
Author

@Mukller - ready for another review.

OKAN-SOFTWARE

This comment was marked as low quality.

@cjac

cjac commented Oct 31, 2026

Copy link
Copy Markdown
Author

Thank you for your review, OKAN-SOFTWARE. I'll take a look this weekend.

@cjac
cjac force-pushed the fix-no-proxy-2025-10-24 branch from 11878d0 to a1582a2 Compare October 31, 2026 17:09
@cjac

cjac commented Oct 31, 2026 •

Copy link
Copy Markdown
Author

@OKAN-SOFTWARE Good catch on Session.trust_env = False. Thanks for tracing both Session.send and select_proxy.

Rebased onto main and squashed into 1667efd1:

  • Environment/OS no_proxy fallback now only runs when trust_env=True. Explicit 'no_proxy'/'no' keys in proxies still apply regardless of trust_env, and passing "" explicitly disables bypass even when NO_PROXY is set in the environment.
  • select_proxy now takes trust_env: bool = True (matching resolve_proxies), and HTTPAdapter passes trust_env=False at its three call sites since Session already evaluates self.trust_env.
  • Session.send clears proxies on a shallow copy of kwargs for adapter.send so explicit proxies remain intact across response hooks and redirects.
  • Reworked the tests/test_requests.py cases to run end-to-end against httpbin_secure/httpbin + INVALID_PROXY (covering session.trust_env = False with NO_PROXY, explicit 'no_proxy'/'no', "" overrides, and redirects) and added trust_env coverage in tests/test_utils.py.

Ensure proxy bypass directives (`'no_proxy'` and `'no'` in the `proxies`
mapping, as well as `no_proxy`/`NO_PROXY` environment settings when
`trust_env` is enabled) are evaluated consistently even when explicit
`proxies` are supplied:

- Add `_extract_no_proxy` and `_should_bypass_proxies_with_trust_env` in
  `requests.utils` so explicit `'no_proxy'`/`'no'` keys take precedence
  (including `""` to disable bypass), while environment/OS bypass
  fallback only runs when `trust_env=True`.
- Add `trust_env: bool = True` to `select_proxy` (matching
  `resolve_proxies`) and pass `trust_env=False` from `HTTPAdapter`,
  where `Session` has already evaluated `trust_env`.
- Update `Session.send` to clear `proxies` on a shallow copy of `kwargs`
  passed to `adapter.send` when bypassed, preserving explicit `proxies`
  for response hooks and subsequent redirects.
- Document `'no'` and `'no_proxy'` keys on `requests.request` and
  `Session.request`, and add unit and end-to-end tests in
  `tests/test_utils.py` and `tests/test_requests.py`.

Closes psf#5000
@cjac
cjac force-pushed the fix-no-proxy-2025-10-24 branch from a1582a2 to 1667efd Compare November 2, 2026 18:58
@OKAN-SOFTWARE

Copy link
Copy Markdown

Rechecked 1667efd1: the changes address the trust_env=False paths I flagged. I also ran 75 focused proxy tests locally on Linux; all passed.

The latest fork CI still fails on Windows in both parameterizations of test_no_proxy_in_proxies_dict.

The test uses override_environ(NO_PROXY=host, no_proxy=""). Windows treats environment variable names case-insensitively, so the second assignment clears the first.

Using override_environ(NO_PROXY=host, no_proxy=host) should preserve the intended setup across platforms.

Could you update that setup and rerun the Windows matrix? I haven’t independently run Windows locally.

This branch has not been deployed

No deployments
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.

no_proxy setting ignores the proxies dictionary

3 participants