From 28ce3eaed0240b2080c5b66901abd55fe3b2f23f Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Thu, 5 Dec 2024 08:45:20 -0500 Subject: [PATCH] Domains: check for urlparse errors (#11824) 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 https://github.com/python/cpython/issues/122792. --- readthedocs/projects/forms.py | 11 +++++++++-- readthedocs/rtd_tests/tests/test_domains.py | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/readthedocs/projects/forms.py b/readthedocs/projects/forms.py index e37e4ffd73c..e7c79c82e25 100644 --- a/readthedocs/projects/forms.py +++ b/readthedocs/projects/forms.py @@ -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.") @@ -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 diff --git a/readthedocs/rtd_tests/tests/test_domains.py b/readthedocs/rtd_tests/tests/test_domains.py index 9ad254b63b7..89de9f0ec32 100644 --- a/readthedocs/rtd_tests/tests/test_domains.py +++ b/readthedocs/rtd_tests/tests/test_domains.py @@ -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(