Skip to content

Commit a5425e5

Browse files
Merge pull request #921 from NVIDIA/yashraj/fix-javascript-template-completeness
Fix false parser limits in Node assertion templates
2 parents d3f2e97 + 123ec27 commit a5425e5

7 files changed

Lines changed: 837 additions & 3 deletions

File tree

Lines changed: 381 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,381 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
"""Bounded lexical ownership for Node assertion template-message delimiters.
5+
6+
This is not a JavaScript parser or a shell-sink/dataflow model. Only a direct
7+
diagnostic argument of one unaliased built-in assertion import may borrow
8+
ownership. Unsupported source keeps its original conservative shell reading.
9+
"""
10+
11+
from __future__ import annotations
12+
13+
import re
14+
from collections.abc import Callable
15+
from dataclasses import dataclass
16+
17+
_MAX_SOURCE_CHARS = 1_000_000
18+
_MAX_TOKENS = 100_000
19+
_MAX_NESTING = 128
20+
_LINE_ENDS = "\r\n\u2028\u2029"
21+
_IDENTIFIER = re.compile(r"[A-Za-z_$][A-Za-z0-9_$]*")
22+
_TEMPLATE_VALUE = re.compile(
23+
r"\s*[A-Za-z_$][A-Za-z0-9_$]*(?:\s*\.\s*[A-Za-z_$][A-Za-z0-9_$]*)*\s*\Z"
24+
)
25+
_NODE_SHEBANG = re.compile(r"#![ \t]*(?:\S*/)?(?:env[ \t]+)?node(?:js)?(?=[ \t\r\n]|$)")
26+
_ASSERT_MODULES = frozenset({"node:assert", "node:assert/strict"})
27+
_ASSERT_MODULE_ALIASES = _ASSERT_MODULES | {"assert", "assert/strict"}
28+
_MESSAGE_ARGUMENTS = {
29+
"ok": 2,
30+
"equal": 3,
31+
"notEqual": 3,
32+
"strictEqual": 3,
33+
"notStrictEqual": 3,
34+
"deepEqual": 3,
35+
"notDeepEqual": 3,
36+
"deepStrictEqual": 3,
37+
"notDeepStrictEqual": 3,
38+
"match": 3,
39+
"doesNotMatch": 3,
40+
"throws": 3,
41+
"doesNotThrow": 3,
42+
}
43+
_EXPRESSION_PREFIX_WORDS = frozenset(
44+
{
45+
"return",
46+
"throw",
47+
"case",
48+
"delete",
49+
"void",
50+
"typeof",
51+
"new",
52+
"yield",
53+
"await",
54+
"in",
55+
"instanceof",
56+
"else",
57+
"do",
58+
}
59+
)
60+
_OPERATORS = frozenset(
61+
{
62+
"===",
63+
"!==",
64+
">>>",
65+
"**=",
66+
"&&=",
67+
"||=",
68+
"??=",
69+
"...",
70+
"=>",
71+
"==",
72+
"!=",
73+
"<=",
74+
">=",
75+
"&&",
76+
"||",
77+
"??",
78+
"++",
79+
"--",
80+
"**",
81+
"+=",
82+
"-=",
83+
"*=",
84+
"%=",
85+
"&=",
86+
"|=",
87+
"^=",
88+
"<<",
89+
">>",
90+
"?.",
91+
}
92+
)
93+
94+
95+
@dataclass(frozen=True, slots=True)
96+
class _Token:
97+
kind: str
98+
start: int
99+
end: int
100+
depth: int
101+
interpolation_names: tuple[str, ...] = ()
102+
103+
104+
def _tokens(
105+
content: str, check_runtime: Callable[[], None]
106+
) -> tuple[list[_Token], dict[int, int]] | None:
107+
"""Tokenize the limited lexical forms needed to prove message boundaries."""
108+
tokens: list[_Token] = []
109+
pairs: dict[int, int] = {}
110+
stack: list[tuple[str, int]] = []
111+
cursor = 0
112+
expression_start = True
113+
next_check = 0
114+
115+
def check(position: int) -> None:
116+
nonlocal next_check
117+
if position >= next_check:
118+
check_runtime()
119+
next_check = position + 4096
120+
121+
if content.startswith("#!"):
122+
if _NODE_SHEBANG.match(content) is None:
123+
return None
124+
cursor = content.find("\n")
125+
if cursor < 0:
126+
return None
127+
while cursor < len(content):
128+
check(cursor)
129+
character = content[cursor]
130+
if character.isspace():
131+
cursor += 1
132+
continue
133+
if character == "\x00" or len(tokens) >= _MAX_TOKENS:
134+
return None
135+
if content.startswith("//", cursor):
136+
while cursor < len(content) and content[cursor] not in _LINE_ENDS:
137+
check(cursor)
138+
cursor += 1
139+
continue
140+
if content.startswith("/*", cursor):
141+
cursor += 2
142+
while cursor < len(content) and not content.startswith("*/", cursor):
143+
check(cursor)
144+
cursor += 1
145+
if cursor == len(content):
146+
return None
147+
cursor += 2
148+
continue
149+
start = cursor
150+
kind = "punctuation"
151+
interpolation_names: list[str] = []
152+
if character in "'\"`":
153+
kind = "template" if character == "`" else "string"
154+
cursor += 1
155+
while cursor < len(content):
156+
check(cursor)
157+
current = content[cursor]
158+
if current == "\\":
159+
cursor += 2
160+
if cursor <= len(content) and content[cursor - 1 : cursor + 1] == "\r\n":
161+
cursor += 1
162+
continue
163+
if current == character:
164+
cursor += 1
165+
break
166+
if kind == "string" and current in _LINE_ENDS:
167+
return None
168+
if kind == "template" and content.startswith("${", cursor):
169+
value_start = cursor + 2
170+
cursor = value_start
171+
while cursor < len(content) and content[cursor] != "}":
172+
check(cursor)
173+
# Calls, computed members, comments, nested templates,
174+
# and other effectful expressions never gain ownership.
175+
if not content[cursor].isascii():
176+
return None
177+
cursor += 1
178+
value = content[value_start:cursor]
179+
if cursor == len(content) or _TEMPLATE_VALUE.fullmatch(value) is None:
180+
return None
181+
interpolation_names.extend(_IDENTIFIER.findall(value))
182+
cursor += 1
183+
continue
184+
cursor += 1
185+
else:
186+
return None
187+
expression_start = False
188+
elif character == "/":
189+
# Division versus a statement-level regexp needs more grammar.
190+
# Only a clear expression prefix establishes regexp ownership.
191+
if not expression_start:
192+
return None
193+
kind = "regexp"
194+
cursor += 1
195+
in_class = False
196+
while cursor < len(content):
197+
check(cursor)
198+
current = content[cursor]
199+
if current in _LINE_ENDS:
200+
return None
201+
if current == "\\":
202+
if cursor + 1 == len(content) or content[cursor + 1] in _LINE_ENDS:
203+
return None
204+
cursor += 2
205+
continue
206+
if current == "[":
207+
if in_class:
208+
# Unicode-set nested classes require a different grammar.
209+
return None
210+
in_class = True
211+
elif current == "]":
212+
in_class = False
213+
elif current == "/" and not in_class:
214+
cursor += 1
215+
break
216+
cursor += 1
217+
else:
218+
return None
219+
flags_start = cursor
220+
while cursor < len(content) and content[cursor].isascii() and content[cursor].isalpha():
221+
check(cursor)
222+
cursor += 1
223+
flags = content[flags_start:cursor]
224+
if len(set(flags)) != len(flags) or set(flags) - set("dgimsuy"):
225+
return None
226+
expression_start = False
227+
elif (identifier := _IDENTIFIER.match(content, cursor)) is not None:
228+
kind = "identifier"
229+
cursor = identifier.end()
230+
previous = content[tokens[-1].start : tokens[-1].end] if tokens else ""
231+
expression_start = identifier[0] in _EXPRESSION_PREFIX_WORDS and previous not in {
232+
".",
233+
"?.",
234+
}
235+
elif character.isascii() and character.isdigit():
236+
kind = "number"
237+
cursor += 1
238+
while cursor < len(content) and content[cursor] in "0123456789abcdefABCDEFxXoObBn._":
239+
check(cursor)
240+
cursor += 1
241+
expression_start = False
242+
elif character in "()[]{};,:.?=<>!~+-*%&|^":
243+
width = 1
244+
if content[cursor : cursor + 3] in _OPERATORS:
245+
width = 3
246+
elif content[cursor : cursor + 2] in _OPERATORS:
247+
width = 2
248+
cursor += width
249+
operator = content[start:cursor]
250+
if operator == "!" and not expression_start:
251+
# TypeScript's postfix assertion leaves division possible.
252+
return None
253+
if operator in "([{":
254+
if len(stack) >= _MAX_NESTING:
255+
return None
256+
stack.append((operator, len(tokens)))
257+
elif operator in ")]}":
258+
if not stack or stack[-1][0] != {"}": "{", ")": "(", "]": "["}[operator]:
259+
return None
260+
_, opening = stack.pop()
261+
pairs[opening] = len(tokens)
262+
expression_start = operator not in {")", "]", "}", ".", "?.", "++", "--"}
263+
else:
264+
# Escaped/Unicode identifiers, JSX, and unknown lexical forms are
265+
# not proof of a Node assertion receiver or template boundary.
266+
return None
267+
tokens.append(_Token(kind, start, cursor, len(stack), tuple(interpolation_names)))
268+
check_runtime()
269+
return None if stack else (tokens, pairs)
270+
271+
272+
def assertion_template_shell_text(content: str, check_runtime: Callable[[], None]) -> str:
273+
"""Project only proven Node assertion message delimiters for shell completeness.
274+
275+
Literal bodies and interpolations remain byte-for-byte present. Findings
276+
must still be produced from the original source. Unknown calls, tagged or
277+
concatenated templates, ambiguous imports, and receiver mutation/shadowing
278+
receive no exemption. Runtime interruption propagates to the caller.
279+
280+
This assumes the standard built-in module. It cannot prove the absence of
281+
arbitrary runtime monkeypatching or object getters.
282+
"""
283+
if len(content) > _MAX_SOURCE_CHARS or "node:assert" not in content or "`" not in content:
284+
return content
285+
parsed = _tokens(content, check_runtime)
286+
if parsed is None:
287+
return content
288+
tokens, pairs = parsed
289+
values = [content[token.start : token.end] for token in tokens]
290+
imports: list[tuple[str, int, int]] = []
291+
for index, token in enumerate(tokens):
292+
if index % 256 == 0:
293+
check_runtime()
294+
if token.kind == "identifier" and (
295+
values[index] == "require"
296+
or values[index] == "import"
297+
and values[index + 1 : index + 2] == ["("]
298+
):
299+
return content
300+
if token.kind == "string" and index and values[index - 1] in {"import", "from"}:
301+
# Escaped module names can alias the same mutable built-in object.
302+
if "\\" in values[index]:
303+
return content
304+
if values[index] != "import" or token.depth:
305+
continue
306+
following = values[index : index + 7]
307+
if len(following) >= 5 and following[2] == "from" and following[4] == ";":
308+
binding_index, module_index = index + 1, index + 3
309+
elif (
310+
len(following) == 7
311+
and following[1:3] == ["*", "as"]
312+
and following[4] == "from"
313+
and following[6] == ";"
314+
):
315+
binding_index, module_index = index + 3, index + 5
316+
else:
317+
continue
318+
if (
319+
tokens[binding_index].kind == "identifier"
320+
and tokens[module_index].kind == "string"
321+
and values[module_index][1:-1] in _ASSERT_MODULES
322+
):
323+
imports.append((values[binding_index], binding_index, module_index))
324+
if len(imports) != 1:
325+
return content
326+
binding, binding_index, module_index = imports[0]
327+
if any(
328+
token.kind == "string"
329+
and index != module_index
330+
and values[index][1:-1] in _ASSERT_MODULE_ALIASES
331+
or binding in token.interpolation_names
332+
for index, token in enumerate(tokens)
333+
):
334+
return content
335+
delimiters: list[int] = []
336+
for index, token in enumerate(tokens):
337+
if index % 256 == 0:
338+
check_runtime()
339+
if token.kind != "identifier" or values[index] != binding or index == binding_index:
340+
continue
341+
if (index and values[index - 1] in {".", "?.", "new"}) or values[index + 1 : index + 2] != [
342+
"."
343+
]:
344+
return content
345+
if (
346+
index + 3 >= len(tokens)
347+
or values[index + 2] not in _MESSAGE_ARGUMENTS
348+
or values[index + 3] != "("
349+
):
350+
return content
351+
opening = index + 3
352+
closing = pairs.get(opening)
353+
if closing is None:
354+
return content
355+
arguments: list[tuple[int, int]] = []
356+
begin = cursor = opening + 1
357+
while cursor < closing:
358+
check_runtime()
359+
if cursor in pairs:
360+
cursor = pairs[cursor] + 1
361+
continue
362+
if values[cursor] == ",":
363+
arguments.append((begin, cursor))
364+
begin = cursor + 1
365+
cursor += 1
366+
if begin < closing:
367+
arguments.append((begin, closing))
368+
if len(arguments) != _MESSAGE_ARGUMENTS[values[index + 2]] or any(
369+
start == end or values[start] == "..." for start, end in arguments
370+
):
371+
continue
372+
start, end = arguments[-1]
373+
if end == start + 1 and tokens[start].kind == "template":
374+
delimiters.extend((tokens[start].start, tokens[start].end - 1))
375+
if not delimiters:
376+
return content
377+
output = list(content)
378+
for position in delimiters:
379+
output[position] = " "
380+
check_runtime()
381+
return "".join(output)

‎src/skillspector/nested_artifacts.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,8 @@ def _virtual_type(
575575
".toml": "toml",
576576
".txt": "text",
577577
".js": "javascript",
578+
".mjs": "javascript",
579+
".cjs": "javascript",
578580
".ts": "typescript",
579581
".rb": "ruby",
580582
".go": "go",

0 commit comments

Comments
 (0)