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

Ensure ASCII host case normalization happens before validation #1442

Merged
merged 4 commits into from
Dec 1, 2024
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/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