Skip to content

Commit 9ad835d

Browse files
committed
Fix port matching
1 parent bb55585 commit 9ad835d

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/guardrails/checks/text/urls.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,6 @@ def _is_url_allowed(
327327
# Malformed port (out of range or invalid) - reject the URL
328328
return False
329329
url_port = _safe_get_port(parsed_url, scheme_lower)
330-
# If port is invalid (None from _safe_get_port due to ValueError), reject the URL
331-
if url_port is None and parsed_url.netloc and ":" in parsed_url.netloc:
332-
return False
333330
url_path = parsed_url.path or "/"
334331
url_query = parsed_url.query
335332
url_fragment = parsed_url.fragment

tests/unit/checks/test_urls.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,26 @@ async def test_urls_guardrail_scheme_matching_with_qualified_allow_list() -> Non
395395
assert result3.tripwire_triggered is True # noqa: S101
396396

397397

398+
def test_is_url_allowed_handles_ipv6_addresses() -> None:
399+
"""IPv6 addresses should be handled correctly (colons are not ports)."""
400+
config = URLConfig(
401+
url_allow_list=["[2001:db8::1]", "ftp://[2001:db8::2]"],
402+
allow_subdomains=False,
403+
allowed_schemes={"https", "ftp"},
404+
)
405+
# IPv6 without scheme
406+
ipv6_no_scheme, _, had_scheme1 = _validate_url_security("[2001:db8::1]", config)
407+
# IPv6 with ftp scheme
408+
ipv6_with_ftp, _, had_scheme2 = _validate_url_security("ftp://[2001:db8::2]", config)
409+
410+
assert ipv6_no_scheme is not None # noqa: S101
411+
assert ipv6_with_ftp is not None # noqa: S101
412+
413+
# Both should be allowed
414+
assert _is_url_allowed(ipv6_no_scheme, config.url_allow_list, config.allow_subdomains, had_scheme1) is True # noqa: S101
415+
assert _is_url_allowed(ipv6_with_ftp, config.url_allow_list, config.allow_subdomains, had_scheme2) is True # noqa: S101
416+
417+
398418
@pytest.mark.asyncio
399419
async def test_urls_guardrail_blocks_subdomains_and_paths_correctly() -> None:
400420
"""Verify subdomains and paths are still blocked according to allow list rules."""

0 commit comments

Comments
 (0)