Skip to content

Commit 1c3bce4

Browse files
committed
fix(taint): preserve reflective call semantics
Signed-off-by: Whj9283 <[email protected]>
1 parent cddfb2b commit 1c3bce4

2 files changed

Lines changed: 191 additions & 41 deletions

File tree

‎src/skillspector/nodes/analyzers/behavioral_taint_tracking.py‎

Lines changed: 108 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,16 @@ def clone(self) -> _ReflectiveScope:
338338
class _LocalBindingCollector(ast.NodeVisitor):
339339
"""Collect names local to one function without entering nested scopes."""
340340

341-
def __init__(self) -> None:
341+
def __init__(self, check_runtime: Callable[[], None] | None = None) -> None:
342342
self.names: set[str] = set()
343343
self.global_names: set[str] = set()
344344
self.nonlocal_names: set[str] = set()
345+
self.check_runtime = check_runtime
346+
347+
def visit(self, node: ast.AST) -> object:
348+
if self.check_runtime is not None:
349+
self.check_runtime()
350+
return super().visit(node)
345351

346352
def visit_Name(self, node: ast.Name) -> None:
347353
if isinstance(node.ctx, (ast.Store, ast.Del)):
@@ -426,11 +432,23 @@ def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
426432
class _ReflectiveSinkResolver(ast.NodeVisitor):
427433
"""Resolve reflective sink handles at each call site with lexical scoping."""
428434

429-
def __init__(self) -> None:
435+
def __init__(self, check_runtime: Callable[[], None] | None = None) -> None:
430436
self.scopes = [_ReflectiveScope()]
431437
self.call_sinks: dict[ast.Call, str] = {}
432438
self.active_functions: set[ast.FunctionDef | ast.AsyncFunctionDef] = set()
433439
self.called_functions: set[ast.FunctionDef | ast.AsyncFunctionDef] = set()
440+
self.function_closures: dict[
441+
ast.FunctionDef | ast.AsyncFunctionDef, list[_ReflectiveScope]
442+
] = {}
443+
self.check_runtime = check_runtime
444+
445+
def _check_runtime(self) -> None:
446+
if self.check_runtime is not None:
447+
self.check_runtime()
448+
449+
def visit(self, node: ast.AST) -> object:
450+
self._check_runtime()
451+
return super().visit(node)
434452

435453
@property
436454
def scope(self) -> _ReflectiveScope:
@@ -588,23 +606,49 @@ def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
588606
if node.level == 0 and node.module is not None:
589607
self._set_binding("module", local_name, f"{node.module}.{imported.name}")
590608

591-
def _record_call(self, node: ast.Call) -> None:
609+
def _record_call(self, node: ast.Call) -> ast.FunctionDef | ast.AsyncFunctionDef | None:
610+
"""Record the callee selected before argument evaluation, without invoking it."""
592611
if isinstance(node.func, ast.Name):
593612
sink = self._lookup("callable", node.func.id)
594613
if sink is not None:
595614
self.call_sinks[node] = sink
596-
function = self._lookup_function(node.func.id)
597-
if function is not None and function not in self.active_functions:
598-
self.called_functions.add(function)
599-
self.active_functions.add(function)
600-
snapshot = [scope.clone() for scope in self.scopes]
601-
self._analyze_function(function)
602-
self.scopes = snapshot
603-
self.active_functions.remove(function)
615+
return self._lookup_function(node.func.id)
604616
else:
605617
sink = self._reflective_callable(node.func)
606618
if sink is not None:
607619
self.call_sinks[node] = sink
620+
return None
621+
622+
def _register_function(
623+
self,
624+
node: ast.FunctionDef | ast.AsyncFunctionDef,
625+
*,
626+
closure: list[_ReflectiveScope] | None = None,
627+
) -> None:
628+
self._shadow_names([node.name])
629+
self._binding_scope(node.name).functions[node.name] = node
630+
self.function_closures[node] = (
631+
[scope.clone() for scope in closure]
632+
if closure is not None
633+
else [scope.clone() for scope in self.scopes[1:]]
634+
)
635+
636+
def _invoke_function(self, node: ast.FunctionDef | ast.AsyncFunctionDef) -> None:
637+
if node in self.active_functions:
638+
return
639+
self._check_runtime()
640+
self.called_functions.add(node)
641+
self.active_functions.add(node)
642+
caller_scopes = self.scopes
643+
closure = self.function_closures.get(node, [])
644+
# Globals remain live at the call site; caller locals do not leak into a
645+
# separately-defined function's lexical environment.
646+
self.scopes = [caller_scopes[0].clone(), *[scope.clone() for scope in closure]]
647+
try:
648+
self._analyze_function(node)
649+
finally:
650+
self.scopes = caller_scopes
651+
self.active_functions.remove(node)
608652

