Skip to content
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

Allow URLs where username or password contains unescaped '@'. #2986

Merged
merged 4 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions httpx/_urlparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
(
r"(?:(?P<userinfo>{userinfo})@)?" r"(?P<host>{host})" r":?(?P<port>{port})?"
).format(
userinfo="[^@]*", # Any character sequence not including '@'.
host="(\\[.*\\]|[^:]*)", # Either any character sequence not including ':',
userinfo=".*", # Any character sequence.
host="(\\[.*\\]|[^:@]*)", # Either any character sequence excluding ':' or '@',
# or an IPv6 address enclosed within square brackets.
port=".*", # Any character sequence.
)
Expand Down
48 changes: 48 additions & 0 deletions tests/models/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,54 @@ def test_url_params():
assert url.params == httpx.QueryParams({"a": "123"})


# Tests for username and password


@pytest.mark.parametrize(
"url,userinfo,username,password",
[
# username and password in URL.
(
"https://username:password@example.com",
b"username:password",
"username",
"password",
),
# username and password in URL with percent escape sequences.
(
"https://username%40gmail.com:pa%20ssword@example.com",
b"username%40gmail.com:pa%20ssword",
"username@gmail.com",
"pa ssword",
),
(
"https://user%20name:p%40ssword@example.com",
b"user%20name:p%40ssword",
"user name",
"p@ssword",
),
# username and password in URL without percent escape sequences.
(
"https://username@gmail.com:pa ssword@example.com",
b"username%40gmail.com:pa%20ssword",
"username@gmail.com",
"pa ssword",
),
(
"https://user name:p@ssword@example.com",
b"user%20name:p%40ssword",
"user name",
"p@ssword",
),
],
)
def test_url_username_and_password(url, userinfo, username, password):
url = httpx.URL(url)
assert url.userinfo == userinfo
assert url.username == username
assert url.password == password


# Tests for different host types


Expand Down