Skip to content

Commit ac40099

Browse files
committed
fix(e2): visit annotations and report pass-through copies as LOW
The flow pass skipped argument and return annotations, type parameters and type alias values, so a copy read there was marked as pass-through. Visit them as reads that run when defined or later. Comprehension targets that only load a name no longer bind it, and an augmented assignment other than |= reads the old value. A copy traced only to a child process's env= is now reported as an E2 LOW at confidence 0.2 instead of being dropped. The pass covers ordinary Python evaluation, not frames, hooks or other introspection, so a read it misses lowers the finding rather than removing a HIGH. Signed-off-by: Malin Fossum <[email protected]>
1 parent 42fa2cf commit ac40099

3 files changed

Lines changed: 384 additions & 45 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ SkillSpector detects **71 vulnerability patterns** across 17 categories:
561561
| ID | Pattern | Severity | Description |
562562
|----|---------|----------|-------------|
563563
| E1 | External Transmission | MEDIUM | Sending data to external URLs |
564-
| E2 | Env Variable Harvesting | HIGH | Enumerating, copying, or searching environment data to collect secrets |
564+
| E2 | Env Variable Harvesting | HIGH | Enumerating, copying, or searching environment data to collect secrets. A copy traced only to a child process's `env=` is reported LOW |
565565
| E3 | File System Enumeration | MEDIUM | Scanning directories for sensitive files |
566566
| E4 | Context Leakage | HIGH | Transmitting conversation context externally |
567567

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

Lines changed: 104 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@
120120
}
121121
)
122122
_LAUNCHER_MODULES = frozenset(name.partition(".")[0] for name in _CHILD_PROCESS_ENV_CALLS)
123+
# A copy the flow pass traces only to a child process stays an E2 finding, but as a
124+
# low-confidence LOW. The proof covers ordinary Python, not every introspection
125+
# trick, so a read it misses lowers the finding instead of removing it.
126+
_CHILD_PROCESS_PASSTHROUGH_CONFIDENCE = 0.2
123127
E3_CODE_PATTERNS = [
124128
(r"glob\s*\.\s*glob\s*\([^)]*(?:\.env|\.ssh|\.aws|\.config|credentials)", 0.8),
125129
(r"os\s*\.\s*walk\s*\([^)]*(?:home|~|/Users|/home)", 0.6),
@@ -634,6 +638,36 @@ def _argument_names(arguments: ast.arguments) -> set[str]:
634638
}
635639

636640

