Skip to content

Commit dcfa36b

Browse files
committed
feat(manager): enforce stricter custom domain regexes
1 parent d9f8abe commit dcfa36b

1 file changed

Lines changed: 14 additions & 9 deletions

File tree

manager/director/apps/sites/forms.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,17 @@ class DomainForm(forms.Form):
143143
required=False,
144144
validators=[
145145
validators.RegexValidator(
146-
regex=r"^(?!(.*\.)?sites\.tjhsst\.edu$)[0-9a-zA-Z_\- .]+$",
146+
regex=r"^.*sites\.tjhsst\.edu$",
147+
inverse_match=True,
147148
message="You can only have one sites.tjhsst.edu domain, the automatically generated"
148149
" one that matches the name of your site.",
149150
),
151+
validators.RegexValidator(
152+
regex=r"^[a-z0-9]+(-[a-z0-9]+)*(\.[a-z0-9]+(-[a-z0-9]+)*)+$",
153+
message="This is an invalid domain. Ensure that you do "
154+
"not have trailing slashes and you do not "
155+
"prepend with http:// or https://.",
156+
),
150157
],
151158
widget=forms.TextInput(attrs={"class": "form-control"}),
152159
)
@@ -164,17 +171,15 @@ def clean(self) -> Dict[str, Any]:
164171
self.add_error("domain", "Only administrators can add tjhsst.edu domains")
165172

166173
# Check if this domain CNAMEs to the proper value
167-
if "domain" in cleaned_data and cleaned_data["domain"] and not self.user_is_superuser:
174+
if cleaned_data.get("domain") and not self.user_is_superuser:
168175
try:
169176
# Attempt to resolve this domain (check for CNAME records)
170-
result = dns.resolver.resolve(
171-
cleaned_data["domain"], "CNAME", raise_on_no_answer=False
172-
)
177+
result = dns.resolver.resolve(cleaned_data["domain"], "CNAME")
173178
# If the result set is None (no CNAMEs) or if there are no records that
174179
# point to the CNAME configured in settings, raise a ValueError (which is caught)
175-
if result.rrset is None or not any(
180+
if not any(
176181
[
177-
settings.DIRECTOR_CUSTOM_DOMAIN_FQDN_CNAME in response.to_text()
182+
settings.DIRECTOR_CUSTOM_DOMAIN_FQDN_CNAME == response.to_text().rstrip(".")
178183
for response in result.rrset
179184
]
180185
):
@@ -188,8 +193,8 @@ def clean(self) -> Dict[str, Any]:
188193
):
189194
self.add_error(
190195
"domain",
191-
"This domain is not CNAMEd properly, or this domain does not exist. "
192-
"For further guidance, please see the documentation.",
196+
"This domain does not have the correct CNAME record configured, "
197+
"or this domain does not exist. For further guidance, see the documentation.",
193198
)
194199

195200
return cleaned_data

0 commit comments

Comments
 (0)