Skip to content

Commit

Permalink
Domains: check for urlparse errors (#11824)
Browse files Browse the repository at this point in the history
urlparse can throw ValueError when parsing a URL, we didn't experience
this before because none of our tests were failing when calling
urlparse, but there was a new release of python that now fails parsing
some of our test cases python/cpython#122792.
  • Loading branch information
stsewd authored Dec 5, 2024
1 parent 8953c90 commit 28ce3ea
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
11 changes: 9 additions & 2 deletions readthedocs/projects/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -994,11 +994,11 @@ def clean_project(self):
def clean_domain(self):
"""Validates domain."""
domain = self.cleaned_data["domain"].lower()
parsed = urlparse(domain)
parsed = self._safe_urlparse(domain)

# Force the scheme to have a valid netloc.
if not parsed.scheme:
parsed = urlparse(f"https://{domain}")
parsed = self._safe_urlparse(f"https://{domain}")

if not parsed.netloc:
raise forms.ValidationError(f"{domain} is not a valid domain.")
Expand Down Expand Up @@ -1084,6 +1084,13 @@ def _get_cname(self, domain):
_("The domain is not valid."),
)

def _safe_urlparse(self, url):
"""Wrapper around urlparse to throw ValueError exceptions as ValidationError."""
try:
return urlparse(url)
except ValueError:
raise forms.ValidationError("Invalid domain")

def clean_canonical(self):
canonical = self.cleaned_data["canonical"]
pk = self.instance.pk
Expand Down
1 change: 1 addition & 0 deletions readthedocs/rtd_tests/tests/test_domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def test_invalid_domains(self):
"1.23.45.67",
"127.0.0.1",
"127.0.0.10",
"[1.2.3.4.com",
]
for domain in domains:
form = DomainForm(
Expand Down

0 comments on commit 28ce3ea

Please sign in to comment.