From fa02d38418a05b6c2875a8a66024b47cc5b906be Mon Sep 17 00:00:00 2001 From: Varshith Puli Date: Mon, 9 Nov 2026 04:48:04 +0000 Subject: [PATCH 1/5] fix(analyzer): ignore regex .exec() when detecting shell execution (#1071) The shell-execution check treated every member call named exec as a shell surface, so a const regex binding or a new RegExp value calling .exec() left template literals unowned and scored the file AE1 HIGH. _ordinary_native_calls already owned the literal /.../.exec() form; extend the same proof to const-bound regex identifiers and new RegExp(...) receivers, keeping child_process, unknown receivers, let/var bindings, reassignment, shadowing, and template arguments fail-closed. Direction 2 of #1071 (bundle-local imports) is deliberately untouched. Signed-off-by: Varshith Puli --- src/skillspector/javascript_native_calls.py | 38 +++++++- .../test_javascript_template_ownership.py | 91 +++++++++++++++++++ 2 files changed, 126 insertions(+), 3 deletions(-) diff --git a/src/skillspector/javascript_native_calls.py b/src/skillspector/javascript_native_calls.py index c0b0c7910..892cff94d 100644 --- a/src/skillspector/javascript_native_calls.py +++ b/src/skillspector/javascript_native_calls.py @@ -68,6 +68,30 @@ ) +def _regex_receiver_call(program: JavaScriptProgram, value: Node) -> Node | None: + """Return the .exec/.test member for a direct regex receiver, else None. + + The call must take no template-string argument: a template argument + keeps the conservative reading, same as for a literal receiver. + """ + member = value.parent + call = None if member is None else member.parent + arguments = None if call is None else call.child_by_field_name("arguments") + if ( + member is not None + and member.type == "member_expression" + and member.child_by_field_name("object") == value + and _callable_member(program, member) in {"exec", "test"} + and call is not None + and call.type == "call_expression" + and call.child_by_field_name("function") == member + and arguments is not None + and arguments.type != "template_string" + ): + return member + return None + + def _local_regex_arguments( program: JavaScriptProgram, references: dict[str, list[Node]], @@ -453,7 +477,6 @@ def _ordinary_native_calls( local_regex_calls: list[Node] = [] regex_arguments: list[Node] = [] regex_constructors: list[Node] = [] - constructed_bindings: set[int] = set() override_names = { "RegExp", "Reflect", @@ -573,6 +596,16 @@ def _ordinary_native_calls( and constructor.type == "identifier" and program.text(constructor) == "RegExp" ): + # A direct .exec/.test call on a constructed regex is owned + # like the literal form, and is not an escaping constructor. + # It rides local_regex_calls, not regex_calls: the RegExp + # constructor name itself sets intrinsic_override, which + # clears regex_calls, so a direct append would be wiped. + member = _regex_receiver_call(program, node) + if member is not None: + if _callable_member(program, member) == "exec": + local_regex_calls.append(member) + continue owner = node.parent binding = None if owner is None else owner.child_by_field_name("name") declaration = None if owner is None else owner.parent @@ -589,7 +622,6 @@ def _ordinary_native_calls( and declaration.parent.type in {"program", "statement_block"} ): regex_bindings[program.text(binding)] = binding - constructed_bindings.add(binding.id) else: regex_constructors.append(node) if node.type == "regex": @@ -740,7 +772,7 @@ def _ordinary_native_calls( intrinsic_override = True regexp_prototype_unproven = True break - if method == "exec" and binding.id not in constructed_bindings: + if method == "exec": named_calls.append(member) else: local_regex_calls.extend(named_calls) diff --git a/tests/nodes/analyzers/test_javascript_template_ownership.py b/tests/nodes/analyzers/test_javascript_template_ownership.py index 16fc25412..9eda3d540 100644 --- a/tests/nodes/analyzers/test_javascript_template_ownership.py +++ b/tests/nodes/analyzers/test_javascript_template_ownership.py @@ -167,6 +167,39 @@ def test_parameter_default_values_do_not_keep_template_files_partial(source: str ) +@pytest.mark.parametrize( + "source", + [ + pytest.param( + "export function formatDate(text) {\n" + " const m = /^(\\d{4})-(\\d{2})-(\\d{2})$/.exec(text);\n" + " return m ? `${m[1]}/${m[2]}/${m[3]}` : null;\n" + "}\n", + id="regex-literal-exec", + ), + pytest.param( + "const DAY = /^(\\d{4})$/;\nconst m = DAY.exec(t);\nreturn m ? `${m[1]}` : null;\n", + id="const-regex-binding-exec", + ), + pytest.param( + 'const r2 = new RegExp("^\\\\d+$");\n' + "const m = r2.exec(t);\n" + "return m ? `${m[1]}` : null;\n", + id="const-new-regexp-binding-exec", + ), + pytest.param( + 'const m = new RegExp("^\\\\d+$").exec(t);\nreturn m ? `${m[1]}` : null;\n', + id="direct-new-regexp-exec", + ), + ], +) +def test_regex_exec_calls_do_not_keep_template_files_partial(source: str) -> None: + assert ( + tm.has_bounded_parse_exhaustion(source + _PADDING, lambda: None, file_type="javascript") + is False + ) + + @pytest.mark.parametrize( "source", [ @@ -199,6 +232,47 @@ def test_uncertain_parameter_calls_keep_template_files_partial(source: str) -> N ) +@pytest.mark.parametrize( + "source", + [ + pytest.param( + 'const cp = require("child_process");\ncp.exec(`${tool} -rf /`);', + id="child-process-alias-exec", + ), + pytest.param( + "const m = foo.exec(t);\nreturn m ? `${m[1]}` : null;\n", + id="unknown-receiver-exec", + ), + pytest.param( + "let re = /^\\d+$/;\nconst m = re.exec(t);\nreturn m ? `${m[1]}` : null;\n", + id="let-bound-regex-exec", + ), + pytest.param( + "const re = /^\\d+$/;\nre = other;\nconst m = re.exec(t);\n" + "return m ? `${m[1]}` : null;\n", + id="reassigned-regex-binding-exec", + ), + pytest.param( + "function RegExp() { return { exec: shellExec }; }\n" + "const r2 = new RegExp();\n" + "const m = r2.exec(t);\n" + "return m ? `${m[1]}` : null;\n", + id="shadowed-regexp-constructor-exec", + ), + pytest.param( + "import { formatMessage } from './format.mjs';\n" + "console.log(formatMessage(`Hello, ${name}`));", + id="imported-helper-template-argument", + ), + ], +) +def test_unproven_exec_receivers_keep_template_files_partial(source: str) -> None: + assert ( + tm.has_bounded_parse_exhaustion(source + _PADDING, lambda: None, file_type="javascript") + is True + ) + + def test_ledger_completes_parameter_default_value_template() -> None: path = "scripts/check.mjs" source = ( @@ -216,6 +290,23 @@ def test_ledger_completes_parameter_default_value_template() -> None: assert "reason_code" not in row +def test_ledger_completes_regex_exec_template() -> None: + path = "scripts/format-date.js" + source = ( + "export function formatDate(text) {\n" + " const m = /^(\\d{4})-(\\d{2})-(\\d{2})$/.exec(text);\n" + " return m ? `${m[1]}/${m[2]}/${m[3]}` : null;\n" + "}\n" + "console.log(formatDate(process.argv[2] ?? ''));\n" + _PADDING + ) + result = static_runner.run_static_patterns_with_ledger( + {"components": [path], "file_cache": {path: source}}, [tm] + ) + row = result["inspection_ledger"][0] + assert row["outcome"] is LedgerOutcome.COMPLETED + assert "reason_code" not in row + + @pytest.mark.parametrize( "source", ["const value = `unterminated;", "const = `malformed ${value}`;"] ) From 0435b6893e698f7645ccf09858e67f34ab845338 Mon Sep 17 00:00:00 2001 From: Varshith Puli Date: Mon, 9 Nov 2026 19:23:39 +0000 Subject: [PATCH 2/5] fix(analyzer): treat shorthand RegExp bindings as overrides in JS ownership (#1072) A catch or for-of destructured RegExp binding is a shorthand_property_identifier_pattern node, which the override check never examined, so a caught arbitrary constructor could borrow the global RegExp proof and own its .exec member. Include shorthand binding nodes in override detection and collect catch_clause parameters as uncertain bindings. Add negative regression controls for destructured catch, for-of, and parameter shadowing, retaining the real global-constructor positive cases. Signed-off-by: Varshith Puli --- src/skillspector/javascript_native_calls.py | 13 +++++++++++++ src/skillspector/javascript_tokens.py | 6 ++++++ .../analyzers/test_javascript_template_ownership.py | 12 ++++++++++++ 3 files changed, 31 insertions(+) diff --git a/src/skillspector/javascript_native_calls.py b/src/skillspector/javascript_native_calls.py index 892cff94d..ed0ac6131 100644 --- a/src/skillspector/javascript_native_calls.py +++ b/src/skillspector/javascript_native_calls.py @@ -574,6 +574,19 @@ def _ordinary_native_calls( ): intrinsic_override = True regexp_prototype_unproven = True + if node.type in { + "shorthand_property_identifier", + "shorthand_property_identifier_pattern", + }: + # Shorthand bindings bind the name too: catch ({RegExp}) and + # for (const {RegExp} of ...) bind through + # shorthand_property_identifier_pattern, not identifier. Only the + # constructor-position identifier itself is exempted, and that + # exemption only matches identifier nodes, so a shorthand node + # spelling an override name always poisons. + if program.text(node).strip("\"'") in override_names: + intrinsic_override = True + regexp_prototype_unproven = True if node.type in { "assignment_expression", "augmented_assignment_expression", diff --git a/src/skillspector/javascript_tokens.py b/src/skillspector/javascript_tokens.py index c22abc27a..a67a165f0 100644 --- a/src/skillspector/javascript_tokens.py +++ b/src/skillspector/javascript_tokens.py @@ -2809,6 +2809,12 @@ def _has_shell_execution_call( value = node.child_by_field_name("right") elif node.type == "formal_parameters": target = node + elif node.type == "catch_clause": + # A catch parameter binds like a formal parameter, including + # destructured forms such as catch ({RegExp}). Without this, a + # caught arbitrary constructor could borrow the global RegExp + # proof in _ordinary_native_calls. + target = node.child_by_field_name("parameter") elif node.type == "arrow_function": target = node.child_by_field_name("parameter") elif node.type == "import_statement": diff --git a/tests/nodes/analyzers/test_javascript_template_ownership.py b/tests/nodes/analyzers/test_javascript_template_ownership.py index 9eda3d540..4eeaedc6e 100644 --- a/tests/nodes/analyzers/test_javascript_template_ownership.py +++ b/tests/nodes/analyzers/test_javascript_template_ownership.py @@ -259,6 +259,18 @@ def test_uncertain_parameter_calls_keep_template_files_partial(source: str) -> N "return m ? `${m[1]}` : null;\n", id="shadowed-regexp-constructor-exec", ), + pytest.param( + "try { step(); } catch ({RegExp}) { const re = new RegExp(); re.exec(`${command}`); }\n", + id="catch-destructured-regexp-exec", + ), + pytest.param( + "for (const {RegExp} of sources) { const re = new RegExp(); re.exec(`${command}`); }\n", + id="for-of-destructured-regexp-exec", + ), + pytest.param( + "function run({RegExp}) { const re = new RegExp(); re.exec(`${command}`); }\n", + id="parameter-destructured-regexp-exec", + ), pytest.param( "import { formatMessage } from './format.mjs';\n" "console.log(formatMessage(`Hello, ${name}`));", From 119520017a7c1d5853bf7d96c0e2f153b93c289f Mon Sep 17 00:00:00 2001 From: Varshith Puli Date: Tue, 10 Nov 2026 07:17:14 +0000 Subject: [PATCH 3/5] fix(analyzer): scope regex ownership proof and decode escaped RegExp bindings (#1072) Constrain the const-regex proof to references inside the binding scope and after its declaration, so an outer or sibling receiver with the same name cannot borrow it. Decode JS escape sequences in shorthand binding names before comparing against override names and recording uncertain bindings, and include shorthand_property_identifier_pattern in the backslash fail-closed guard, so an escaped RegExp binding cannot grant the constructor exemption. Signed-off-by: Varshith Puli --- src/skillspector/javascript_callables.py | 33 ++++++++++++++++++- src/skillspector/javascript_native_calls.py | 27 +++++++++++---- src/skillspector/javascript_tokens.py | 9 ++++- .../test_javascript_template_ownership.py | 21 ++++++++++++ 4 files changed, 82 insertions(+), 8 deletions(-) diff --git a/src/skillspector/javascript_callables.py b/src/skillspector/javascript_callables.py index 3f57d0aab..2ac30f77f 100644 --- a/src/skillspector/javascript_callables.py +++ b/src/skillspector/javascript_callables.py @@ -5,6 +5,7 @@ from __future__ import annotations +import re from collections.abc import Callable from html.parser import HTMLParser @@ -15,6 +16,33 @@ _MAX_BINDING_VISITS = 400_000 +_IDENTIFIER_ESCAPE_PATTERN = re.compile( + r"\\(?:u\{([0-9a-fA-F]+)\}|u([0-9a-fA-F]{4})|x([0-9a-fA-F]{2}))" +) + + +def _decoded_identifier_text(program: JavaScriptProgram, node: Node) -> str: + """Return the identifier spelling with JS escape sequences resolved. + + ``\\u0052egExp``, ``\\u{52}egExp`` and ``\\x52egExp`` all bind the name + RegExp, so raw source spelling cannot be compared against override + names or recorded bindings. An invalid escape is left as-is, which + keeps the proof fail-closed. + """ + spelling = program.text(node) + if "\\" not in spelling: + return spelling + + def _replace(match: re.Match[str]) -> str: + code_point = next(group for group in match.groups() if group is not None) + try: + return chr(int(code_point, 16)) + except (ValueError, OverflowError): + return match.group(0) + + return _IDENTIFIER_ESCAPE_PATTERN.sub(_replace, spelling) + + _SHELL_EXECUTION_CALLS = frozenset( { "exec", @@ -62,7 +90,10 @@ def _pattern_binding_names( check_runtime() node = pending.pop() if node.type in {"identifier", "shorthand_property_identifier_pattern"}: - names.add(program.text(node)) + # Decode escape sequences: {\u0052egExp} binds RegExp, and + # the plain constructor name must match the recorded binding + # for the uncertain-binding check to see the shadow. + names.add(_decoded_identifier_text(program, node)) elif node.type in {"assignment_pattern", "object_assignment_pattern"}: left = node.child_by_field_name("left") if left is not None: diff --git a/src/skillspector/javascript_native_calls.py b/src/skillspector/javascript_native_calls.py index ed0ac6131..2b6dad0e7 100644 --- a/src/skillspector/javascript_native_calls.py +++ b/src/skillspector/javascript_native_calls.py @@ -11,7 +11,7 @@ from tree_sitter import Node from skillspector.javascript_ast import JavaScriptProgram -from skillspector.javascript_callables import _callable_member +from skillspector.javascript_callables import _callable_member, _decoded_identifier_text _PATH_FUNCTIONS = frozenset( { @@ -513,7 +513,9 @@ def _ordinary_native_calls( }: references.setdefault(program.text(node), []).append(node) if node.type in {"identifier", "property_identifier", "string"}: - spelling = program.text(node).strip("\"'") + # The spelling is decoded first: {\u0052egExp} binds RegExp, + # and raw source spelling would miss the override name. + spelling = _decoded_identifier_text(program, node).strip("\"'") if spelling in {"Proxy", "__defineGetter__", "__defineSetter__"}: process_options_unproven = True if spelling in { @@ -584,7 +586,7 @@ def _ordinary_native_calls( # constructor-position identifier itself is exempted, and that # exemption only matches identifier nodes, so a shorthand node # spelling an override name always poisons. - if program.text(node).strip("\"'") in override_names: + if _decoded_identifier_text(program, node).strip("\"'") in override_names: intrinsic_override = True regexp_prototype_unproven = True if node.type in { @@ -745,17 +747,30 @@ def _ordinary_native_calls( module_unproven = True pending.extend(specifier.named_children) for name, binding in regex_bindings.items(): + # The proof belongs to one binding, so only references that can + # resolve to it count. A const inside a function body must not + # prove an outer or sibling receiver with the same name: the + # final `re` in `function local() { const re = /x/; re.test(t); } + # re.exec(...)` is outside that binding's scope, so it is skipped + # here and stays unowned. References before the declaration are + # refused too, matching the scalar proof's declaration-end rule. named_calls: list[Node] = [] declarator = binding.parent declaration = None if declarator is None else declarator.parent scope = None if declaration is None else declaration.parent for reference in references[name]: check_runtime() - if scope is None or not scope.start_byte <= reference.start_byte < scope.end_byte: - regexp_prototype_unproven = True - break if reference == binding: continue + if ( + scope is None + or declarator is None + or not ( + scope.start_byte <= reference.start_byte < scope.end_byte + and reference.start_byte > declarator.end_byte + ) + ): + continue member = reference.parent call = None if member is None else member.parent method = None if member is None else _callable_member(program, member) diff --git a/src/skillspector/javascript_tokens.py b/src/skillspector/javascript_tokens.py index a67a165f0..d31f5c5a4 100644 --- a/src/skillspector/javascript_tokens.py +++ b/src/skillspector/javascript_tokens.py @@ -2901,7 +2901,11 @@ def _has_shell_execution_call( pending_names.append(alias) for node in program.nodes: check_runtime() - if node.type in {"identifier", "shorthand_property_identifier"} or ( + if node.type in { + "identifier", + "shorthand_property_identifier", + "shorthand_property_identifier_pattern", + } or ( node.type == "property_identifier" and node.parent is not None and node.parent.type == "member_expression" @@ -2918,6 +2922,9 @@ def _has_shell_execution_call( # separate nodes, including any later invocation or escape. continue # Escaped names and extracted callable aliases are not resolved here. + # The pattern form is included too: catch ({\u0052egExp}) binds + # an escaped name, and it must fail closed like any other + # escaped identifier. if ( "\\" in spelling or spelling == "Reflect" diff --git a/tests/nodes/analyzers/test_javascript_template_ownership.py b/tests/nodes/analyzers/test_javascript_template_ownership.py index 4eeaedc6e..9b14c30e6 100644 --- a/tests/nodes/analyzers/test_javascript_template_ownership.py +++ b/tests/nodes/analyzers/test_javascript_template_ownership.py @@ -271,6 +271,27 @@ def test_uncertain_parameter_calls_keep_template_files_partial(source: str) -> N "function run({RegExp}) { const re = new RegExp(); re.exec(`${command}`); }\n", id="parameter-destructured-regexp-exec", ), + pytest.param( + "function local() { const re = /x/; re.test(text); }\nre.exec(`${command}`);\n", + id="out-of-scope-regex-receiver-exec", + ), + pytest.param( + "function a() { const re = /x/; re.test(t); }\n" + "function b() { re.exec(`${command}`); }\n", + id="sibling-function-regex-receiver-exec", + ), + pytest.param( + "function f({\\u0052egExp}) { const re = new RegExp(); re.exec(`${command}`); }\n", + id="escaped-shorthand-parameter-regexp-exec", + ), + pytest.param( + "try { step(); } catch ({\\u0052egExp}) { const re = new RegExp(); re.exec(`${command}`); }\n", + id="escaped-shorthand-catch-regexp-exec", + ), + pytest.param( + "for (const {\\u0052egExp} of sources) { const re = new RegExp(); re.exec(`${command}`); }\n", + id="escaped-shorthand-for-of-regexp-exec", + ), pytest.param( "import { formatMessage } from './format.mjs';\n" "console.log(formatMessage(`Hello, ${name}`));", From 9f64876dd9449ce0c63275d21e5ac58bb58c5972 Mon Sep 17 00:00:00 2001 From: Varshith Puli Date: Tue, 10 Nov 2026 16:20:43 +0000 Subject: [PATCH 4/5] fix(analyzer): validate pre-declaration regex references and every same-named binding (#1072) In-scope references before a const regex declaration were skipped, so a closure that overwrites re.exec before the declaration never invalidated the regex proof and the later .exec kept native ownership. Validate those references like any other in-scope use, matching the old loop: a mutation or escape refuses the proof. A later same-named regex binding also overwrote the earlier one in regex_bindings, so an escape of the first binding went unchecked while the second .exec stayed trusted. Keep every binding and validate each against its own scope. Add a closure regression case and a duplicate-binding regression case; both fail before the fix and pass after it. Signed-off-by: Varshith Puli --- src/skillspector/javascript_native_calls.py | 114 +++++++++--------- .../test_javascript_template_ownership.py | 14 +++ 2 files changed, 72 insertions(+), 56 deletions(-) diff --git a/src/skillspector/javascript_native_calls.py b/src/skillspector/javascript_native_calls.py index 2b6dad0e7..32b19907b 100644 --- a/src/skillspector/javascript_native_calls.py +++ b/src/skillspector/javascript_native_calls.py @@ -467,7 +467,7 @@ def _ordinary_native_calls( references: dict[str, list[Node]] = {} bindings: dict[str, tuple[Node, str, str, bool]] = {} regex_calls: list[Node] = [] - regex_bindings: dict[str, Node] = {} + regex_bindings: dict[str, list[Node]] = {} intrinsic_override = False module_unproven = False imports: dict[str, int] = {} @@ -636,7 +636,7 @@ def _ordinary_native_calls( and declaration.parent is not None and declaration.parent.type in {"program", "statement_block"} ): - regex_bindings[program.text(binding)] = binding + regex_bindings.setdefault(program.text(binding), []).append(binding) else: regex_constructors.append(node) if node.type == "regex": @@ -668,7 +668,7 @@ def _ordinary_native_calls( and declaration.parent is not None and declaration.parent.type in {"program", "statement_block"} ): - regex_bindings[program.text(binding)] = binding + regex_bindings.setdefault(program.text(binding), []).append(binding) else: intrinsic_override = True regexp_prototype_unproven = True @@ -746,64 +746,66 @@ def _ordinary_native_calls( else: module_unproven = True pending.extend(specifier.named_children) - for name, binding in regex_bindings.items(): + for name, binding_nodes in regex_bindings.items(): # The proof belongs to one binding, so only references that can # resolve to it count. A const inside a function body must not # prove an outer or sibling receiver with the same name: the # final `re` in `function local() { const re = /x/; re.test(t); } # re.exec(...)` is outside that binding's scope, so it is skipped - # here and stays unowned. References before the declaration are - # refused too, matching the scalar proof's declaration-end rule. - named_calls: list[Node] = [] - declarator = binding.parent - declaration = None if declarator is None else declarator.parent - scope = None if declaration is None else declaration.parent - for reference in references[name]: - check_runtime() - if reference == binding: - continue - if ( - scope is None - or declarator is None - or not ( - scope.start_byte <= reference.start_byte < scope.end_byte - and reference.start_byte > declarator.end_byte - ) - ): - continue - member = reference.parent - call = None if member is None else member.parent - method = None if member is None else _callable_member(program, member) - if ( - member is not None - and member.type == "member_expression" - and member.child_by_field_name("object") == reference - and method == "lastIndex" - and call is not None - and call.type == "assignment_expression" - and call.child_by_field_name("left") == member - and (value := call.child_by_field_name("right")) is not None - and value.type == "number" - ): - continue - if not ( - member is not None - and member.type == "member_expression" - and member.child_by_field_name("object") == reference - and method in {"test", "exec"} - and call is not None - and call.type == "call_expression" - and call.child_by_field_name("function") == member - and (arguments := call.child_by_field_name("arguments")) is not None - and arguments.type != "template_string" - ): - intrinsic_override = True - regexp_prototype_unproven = True - break - if method == "exec": - named_calls.append(member) - else: - local_regex_calls.extend(named_calls) + # here and stays unowned. Every same-named binding is validated: + # a later binding must not overwrite an earlier one, or an escape + # of the earlier binding would go unchecked. References before the + # declaration are validated too, matching the old loop: a closure + # defined before the declaration can run after initialization, so + # a mutation or escape there refuses the proof like any other use. + for binding in binding_nodes: + named_calls: list[Node] = [] + declarator = binding.parent + declaration = None if declarator is None else declarator.parent + scope = None if declaration is None else declaration.parent + for reference in references[name]: + check_runtime() + if reference == binding: + continue + if ( + scope is None + or declarator is None + or not (scope.start_byte <= reference.start_byte < scope.end_byte) + ): + continue + member = reference.parent + call = None if member is None else member.parent + method = None if member is None else _callable_member(program, member) + if ( + member is not None + and member.type == "member_expression" + and member.child_by_field_name("object") == reference + and method == "lastIndex" + and call is not None + and call.type == "assignment_expression" + and call.child_by_field_name("left") == member + and (value := call.child_by_field_name("right")) is not None + and value.type == "number" + ): + continue + if not ( + member is not None + and member.type == "member_expression" + and member.child_by_field_name("object") == reference + and method in {"test", "exec"} + and call is not None + and call.type == "call_expression" + and call.child_by_field_name("function") == member + and (arguments := call.child_by_field_name("arguments")) is not None + and arguments.type != "template_string" + ): + intrinsic_override = True + regexp_prototype_unproven = True + break + if method == "exec": + named_calls.append(member) + else: + local_regex_calls.extend(named_calls) for reference in references.get("Object", ()): parent = reference.parent if ( diff --git a/tests/nodes/analyzers/test_javascript_template_ownership.py b/tests/nodes/analyzers/test_javascript_template_ownership.py index 9b14c30e6..210555852 100644 --- a/tests/nodes/analyzers/test_javascript_template_ownership.py +++ b/tests/nodes/analyzers/test_javascript_template_ownership.py @@ -275,6 +275,20 @@ def test_uncertain_parameter_calls_keep_template_files_partial(source: str) -> N "function local() { const re = /x/; re.test(text); }\nre.exec(`${command}`);\n", id="out-of-scope-regex-receiver-exec", ), + pytest.param( + "function check(handler) {\n" + " function patch() { re['exec'] = handler; }\n" + " const re = /x/;\n" + " patch();\n" + " re.exec(`${command}`);\n" + "}\n", + id="closure-mutated-regex-before-declaration-exec", + ), + pytest.param( + "{ const re = /x/; mutate(re, handler); }\n" + "{ const re = /y/; re.exec(`${command}`); }\n", + id="duplicate-regex-binding-escape-exec", + ), pytest.param( "function a() { const re = /x/; re.test(t); }\n" "function b() { re.exec(`${command}`); }\n", From bc07e742f72804fbe868f9e9f4adb12c9d2104b0 Mon Sep 17 00:00:00 2001 From: Varshith Puli Date: Thu, 19 Nov 2026 16:44:31 +0000 Subject: [PATCH 5/5] fix(analyzer): index regex references by scope in JS ownership proof (#1072) The binding proof visited every reference to a name for every same-named binding, then discarded the ones outside the binding scope. Thousands of sibling blocks each defining `const re` therefore cost bindings times references inside the shared static-analysis deadline. references[name] is in document order (program.nodes is a pre-order walk), so each binding now slices only its own scope range with bisect. The slice visits exactly the references the old range check kept, so pre-declaration closures and every same-named binding are still validated the same way. Add a deterministic work-count regression with 200 sibling scopes: the quadratic scan needed about 102,000 check_runtime calls, the scope slice about 22,000, and the file still stays partial because the trailing `re` is outside every binding scope. Signed-off-by: Varshith Puli --- src/skillspector/javascript_native_calls.py | 22 ++++++++++++------ .../test_javascript_template_ownership.py | 23 +++++++++++++++++++ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/skillspector/javascript_native_calls.py b/src/skillspector/javascript_native_calls.py index 32b19907b..da9f848d8 100644 --- a/src/skillspector/javascript_native_calls.py +++ b/src/skillspector/javascript_native_calls.py @@ -6,6 +6,7 @@ from __future__ import annotations import re +from bisect import bisect_left from collections.abc import Callable from tree_sitter import Node @@ -758,21 +759,28 @@ def _ordinary_native_calls( # declaration are validated too, matching the old loop: a closure # defined before the declaration can run after initialization, so # a mutation or escape there refuses the proof like any other use. + # references[name] is in document order (program.nodes is a + # pre-order walk), so each binding slices only its own scope range + # instead of scanning every same-named reference in the file: with + # thousands of sibling blocks each defining `const re`, the old scan + # cost bindings times references. The slice visits exactly the + # references the old range check kept, so pre-declaration closures + # and every same-named binding are still validated the same way. + name_references = references[name] + reference_starts = [node.start_byte for node in name_references] for binding in binding_nodes: named_calls: list[Node] = [] declarator = binding.parent declaration = None if declarator is None else declarator.parent scope = None if declaration is None else declaration.parent - for reference in references[name]: + if scope is None or declarator is None: + continue + first = bisect_left(reference_starts, scope.start_byte) + end = bisect_left(reference_starts, scope.end_byte) + for reference in name_references[first:end]: check_runtime() if reference == binding: continue - if ( - scope is None - or declarator is None - or not (scope.start_byte <= reference.start_byte < scope.end_byte) - ): - continue member = reference.parent call = None if member is None else member.parent method = None if member is None else _callable_member(program, member) diff --git a/tests/nodes/analyzers/test_javascript_template_ownership.py b/tests/nodes/analyzers/test_javascript_template_ownership.py index 210555852..bfeda5651 100644 --- a/tests/nodes/analyzers/test_javascript_template_ownership.py +++ b/tests/nodes/analyzers/test_javascript_template_ownership.py @@ -320,6 +320,29 @@ def test_unproven_exec_receivers_keep_template_files_partial(source: str) -> Non ) +def test_many_sibling_regex_scopes_keep_proof_work_linear() -> None: + # Each block defines and uses its own `const re`, so the binding proof + # must visit only that block's references. Scanning every same-named + # reference for every binding cost bindings times references here. + blocks = "".join("{ const re = /x/; re.test(t); }\n" for _ in range(200)) + source = blocks + "re.exec(`${command}`);\n" + calls = [0] + + def counting_check_runtime() -> None: + calls[0] += 1 + + assert ( + tm.has_bounded_parse_exhaustion(source, counting_check_runtime, file_type="javascript") + is True + ) + # Deterministic work count, not timing: the quadratic scan needed about + # 102,000 check_runtime calls for these 200 scopes, the scope slice + # needs about 22,000. The bound sits between the two with room on + # both sides, and the file still stays partial because the trailing + # `re` is outside every binding scope. + assert calls[0] < 40000 + + def test_ledger_completes_parameter_default_value_template() -> None: path = "scripts/check.mjs" source = (