641+
def _annotation_expressions(node: ast.FunctionDef | ast.AsyncFunctionDef) -> list[ast.expr | None]:
642+
arguments = node.args
643+
return [
644+
*(
645+
argument.annotation
646+
for argument in (
647+
*arguments.posonlyargs,
648+
*arguments.args,
649+
*arguments.kwonlyargs,
650+
arguments.vararg,
651+
arguments.kwarg,
652+
)
653+
if argument is not None
654+
),
655+
node.returns,
656+
]
657+
658+
659+
def _type_param_expressions(type_params: Sequence[ast.type_param]) -> list[ast.expr | None]:
660+
# Type parameter defaults arrived in Python 3.13.
661+
return [
662+
expression
663+
for type_param in type_params
664+
for expression in (
665+
getattr(type_param, "bound", None),
666+
getattr(type_param, "default_value", None),
667+
)
668+
]
669+
670+
637671
class _EnvironmentFlowVisitor(ast.NodeVisitor):
638672
"""Decide which environment mappings only ever reach a child-process ``env=``.
639673
@@ -643,16 +677,16 @@ class _EnvironmentFlowVisitor(ast.NodeVisitor):
643677
``env = os.environ.copy()``, and a nested scope's own ``env`` cannot close the
644678
outer one. A binding passes through only if every use is a child-process
645679
``env=`` argument or an in-place edit of the mapping; any other use keeps the
646-
finding.
680+
finding HIGH.
647681
648682
The pass sees one file, so a binding only passes through by name where no
649683
other file can reach it: a function local, or a module global bound in a
650684
top-level ``if __name__ == "__main__":`` body, which importers never run. A
651685
launcher only counts when its root name is bound solely by an absolute import
652686
from ``subprocess`` or ``asyncio`` and nothing in the file writes to that
653-
module, so a local ``def run`` or ``class subprocess`` gets no exemption.
687+
module, so a local ``def run`` or ``class subprocess`` is never trusted.
654688
655-
Control flow errs toward keeping the finding. A rebinding closes only the
689+
Control flow errs toward keeping the finding HIGH. A rebinding closes only the
656690
bindings made in its own block or in blocks nested inside it, so one inside an
657691
``if``, ``try``, ``with``, ``match`` or loop body leaves the outer binding open
658692
next to the new one, and later uses count against both. A rebinding never
@@ -686,6 +720,7 @@ def __init__(self, tree: ast.AST, aliases: dict[str, str]) -> None:
686720
if isinstance(statement, ast.If) and _is_main_guard(statement.test)
687721
}
688722
self._in_main_guard = False
723+
self._lazy = 0
689724
for node in ast.walk(tree):
690725
if _reads_names_dynamically(node, aliases):
691726
self._dynamic_reads = True
@@ -709,7 +744,13 @@ def __init__(self, tree: ast.AST, aliases: dict[str, str]) -> None:
709744
and isinstance(node.value, ast.Name)
710745
):
711746
self._mutated_names.add(id(node.value))
712-
elif isinstance(node, ast.AugAssign) and isinstance(node.target, ast.Name):
747+
elif (
748+
isinstance(node, ast.AugAssign)
749+
and isinstance(node.op, ast.BitOr)
750+
and isinstance(node.target, ast.Name)
751+
):
752+
# Only ``dict.__ior__`` exists. Any other operator hands the whole
753+
# mapping to the right operand's reflected method.
713754
self._mutated_names.add(id(node.target))
714755
self._scopes: list[_Scope] = [_Scope(local_names=None)]
715756
self._passed_through: set[int] = set()
@@ -751,7 +792,7 @@ def _close_scope(self, scope: _Scope) -> None:
751792

752793
def _resolve(self, name: str, *, walrus: bool = False) -> tuple[_Scope, bool]:
753794
"""Return the scope that owns ``name`` here, and whether this access runs later."""
754-
deferred = False
795+
deferred = self._lazy > 0
755796
innermost = True
756797
for scope in reversed(self._scopes):
757798
if walrus and scope.is_comprehension:
@@ -842,6 +883,19 @@ def _visit_scope(self, scope: _Scope, body: list[ast.stmt] | list[ast.expr]) ->
842883
self._close_scope(self._scopes.pop())
843884
self._protected = outer_protected
844885

886+
def _visit_lazy(self, expressions: Sequence[ast.expr | None]) -> None:
887+
"""Visit expressions that run where they are defined, or only when read.
888+
889+
Annotations run eagerly before Python 3.14 and lazily from it on, and type
890+
parameters and ``type`` alias values always run lazily, so a read here
891+
counts against the bindings open now and every later one.
892+
"""
893+
self._lazy += 1
894+
for expression in expressions:
895+
if expression is not None:
896+
self.visit(expression)
897+
self._lazy -= 1
898+
845899
def _visit_defaults(self, arguments: ast.arguments) -> None:
846900
for default in (*arguments.defaults, *arguments.kw_defaults):
847901
if default is not None:
@@ -851,6 +905,9 @@ def visit_FunctionDef(self, node: ast.FunctionDef | ast.AsyncFunctionDef) -> Non
851905
for decorator in node.decorator_list:
852906
self.visit(decorator)
853907
self._visit_defaults(node.args)
908+
self._visit_lazy(
909+
[*_type_param_expressions(node.type_params), *_annotation_expressions(node)]
910+
)
854911
bound, declared_global = _scope_declarations(node.body)
855912
scope = _Scope(bound | _argument_names(node.args), declared_global, deferred=True)
856913
self._visit_scope(scope, node.body)
@@ -866,6 +923,7 @@ def visit_Lambda(self, node: ast.Lambda) -> None:
866923
self._visit_scope(scope, [node.body])
867924

868925
def visit_ClassDef(self, node: ast.ClassDef) -> None:
926+
self._visit_lazy(_type_param_expressions(node.type_params))
869927
for expression in (*node.decorator_list, *node.bases):
870928
self.visit(expression)
871929
for keyword in node.keywords:
@@ -885,7 +943,7 @@ def _visit_comprehension(
885943
name.id
886944
for generator in node.generators
887945
for name in ast.walk(generator.target)
888-
if isinstance(name, ast.Name)
946+
if isinstance(name, ast.Name) and isinstance(name.ctx, ast.Store)
889947
}
890948
deferred = isinstance(node, ast.GeneratorExp)
891949
self._scopes.append(_Scope(targets, deferred=deferred, is_comprehension=True))
@@ -916,18 +974,30 @@ def visit_Assign(self, node: ast.Assign) -> None:
916974
self._bind(target, id(node.value))
917975

918976
def visit_AnnAssign(self, node: ast.AnnAssign) -> None:
919-
if node.value is None:
920-
return
921-
self.visit(node.value)
922-
self._bind(node.target, id(node.value))
977+
if node.value is not None:
978+
self.visit(node.value)
979+
self._bind(node.target, id(node.value))
980+
elif not isinstance(node.target, ast.Name):
981+
# Without a value, an attribute or subscript target still evaluates its base.
982+
self.visit(node.target)
983+
# A function never evaluates its local annotations; counting them anyway is safe.
984+
self._visit_lazy([node.annotation])
985+
986+
def visit_TypeAlias(self, node: ast.TypeAlias) -> None:
987+
self.visit(node.name)
988+
self._visit_lazy([*_type_param_expressions(node.type_params), node.value])
923989

924990
def visit_NamedExpr(self, node: ast.NamedExpr) -> None:
925991
self.visit(node.value)
926992
self._bind(node.target, id(node.value), walrus=True)
927993

928994
def visit_AugAssign(self, node: ast.AugAssign) -> None:
929995
self.visit(node.value)
930-
self.visit(node.target)
996+
target = node.target
997+
if isinstance(target, ast.Name) and id(target) not in self._mutated_names:
998+
# The operator reads the old value before the name is rebound.
999+
self.visit(ast.copy_location(ast.Name(target.id, ast.Load()), target))
1000+
self.visit(target)
9311001

9321002
def visit_For(self, node: ast.For | ast.AsyncFor) -> None:
9331003
self.visit(node.iter)
@@ -983,7 +1053,8 @@ def _analyze_python_environment_reads(
9831053
"""Detect materializing or enumerating the complete ``os.environ`` mapping.
9841054
9851055
A full mapping copy or enumeration is an environment-harvesting signal, unlike a
986-
targeted single-key lookup or passing ``os.environ`` through to a child process.
1056+
targeted single-key lookup. A copy that the flow pass traces only to a child
1057+
process is still reported, but as a low-confidence LOW pass-through.
9871058
Credential flows to network and execution sinks remain covered by the behavioral
9881059
taint analyzer. AST parsing makes this check insensitive to formatting and lets it
9891060
resolve ``os`` / ``environ`` import aliases.
@@ -1005,8 +1076,8 @@ def _analyze_python_environment_reads(
10051076
flow.visit(tree)
10061077
child_process_passthroughs = flow.passthrough_ids()
10071078
except RecursionError:
1008-
# Too deep for the recursive flow pass: report every full read, as before
1009-
# the child-process exemption existed, instead of losing the whole file.
1079+
# Too deep for the recursive flow pass: report every full read as HIGH
1080+
# instead of losing the whole file.
10101081
child_process_passthroughs = set()
10111082
findings: list[AnalyzerFinding] = []
10121083
emitted: set[int] = set()
@@ -1016,19 +1087,33 @@ def emit(node: ast.AST, confidence: float) -> None:
10161087
node_id = id(node)
10171088
if node_id in emitted:
10181089
return
1019-
if node_id in child_process_passthroughs:
1020-
return
10211090
emitted.add(node_id)
1091+
passthrough = node_id in child_process_passthroughs
10221092
lineno = getattr(node, "lineno", 1)
10231093
end_lineno = getattr(node, "end_lineno", None)
10241094
matched_text = ast.get_source_segment(content, node) or "os.environ"
10251095
findings.append(
10261096
AnalyzerFinding(
10271097
rule_id="E2",
1028-
message="Env Variable Harvesting",
1029-
severity=Severity.HIGH,
1098+
message=(
1099+
"Environment Pass-Through to Child Process"
1100+
if passthrough
1101+
else "Env Variable Harvesting"
1102+
),
1103+
severity=Severity.LOW if passthrough else Severity.HIGH,
10301104
location=Location(file=file_path, start_line=lineno, end_line=end_lineno),
1031-
confidence=confidence,
1105+
confidence=(
1106+
min(confidence, _CHILD_PROCESS_PASSTHROUGH_CONFIDENCE)
1107+
if passthrough
1108+
else confidence
1109+
),
1110+
explanation=(
1111+
"The copy of os.environ is only handed to a child process launched "
1112+
"through subprocess or asyncio, so the child receives the environment "
1113+
"the skill already runs in. No other read of the copy was found."
1114+
if passthrough
1115+
else None
1116+
),
10321117
tags=tag,
10331118
context=get_context_from_lines(lines, lineno),
10341119
matched_text=matched_text[:200],

0 commit comments

Comments
 (0)