Repository navigation
Expand file tree
/
Copy pathengine.py
More file actions
106 lines (88 loc) · 3.62 KB
/
Copy pathengine.py
File metadata and controls
106 lines (88 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""Composition root: the full analysis DAG, plugin loading and one check run.
This is the only module that knows every layer. The CLI and future hosts call it;
plugins and analyses never import it.
"""
from __future__ import annotations
from collections.abc import Sequence
from pathlib import Path
from coretrace_python import __version__
from coretrace_python.abstract import ConstantPropagation
from coretrace_python.analysis import AnalysisManager, AnyAnalysis
from coretrace_python.cfg import CFGAnalysis, CFGError, DominanceAnalysis, PostDominanceAnalysis
from coretrace_python.findings import Confidence, Finding, Severity
from coretrace_python.frontend import build_hir
from coretrace_python.hir import nodes
from coretrace_python.ir.defuse import DefUseAnalysis
from coretrace_python.ir.lowering import (
LoweringError,
ModuleIRAnalysis,
PyIRAnalysis,
analyzable_functions,
)
from coretrace_python.ir.ssa import SSAAnalysis
from coretrace_python.plugins import PluginRegistry, discover_plugins, run_plugins
from coretrace_python.reporters import Report
from coretrace_python.semantic import SEMANTIC_ANALYSES
from coretrace_python.source import SourceFile
from coretrace_python.taint import SecurityModelAnalysis, SecurityModelRegistry, TaintAnalysis
TOOL_NAME = "coretrace-python-analyzer"
ALL_ANALYSES: tuple[AnyAnalysis, ...] = (
*SEMANTIC_ANALYSES,
CFGAnalysis,
DominanceAnalysis,
PostDominanceAnalysis,
PyIRAnalysis,
ModuleIRAnalysis,
SSAAnalysis,
DefUseAnalysis,
ConstantPropagation,
SecurityModelAnalysis,
TaintAnalysis,
)
def build_manager(
module: nodes.Module, models: SecurityModelRegistry | None = None
) -> AnalysisManager:
"""A manager with every engine analysis registered and the security models provided."""
manager = _register_all(module)
manager.provide(SecurityModelAnalysis, (models or SecurityModelRegistry()).freeze())
return manager
def check(source: SourceFile, plugin_roots: Sequence[Path]) -> tuple[Finding, ...]:
"""Run every plugin found under ``plugin_roots`` against ``source``.
Plugins are loaded first so the models they contribute are all registered before the
table is provided; framework models and detectors compose without knowing each other.
"""
manager = _register_all(build_hir(source))
registry = PluginRegistry()
for root in plugin_roots:
for loaded in discover_plugins(root, manager):
registry.add(loaded)
models = SecurityModelRegistry()
for loaded in registry:
models.register(*loaded.plugin.models)
manager.provide(SecurityModelAnalysis, models.freeze())
supported: list[nodes.Function] = []
notes: list[Finding] = []
for function in analyzable_functions(manager.module):
try:
manager.get(SSAAnalysis, function)
except (LoweringError, CFGError) as error:
notes.append(
Finding(
rule_id="unsupported-syntax",
message=str(error),
severity=Severity.INFO,
confidence=Confidence.HIGH,
span=function.span,
function=function.name,
)
)
else:
supported.append(function)
findings = run_plugins(manager, [loaded.plugin for loaded in registry], tuple(supported))
return (*findings, *notes)
def _register_all(module: nodes.Module) -> AnalysisManager:
manager = AnalysisManager(module)
manager.register(*ALL_ANALYSES)
return manager
def report(findings: Sequence[Finding]) -> Report:
return Report(tuple(findings), TOOL_NAME, __version__)