609653
def _visit_comprehension(
610654
self, node: ast.ListComp | ast.SetComp | ast.DictComp | ast.GeneratorExp
@@ -629,23 +673,45 @@ def _visit_comprehension(
629673
self.scopes.pop()
630674

631675
def _visit_expression(self, expression: ast.expr) -> None:
632-
pending: list[ast.expr] = [expression]
676+
pending: list[tuple[str, ast.AST]] = [("visit", expression)]
633677
while pending:
634-
node = pending.pop()
678+
self._check_runtime()
679+
action, item = pending.pop()
680+
if action == "bind_named":
681+
assert isinstance(item, ast.NamedExpr)
682+
self._bind([item.target], item.value)
683+
continue
684+
if action == "finish_call_callee":
685+
assert isinstance(item, ast.Call)
686+
function = self._record_call(item)
687+
if function is not None:
688+
pending.append(("invoke", function))
689+
arguments = [*item.args, *(keyword.value for keyword in item.keywords)]
690+
pending.extend(("visit", argument) for argument in reversed(arguments))
691+
continue
692+
if action == "invoke":
693+
assert isinstance(item, (ast.FunctionDef, ast.AsyncFunctionDef))
694+
self._invoke_function(item)
695+
continue
696+
assert action == "visit"
697+
node = item
635698
if isinstance(node, ast.Lambda):
636699
self.visit_Lambda(node)
637700
continue
638701
if isinstance(node, (ast.ListComp, ast.SetComp, ast.DictComp, ast.GeneratorExp)):
639702
self._visit_comprehension(node)
640703
continue
641704
if isinstance(node, ast.NamedExpr):
642-
self.visit(node.value)
643-
self._bind([node.target], node.value)
705+
pending.append(("bind_named", node))
706+
pending.append(("visit", node.value))
644707
continue
645708
if isinstance(node, ast.Call):
646-
self._record_call(node)
709+
pending.append(("finish_call_callee", node))
710+
pending.append(("visit", node.func))
711+
continue
647712
pending.extend(
648-
reversed(
713+
("visit", child)
714+
for child in reversed(
649715
[child for child in ast.iter_child_nodes(node) if isinstance(child, ast.expr)]
650716
)
651717
)
@@ -675,7 +741,7 @@ def _prepare_function(self, node: ast.FunctionDef | ast.AsyncFunctionDef) -> Non
675741
self.visit(expression)
676742

677743
def _analyze_function(self, node: ast.FunctionDef | ast.AsyncFunctionDef) -> None:
678-
collector = _LocalBindingCollector()
744+
collector = _LocalBindingCollector(self.check_runtime)
679745
for statement in node.body:
680746
collector.visit(statement)
681747
local_names = (collector.names | self._argument_names(node.args)) - (
@@ -693,15 +759,13 @@ def _analyze_function(self, node: ast.FunctionDef | ast.AsyncFunctionDef) -> Non
693759

694760
def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
695761
self._prepare_function(node)
696-
self._shadow_names([node.name])
697-
self._binding_scope(node.name).functions[node.name] = node
698-
self._analyze_function(node)
762+
self._register_function(node)
763+
self._invoke_function(node)
699764

700765
def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> None:
701766
self._prepare_function(node)
702-
self._shadow_names([node.name])
703-
self._binding_scope(node.name).functions[node.name] = node
704-
self._analyze_function(node)
767+
self._register_function(node)
768+
self._invoke_function(node)
705769

706770
def visit_Lambda(self, node: ast.Lambda) -> None:
707771
for expression in [*node.args.defaults, *node.args.kw_defaults]:
@@ -725,20 +789,19 @@ def visit_ClassDef(self, node: ast.ClassDef) -> None:
725789
for child in node.body
726790
if isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef))
727791
]
792+
outer_scopes = [scope.clone() for scope in self.scopes[1:]]
728793
self.scopes.append(_ReflectiveScope())
729-
self._visit_statements(
730-
[
731-
child
732-
for child in node.body
733-
if not isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef))
734-
]
735-
)
794+
for child in node.body:
795+
self._check_runtime()
796+
if isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef)):
797+
self._prepare_function(child)
798+
self._register_function(child, closure=outer_scopes)
799+
else:
800+
self._visit_statements([child])
736801
self.scopes.pop()
737802
for method in methods:
738-
self._prepare_function(method)
739-
snapshot = [scope.clone() for scope in self.scopes]
740-
self._analyze_function(method)
741-
self.scopes = snapshot
803+
if method not in self.called_functions:
804+
self._invoke_function(method)
742805

743806
@staticmethod
744807
def _merge_scope(
@@ -882,10 +945,10 @@ def visit_Match(self, node: ast.Match) -> None:
882945
def _visit_statements(self, statements: list[ast.stmt]) -> None:
883946
deferred: list[ast.FunctionDef | ast.AsyncFunctionDef] = []
884947
for statement in statements:
948+
self._check_runtime()
885949
if isinstance(statement, (ast.FunctionDef, ast.AsyncFunctionDef)):
886950
self._prepare_function(statement)
887-
self._shadow_names([statement.name])
888-
self._binding_scope(statement.name).functions[statement.name] = statement
951+
self._register_function(statement)
889952
deferred.append(statement)
890953
continue
891954
if isinstance(statement, ast.ClassDef):
@@ -898,15 +961,17 @@ def _visit_statements(self, statements: list[ast.stmt]) -> None:
898961
):
899962
continue
900963
outer_scopes = [scope.clone() for scope in self.scopes]
901-
self._analyze_function(function)
964+
self._invoke_function(function)
902965
self.scopes = outer_scopes
903966

