Add MAX_HOSTNAME_LENGTH and MAX_LABEL_LENGTH constants

This commit is contained in:
2026-02-08 14:48:02 +01:00
parent 34e31297f8
commit 6af6176dec
+10 -5
View File
@@ -15,6 +15,10 @@ import dns.tsigkeyring
import dns.update import dns.update
# DNS name length limits (RFC 1035)
MAX_HOSTNAME_LENGTH = 253
MAX_LABEL_LENGTH = 63
# Valid hostname label pattern (after punycode encoding) # Valid hostname label pattern (after punycode encoding)
LABEL_PATTERN = re.compile( LABEL_PATTERN = re.compile(
r'^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$', re.IGNORECASE r'^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$', re.IGNORECASE
@@ -54,8 +58,9 @@ def encode_dnsname(hostname):
if hostname.endswith('.'): if hostname.endswith('.'):
hostname = hostname[:-1] hostname = hostname[:-1]
if len(hostname) > 253: if len(hostname) > MAX_HOSTNAME_LENGTH:
raise EncodingError("Hostname too long (max 253 characters)") raise EncodingError(
f"Hostname too long (max {MAX_HOSTNAME_LENGTH} characters)")
try: try:
# Encode each label using IDNA # Encode each label using IDNA
@@ -72,9 +77,9 @@ def encode_dnsname(hostname):
except UnicodeError as e: except UnicodeError as e:
raise EncodingError(f"Invalid label '{label}': {e}") raise EncodingError(f"Invalid label '{label}': {e}")
if len(encoded) > 63: if len(encoded) > MAX_LABEL_LENGTH:
raise EncodingError( raise EncodingError(
f"Label '{label}' too long (max 63 characters)" f"Label '{label}' too long (max {MAX_LABEL_LENGTH} characters)"
) )
if not LABEL_PATTERN.match(encoded): if not LABEL_PATTERN.match(encoded):
@@ -209,7 +214,7 @@ def parse_bind_key_file(path):
except DNSError: except DNSError:
raise raise
except Exception as e: except Exception as e:
raise DNSError(f"Failed to parse key file {path}: {e}") raise DNSError(f"Failed to parse key file {path}: {e}") from e
class DNSService: class DNSService: