From fd344e9ab32c9960c9515c62017d2950835eee13 Mon Sep 17 00:00:00 2001 From: Anish Pentyala Date: Thu, 12 Nov 2026 17:05:18 -0800 Subject: [PATCH 1/2] Reject unparseable IRI authorities: iri.py Preserve malformed nonempty authorities as InvalidAuthority rather than silently erasing them during encode. Source independently reviewed at bc90e68a6a8acf1f483359455c0ddf12224976da. --- src/rfc3986/iri.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/rfc3986/iri.py b/src/rfc3986/iri.py index 205221e..eeec4a0 100644 --- a/src/rfc3986/iri.py +++ b/src/rfc3986/iri.py @@ -126,6 +126,7 @@ def encode( # noqa: C901 Function that encodes each part of the host component If not given will raise an exception if the IRI contains a host component. + :raises InvalidAuthority: If a nonempty authority cannot be parsed. :rtype: uri.URIReference :returns: A URI reference """ @@ -150,6 +151,9 @@ def idna_encoder(name: str) -> t.Union[str, bytes]: raise exceptions.InvalidAuthority(self.authority) return name + # The convenience properties suppress InvalidAuthority, which + # would silently discard an unparseable authority here. + self.authority_info() authority = "" if self.host: authority = ".".join( From 7a0cc3aba09a604f484a8c6e9f58abe91aade196 Mon Sep 17 00:00:00 2001 From: Anish Pentyala Date: Thu, 12 Nov 2026 17:05:31 -0800 Subject: [PATCH 2/2] Reject unparseable IRI authorities: test_iri.py Preserve malformed nonempty authorities as InvalidAuthority rather than silently erasing them during encode. Source independently reviewed at bc90e68a6a8acf1f483359455c0ddf12224976da. --- tests/test_iri.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/tests/test_iri.py b/tests/test_iri.py index 385ca67..a1d3d4f 100644 --- a/tests/test_iri.py +++ b/tests/test_iri.py @@ -72,3 +72,67 @@ def test_encode_invalid_iri(iri): iri_ref = rfc3986.iri_reference(iri) with pytest.raises(InvalidAuthority): iri_ref.encode() + + +@pytest.mark.parametrize( + "authority", + [ + "example.com:bad", + "example.com:80:90", + "user:password@exa mple.com", + "[::g]", + "[::1", + "[::1]suffix", + "256.1.2.3", + "bad\x80.example", + ], +) +@pytest.mark.parametrize( + "custom_encoder", [pytest.param(False, marks=requires_idna), True] +) +def test_encode_rejects_unparseable_authority(authority, custom_encoder): + reference = rfc3986.iri_reference( + "https://" + authority + "/path?query#fragment" + ) + calls = [] + + def encoder(label): + calls.append(label) + return label + + with pytest.raises(InvalidAuthority): + if custom_encoder: + reference.encode(idna_encoder=encoder) + else: + reference.encode() + assert reference.authority == authority + assert calls == [] + + +@pytest.mark.parametrize( + "value", + [ + "https://user:password@example.com:080/path?query#fragment", + "https://[::1]:443/path", + "https://[fe80::1%25eth0]/path", + "https://user@:80/path", + "mailto:user@example.com", + "/relative/path?query#fragment", + ], +) +@pytest.mark.parametrize( + "custom_encoder", [pytest.param(False, marks=requires_idna), True] +) +def test_encode_preserves_parseable_authority(value, custom_encoder): + reference = rfc3986.iri_reference(value) + if custom_encoder: + encoded = reference.encode(idna_encoder=lambda label: label) + else: + encoded = reference.encode() + assert encoded.unsplit() == value + + +@requires_idna +def test_encode_empty_port_retains_existing_normalization(): + reference = rfc3986.iri_reference("https://example.com:/path") + assert reference.encode().unsplit() == "https://example.com/path"