904967
def visit_Module(self, node: ast.Module) -> None:
905968
self._visit_statements(node.body)
906969

907970

908-
def _build_reflective_sink_aliases(tree: ast.Module) -> dict[ast.Call, str]:
909-
resolver = _ReflectiveSinkResolver()
971+
def _build_reflective_sink_aliases(
972+
tree: ast.Module, check_runtime: Callable[[], None] | None = None
973+
) -> dict[ast.Call, str]:
974+
resolver = _ReflectiveSinkResolver(check_runtime=check_runtime)
910975
resolver.visit(tree)
911976
return resolver.call_sinks
912977

@@ -1091,7 +1156,9 @@ def _analyze_python(
10911156

10921157
aliases = python_ast.import_aliases
10931158
type_map = build_type_map(tree, aliases)
1094-
reflective_sinks = _build_reflective_sink_aliases(tree)
1159+
reflective_sinks = _build_reflective_sink_aliases(
1160+
tree, budget.check_runtime if budget is not None else None
1161+
)
10951162
lines = python_ast.lines
10961163
findings: list[AnalyzerFinding] = []
10971164
tainted: dict[str, _TaintedVar] = {}

‎tests/nodes/analyzers/test_behavioral_taint_tracking.py‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from __future__ import annotations
1919

20+
import ast
2021
import json
2122
import textwrap
2223

@@ -132,6 +133,88 @@ def test_function_call_before_reflective_binding_stays_safe(self):
132133
)
133134
assert "TT3" not in _rule_ids(_run(code))
134135

136+
@pytest.mark.parametrize(
137+
("initial_binding", "argument_binding", "expected"),
138+
[
139+
("opener = lambda value: value", 'getattr(module, "urlopen")', True),
140+
('opener = getattr(module, "urlopen")', "lambda value: value", False),
141+
],
142+
)
143+
def test_function_call_uses_callee_before_arguments_and_globals_after_arguments(
144+
self, initial_binding, argument_binding, expected
145+
):
146+
code = (
147+
"import importlib, os\n"
148+
'module = importlib.import_module("urllib.request")\n'
149+
f"{initial_binding}\n"
150+
"def send(unused):\n"
151+
' opener(os.environ.get("API_KEY"))\n'
152+
f"send(opener := {argument_binding})\n"
153+
)
154+
assert ("TT3" in _rule_ids(_run(code))) is expected
155+
156+
@pytest.mark.parametrize(
157+
("global_binding", "caller_binding", "expected"),
158+
[
159+
('getattr(module, "urlopen")', "lambda value: value", True),
160+
("lambda value: value", 'getattr(module, "urlopen")', False),
161+
],
162+
)
163+
def test_function_call_uses_lexical_scope_not_caller_locals(
164+
self, global_binding, caller_binding, expected
165+
):
166+
code = (
167+
"import importlib, os\n"
168+
'module = importlib.import_module("urllib.request")\n'
169+
f"opener = {global_binding}\n"
170+
"def send():\n"
171+
' opener(os.environ.get("API_KEY"))\n'
172+
"def wrapper():\n"
173+
f" opener = {caller_binding}\n"
174+
" send()\n"
175+
"wrapper()\n"
176+
)
177+
assert ("TT3" in _rule_ids(_run(code))) is expected
178+
179+
def test_class_method_definition_replaces_reflective_class_binding(self):
180+
code = (
181+
"import importlib, os\n"
182+
"class Client:\n"
183+
' module = importlib.import_module("urllib.request")\n'
184+
' opener = getattr(module, "urlopen")\n'
185+
" def opener(value):\n"
186+
" return value\n"
187+
' opener(os.environ.get("API_KEY"))\n'
188+
)
189+
assert "TT3" not in _rule_ids(_run(code))
190+
191+
def test_reflective_prepass_checks_shared_runtime_budget_during_expansion(self):
192+
source = (
193+
"def f0():\n pass\n"
194+
+ "".join(
195+
f"def f{index}():\n f{index - 1}()\n f{index - 1}()\n"
196+
for index in range(1, 21)
197+
)
198+
+ "f20()\n"
199+
)
200+
tree = ast.parse(source)
201+
checks = 0
202+
203+
class BudgetExpiredError(RuntimeError):
204+
pass
205+
206+
def check_runtime():
207+
nonlocal checks
208+
checks += 1
209+
if checks == 500:
210+
raise BudgetExpiredError
211+
212+
with pytest.raises(BudgetExpiredError):
213+
behavioral_taint_tracking._build_reflective_sink_aliases(
214+
tree, check_runtime=check_runtime
215+
)
216+
assert checks == 500
217+
135218
@pytest.mark.parametrize(
136219
("replacement", "call"),
137220
[

0 commit comments

Comments
 (0)