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
6 changes: 4 additions & 2 deletions src/specify_cli/authentication/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,14 @@ def find_entries_for_url(
) -> list[AuthConfigEntry]:
"""Return entries whose ``hosts`` match the hostname of *url*."""
# A malformed authority (e.g. an unterminated IPv6 bracket "https://[::1")
# makes urlparse/hostname raise ValueError. Treat that the same as a
# makes urlparse, hostname, or port raise ValueError. Treat that the same as a
# host-less URL: no entry can match, so return no matches rather than
# leaking a raw ValueError out of the shared HTTP client (build_request /
# open_url call this before any URL validation).
try:
hostname = (urlparse(url).hostname or "").lower()
parsed = urlparse(url)
hostname = (parsed.hostname or "").lower()
_ = parsed.port
except ValueError:
return []
if not hostname:
Expand Down
6 changes: 4 additions & 2 deletions tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def test_world_readable_warns(self, tmp_path):
class TestFindEntriesForUrl:
def test_exact_match(self):
entry = _github_entry()
result = find_entries_for_url("https://github.com/org/repo", [entry])
result = find_entries_for_url("https://github.com:443/org/repo", [entry])
assert result == [entry]

def test_wildcard_match(self):
Expand Down Expand Up @@ -409,10 +409,12 @@ def test_empty_url_returns_empty(self):
[
"https://[::1", # unterminated ipv6 bracket
"https://[not-an-ip]/file", # bracketed non-ip host
"https://github.com:notaport/x", # non-numeric port
"https://github.com:99999/x", # out-of-range port
],
)
def test_malformed_url_returns_empty(self, url):
# A malformed authority makes urlparse/hostname raise ValueError.
# A malformed authority makes urlparse, hostname, or port raise ValueError.
# Since no entry can match such a URL, this must return no matches
# (like a host-less URL) rather than leaking a raw ValueError out of
# the shared HTTP client.
Expand Down