Repository navigation
gh-158907: Do not send proxy credentials to a direct origin on 407 #158956
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rajorin
wants to merge
3
commits into
python:main
Choose a base branch
from
rajorin:gh-158907-proxy-auth-direct-407
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
gh-158907: Do not send proxy credentials to a direct origin on 407
ProxyBasicAuthHandler looked up passwords with the scheme-less origin host, so an HTTP 407 could match credentials stored for HTTPS and retry in cleartext.
- Loading branch information
commit 6c9f08f830c0f51fb3139c9fd642dbd9cdade2d0
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1048,6 +1048,12 @@ class ProxyBasicAuthHandler(AbstractBasicAuthHandler, BaseHandler): | |||||||||||
| auth_header = 'Proxy-authorization' | ||||||||||||
|
|
||||||||||||
| def http_error_407(self, req, fp, code, msg, headers): | ||||||||||||
| # A direct origin is not a proxy. req.host has no scheme, so a | ||||||||||||
| # password stored for https://HOST/ would match and be sent in | ||||||||||||
| # cleartext on the retry (gh-158907). HTTPS tunnels set | ||||||||||||
| # _tunnel_host instead of making has_proxy() true. | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about making this comment a bit less verbose?
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shortened it to that line. |
||||||||||||
| if not req.has_proxy() and not req._tunnel_host: | ||||||||||||
| return None | ||||||||||||
| # http_error_auth_reqed requires that there is no userinfo component in | ||||||||||||
| # authority. Assume there isn't one, since urllib.request does not (and | ||||||||||||
| # should not, RFC 3986 s. 3.2.1) support requests for URLs containing | ||||||||||||
|
|
||||||||||||
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Security/2026-10-07-13-30-00.gh-issue-158907.k4Qm2p.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| :class:`~urllib.request.ProxyBasicAuthHandler` no longer sends credentials | ||
| in response to ``407`` from a direct origin. A scheme-less lookup against | ||
| the origin host could match passwords stored for an HTTPS URL and retry | ||
| the cleartext request with ``Proxy-Authorization``. |
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more thing. It's possible I'm confused here!
I'm not sure if 407 from the proxy during an HTTPS tunnel even reaches
http_error_407().HTTPConnection._tunnel()raisesOSErroron a non-200CONNECTreply:cpython/Lib/http/client.py
Lines 1039 to 1041 in 2639fd6
I think this is #51540
Perhaps then:
Another approach is removing this test. I believe it passes even without the PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right on both counts.
HTTPConnection._tunnel()raisesOSErrorfor a non-200 CONNECT, anddo_openturns that intoURLError, so that 407 never reacheshttp_error_407. That's the existing gap in gh-51540.The tunnel test also passes on main, so I removed it.
_tunnel_hoststays in the guard because an HTTPS proxy request does not sethas_proxy(), and a 407 that does reach this handler should still be answered. On the retry,Proxy-Authorizationis sent on CONNECT, not to the origin.