Skip to content

Commit 7228bb1

Browse files
committed
feat: analyse module bodies and class bodies as functions
The module's statements outside its definitions are lowered as a function named <module>, and a top-level class's statements outside its methods as Cls.<body>, both carrying the span of what they stand for so their names resolve in the right scope. Their calls are call sites, their values carry taint and an advisory entry point called at import time is reachable. Inside the module body, the variables the module assigns are the body's locals: x = input(); os.system(x) connects in SSA and a module-level for loop has a target, while functions, classes and imports stay globals for the call graph and the symbol resolution. Wildcard imports lower to nothing. Coverage counts the bodies; the cache format is bumped. Regression snapshots re-recorded: 13 missing-timeout, 2 ssrf, 1 command-injection and 1 reachable-vulnerability at module level, and one stale syntax-error entry main no longer produced. Closes #113
1 parent c3d8f8c commit 7228bb1

9 files changed

Lines changed: 93 additions & 12 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Analysis
6+
7+
- The module body and the bodies of top-level classes are analysed as the functions `<module>` and `Cls.<body>`: their calls are call sites, their values carry taint, and an advisory entry point called at import time or in a class body is reachable. Coverage counts them; the cache format is bumped so cached results gain their findings (#113).
8+
59
### Dependencies
610

711
- Advisories distinguish the `affected_symbols` a fix changed from the public `entry_points` through which they are reached, each justified and carrying `conditions` (checkable argument values, or `semantic` ones kept as pending review); `modules` names what the package installs. Every dependency finding records its evidence `level`: `declared`, `imported`, `reachable` or `exploitable`.

‎docs/usage.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ its package (`app/views.py` is `app.views`). Hidden directories, `node_modules`,
7171
dependency graph. Configuration files under the root (`.env`, YAML, TOML, JSON, INI,
7272
properties) are scanned for secrets.
7373

74-
Functions and methods are analysed; module-level statements outside functions are not.
74+
Functions and methods are analysed, and so are the module body and the bodies of
75+
top-level classes, as the functions `<module>` and `Cls.<body>`: `application =
76+
get_asgi_application()` in `asgi.py` or `email = forms.EmailField()` in a form class are
77+
calls the analyzer sees, and a module-level `os.system(input())` is a finding in `<module>`.
7578
A function using syntax outside the supported subset is reported as an
7679
`unsupported-syntax` note and the other functions are still analysed; a file Python
7780
itself cannot parse is reported as a `syntax-error`. The coverage line and the JSON

‎src/coretrace_python/cache.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from coretrace_python.semantic.symbols import SymbolId
3838
from coretrace_python.source import SourceId, SourceSpan
3939

40-
CACHE_FORMAT = 4
40+
CACHE_FORMAT = 5
4141

4242

4343
@dataclass(frozen=True)

‎src/coretrace_python/interprocedural/callgraph.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
from coretrace_python.analysis import Analysis, AnalysisContext, AnyAnalysis
1717
from coretrace_python.cfg import CFGError
1818
from coretrace_python.hir import nodes
19-
from coretrace_python.ir.lowering import LoweringError, analyzable_functions, qualified_name
19+
from coretrace_python.ir.lowering import (
20+
MODULE_BODY,
21+
LoweringError,
22+
analyzable_functions,
23+
qualified_name,
24+
)
2025
from coretrace_python.ir.model import (
2126
Await,
2227
Call,
@@ -198,7 +203,9 @@ def resolve_targets(
198203
for block in function.blocks:
199204
for instruction in block.instructions:
200205
if isinstance(instruction, MakeFunction):
201-
qualified = f"{function.name}.{instruction.name}"
206+
# A lambda or def made by the module body is named without a prefix, as
207+
# its scope's parent is the module.
208+
qualified = instruction.name if function.name == MODULE_BODY else f"{function.name}.{instruction.name}"
202209
if qualified in nested:
203210
targets[instruction.result] = KnownFunction(qualified)
204211
elif isinstance(instruction, Global):

‎src/coretrace_python/interprocedural/modulegraph.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ def project_symbol(module_name: str, qualified_name: str) -> SymbolId:
7373
symbols: invalid characters become underscores."""
7474

7575
module = ".".join(_component(part) for part in module_name.split("."))
76-
return SymbolId(f"python.{module}.{qualified_name}")
76+
# A synthetic body function is ``<module>`` or ``Cls.<body>``: valid as a name, not
77+
# as a symbol component. Nothing calls a body, so its symbol only needs to be one.
78+
qualified = ".".join(_component(part) for part in qualified_name.split("."))
79+
return SymbolId(f"python.{module}.{qualified}")
7780

7881

7982
def _component(part: str) -> str:

‎src/coretrace_python/ir/lowering.py‎

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,19 @@ def emit_effect(self, instruction: EffectInstruction) -> None:
120120
def resolve(self, name: str) -> Resolution:
121121
if name in self.cfg.synthetic_locals:
122122
return Resolution(ResolutionKind.LOCAL, self.scope.id)
123-
return self.scopes.resolve(self.scope.id, name)
123+
resolution = self.scopes.resolve(self.scope.id, name)
124+
# The module body runs as a script: the variables it assigns are its locals, so
125+
# ``x = input(); os.system(x)`` connects in SSA and ``for x in ...`` has a target.
126+
# Its functions, classes and imports stay globals, which is how the call graph
127+
# and the symbol resolution find them.
128+
if (
129+
resolution.kind is ResolutionKind.GLOBAL
130+
and self.scope.kind is ScopeKind.MODULE
131+
and (binding := self.scope.bindings.get(name)) is not None
132+
and binding.kind is BindingKind.LOCAL
133+
):
134+
return Resolution(ResolutionKind.LOCAL, self.scope.id)
135+
return resolution
124136

125137
# ------------------------------------------------------------------ expressions
126138

@@ -360,6 +372,9 @@ def statement(self, node: nodes.Statement) -> None:
360372
# records that the import runs here (§39 rule 3).
361373
module = "." * node.level + (node.module or "") if isinstance(node, nodes.ImportFrom) else ""
362374
for alias in node.names:
375+
if alias.name == "*":
376+
# ``from m import *`` binds through the scope's wildcards, not a name.
377+
continue
363378
bound = alias.as_name or alias.name.partition(".")[0]
364379
symbol = self.symbols.resolve(self.scope.id, bound)
365380
if symbol is None:
@@ -563,8 +578,9 @@ def compute(cls, ctx: AnalysisContext, function: nodes.Function) -> FunctionIR:
563578

564579

565580
def analyzable_functions(module: nodes.Module) -> tuple[nodes.Function, ...]:
566-
"""Top-level functions, the methods of top-level classes, and the functions and
567-
lambdas nested inside them, in source order. Computed once per module: the result is
581+
"""The module body, top-level functions, top-level classes' bodies and methods, and
582+
the functions and lambdas nested inside them, in source order. Computed once per
583+
module: the result is
568584
memoised for the last few modules seen, keyed by identity, so every consumer shares
569585
one tuple and the synthesized lambda functions it holds."""
570586

@@ -581,16 +597,54 @@ def analyzable_functions(module: nodes.Module) -> tuple[nodes.Function, ...]:
581597

582598
def _analyzable_functions(module: nodes.Module) -> tuple[nodes.Function, ...]:
583599
functions: list[nodes.Function] = []
600+
body = module_function(module)
601+
if body is not None:
602+
_collect(body, functions)
584603
for statement in module.body:
585604
if isinstance(statement, nodes.Function):
586605
_collect(statement, functions)
587606
elif isinstance(statement, nodes.Class):
607+
body = class_body_function(statement)
608+
if body is not None:
609+
_collect(body, functions)
588610
for member in statement.body:
589611
if isinstance(member, nodes.Function):
590612
_collect(member, functions)
591613
return tuple(functions)
592614

593615

616+
MODULE_BODY = "<module>"
617+
CLASS_BODY = ".<body>"
618+
619+
620+
def module_function(module: nodes.Module) -> nodes.Function | None:
621+
"""The module's statements outside its definitions, as a function named ``<module>``
622+
with the module's span, so its names resolve in the module scope; ``None`` when the
623+
module only defines things."""
624+
625+
statements = tuple(s for s in module.body if not isinstance(s, nodes.Function | nodes.Class))
626+
if not statements:
627+
return None
628+
return nodes.Function(MODULE_BODY, (), statements, False, module.span)
629+
630+
631+
def class_body_function(cls: nodes.Class) -> nodes.Function | None:
632+
"""A top-level class's statements outside its methods (``field = Field()``), as a
633+
function named ``Cls.<body>`` with the class's span, so its names resolve in the
634+
class scope."""
635+
636+
statements = tuple(s for s in cls.body if not isinstance(s, nodes.Function))
637+
if not statements:
638+
return None
639+
return nodes.Function(f"{cls.name}{CLASS_BODY}", (), statements, False, cls.span)
640+
641+
642+
def is_body_function(function: nodes.Function) -> bool:
643+
"""Whether ``function`` is a synthetic module or class body rather than a definition."""
644+
645+
return function.name == MODULE_BODY or function.name.endswith(CLASS_BODY)
646+
647+
594648
def _collect(function: nodes.Function, into: list[nodes.Function]) -> None:
595649
into.append(function)
596650

‎src/coretrace_python/plugins/api.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ def module(self) -> nodes.Module:
163163
return self._manager.module
164164

165165
def functions(self) -> tuple[nodes.Function, ...]:
166-
"""Top-level functions and methods the engine can analyse."""
166+
"""The functions the engine analyses: the module body (``<module>``), top-level
167+
functions, class bodies (``Cls.<body>``) and methods."""
167168

168169
return self._functions
169170

‎src/coretrace_python/semantic/scopes.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,9 @@ def __init__(self, module: nodes.Module) -> None:
218218
ScopeId(str(module.span.source_id)), ScopeKind.MODULE, "<module>", None, module.span
219219
)
220220
self.builders: list[_ScopeBuilder] = [self.module]
221-
self.spans: dict[SourceSpan, ScopeId] = {}
221+
# The module scope is found by the module's span, as the synthetic ``<module>``
222+
# function carries it.
223+
self.spans: dict[SourceSpan, ScopeId] = {module.span: self.module.id}
222224
self.body(module.body, self.module)
223225

224226
def body(self, statements: tuple[nodes.Statement, ...], scope: _ScopeBuilder) -> None:

‎src/coretrace_python/taint/engine.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
SummaryTable,
3939
project_symbol,
4040
)
41-
from coretrace_python.ir.lowering import analyzable_functions
41+
from coretrace_python.ir.lowering import analyzable_functions, is_body_function
4242
from coretrace_python.ir.model import (
4343
BasicBlock,
4444
Branch,
@@ -593,7 +593,12 @@ def local_instances(function: nodes.Function, scopes: ScopeTable, symbols: Symbo
593593

594594

595595
def _enclosing(module: nodes.Module, function: nodes.Function) -> nodes.Function | None:
596-
"""The function whose body defines ``function``, if it is nested."""
596+
"""The function whose body defines ``function``, if it is nested. A module or class
597+
body is nested in nothing, and encloses nothing: a lambda at module level reads
598+
globals, not the locals of a function."""
599+
600+
if is_body_function(function):
601+
return None
597602

598603
def search(
599604
body: tuple[nodes.Statement, ...], parent: nodes.Function | None
@@ -618,6 +623,8 @@ def search(
618623
# analysable function whose span contains theirs.
619624
innermost: nodes.Function | None = None
620625
for candidate in analyzable_functions(module):
626+
if is_body_function(candidate):
627+
continue
621628
if candidate.span != function.span and _contains(candidate.span, function.span):
622629
innermost = candidate
623630
return innermost

0 commit comments

Comments
 (0)