Skip to content

Commit 3fba971

Browse files
committed
Reject non-identity IDNA mappings in Unicode hosts
1 parent e11cec5 commit 3fba971

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

‎Documentation/connection-strings.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ chronicle://[<client-id>:<client-secret>@]<host>[:<port>][/][?skipTlsValidation=
3030
preserved. Rejecting unassigned characters also rejects newly assigned format characters on older Python versions.
3131
- Internationalised host names are accepted as Unicode (`münchen.example`, `例え.テスト`) or ASCII punycode
3232
(`xn--mnchen-3ya.example`, `xn--r8jz45g.xn--zckzah`). The supplied representation is preserved, as in the .NET client.
33+
Unicode host names must round-trip through IDNA unchanged after NFC normalisation and lowercasing; uppercase forms
34+
are accepted. Nameprep mappings that change other characters are rejected, including fullwidth letters or digits,
35+
invisible joiners, mapped dot characters and `ß` mapped to `ss`.
3336
Host names are validated using their IDNA-encoded ASCII form: non-empty labels of 1–63 characters, starting and
3437
ending with a letter or digit, and at most 253 characters in total, ignoring an optional trailing dot, which is
3538
preserved.

‎src/cratis_chronicle/connection_string.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,12 @@ def _parse_host_and_port(host_and_port: str) -> tuple[str, int]:
259259

260260
def _encode_idn_host(host: str) -> str:
261261
"""Validate Unicode DNS names through IDNA without changing the returned host representation."""
262+
ascii_host: str | None
262263
try:
263-
ascii_host = host.encode("idna").decode("ascii")
264+
ascii_host = host.lower().encode("idna").decode("ascii")
265+
round_trip_host = ascii_host.encode("ascii").decode("idna")
266+
if round_trip_host != unicodedata.normalize("NFC", host).lower():
267+
ascii_host = None
264268
except UnicodeError:
265269
ascii_host = None
266270
if ascii_host is None:

‎tests/test_connection_string.py‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,24 +675,28 @@ def test_preserves_percent_encoded_unassigned_or_newly_assigned_format_character
675675
("unicode_host", "ascii_host"),
676676
[
677677
("münchen.example", "xn--mnchen-3ya.example"),
678+
("mu\u0308nchen.example", "xn--mnchen-3ya.example"),
678679
("例え.テスト", "xn--r8jz45g.xn--zckzah"),
679680
("bücher.example", "xn--bcher-kva.example"),
680681
],
681682
)
682683
@pytest.mark.parametrize("host_form", ["unicode", "punycode"])
684+
@pytest.mark.parametrize("host_case", ["lower", "upper"])
683685
@pytest.mark.parametrize("trailing_dot", ["", "."])
684686
def test_accepts_idn_hosts_and_preserves_the_supplied_representation(
685-
unicode_host: str, ascii_host: str, host_form: str, trailing_dot: str
687+
unicode_host: str, ascii_host: str, host_form: str, host_case: str, trailing_dot: str
686688
) -> None:
687689
host = (unicode_host if host_form == "unicode" else ascii_host) + trailing_dot
690+
if host_case == "upper":
691+
host = host.upper()
688692
options = parse_connection_string(f"chronicle://id:secret@{host}")
689693

690694
assert options.host == host
691-
assert options.host.encode("idna").decode("ascii") == ascii_host + trailing_dot
695+
assert options.host.encode("idna").decode("ascii").lower() == ascii_host + trailing_dot
692696
assert str(options) == f"chronicle://id:****@{host}:35000"
693697

694698

695-
@pytest.mark.parametrize("host", ["PRIVATE_HOST\ud800.example", "é" * 58 + ".example"])
699+
@pytest.mark.parametrize("host", ["PRIVATE_HOST\ud800.example", "é" * 58 + ".example", "münchen.xn--"])
696700
def test_idna_conversion_failures_raise_a_chain_free_host_error(host: str) -> None:
697701
connection_string = f"chronicle://PRIVATE_CLIENT:PRIVATE_SECRET@{host}"
698702
with pytest.raises(InvalidHostError) as caught:
@@ -707,6 +711,24 @@ def test_idna_conversion_failures_raise_a_chain_free_host_error(host: str) -> No
707711
assert caught.value.__context__ is None
708712

709713

714+
@pytest.mark.parametrize(
715+
"host", ["example.com", "127.0.0.1", "ex\u034fample.com", "x\u2024y.é", "faß.de"]
716+
)
717+
@pytest.mark.parametrize("trailing_dot", ["", "."])
718+
def test_rejects_idna_mappings_that_change_the_supplied_host(host: str, trailing_dot: str) -> None:
719+
connection_string = f"chronicle://PRIVATE_CLIENT:PRIVATE_SECRET@{host}{trailing_dot}"
720+
with pytest.raises(InvalidHostError) as caught:
721+
parse_connection_string(connection_string)
722+
723+
assert str(caught.value) == "The host is not a valid internationalised host name"
724+
for rendering in (str(caught.value), repr(caught.value), "".join(traceback.format_exception(caught.value))):
725+
assert "PRIVATE_CLIENT" not in rendering
726+
assert "PRIVATE_SECRET" not in rendering
727+
assert host not in rendering
728+
assert caught.value.__cause__ is None
729+
assert caught.value.__context__ is None
730+
731+
710732
@pytest.mark.parametrize("trailing_dot", ["", "."])
711733
def test_accepts_unicode_hosts_at_the_encoded_dns_length_limits(trailing_dot: str) -> None:
712734
host = ".".join(["é" * 57] * 3 + ["a" * 61]) + trailing_dot

0 commit comments

Comments
 (0)