Skip to content

Commit 577c236

Browse files
committed
fix(refutation): prove only the allowlists that are constant and stable
Closes #239
1 parent 074d63f commit 577c236

7 files changed

Lines changed: 651 additions & 51 deletions

File tree

‎docs/usage.md‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ variable is not treated that way yet.
133133
| `exploitable-vulnerability` | An API affected by an advisory of a vulnerable requirement, see Dependencies. |
134134

135135
Each flow is judged before it is reported. A dominating guard that proves the value
136-
safe (`isdigit()`, membership in a constant collection, equality with a constant, a
136+
safe (`isdigit()`, membership in a constant allowlist, equality with a constant, a
137137
numeric value proven by `int()`, `len()` or bounded arithmetic, a validator such as
138138
`re.fullmatch`) refutes it and nothing is reported. A hotspot is reported at medium
139139
confidence. Two things make a flow a hotspot:
@@ -149,6 +149,27 @@ a vulnerability. When the flow passes the object whole, to the sink or to a func
149149
a guard on any of its attributes still counts. The verdict and its evidence are in the
150150
finding's metadata.
151151

152+
A membership test (`in`, `not in`) is a constant allowlist when every member is a constant
153+
that cannot change before the test:
154+
155+
- a tuple, list or set display written in the function, or a dict display by its keys,
156+
tested directly or through `.keys()`;
157+
- `frozenset`, `set`, `tuple` or `list` of one of these;
158+
- a module-level tuple, or `frozenset` of a display, of constants, bound once in its
159+
module and never rebound there, by another assignment, an augmented assignment or a
160+
`global` statement.
161+
162+
A list, set or dict display of the function counts only while the function only reads it:
163+
changed by a method, stored, or passed to a call before the test, it may hold anything. A
164+
module-level list, set or dict can be changed from any module, so it never counts, and
165+
the guard leaves a hotspot. Rebinding a module-level name from another module
166+
(`settings.ALLOWED = ...`) is not seen, as for the module-level strings that fix a URL.
167+
168+
A lookup in a dict display, `{"list": "ls"}[key]` or `.get(key, default)`, gives one of
169+
its values or the default, never the key: input that only selects the entry is not
170+
passed on. The same lookup in a module-level dict passes the key on, as any other
171+
subscript does.
172+
152173
Django's `render_to_string(name, context)` returns HTML in which autoescaping escaped
153174
every variable, so data reaching a response through it is no `xss`, if the template really
154175
escapes. On a directory check, the analyzer reads the project's templates to decide it.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
"""Module-level names whose value is known (architecture §18): the names bound once and
2+
never rebound, and among them the ones bound to an immutable collection of constants,
3+
which an allowlist reads."""
4+
5+
from __future__ import annotations
6+
7+
from collections.abc import Mapping
8+
from types import MappingProxyType
9+
from typing import ClassVar
10+
11+
from coretrace_python.analysis import Analysis, AnalysisContext, AnyAnalysis
12+
from coretrace_python.cfg import CFGError
13+
from coretrace_python.hir import nodes
14+
from coretrace_python.ir.lowering import (
15+
MODULE_BODY,
16+
LoweringError,
17+
PyIRAnalysis,
18+
analyzable_functions,
19+
)
20+
from coretrace_python.ir.model import (
21+
BuildDict,
22+
BuildList,
23+
BuildSet,
24+
BuildTuple,
25+
Call,
26+
Constant,
27+
ForNext,
28+
Instruction,
29+
SetGlobal,
30+
StoreLocal,
31+
Symbol,
32+
Value,
33+
)
34+
from coretrace_python.semantic.symbols import SymbolId
35+
36+
FROZENSET = SymbolId("python.builtins.frozenset")
37+
38+
39+
def module_bindings(ctx: AnalysisContext) -> tuple[Mapping[str, Instruction], Mapping[Value, Instruction]]:
40+
"""The module-level names bound once and never rebound, by no other statement of the
41+
module body, no function assigning them as ``global``, no definition of that name,
42+
each with the instruction that makes its value; and the definitions of the module
43+
body's values. A module that does not lower entirely has none."""
44+
45+
try:
46+
functions = [ctx.get(PyIRAnalysis, function) for function in analyzable_functions(ctx.module)]
47+
except (LoweringError, CFGError):
48+
return MappingProxyType({}), MappingProxyType({})
49+
body = next((function for function in functions if function.name == MODULE_BODY), None)
50+
if body is None:
51+
return MappingProxyType({}), MappingProxyType({})
52+
defs = {i.result: i for block in body.blocks for i in block.instructions if i.result is not None}
53+
stored: dict[str, list[Value | None]] = {}
54+
for block in body.blocks:
55+
for instruction in block.instructions:
56+
if isinstance(instruction, StoreLocal):
57+
stored.setdefault(instruction.name, []).append(instruction.value)
58+
if isinstance(block.terminator, ForNext):
59+
# A ``for`` binds its target to each item.
60+
stored.setdefault(block.terminator.target, []).append(None)
61+
rebound = {
62+
i.name for function in functions for block in function.blocks for i in block.instructions
63+
if isinstance(i, SetGlobal)
64+
}
65+
rebound |= {s.name for s in ctx.module.body if isinstance(s, nodes.Function | nodes.Class)}
66+
bound: dict[str, Instruction] = {}
67+
for name, values in stored.items():
68+
value = values[0] if len(values) == 1 and name not in rebound else None
69+
made = defs.get(value) if value is not None else None
70+
if made is not None:
71+
bound[name] = made
72+
return MappingProxyType(bound), MappingProxyType(defs)
73+
74+
75+
def constant_members(display: Instruction | None, defs: Mapping[Value, Instruction]) -> bool:
76+
"""Whether ``display`` is a tuple, list or set display whose items are all constants,
77+
or a dict display whose keys are, with nothing unpacked into it: the members a
78+
membership test reads."""
79+
80+
if isinstance(display, BuildList | BuildTuple | BuildSet):
81+
return not display.unpacked and all(isinstance(defs.get(e), Constant) for e in display.elements)
82+
if isinstance(display, BuildDict):
83+
return not display.unpacked and all(isinstance(defs.get(key), Constant) for key, _ in display.items)
84+
return False
85+
86+
87+
def converts(call: Call, defs: Mapping[Value, Instruction], symbols: frozenset[SymbolId]) -> Value | None:
88+
"""The one positional argument of ``call`` when it calls one of ``symbols`` with
89+
nothing else (``frozenset(x)``), otherwise ``None``."""
90+
91+
callee = defs.get(call.callee)
92+
if not isinstance(callee, Symbol) or callee.symbol_id not in symbols:
93+
return None
94+
if len(call.arguments) != 1 or call.keywords or call.starred:
95+
return None
96+
return call.arguments[0]
97+
98+
99+
class ModuleCollectionsAnalysis(Analysis[frozenset[str]]):
100+
"""The module-level names bound once and never rebound (``module_bindings``) to an
101+
immutable collection of constants: a tuple display, or ``frozenset`` of a display
102+
whose members are constants (``constant_members``). A list, a set or a dict can still
103+
change after it is bound, so it is not one."""
104+
105+
name: ClassVar[str] = "abstract.module_collections"
106+
requires: ClassVar[frozenset[AnyAnalysis]] = frozenset({PyIRAnalysis})
107+
108+
@classmethod
109+
def compute(cls, ctx: AnalysisContext) -> frozenset[str]:
110+
bound, defs = module_bindings(ctx)
111+
found: set[str] = set()
112+
for name, made in bound.items():
113+
collection: Instruction | None = made
114+
if isinstance(made, Call):
115+
argument = converts(made, defs, frozenset({FROZENSET}))
116+
collection = defs.get(argument) if argument is not None else None
117+
elif not isinstance(made, BuildTuple):
118+
continue
119+
if constant_members(collection, defs):
120+
found.add(name)
121+
return frozenset(found)

‎src/coretrace_python/abstract/strings.py‎

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,15 @@
99
from types import MappingProxyType
1010
from typing import ClassVar
1111

12+
from coretrace_python.abstract.module_constants import module_bindings
1213
from coretrace_python.analysis import Analysis, AnalysisContext, AnyAnalysis
13-
from coretrace_python.cfg import CFGError
14-
from coretrace_python.hir import nodes
15-
from coretrace_python.ir.lowering import (
16-
MODULE_BODY,
17-
LoweringError,
18-
PyIRAnalysis,
19-
analyzable_functions,
20-
)
14+
from coretrace_python.ir.lowering import PyIRAnalysis
2115
from coretrace_python.ir.model import (
2216
BinaryOp,
2317
BuildString,
2418
Constant,
25-
ForNext,
2619
Global,
2720
Instruction,
28-
SetGlobal,
29-
StoreLocal,
3021
Value,
3122
)
3223

@@ -59,40 +50,19 @@ def leading_text(value: Value, defs: Mapping[Value, Instruction], strings: Mappi
5950

6051

6152
class ModuleStringsAnalysis(Analysis[Mapping[str, str]]):
62-
"""The module-level names bound once, to a string literal, and never rebound: by no
63-
other statement of the module body, no function assigning them as ``global``, no
64-
definition of that name. A module that does not lower entirely has none."""
53+
"""The module-level names bound once and never rebound (``module_bindings``) to a
54+
string literal, with that literal."""
6555

6656
name: ClassVar[str] = "abstract.module_strings"
6757
requires: ClassVar[frozenset[AnyAnalysis]] = frozenset({PyIRAnalysis})
6858

6959
@classmethod
7060
def compute(cls, ctx: AnalysisContext) -> Mapping[str, str]:
71-
try:
72-
functions = [ctx.get(PyIRAnalysis, function) for function in analyzable_functions(ctx.module)]
73-
except (LoweringError, CFGError):
74-
return MappingProxyType({})
75-
body = next((function for function in functions if function.name == MODULE_BODY), None)
76-
if body is None:
77-
return MappingProxyType({})
78-
defs = {i.result: i for block in body.blocks for i in block.instructions if i.result is not None}
79-
stored: dict[str, list[Value | None]] = {}
80-
for block in body.blocks:
81-
for instruction in block.instructions:
82-
if isinstance(instruction, StoreLocal):
83-
stored.setdefault(instruction.name, []).append(instruction.value)
84-
if isinstance(block.terminator, ForNext):
85-
# A ``for`` binds its target to each item.
86-
stored.setdefault(block.terminator.target, []).append(None)
87-
rebound = {
88-
i.name for function in functions for block in function.blocks for i in block.instructions
89-
if isinstance(i, SetGlobal)
90-
}
91-
rebound |= {s.name for s in ctx.module.body if isinstance(s, nodes.Function | nodes.Class)}
92-
found: dict[str, str] = {}
93-
for name, values in stored.items():
94-
value = values[0] if len(values) == 1 and name not in rebound else None
95-
made = defs.get(value) if value is not None else None
96-
if isinstance(made, Constant) and isinstance(made.value, str):
97-
found[name] = made.value
98-
return MappingProxyType(found)
61+
bound, _ = module_bindings(ctx)
62+
return MappingProxyType(
63+
{
64+
name: made.value
65+
for name, made in bound.items()
66+
if isinstance(made, Constant) and isinstance(made.value, str)
67+
}
68+
)

‎src/coretrace_python/engine.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from coretrace_python import __version__
2121
from coretrace_python.abstract import ConstantPropagation, HeapAnalysis, RangeAnalysis
22+
from coretrace_python.abstract.module_constants import ModuleCollectionsAnalysis
2223
from coretrace_python.abstract.strings import ModuleStringsAnalysis
2324
from coretrace_python.analysis import (
2425
AnalysisContext,
@@ -158,6 +159,7 @@
158159
ConstantPropagation,
159160
RangeAnalysis,
160161
ModuleStringsAnalysis,
162+
ModuleCollectionsAnalysis,
161163
HeapAnalysis,
162164
CallGraphAnalysis,
163165
SummaryAnalysis,

0 commit comments

Comments
 (0)