Skip to content

fix: user-configured proxies being overridden by http_proxy/https_proxy env vars - #2445

Open
cristianchiriac wants to merge 2 commits into
pycontribs:mainfrom
cristianchiriac:fix/resilientsession-proxies-vs-env
Open

cristianchiriac wants to merge 2 commits into
pycontribs:mainfrom
cristianchiriac:fix/resilientsession-proxies-vs-env

Conversation

@cristianchiriac

Copy link
Copy Markdown

Fixes #2398.

Bug

JIRA(..., proxies={...}) sets self._session.proxies on the ResilientSession, but ResilientSession.request() never forwards a proxies kwarg to requests.Session.request() — it relies on self.proxies being picked up as a fallback. That fallback only works for keys that are still missing by the time requests does its final merge.

Tracing through requests.Session.request():

  1. proxies = proxies or {} — since no proxies kwarg was passed, this starts as an empty dict.
  2. merge_environment_settings(url, proxies, ...) fills that empty dict from the http_proxy/https_proxy environment variables via proxies.setdefault(k, v), then calls proxies = merge_setting(proxies, self.proxies).
  3. merge_setting(request_setting, session_setting) builds the result by copying session_setting (self.proxies, the configured value) first, then calling .update(request_setting) — so the request-level dict (now populated from the environment) overwrites self.proxies for any matching key.

Net effect: whenever http_proxy/https_proxy are set in the environment, they silently win over an explicitly configured proxies dict, even though the explicit configuration should be authoritative.

I confirmed this against the actual requests merge functions:

>>> s.merge_environment_settings('http://example.com', {}, None, None, None)['proxies']
OrderedDict([('http', 'localhost:3128'), ('https', 'localhost:3128')])   # env wins, scheme stripped

>>> s.merge_environment_settings('http://example.com', dict(s.proxies), None, None, None)['proxies']
OrderedDict([('http', 'http://localhost:3128'), ('https', 'http://localhost:3128')])   # configured value kept

Fix

_jira_prepare() now passes self.proxies through explicitly as the proxies kwarg (when set and not already overridden per-call), so it's already populated before merge_environment_settings()'s environment fallback runs — setdefault() then only fills genuinely missing keys instead of a dict that later overwrites the configured values.

Test plan

  • Added test_configured_proxies_are_forwarded, verifying a session-level proxies dict is passed through to requests.Session.request() unchanged.
  • Added test_no_proxies_kwarg_when_unconfigured, verifying no proxies kwarg is added when none was configured (preserving prior behavior/defaults for the common case).
  • Verified the new tests fail against the unmodified code and pass with the fix.
  • pytest tests/test_resilientsession.py: 37 passed.
  • ruff check and ruff format --check on the changed files: clean.

… vars

ResilientSession.request() never forwarded a "proxies" kwarg to
requests.Session.request(), relying entirely on self.proxies as the
fallback. But requests.Session.merge_environment_settings() only uses
self.proxies as a fallback for keys missing from the per-request
proxies dict; when no "proxies" kwarg is passed, that dict starts
empty and gets filled first from the http_proxy/https_proxy
environment variables via setdefault(), before merge_setting() lets
those request-level values win over self.proxies for any matching
key. The end result: a JIRA client configured with an explicit
proxies dict silently used the environment's proxies instead whenever
those env vars were also set.

Pass self.proxies through explicitly as the "proxies" kwarg in
_jira_prepare() so it's already populated before the environment
fallback runs, and can only fill gaps rather than being overridden.

Fixes pycontribs#2398
@cristianchiriac
cristianchiriac requested a review from a team as a code owner September 17, 2026 15:26
@github-actions

Copy link
Copy Markdown

Label error. Requires at least 1 of: breaking, chore, feat, fix. Found: . Follow https://www.conventionalcommits.org to get auto-labeling to work correctly.

@cristianchiriac cristianchiriac changed the title Fix user-configured proxies being overridden by http_proxy/https_proxy env vars fix: user-configured proxies being overridden by http_proxy/https_proxy env vars Sep 17, 2026

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.

User defined proxies not used properly

1 participant