Test 1
fail to passprepend scheme if needed
Requests: Proxy Auth Credentials Stripped by prepend_scheme_if_needed
psf__requests-6028 · cardboard
You are working in the psf/requests repository at commit 0192aac24123735b3eaf9b08df46429bb770c283 (v2.27.0).
A regression introduced in requests 2.27 causes proxy URLs that include authentication credentials (in the form user:pass@host or user@host) to silently lose those credentials when passed through prepend_scheme_if_needed. This causes real-world HTTP 407 Proxy Authentication Required failures: the proxy credentials are present in the configured proxy URL, but by the time the URL is used to open a connection, the user:pass@ segment has been discarded.
The root cause lives in requests/utils.py, in the prepend_scheme_if_needed function. The function was refactored to use urllib3.util.url.parse_url (instead of urllib.parse.urlparse) to decompose the URL, then reassembles the result with urllib.parse.urlunparse. The problem is that parse_url returns an auth component separately, and that component is never woven back into netloc before urlunparse is called. As a result, any user:pass@ prefix that was part of the original netloc is silently dropped.
from requests.utils import prepend_scheme_if_needed
# Both of these should round-trip unchanged (scheme is already present,
# so prepend_scheme_if_needed should return the URL as-is):
result = prepend_scheme_if_needed("http://user:pass@proxy.example.com/", "http")
assert result == "http://user:pass@proxy.example.com/", repr(result)
result = prepend_scheme_if_needed("http://user@proxy.example.com/", "http")
assert result == "http://user@proxy.example.com/", repr(result)Without the fix, both assertions fail because the returned URLs are missing the user:pass@ / user@ segment.
requests/utils.pyprepend_scheme_if_needed(value, default_scheme)auth component (username and optional password) when it reconstructs the URL.user:pass@host, user@host, and no credentials at all.| Input value | default_scheme | Expected return |
|---|---|---|
| "http://user:pass@example.com/path?query" | "http" | "http://user:pass@example.com/path?query" |
| "http://user@example.com/path?query" | "http" | "http://user@example.com/path?query" |
| "example.com/path" | "http" | "http://example.com/path" |
| "//example.com/path" | "http" | "http://example.com/path" |
| "example.com:80" | "http" | "http://example.com:80" |
The first two rows are the new regression cases. The last three rows are existing passing cases that must continue to work.
requests/utils.py unless you have a compelling reason to touch another file.tests/test_utils.py.A submission is accepted when:
1. Both new test_prepend_scheme_if_needed parametrize cases for URLs containing auth credentials pass.
2. All listed pass_to_pass tests in tests/test_utils.py continue to pass.
Accepted runs are additionally ranked on the leaderboard by token count, estimated cost, and wall-clock time; a minimal, focused patch scores best.
Container
not started
Visible tests
111
Hidden tests
0
Last run
Not run
Test 1
fail to passprepend scheme if needed
Test 2
fail to passprepend scheme if needed
Test 3
pass to passio streams
Test 4
pass to passio streams
Test 5
pass to passsuper len correctly calculates len of partially read file
Test 6
pass to passsuper len handles files raising weird errors in tell
Test 7
pass to passsuper len handles files raising weird errors in tell
Test 8
pass to passsuper len tell ioerror
Test 9
pass to passsuper len tell ioerror
Test 10
pass to passstring
Test 11
pass to passfile
Test 12
pass to passfile
Test 13
pass to passtarfile member
Test 14
pass to passsuper len with len
Test 15
pass to passsuper len with no len
Test 16
pass to passsuper len with tell
Test 17
pass to passsuper len with fileno
Test 18
pass to passsuper len with no matches
Test 19
pass to passvalid
Test 20
pass to passvalid
Test 21
pass to passvalid
Test 22
pass to passvalid
Test 23
pass to passinvalid
Test 24
pass to passbypass
Test 25
pass to passbypass
Test 26
pass to passbypass
Test 27
pass to passbypass
Test 28
pass to passbypass
Test 29
pass to passbypass
Test 30
pass to passbypass
Test 31
pass to passbypass
Test 32
pass to passbypass
Test 33
pass to passbypass
Test 34
pass to passnot bypass
Test 35
pass to passnot bypass
Test 36
pass to passnot bypass
Test 37
pass to passnot bypass
Test 38
pass to passnot bypass
Test 39
pass to passnot bypass
Test 40
pass to passvalid
Test 41
pass to passinvalid
Test 42
pass to passinvalid
Test 43
pass to passvalid
Test 44
pass to passinvalid
Test 45
pass to passinvalid
Test 46
pass to passinvalid
Test 47
pass to passinvalid
Test 48
pass to passinvalid
Test 49
pass to passvalid
Test 50
pass to passinvalid
Test 51
pass to passguess filename invalid
Test 52
pass to passguess filename invalid
Test 53
pass to passguess filename valid
Test 54
pass to passguess filename valid
Test 55
pass to passunzipped paths unchanged
Test 56
pass to passunzipped paths unchanged
Test 57
pass to passunzipped paths unchanged
Test 58
pass to passunzipped paths unchanged
Test 59
pass to passzipped paths extracted
Test 60
pass to passinvalid unc path
Test 61
pass to passnone
Test 62
pass to passencoded
Test 63
pass to passencoded
Test 64
pass to passencoded
Test 65
pass to passencoded
Test 66
pass to passencoded
Test 67
pass to passencoded
Test 68
pass to passencoded
Test 69
pass to passencoded
Test 70
pass to passbad utf like encoding
Test 71
pass to passguess by bom
Test 72
pass to passguess by bom
Test 73
pass to passguess by bom
Test 74
pass to passguess by bom
Test 75
pass to passdotted netmask
Test 76
pass to passdotted netmask
Test 77
pass to passdotted netmask
Test 78
pass to passprepend scheme if needed
Test 79
pass to passprepend scheme if needed
Test 80
pass to passprepend scheme if needed
Test 81
pass to passurldefragauth
Test 82
pass to passurldefragauth
Test 83
pass to passurldefragauth
Test 84
pass to passurldefragauth
Test 85
pass to passurldefragauth
Test 86
pass to passurldefragauth
Test 87
pass to passshould bypass proxies
Test 88
pass to passshould bypass proxies
Test 89
pass to passshould bypass proxies
Test 90
pass to passshould bypass proxies
Test 91
pass to passshould bypass proxies
Test 92
pass to passshould bypass proxies
Test 93
pass to passshould bypass proxies
Test 94
pass to passshould bypass proxies
Test 95
pass to passshould bypass proxies
Test 96
pass to passshould bypass proxies
Test 97
pass to passadd dict to cookiejar
Test 98
pass to passadd dict to cookiejar
Test 99
pass to passshould bypass proxies no proxy
Test 100
pass to passshould bypass proxies no proxy
Test 101
pass to passshould bypass proxies no proxy
Test 102
pass to passshould bypass proxies no proxy
Test 103
pass to passshould bypass proxies no proxy
Test 104
pass to passshould bypass proxies no proxy
Test 105
pass to passshould bypass proxies no proxy
Test 106
pass to passshould bypass proxies no proxy
Test 107
pass to passset environ
Test 108
pass to passset environ
Test 109
pass to passset environ
Test 110
pass to passset environ
Test 111
pass to passset environ raises exception
README.md
psf/requests