From 80fee57b503fb5388f03e37704129e53c6393570 Mon Sep 17 00:00:00 2001 From: waheeda bette Date: Wed, 21 Oct 2026 18:23:37 +0530 Subject: [PATCH] reject unparseable authority when host/port/password checks are set --- src/rfc3986/validators.py | 10 ++++++++++ tests/test_validators.py | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/rfc3986/validators.py b/src/rfc3986/validators.py index d68f16b..18f7ecf 100644 --- a/src/rfc3986/validators.py +++ b/src/rfc3986/validators.py @@ -213,6 +213,16 @@ def validate(self, uri: "uri.URIReference") -> None: :raises InvalidComponentsError: When a component was found to be invalid. """ + if uri.authority is not None and ( + self.allowed_hosts or self.allowed_ports or not self.allow_password + ): + # An unparseable authority makes userinfo, host, and port all + # None, which would otherwise skip the checks below. + try: + uri.authority_info() + except exceptions.InvalidAuthority: + raise exceptions.InvalidComponentsError(uri, "authority") + if not self.allow_password: check_password(uri) diff --git a/tests/test_validators.py b/tests/test_validators.py index 2bab3a1..a7a9d62 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -203,6 +203,27 @@ def test_allowed_hosts_and_schemes(uri, failed_component): assert exc.component_name == failed_component +@pytest.mark.parametrize( + "validator", + [ + validators.Validator().allow_hosts("github.com"), + validators.Validator().allow_ports("443"), + validators.Validator().forbid_use_of_password(), + ], +) +@pytest.mark.parametrize( + "uri", + [ + rfc3986.uri_reference("https://user@user@evil.com/"), + rfc3986.uri_reference("https://user:pass@evil.com:80:80/"), + ], +) +def test_unparseable_authority_fails_subauthority_checks(validator, uri): + """Verify an invalid authority does not skip host/port/password checks.""" + with pytest.raises(exceptions.InvalidComponentsError): + validator.validate(uri) + + @pytest.mark.parametrize( "uri", [