Skip to content

Commit d7a3ebd

Browse files
committed
fix(summaries): keep nothing of the blocks constant propagation proves unreachable
Closes #258
1 parent 1f13cfc commit d7a3ebd

3 files changed

Lines changed: 95 additions & 5 deletions

File tree

‎docs/usage.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ variable is not treated that way yet.
132132
| `plaintext-credential-storage` | A parameter named like a password stored in a database without hashing. Medium confidence, since a name is a hint. |
133133
| `exploitable-vulnerability` | An API affected by an advisory of a vulnerable requirement, see Dependencies. |
134134

135-
Each flow is judged before it is reported. A dominating guard that proves the value
135+
Each flow is judged before it is reported. Code constant propagation proves can never
136+
run (under `if False:`, `while False:`) reports nothing, in the function and through the
137+
project functions that contain it: their summaries keep nothing of such code, neither a
138+
sink nor a returned, merged or stored value. Code that may run is kept, whatever decides
139+
it at run time, a parameter or a module-level name. A dominating guard that proves the value
136140
safe (`isdigit()`, membership in a constant allowlist, equality with a constant, a
137141
numeric value proven by `int()`, `len()` or bounded arithmetic, a validator such as
138142
`re.fullmatch`) refutes it and nothing is reported. A sink that selects code by name,

‎src/coretrace_python/interprocedural/summaries.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,11 @@ def evaluate(self, block: BasicBlock, incoming: Mapping[BlockId, State]) -> Stat
509509
return MappingProxyType(state)
510510

511511
def flow(self, cfg: CFG, block_id: BlockId, incoming: Mapping[BlockId, State]) -> Mapping[BlockId, State]:
512+
# A block constant propagation proves unreachable has no effect: no store, call,
513+
# return or write, and nothing it would pass on reaches its successors or their
514+
# phis (#258). A block that may run is kept, whatever decides it.
515+
if not self.reachable(block_id):
516+
return {}
512517
block = self.blocks[block_id]
513518
state = self.evaluate(block, incoming)
514519
exits = {target: state for target in block.exception_targets}
@@ -576,9 +581,9 @@ def call(self, call: Call, state: dict[Key, Dep]) -> Dep:
576581
given = self.graph.arguments_at(self.name, call.location)
577582
self.record(target.symbol, arguments, named, call.location, None, given)
578583
# Where its result is called: facts the summary keeps whatever the models;
579-
# the consumer decides which kinds of a sink apply there (#240). An invocation
580-
# is kept only when it and the call can run, by constant propagation.
581-
if call.result is not None and self.runs(call):
584+
# the consumer decides which kinds of a sink apply there (#240). The call runs,
585+
# as its block is evaluated; an invocation is kept only when it can run too.
586+
if call.result is not None:
582587
for invocation in invocations(self.function, self.uses, call.result):
583588
if self.runs(invocation):
584589
self.record(target.symbol, arguments, named, call.location, None, given, invocation.location)
@@ -769,7 +774,7 @@ def summarize(
769774
problem.mutated_globals = set()
770775
problem.nonlocal_stores = {}
771776
for block in function.blocks:
772-
if solution.reached(block.id):
777+
if solution.reached(block.id) and problem.reachable(block.id):
773778
problem.evaluate(block, solution.incoming(block.id))
774779
mutations: list[Mutation] = []
775780
for index, parameter in enumerate(function.parameters):

‎tests/test_summary_reachability.py‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"""Acceptance tests for issue #258: a summary keeps nothing of the blocks constant
2+
propagation proves unreachable.
3+
4+
A project function's summary carries its sinks, its return, the arguments it mutates
5+
and the values merged at a join to its callers. A block that can never run contributes
6+
none of them: no flow is reported at a caller for code under ``if False:`` or
7+
``while False:``, through one or several functions. A block that may run is kept,
8+
whatever decides it at run time: a parameter, or a module-level name the analysis of
9+
one function does not resolve.
10+
"""
11+
12+
from __future__ import annotations
13+
14+
import pytest
15+
16+
from coretrace_python import engine
17+
from coretrace_python.source import SourceManager
18+
19+
20+
def injections(body: str) -> list[tuple[int, str]]:
21+
text = "import os\n\n\n" + body
22+
findings = engine.check(SourceManager().add_source("app.py", text), [engine.BUNDLED_PLUGINS])
23+
return [(f.span.start_line, f.metadata["verdict"]) for f in findings if f.rule_id == "command-injection"]
24+
25+
26+
@pytest.mark.parametrize(
27+
"body",
28+
[
29+
# A sink.
30+
"def helper(cmd):\n if False:\n os.system(cmd)\n\n\ndef caller():\n helper(input())\n",
31+
"def helper(cmd):\n while False:\n os.system(cmd)\n\n\ndef caller():\n helper(input())\n",
32+
# A return.
33+
"def helper(cmd):\n if False:\n return cmd\n return 'ls'\n\n\ndef caller():\n os.system(helper(input()))\n",
34+
# A value merged at a join from the block that cannot run.
35+
(
36+
"def helper(cmd):\n result = 'ls'\n if False:\n result = cmd\n return result\n\n\n"
37+
"def caller():\n os.system(helper(input()))\n"
38+
),
39+
# A mutated argument.
40+
(
41+
"def helper(items, cmd):\n if False:\n items.append(cmd)\n\n\n"
42+
"def caller():\n items = []\n helper(items, input())\n os.system(items[0])\n"
43+
),
44+
# Through two functions.
45+
(
46+
"def inner(cmd):\n if False:\n os.system(cmd)\n\n\ndef outer(cmd):\n inner(cmd)\n\n\n"
47+
"def caller():\n outer(input())\n"
48+
),
49+
],
50+
)
51+
def test_nothing_of_a_block_that_cannot_run_reaches_the_callers(body: str) -> None:
52+
assert injections(body) == []
53+
54+
55+
@pytest.mark.parametrize(
56+
("body", "line"),
57+
[
58+
# Decided at run time by a parameter.
59+
("def helper(cmd, flag):\n if flag:\n os.system(cmd)\n\n\ndef caller(flag):\n helper(input(), flag)\n", 10),
60+
# A module-level name, not resolved within the function.
61+
("DEBUG = False\n\n\ndef helper(cmd):\n if DEBUG:\n os.system(cmd)\n\n\ndef caller():\n helper(input())\n", 13),
62+
# The same effects in blocks that run.
63+
("def helper(cmd):\n if True:\n os.system(cmd)\n\n\ndef caller():\n helper(input())\n", 10),
64+
(
65+
(
66+
"def helper(cmd):\n result = 'ls'\n if True:\n result = cmd\n return result\n\n\n"
67+
"def caller():\n os.system(helper(input()))\n"
68+
),
69+
12,
70+
),
71+
(
72+
(
73+
"def helper(items, cmd):\n if True:\n items.append(cmd)\n\n\n"
74+
"def caller():\n items = []\n helper(items, input())\n os.system(items[0])\n"
75+
),
76+
12,
77+
),
78+
],
79+
)
80+
def test_a_block_that_may_run_keeps_its_effects(body: str, line: int) -> None:
81+
assert injections(body) == [(line, "vulnerability")]

0 commit comments

Comments
 (0)