Skip to content

Commit be855ea

Browse files
committed
Merge remote-tracking branch 'origin/main' into fix/project-sanitizers
2 parents 5a6d90d + d092f0f commit be855ea

5 files changed

Lines changed: 73 additions & 13 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+
### Plugins
6+
7+
- Two plugins may declare the same symbol: an identical model is registered once and sinks merge their kinds and positions; a real conflict names both plugins instead of failing the run on the first duplicate (#92).
8+
59
### Precision
610

711
- A `Sanitizer` declared on a function of the analysed project takes precedence over the summary derived from its body, in the same file and across files, so a code base can declare its own validation layer (#95).

‎docs/plugins.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ class BottleModels(ModelPlugin):
142142
| `Validator(symbol, kinds=ALL, argument=0)` | A callable whose truth proves its argument safe, such as `re.fullmatch`; a flow guarded by it is refuted. |
143143
| `AuthorizationGuard(symbol, label)` | A decorator or a condition restricting who reaches the code, such as `login_required`; a flow behind it is a hotspot. |
144144

145+
Two plugins may describe the same symbol. An identical model is registered once; two
146+
`Sink` models merge their kinds and positions, so a plugin can add `SQL` to a sink
147+
another plugin declared for `COMMAND`; any other difference is a conflict that stops
148+
the analysis with a `ModelError` naming both plugins.
149+
145150
A model plugin may also carry `advisories`, a tuple of `Advisory` values
146151
(`coretrace_python.dependency`) with the package, the vulnerable range and the affected
147152
symbols, as the shipped `sample-advisories` plugin does. Requirements matching them are

‎src/coretrace_python/engine.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def load_plugins(plugin_roots: Sequence[Path], manager: AnalysisManager) -> Plug
257257
def plugin_models(plugins: Iterable[Plugin]) -> ModelTable:
258258
models = SecurityModelRegistry()
259259
for plugin in plugins:
260-
models.register(*plugin.models)
260+
models.register(*plugin.models, origin=plugin.name)
261261
return models.freeze()
262262

263263

‎src/coretrace_python/taint/models.py‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ def kinds_at(self, position: int | None) -> TaintKind:
6969
kinds &= ~restricted
7070
return kinds
7171

72+
def merged(self, other: Sink) -> Sink:
73+
"""This sink with ``other``'s kinds and positions added."""
74+
75+
return Sink(self.symbol, self.kinds | other.kinds, self.positions + other.positions)
76+
7277

7378
@dataclass(frozen=True)
7479
class Sanitizer:
@@ -251,11 +256,7 @@ def extended(self, *sinks: Sink) -> ModelTable:
251256
merged = {sink.symbol: sink for sink in self.sinks}
252257
for sink in sinks:
253258
current = merged.get(sink.symbol)
254-
merged[sink.symbol] = (
255-
Sink(sink.symbol, current.kinds | sink.kinds, current.positions + sink.positions)
256-
if current is not None
257-
else sink
258-
)
259+
merged[sink.symbol] = sink if current is None else current.merged(sink)
259260
return ModelTable(
260261
self.sources,
261262
tuple(merged.values()),
@@ -279,15 +280,31 @@ class SecurityModelRegistry:
279280

280281
def __init__(self) -> None:
281282
self._models: dict[tuple[type[Model], SymbolId], Model] = {}
283+
self._origins: dict[tuple[type[Model], SymbolId], str | None] = {}
284+
285+
def register(self, *models: Model, origin: str | None = None) -> None:
286+
"""Add models; ``origin`` names the plugin so a conflict can name both sides.
287+
288+
Two plugins may describe the same symbol: an identical model is ignored, sinks
289+
merge their kinds and positions, any other difference is a conflict."""
282290

283-
def register(self, *models: Model) -> None:
284291
for model in models:
285292
key = (type(model), model.symbol)
286-
if key in self._models:
293+
current = self._models.get(key)
294+
if current is None:
295+
self._models[key] = model
296+
self._origins[key] = origin
297+
elif current == model:
298+
continue
299+
elif isinstance(model, Sink) and isinstance(current, Sink):
300+
self._models[key] = current.merged(model)
301+
else:
302+
registered = self._origins[key]
303+
sides = f" by {registered!r} and {origin!r}" if registered and origin else ""
287304
raise ModelError(
288-
f"{type(model).__name__.lower()} model for {model.symbol} is already registered"
305+
f"{type(model).__name__.lower()} model for {model.symbol}"
306+
f" is registered twice{sides} with different values"
289307
)
290-
self._models[key] = model
291308

292309
def freeze(self) -> ModelTable:
293310
models = list(self._models.values())

‎tests/test_taint.py‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,44 @@ def test_registry_indexes_models_by_symbol() -> None:
131131

132132
def test_registry_rejects_conflicting_models() -> None:
133133
registry = SecurityModelRegistry()
134-
registry.register(Sink(sym("python.os.system"), TaintKind.COMMAND))
134+
registry.register(Source(sym("python.flask.request.args"), "http"))
135135

136-
with pytest.raises(ModelError, match="python.os.system"):
137-
registry.register(Sink(sym("python.os.system"), TaintKind.SQL))
136+
with pytest.raises(ModelError, match="python.flask.request.args"):
137+
registry.register(Source(sym("python.flask.request.args"), "cli"))
138+
139+
140+
def test_registry_ignores_an_identical_model() -> None:
141+
registry = SecurityModelRegistry()
142+
registry.register(Sanitizer(sym("python.html.escape"), TaintKind.HTML))
143+
registry.register(Sanitizer(sym("python.html.escape"), TaintKind.HTML))
144+
145+
assert registry.freeze().sanitizers == (Sanitizer(sym("python.html.escape"), TaintKind.HTML),)
146+
147+
148+
def test_registry_merges_sinks_on_one_symbol() -> None:
149+
registry = SecurityModelRegistry()
150+
registry.register(Sink(sym("python.os.system"), TaintKind.COMMAND, ((TaintKind.COMMAND, (0,)),)))
151+
registry.register(Sink(sym("python.os.system"), TaintKind.SQL))
152+
153+
sink = registry.freeze().sink(sym("python.os.system"))
154+
assert sink == Sink(
155+
sym("python.os.system"), TaintKind.COMMAND | TaintKind.SQL, ((TaintKind.COMMAND, (0,)),)
156+
)
157+
158+
159+
def test_plugin_models_names_both_plugins_of_a_conflict() -> None:
160+
from coretrace_python.plugins import ModelPlugin
161+
162+
class First(ModelPlugin):
163+
name = "first-models"
164+
models = (Source(sym("python.flask.request.args"), "http"),)
165+
166+
class Second(ModelPlugin):
167+
name = "second-models"
168+
models = (Source(sym("python.flask.request.args"), "cli"),)
169+
170+
with pytest.raises(ModelError, match="'first-models' and 'second-models'"):
171+
engine.plugin_models([First(), Second()])
138172

139173

140174
def test_models_and_tables_are_immutable() -> None:

0 commit comments

Comments
 (0)