Promptice

Requests: Proxy Auth Credentials Stripped by prepend_scheme_if_needed

psf__requests-6028 · cardboard

Requests: Proxy Auth Credentials Stripped by prepend_scheme_if_needed

You are working in the psf/requests repository at commit 0192aac24123735b3eaf9b08df46429bb770c283 (v2.27.0).

Problem

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.

Reproduction

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.

Requirements / Interface

  • File to modify: requests/utils.py
  • Function to fix: prepend_scheme_if_needed(value, default_scheme)
  • The function must preserve the auth component (username and optional password) when it reconstructs the URL.
  • The fix must work for all three credential forms: user:pass@host, user@host, and no credentials at all.
  • The function signature and all other observable behavior must remain unchanged.

Examples

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

Constraints

  • Change only requests/utils.py unless you have a compelling reason to touch another file.
  • Do not change the function's public signature.
  • Do not introduce new dependencies (urllib3 is already a dependency).
  • Do not break any other tests in tests/test_utils.py.
  • The hidden grader applies its own test patch; do not rely on modifying the test file to make tests pass.

Scoring

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

111 total0 passed0 failed
1

Test 1

fail to pass

prepend scheme if needed

2

Test 2

fail to pass

prepend scheme if needed

3

Test 3

pass to pass

io streams

4

Test 4

pass to pass

io streams

5

Test 5

pass to pass

super len correctly calculates len of partially read file

6

Test 6

pass to pass

super len handles files raising weird errors in tell

7

Test 7

pass to pass

super len handles files raising weird errors in tell

8

Test 8

pass to pass

super len tell ioerror

9

Test 9

pass to pass

super len tell ioerror

10

Test 10

pass to pass

string

11

Test 11

pass to pass

file

12

Test 12

pass to pass

file

13

Test 13

pass to pass

tarfile member

14

Test 14

pass to pass

super len with len

15

Test 15

pass to pass

super len with no len

16

Test 16

pass to pass

super len with tell

17

Test 17

pass to pass

super len with fileno

18

Test 18

pass to pass

super len with no matches

19

Test 19

pass to pass

valid

20

Test 20

pass to pass

valid

21

Test 21

pass to pass

valid

22

Test 22

pass to pass

valid

23

Test 23

pass to pass

invalid

24

Test 24

pass to pass

bypass

25

Test 25

pass to pass

bypass

26

Test 26

pass to pass

bypass

27

Test 27

pass to pass

bypass

28

Test 28

pass to pass

bypass

29

Test 29

pass to pass

bypass

30

Test 30

pass to pass

bypass

31

Test 31

pass to pass

bypass

32

Test 32

pass to pass

bypass

33

Test 33

pass to pass

bypass

34

Test 34

pass to pass

not bypass

35

Test 35

pass to pass

not bypass

36

Test 36

pass to pass

not bypass

37

Test 37

pass to pass

not bypass

38

Test 38

pass to pass

not bypass

39

Test 39

pass to pass

not bypass

40

Test 40

pass to pass

valid

41

Test 41

pass to pass

invalid

42

Test 42

pass to pass

invalid

43

Test 43

pass to pass

valid

44

Test 44

pass to pass

invalid

45

Test 45

pass to pass

invalid

46

Test 46

pass to pass

invalid

47

Test 47

pass to pass

invalid

48

Test 48

pass to pass

invalid

49

Test 49

pass to pass

valid

50

Test 50

pass to pass

invalid

51

Test 51

pass to pass

guess filename invalid

52

Test 52

pass to pass

guess filename invalid

53

Test 53

pass to pass

guess filename valid

54

Test 54

pass to pass

guess filename valid

55

Test 55

pass to pass

unzipped paths unchanged

56

Test 56

pass to pass

unzipped paths unchanged

57

Test 57

pass to pass

unzipped paths unchanged

58

Test 58

pass to pass

unzipped paths unchanged

59

Test 59

pass to pass

zipped paths extracted

60

Test 60

pass to pass

invalid unc path

61

Test 61

pass to pass

none

62

Test 62

pass to pass

encoded

63

Test 63

pass to pass

encoded

64

Test 64

pass to pass

encoded

65

Test 65

pass to pass

encoded

66

Test 66

pass to pass

encoded

67

Test 67

pass to pass

encoded

68

Test 68

pass to pass

encoded

69

Test 69

pass to pass

encoded

70

Test 70

pass to pass

bad utf like encoding

71

Test 71

pass to pass

guess by bom

72

Test 72

pass to pass

guess by bom

73

Test 73

pass to pass

guess by bom

74

Test 74

pass to pass

guess by bom

75

Test 75

pass to pass

dotted netmask

76

Test 76

pass to pass

dotted netmask

77

Test 77

pass to pass

dotted netmask

78

Test 78

pass to pass

prepend scheme if needed

79

Test 79

pass to pass

prepend scheme if needed

80

Test 80

pass to pass

prepend scheme if needed

81

Test 81

pass to pass

urldefragauth

82

Test 82

pass to pass

urldefragauth

83

Test 83

pass to pass

urldefragauth

84

Test 84

pass to pass

urldefragauth

85

Test 85

pass to pass

urldefragauth

86

Test 86

pass to pass

urldefragauth

87

Test 87

pass to pass

should bypass proxies

88

Test 88

pass to pass

should bypass proxies

89

Test 89

pass to pass

should bypass proxies

90

Test 90

pass to pass

should bypass proxies

91

Test 91

pass to pass

should bypass proxies

92

Test 92

pass to pass

should bypass proxies

93

Test 93

pass to pass

should bypass proxies

94

Test 94

pass to pass

should bypass proxies

95

Test 95

pass to pass

should bypass proxies

96

Test 96

pass to pass

should bypass proxies

97

Test 97

pass to pass

add dict to cookiejar

98

Test 98

pass to pass

add dict to cookiejar

99

Test 99

pass to pass

should bypass proxies no proxy

100

Test 100

pass to pass

should bypass proxies no proxy

101

Test 101

pass to pass

should bypass proxies no proxy

102

Test 102

pass to pass

should bypass proxies no proxy

103

Test 103

pass to pass

should bypass proxies no proxy

104

Test 104

pass to pass

should bypass proxies no proxy

105

Test 105

pass to pass

should bypass proxies no proxy

106

Test 106

pass to pass

should bypass proxies no proxy

107

Test 107

pass to pass

set environ

108

Test 108

pass to pass

set environ

109

Test 109

pass to pass

set environ

110

Test 110

pass to pass

set environ

111

Test 111

pass to pass

set environ raises exception

README.md

psf/requests

Loading repository...code-server
Loading...
Workspace Terminal