Skip to content
Open
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
10 changes: 10 additions & 0 deletions src/rfc3986/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
21 changes: 21 additions & 0 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
[
Expand Down