From 22399c3f8a15652905d965c88214c52f2214ef4c Mon Sep 17 00:00:00 2001 From: jmestwa-coder Date: Sat, 30 May 2026 07:45:31 +0530 Subject: [PATCH] enforce 253 character limit in domain validator --- src/validators/domain.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/validators/domain.py b/src/validators/domain.py index 8109573..0d27169 100644 --- a/src/validators/domain.py +++ b/src/validators/domain.py @@ -83,6 +83,11 @@ def domain( service_record = r"_" if rfc_2782 else "" trailing_dot = r"\.?$" if rfc_1034 else r"$" + encoded = value.encode("idna").decode("utf-8") + if len(encoded.rstrip(".")) > 253: + # ref: RFC 1035, presentation form is limited to 253 characters + return False + return not re.search(r"\s|__+", value) and re.match( # First character of the domain rf"^(?:[a-z0-9{service_record}]" @@ -94,7 +99,7 @@ def domain( + r"+[a-z0-9][a-z0-9-_]{0,61}" # Last character of the gTLD + rf"[a-z]{trailing_dot}", - value.encode("idna").decode("utf-8"), + encoded, re.IGNORECASE, ) except UnicodeError as err: