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

Correct square bracket handling in URL netloc #882

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/876.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed the human representation of URLs with square brackets in usernames and passwords.
10 changes: 2 additions & 8 deletions tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,6 @@ def test_compressed_ipv6():
assert url.host == url.raw_host


def test_ipv6_zone():
url = URL("http://[fe80::822a:a8ff:fe49:470c%тест%42]:123")
assert url.raw_host == "fe80::822a:a8ff:fe49:470c%тест%42"
assert url.host == url.raw_host


def test_ipv4_zone():
# I'm unsure if it is correct.
url = URL("http://1.2.3.4%тест%42:123")
Expand Down Expand Up @@ -1660,8 +1654,8 @@ def test_human_repr_delimiters():
s = url.human_repr()
assert URL(s) == url
assert (
s == "http:// !\"%23$%25&'()*+,-.%2F%3A;<=>%3F%40[\\]^_`{|}~"
": !\"%23$%25&'()*+,-.%2F%3A;<=>%3F%40[\\]^_`{|}~"
s == "http:// !\"%23$%25&'()*+,-.%2F%3A;<=>%3F%40%5B\\%5D^_`{|}~"
": !\"%23$%25&'()*+,-.%2F%3A;<=>%3F%40%5B\\%5D^_`{|}~"
"@хост.домен:8080"
"/ !\"%23$%25&'()*+,-./:;<=>%3F@[\\]^_`{|}~"
"? !\"%23$%25%26'()*%2B,-./:%3B<%3D>?@[\\]^_`{|}~"
Expand Down
28 changes: 2 additions & 26 deletions tests/test_url_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,6 @@ def test_ipv4(self):
assert u.query_string == ""
assert u.fragment == ""

def test_masked_ipv4(self):
u = URL("//[127.0.0.1]/")
assert u.scheme == ""
assert u.host == "127.0.0.1"
assert u.path == "/"
assert u.query_string == ""
assert u.fragment == ""

def test_ipv6(self):
u = URL("//[::1]/")
assert u.scheme == ""
Expand All @@ -194,30 +186,14 @@ def test_ipv6(self):
assert u.query_string == ""
assert u.fragment == ""

def test_strange_ip(self):
u = URL("//[-1]/")
assert u.scheme == ""
assert u.host == "-1"
assert u.path == "/"
assert u.query_string == ""
assert u.fragment == ""

def test_strange_ip_2(self):
def test_ipvfuture_address(self):
u = URL("//[v1.-1]/")
assert u.scheme == ""
assert u.host == "v1.-1"
assert u.path == "/"
assert u.query_string == ""
assert u.fragment == ""

def test_strange_ip_3(self):
u = URL("//v1.[::1]/")
assert u.scheme == ""
assert u.host == "::1"
assert u.path == "/"
assert u.query_string == ""
assert u.fragment == ""


class TestPort:
def test_canonical(self):
Expand Down Expand Up @@ -320,7 +296,7 @@ def test_weird_user2(self):
assert u.fragment == ""

def test_weird_user3(self):
u = URL("//[some]@host")
u = URL("//%5Bsome%5D@host")
assert u.scheme == ""
assert u.user == "[some]"
assert u.password is None
Expand Down
4 changes: 2 additions & 2 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -1118,8 +1118,8 @@ def joinpath(self, *other, encoded=False):

def human_repr(self):
"""Return decoded human readable string for URL representation."""
user = _human_quote(self.user, "#/:?@")
password = _human_quote(self.password, "#/:?@")
user = _human_quote(self.user, "#/:?@[]")
password = _human_quote(self.password, "#/:?@[]")
host = self.host
if host:
host = self._encode_host(self.host, human=True)
Expand Down