Skip to content

Commit 28ce3ea

Browse files
authored
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 python/cpython#122792.
1 parent 8953c90 commit 28ce3ea

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

readthedocs/projects/forms.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -994,11 +994,11 @@ def clean_project(self):
994994
def clean_domain(self):
995995
"""Validates domain."""
996996
domain = self.cleaned_data["domain"].lower()
997-
parsed = urlparse(domain)
997+
parsed = self._safe_urlparse(domain)
998998

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

10031003
if not parsed.netloc:
10041004
raise forms.ValidationError(f"{domain} is not a valid domain.")
@@ -1084,6 +1084,13 @@ def _get_cname(self, domain):
10841084
_("The domain is not valid."),
10851085
)
10861086

1087+
def _safe_urlparse(self, url):
1088+
"""Wrapper around urlparse to throw ValueError exceptions as ValidationError."""
1089+
try:
1090+
return urlparse(url)
1091+
except ValueError:
1092+
raise forms.ValidationError("Invalid domain")
1093+
10871094
def clean_canonical(self):
10881095
canonical = self.cleaned_data["canonical"]
10891096
pk = self.instance.pk

readthedocs/rtd_tests/tests/test_domains.py

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ def test_invalid_domains(self):
154154
"1.23.45.67",
155155
"127.0.0.1",
156156
"127.0.0.10",
157+
"[1.2.3.4.com",
157158
]
158159
for domain in domains:
159160
form = DomainForm(

0 commit comments

Comments
 (0)