Skip to content

Commit 5bad2b6

Browse files
committed
fix: return False instead of raising on a malformed authority
urlparse raises ValueError on "https://[::1", and long_url reaches the validator as a plain str, so the exception escaped as a 500 on the shorten endpoints rather than a clean rejection.
1 parent 68539b0 commit 5bad2b6

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

shared/validators.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,15 @@ def validate_url(
5050
loops. Matching is host-scoped: a destination that merely
5151
mentions the name in its path or query is not a loop.
5252
"""
53+
# urlparse raises on a malformed IPv6 authority ("https://[::1"), and
54+
# long_url reaches here as a plain str, so an unguarded parse escapes as
55+
# a 500 on the shorten endpoints instead of a 400.
56+
try:
57+
parsed = urlparse(url)
58+
except ValueError:
59+
return False
5360
# Scheme allowlist defends against ftp/file/data/etc even if the
5461
# validators package widens its accepted schemes upstream.
55-
parsed = urlparse(url)
5662
if parsed.scheme not in _ALLOWED_URL_SCHEMES:
5763
return False
5864
if not _validators.url(url, skip_ipv4_addr=True, skip_ipv6_addr=True):

tests/unit/shared/test_validators.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
("https://www.spoo.me/abc", False), # subdomain of a blocked host
2525
("not-a-url", False),
2626
("http://192.168.1.1/path", False), # IPv4 skipped by validator
27+
("https://[::1", False), # urlparse raises on this, must not escape
2728
# Host-scoped: a foreign destination that merely mentions the blocked
2829
# name in its path, query or fragment is not a redirect loop. The
2930
# dashboard hit this shortening a PostHog analytics URL filtered on
@@ -44,6 +45,7 @@
4445
"self_ref_subdomain",
4546
"plain_text",
4647
"ipv4",
48+
"malformed_ipv6_authority",
4749
"foreign_host_mentions_in_query",
4850
"foreign_host_mentions_in_path",
4951
"foreign_host_mentions_in_fragment",

0 commit comments

Comments
 (0)