Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rfc cases in the domain validator #367

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/validators/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,21 @@ def domain(
return False

try:
return not re.search(r"\s", value) and re.match(

service_record = r"_" if rfc_2782 else ""
trailing_dot = r"\.?$" if rfc_1034 else r"$"

return not re.search(r"\s|__+", value) and re.match(
yozachar marked this conversation as resolved.
Show resolved Hide resolved
# First character of the domain
rf"^(?:[a-z0-9{r'_?'if rfc_2782 else ''}]"
rf"^(?:[a-z0-9{service_record}]"
# Sub-domain
+ rf"(?:[a-z0-9-{r'_?'if rfc_2782 else ''}]{{0,61}}"
+ rf"(?:[a-z0-9-{service_record}]{{0,61}}"
# Hostname
+ rf"[a-z0-9{r'_?'if rfc_2782 else ''}])?\.)"
+ rf"[a-z0-9{service_record}])?\.)"
# First 61 characters of the gTLD
+ r"+[a-z0-9][a-z0-9-_]{0,61}"
# Last character of the gTLD
+ rf"[a-z]{r'.?$' if rfc_1034 else r'$'}",
+ rf"[a-z]{trailing_dot}",
value.encode("idna").decode("utf-8"),
re.IGNORECASE,
)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
("3.cn.", True, False),
("_example.com", False, True),
("example_.com", False, True),
("_exa_mple_.com", False, True),
("a.cn", False, False),
("sub1.sub2.sample.co.uk", False, False),
("somerandomexample.xn--fiqs8s", False, False),
Expand Down Expand Up @@ -67,6 +68,9 @@ def test_returns_true_on_valid_top_level_domain(
("_example._com", False, False),
("example_.com", False, False),
("example", False, False),
("example.com!", True, False),
("example?.com", True, False),
("__exa__mple__.com", False, True),
("a......b.com", False, False),
("a.123", False, False),
("123.123", False, False),
Expand Down