Skip to content

Commit 963372f

Browse files
elhoimclaude
andcommitted
fix: avoid nested-quantifier regex on network-fetched input (CodeQL)
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 390a27d commit 963372f

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

tools/generate-parking-domain-ns.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,25 @@
4949

5050
DST = "parking-domain-ns"
5151

52-
# A host name made only of ordinary labels: no SQL wildcard anywhere.
53-
CLEAN_HOST = re.compile(r"^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$")
52+
# Structural validation is done with plain string operations rather than a
53+
# nested-quantifier regex: a pattern like
54+
# ^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$ backtracks
55+
# polynomially, and this runs over JSON fetched from the network.
56+
LABEL_CHARS = frozenset("abcdefghijklmnopqrstuvwxyz0123456789-")
57+
58+
59+
def is_clean_host(token):
60+
"""True for a host name of ordinary labels with no SQL wildcard."""
61+
if not token or len(token) > 253 or token.count(".") < 1:
62+
return False
63+
for label in token.split("."):
64+
if not label or len(label) > 63:
65+
return False
66+
if label[0] == "-" or label[-1] == "-":
67+
return False
68+
if not set(label) <= LABEL_CHARS:
69+
return False
70+
return True
5471

5572

5673
def registrable_domain(host):
@@ -106,7 +123,7 @@ def extract_ns_domains(services):
106123
continue
107124
candidate = value
108125

109-
if not CLEAN_HOST.match(candidate):
126+
if not is_clean_host(candidate):
110127
logging.warning("%s: skipping unparseable NS host: %s", key, indicator)
111128
continue
112129
if candidate.endswith(MULTIPART_SUFFIXES):

0 commit comments

Comments
 (0)