Skip to content

Commit

Permalink
Ensure ASCII host case normalization happens before validation (#1442)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Dec 1, 2024
1 parent 4706c4c commit 20661d6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES/1442.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed uppercase ASCII hosts being rejected by :meth:`URL.build() <yarl.URL.build>` and :py:meth:`~yarl.URL.with_host` -- by :user:`bdraco`.
1 change: 1 addition & 0 deletions CHANGES/954.bugfix.rst
8 changes: 8 additions & 0 deletions tests/test_url_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,11 @@ def test_build_with_none_query_string():
def test_build_with_none_fragment():
with pytest.raises(TypeError):
URL.build(scheme="http", host="example.com", fragment=None)


def test_build_uppercase_host():
u = URL.build(
host="UPPER.case",
encoded=False,
)
assert u.host == "upper.case"
3 changes: 2 additions & 1 deletion yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,7 @@ def _encode_host(host: str, validate_host: bool) -> str:
if host.isascii():
# Check for invalid characters explicitly; _idna_encode() does this
# for non-ascii host names.
host = host.lower()
if validate_host and (invalid := NOT_REG_NAME.search(host)):
value, pos, extra = invalid.group(), invalid.start(), ""
if value == "@" or (value == ":" and "@" in host[pos:]):
Expand All @@ -1510,7 +1511,7 @@ def _encode_host(host: str, validate_host: bool) -> str:
raise ValueError(
f"Host {host!r} cannot contain {value!r} (at position {pos}){extra}"
) from None
return host.lower()
return host

return _idna_encode(host)

Expand Down

0 comments on commit 20661d6

Please sign in to comment.