109109 "asyncio.create_subprocess_shell" ,
110110 }
111111)
112+ _LAUNCHER_MODULES = frozenset (name .partition ("." )[0 ] for name in _CHILD_PROCESS_ENV_CALLS )
112113E3_CODE_PATTERNS = [
113114 (r"glob\s*\.\s*glob\s*\([^)]*(?:\.env|\.ssh|\.aws|\.config|credentials)" , 0.8 ),
114115 (r"os\s*\.\s*walk\s*\([^)]*(?:home|~|/Users|/home)" , 0.6 ),
@@ -207,11 +208,10 @@ def _is_dynamic_copy_call(call: ast.Call, aliases: dict[str, str]) -> bool:
207208
208209# In-place edits of a mapping that keep the values on the host. ``update`` and
209210# ``clear`` return ``None``. The extraction methods hand a value back, so they
210- # only count as edits when that value is thrown away, or when it is the value of
211- # one literal key: a targeted lookup, which E2 does not treat as harvesting .
211+ # only count as edits when that value is thrown away. A value kept from the copy,
212+ # even by one literal key, is invisible to taint tracking, so it keeps the finding .
212213_ENVIRONMENT_MUTATION_METHODS = frozenset ({"update" , "clear" })
213214_ENVIRONMENT_EXTRACTION_METHODS = frozenset ({"pop" , "popitem" , "setdefault" })
214- _ENVIRONMENT_SINGLE_KEY_METHODS = frozenset ({"pop" , "setdefault" })
215215
216216# Statement lists a raise, ``break`` or ``continue`` can leave before a later
217217# rebinding runs, while code that still sees the older binding goes on to run:
@@ -229,8 +229,17 @@ def _is_dynamic_copy_call(call: ast.Call, aliases: dict[str, str]) -> bool:
229229 }
230230)
231231
232- # Builtins that read variables by a name only known at run time.
233- _DYNAMIC_NAMESPACE_CALLS = frozenset ({"globals" , "locals" , "vars" , "eval" , "exec" })
232+ # Builtins, attributes and modules that read variables by a name only known at
233+ # run time.
234+ _DYNAMIC_NAMESPACE_NAMES = frozenset ({"globals" , "locals" , "vars" , "eval" , "exec" , "sys.modules" })
235+ _DYNAMIC_NAMESPACE_ATTRIBUTES = frozenset ({"f_locals" , "f_globals" , "__globals__" })
236+
237+ # Calls that can replace an attribute of the object passed first, and the
238+ # ``sys.modules`` methods that can replace a module before it is imported.
239+ _ATTRIBUTE_WRITE_CALLS = frozenset ({"setattr" , "delattr" , "vars" })
240+ _MODULE_TABLE_WRITE_METHODS = frozenset (
241+ {"update" , "setdefault" , "pop" , "popitem" , "clear" , "__setitem__" , "__delitem__" }
242+ )
234243
235244# Bindings of one name that may all still be live, past which the name is
236245# reported rather than followed. Keeps crafted files linear.
@@ -254,17 +263,140 @@ def _edit_receiver(node: ast.AST) -> ast.Name | None:
254263 receiver = _method_receiver (node , _ENVIRONMENT_MUTATION_METHODS )
255264 if receiver is None and isinstance (node , ast .Expr ):
256265 receiver = _method_receiver (node .value , _ENVIRONMENT_EXTRACTION_METHODS )
257- if receiver is None and isinstance (node , ast .Call ):
258- single_key = _method_receiver (node , _ENVIRONMENT_SINGLE_KEY_METHODS )
259- if single_key is not None and (
260- node .args
261- and isinstance (node .args [0 ], ast .Constant )
262- and isinstance (node .args [0 ].value , str )
263- ):
264- receiver = single_key
265266 return receiver
266267
267268
269+ def _is_main_guard (test : ast .expr ) -> bool :
270+ """Return whether ``test`` is ``__name__ == "__main__"``, either way round."""
271+ if not (
272+ isinstance (test , ast .Compare ) and len (test .ops ) == 1 and isinstance (test .ops [0 ], ast .Eq )
273+ ):
274+ return False
275+ sides = (test .left , test .comparators [0 ])
276+ return any (isinstance (side , ast .Name ) and side .id == "__name__" for side in sides ) and any (
277+ isinstance (side , ast .Constant ) and side .value == "__main__" for side in sides
278+ )
279+
280+
281+ def _reads_names_dynamically (node : ast .AST , aliases : dict [str , str ]) -> bool :
282+ """Return whether ``node`` can read a variable by a name only known at run time.
283+
284+ Any reference counts, not only a call, so ``namespace = locals`` is caught too.
285+ """
286+ if isinstance (node , ast .Attribute ):
287+ if node .attr in _DYNAMIC_NAMESPACE_ATTRIBUTES :
288+ return True
289+ # Two parts at most (``sys.modules``, ``builtins.locals``) keeps this linear.
290+ if not isinstance (node .value , ast .Name ):
291+ return False
292+ name = apply_import_aliases (f"{ node .value .id } .{ node .attr } " , aliases )
293+ elif isinstance (node , ast .Name ) and isinstance (node .ctx , ast .Load ):
294+ name = apply_import_aliases (node .id , aliases )
295+ else :
296+ return False
297+ return name in _DYNAMIC_NAMESPACE_NAMES
298+
299+
300+ class _FileBindings :
301+ """How one file binds its names: by import or otherwise, and what it patches.
302+
303+ The flow pass sees one file, and call names are matched by spelling, so a
304+ launcher or a ``__main__`` guard is only trusted when nothing in the file can
305+ have replaced it.
306+ """
307+
308+ def __init__ (self , tree : ast .AST , aliases : dict [str , str ]) -> None :
309+ # Local name to the absolute import paths bound to it. ``""`` marks a
310+ # relative import, which may load a file the skill ships.
311+ self ._imports : dict [str , set [str ]] = {}
312+ # Names bound other than by an import, anywhere in the file.
313+ self ._rebound : set [str ] = set ()
314+ # Top-level modules with an attribute written from this file.
315+ self ._patched : set [str ] = set ()
316+ # A star import or a write to ``sys.modules`` can replace any import.
317+ self ._imports_replaceable = False
318+ for node in ast .walk (tree ):
319+ if isinstance (node , ast .Import ):
320+ for alias in node .names :
321+ # ``import a.b`` binds ``a``; ``import a.b as c`` binds ``c`` to ``a.b``.
322+ root = alias .name .partition ("." )[0 ]
323+ local , path = (alias .asname , alias .name ) if alias .asname else (root , root )
324+ self ._imports .setdefault (local , set ()).add (path )
325+ elif isinstance (node , ast .ImportFrom ):
326+ for alias in node .names :
327+ if alias .name == "*" :
328+ self ._imports_replaceable = True
329+ continue
330+ path = f"{ node .module } .{ alias .name } " if node .module and not node .level else ""
331+ self ._imports .setdefault (alias .asname or alias .name , set ()).add (path )
332+ elif isinstance (node , (ast .FunctionDef , ast .AsyncFunctionDef , ast .ClassDef )):
333+ self ._rebound .add (node .name )
334+ elif isinstance (node , ast .Name ) and not isinstance (node .ctx , ast .Load ):
335+ self ._rebound .add (node .id )
336+ elif isinstance (node , ast .arg ):
337+ self ._rebound .add (node .arg )
338+ elif isinstance (node , (ast .Global , ast .Nonlocal )):
339+ self ._rebound .update (node .names )
340+ elif isinstance (node , (ast .ExceptHandler , ast .MatchAs , ast .MatchStar )) and node .name :
341+ self ._rebound .add (node .name )
342+ elif isinstance (node , ast .MatchMapping ) and node .rest :
343+ self ._rebound .add (node .rest )
344+ else :
345+ self ._record_patch (node , aliases )
346+
347+ def _record_patch (self , node : ast .AST , aliases : dict [str , str ]) -> None :
348+ """Note a module whose attributes ``node`` may replace."""
349+ written : ast .expr | None = None
350+ if isinstance (node , (ast .Attribute , ast .Subscript )) and not isinstance (node .ctx , ast .Load ):
351+ written = node .value if isinstance (node , ast .Subscript ) else node
352+ elif (
353+ isinstance (node , ast .Attribute )
354+ and node .attr == "__dict__"
355+ and isinstance (node .value , ast .Name )
356+ ):
357+ written = node .value
358+ elif isinstance (node , ast .Call ) and node .args :
359+ call_name = resolve_call_name (node , aliases )
360+ if call_name in _ATTRIBUTE_WRITE_CALLS :
361+ written = node .args [0 ]
362+ elif call_name is not None and call_name .startswith ("sys.modules." ):
363+ if call_name .rpartition ("." )[2 ] in _MODULE_TABLE_WRITE_METHODS :
364+ written = node .func
365+ if written is None :
366+ return
367+ name = _resolve_expression_name (written , aliases )
368+ if name is None :
369+ return
370+ if name == "sys.modules" or name .startswith ("sys.modules." ):
371+ self ._imports_replaceable = True
372+ self ._patched .add (name .partition ("." )[0 ])
373+
374+ def launchers (self ) -> set [str ]:
375+ """Local names a child-process launcher can be called through.
376+
377+ Each one is bound only by absolute imports of one path in a launcher
378+ module, and nothing in the file writes to that module.
379+ """
380+ if self ._imports_replaceable :
381+ return set ()
382+ trusted : set [str ] = set ()
383+ for name , paths in self ._imports .items ():
384+ if name in self ._rebound or len (paths ) != 1 :
385+ continue
386+ module = next (iter (paths )).partition ("." )[0 ]
387+ if module in _LAUNCHER_MODULES and module not in self ._patched :
388+ trusted .add (name )
389+ return trusted
390+
391+ def main_guard_holds (self ) -> bool :
392+ """Whether ``__name__`` keeps its import-time value in this file."""
393+ return (
394+ not self ._imports_replaceable
395+ and "__name__" not in self ._rebound
396+ and "__name__" not in self ._imports
397+ )
398+
399+
268400@dataclass
269401class _EnvironmentBinding :
270402 """One name bound to a candidate expression, tracked until a rebinding that surely runs."""
@@ -372,6 +504,13 @@ class _EnvironmentFlowVisitor(ast.NodeVisitor):
372504 ``env=`` argument or an in-place edit of the mapping; any other use keeps the
373505 finding.
374506
507+ The pass sees one file, so a binding only passes through by name where no
508+ other file can reach it: a function local, or a module global bound in a
509+ top-level ``if __name__ == "__main__":`` body, which importers never run. A
510+ launcher only counts when its root name is bound solely by an absolute import
511+ from ``subprocess`` or ``asyncio`` and nothing in the file writes to that
512+ module, so a local ``def run`` or ``class subprocess`` gets no exemption.
513+
375514 Control flow errs toward keeping the finding. A rebinding closes only the
376515 bindings made in its own block or in blocks nested inside it, so one inside an
377516 ``if``, ``try``, ``with``, ``match`` or loop body leaves the outer binding open
@@ -380,8 +519,9 @@ class _EnvironmentFlowVisitor(ast.NodeVisitor):
380519 ``except``, ``with`` and loop bodies), or as a walrus, which can sit in a
381520 short-circuit. A loop body may run again, so a read anywhere in a loop counts
382521 against every binding made in that loop. A walrus result, a class attribute
383- and any name in a file that reads names dynamically (``globals()``,
384- ``eval``) keep the finding, as does a name with too many live bindings.
522+ and any name in a file that can read names dynamically (``globals``,
523+ ``eval``, frame locals, ``__globals__``, ``sys.modules``) keep the finding,
524+ as does a name with too many live bindings.
385525
386526 A function or lambda body runs at some later time, so its reads of an
387527 enclosing name count against every binding of that name that is open when the
@@ -394,18 +534,34 @@ def __init__(self, tree: ast.AST, aliases: dict[str, str]) -> None:
394534 self ._env_arguments : set [int ] = set ()
395535 self ._mutated_names : set [int ] = set ()
396536 self ._dynamic_reads = False
537+ bindings = _FileBindings (tree , aliases )
538+ launchers = bindings .launchers ()
539+ # Importers never run a top-level ``if __name__ == "__main__":`` body.
540+ self ._main_guards : set [int ] = set ()
541+ if isinstance (tree , ast .Module ) and bindings .main_guard_holds ():
542+ self ._main_guards = {
543+ id (statement )
544+ for statement in tree .body
545+ if isinstance (statement , ast .If ) and _is_main_guard (statement .test )
546+ }
547+ self ._in_main_guard = False
397548 for node in ast .walk (tree ):
549+ if _reads_names_dynamically (node , aliases ):
550+ self ._dynamic_reads = True
398551 receiver = _edit_receiver (node )
399552 if receiver is not None :
400553 self ._mutated_names .add (id (receiver ))
401554 elif isinstance (node , ast .Call ):
402555 call_name = resolve_call_name (node , aliases )
403- if call_name in _CHILD_PROCESS_ENV_CALLS :
556+ spelling = resolve_dotted_name (node .func )
557+ if (
558+ call_name in _CHILD_PROCESS_ENV_CALLS
559+ and spelling is not None
560+ and spelling .partition ("." )[0 ] in launchers
561+ ):
404562 self ._env_arguments .update (
405563 id (keyword .value ) for keyword in node .keywords if keyword .arg == "env"
406564 )
407- elif call_name in _DYNAMIC_NAMESPACE_CALLS :
408- self ._dynamic_reads = True
409565 elif (
410566 isinstance (node , ast .Subscript )
411567 and isinstance (node .ctx , (ast .Store , ast .Del ))
@@ -478,9 +634,15 @@ def _store(self, name: str, value_id: int | None, *, walrus: bool = False) -> No
478634 self ._close (scope , name , self ._block )
479635 if value_id is None :
480636 return
481- # A walrus also hands the value to the enclosing expression, and a class
482- # attribute outlives the class body.
483- escaped = walrus or scope .is_class or name in scope .read_later
637+ # A walrus also hands the value to the enclosing expression, a class
638+ # attribute outlives the class body, and another file can import a module
639+ # global this pass never sees.
640+ escaped = (
641+ walrus
642+ or scope .is_class
643+ or name in scope .read_later
644+ or (scope is self ._scopes [0 ] and not self ._in_main_guard )
645+ )
484646 binding = _EnvironmentBinding (value_id , self ._block , escaped = escaped )
485647 bindings = scope .open .setdefault (name , [])
486648 bindings .append (binding )
@@ -635,6 +797,16 @@ def visit_For(self, node: ast.For | ast.AsyncFor) -> None:
635797 def visit_AsyncFor (self , node : ast .AsyncFor ) -> None :
636798 self .visit_For (node )
637799
800+ def visit_If (self , node : ast .If ) -> None :
801+ if id (node ) not in self ._main_guards :
802+ self .generic_visit (node )
803+ return
804+ self .visit (node .test )
805+ self ._in_main_guard = True
806+ self ._visit_block (node .body )
807+ self ._in_main_guard = False
808+ self ._visit_block (node .orelse )
809+
638810 def visit_While (self , node : ast .While ) -> None :
639811 # The test runs again before every pass.
640812 self ._visit_loop ([node .test ], node .body )
0 commit comments