Skip to content

Commit 20661d6

Browse files
authored
Ensure ASCII host case normalization happens before validation (#1442)
1 parent 4706c4c commit 20661d6

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

CHANGES/1442.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed uppercase ASCII hosts being rejected by :meth:`URL.build() <yarl.URL.build>` and :py:meth:`~yarl.URL.with_host` -- by :user:`bdraco`.

CHANGES/954.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1442.bugfix.rst

tests/test_url_build.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,11 @@ def test_build_with_none_query_string():
382382
def test_build_with_none_fragment():
383383
with pytest.raises(TypeError):
384384
URL.build(scheme="http", host="example.com", fragment=None)
385+
386+
387+
def test_build_uppercase_host():
388+
u = URL.build(
389+
host="UPPER.case",
390+
encoded=False,
391+
)
392+
assert u.host == "upper.case"

yarl/_url.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,7 @@ def _encode_host(host: str, validate_host: bool) -> str:
14991499
if host.isascii():
15001500
# Check for invalid characters explicitly; _idna_encode() does this
15011501
# for non-ascii host names.
1502+
host = host.lower()
15021503
if validate_host and (invalid := NOT_REG_NAME.search(host)):
15031504
value, pos, extra = invalid.group(), invalid.start(), ""
15041505
if value == "@" or (value == ":" and "@" in host[pos:]):
@@ -1510,7 +1511,7 @@ def _encode_host(host: str, validate_host: bool) -> str:
15101511
raise ValueError(
15111512
f"Host {host!r} cannot contain {value!r} (at position {pos}){extra}"
15121513
) from None
1513-
return host.lower()
1514+
return host
15141515

15151516
return _idna_encode(host)
15161517

0 commit comments

Comments
 (0)