Skip to content

Commit c9906e1

Browse files
authored
Merge pull request #200 from CoreTrace/feat/advisory-entry-suffixes
feat: match curated entry points by suffix for untypeable receivers
2 parents 021db94 + dbc0885 commit c9906e1

11 files changed

Lines changed: 444 additions & 41 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Advisories
66

77
- An `argument` condition may carry `present: true`: the argument being passed at all, whatever its value, is the condition, as a `\r` in `web.Response(reason=…)` needs `reason` given, constant or not. It names the argument by keyword or position as today and excludes `values` and `default`. A call giving the argument — positionally, by keyword, or through a `**` literal with constant keys, folded into the keywords — meets it even when the value is unknown, so attacker input there makes the call exploitable; a call surely not giving it is ruled out (`app:5 python.aiohttp.web.Response(reason absent)`) and a project passing only such calls stays `not_affected`; a call unpacking `*args` or a `**mapping` whose keys are not written leaves it pending review (#195).
8+
- A curated entry point may declare `suffixes`, for receivers the engine cannot type: `Item.objects.annotate(...)` derives the project's own `python.app.models.Item.objects.annotate`, which the canonical `python.django.db.models.QuerySet.annotate` entry point never matches. A suffix matches any symbol of the project's own modules ending with a dot plus the suffix, on dot boundaries as a `SuffixSink` matches, never another package's symbol; a queryset chain is declared explicitly (`objects.filter.annotate`), and a suffix is at least two dot-separated names. The finding keeps the advisory's symbol as `entry_point` and the derived one as `symbol`; conditions, `attacker_arguments` and the ruled-out evidence of contradicted calls apply through a suffix as through the exact symbol. An exact advisory sink whose symbol a bundled suffix sink also matches (`objects.extra`) now composes its kinds with the suffix sink's instead of shadowing them (#197).
89

910
## 0.20.0 (2026-09-30)
1011

‎docs/plugins.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ reported, calls to the affected symbols become reachable vulnerabilities and tai
197197
calls become exploitable ones. An advisory's entry points carry `conditions`, of kind
198198
`argument` (on the value, or with `present` on the argument being passed at all),
199199
`host`, `semantic` or `keyword_name`, and `attacker_arguments`, decided as
200-
the [usage guide](usage.md) describes.
200+
the [usage guide](usage.md) describes. An entry point may also declare `suffixes`,
201+
matched against the tail of a derived symbol of the project's own modules on dot
202+
boundaries, as a `SuffixSink` matches, for receivers the engine cannot type (see the
203+
usage guide).
201204

202205
### Reclassifying findings: `ProjectPlugin.refine`
203206

‎docs/usage.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,29 @@ it is reachable, called or not — `request.form['name']`, `for key in request.f
370370
`request.form.get('name')` all read it. A function reading it several times is reported
371371
once, where it first reads it.
372372

373+
An entry point may declare `suffixes`, for receivers the engine cannot type. A project
374+
reaches `python.django.db.models.QuerySet.annotate` through a manager on its own model,
375+
`Item.objects.annotate(...)`, whose call derives the project's own symbol
376+
`python.app.models.Item.objects.annotate` — `Item` is a project class, `objects` an
377+
attribute the engine does not type — so the canonical entry point never matches. A
378+
suffix matches any symbol of the project's own modules ending with a dot plus the
379+
suffix:
380+
381+
```json
382+
{"symbol": "python.django.db.models.QuerySet.annotate", "justification": "…",
383+
"suffixes": ["objects.annotate", "objects.filter.annotate"]}
384+
```
385+
386+
A queryset chain is declared explicitly: `Item.objects.filter(...).annotate(...)`
387+
derives `…objects.filter.annotate`, which only the second suffix matches. A suffix is
388+
at least two dot-separated names, so a bare method name, matching every project method
389+
of that name, cannot be declared; another package's symbol, such as
390+
`other_lib.objects.annotate`, never matches. The finding names the advisory's symbol as
391+
`entry_point` and the derived one as `symbol`, and the entry point's conditions and
392+
`attacker_arguments` apply through a suffix as through the exact symbol. A project
393+
attribute chain genuinely ending with a declared suffix matches too — the vulnerable
394+
version range and the conditions still gate what is reported.
395+
373396
A filter of Django's own libraries that a project template applies is a call to the
374397
function behind it, placed at the template's line: `{{ bio|striptags }}` calls
375398
`python.django.template.defaultfilters.striptags`, and so do filters in tag arguments

‎src/coretrace_python/bundled/dependency/reachable_vulnerability/reachable_vulnerability.py‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
from typing import ClassVar
99

1010
from coretrace_python.analysis import AnyAnalysis
11-
from coretrace_python.dependency import Advisory, DependencyAnalysis
11+
from coretrace_python.dependency import Advisory, AdvisoryEntryPoint, DependencyAnalysis
1212
from coretrace_python.dependency.correlation import (
1313
ConditionCheck,
14+
affected_entries,
1415
affected_symbols,
1516
check_conditions,
1617
evidence,
18+
suffix_index,
1719
)
1820
from coretrace_python.findings import Confidence, Finding
1921
from coretrace_python.interprocedural import CallGraphAnalysis, ExternalSymbol
@@ -30,37 +32,36 @@ def analyze_project(self, ctx: ProjectContext) -> Sequence[Finding]:
3032
affected = affected_symbols(ctx.dependencies, ctx.advisories)
3133
if not affected:
3234
return ()
35+
suffixes = suffix_index(affected, ctx.modules)
3336
findings: list[Finding] = []
3437
for module in ctx.modules:
3538
graph = ctx.call_graph(module)
3639
for function in graph.functions:
3740
for site in graph.sites(function):
3841
if not isinstance(site.target, ExternalSymbol):
3942
continue
40-
for advisory in affected.get(site.target.symbol, ()):
41-
entry = advisory.entry_point(site.target.symbol)
43+
for advisory, entry in affected_entries(site.target.symbol, affected, suffixes):
4244
if entry is not None and entry.read:
4345
continue # a call reads its callee first: reported with the reads
4446
check = check_conditions(entry, site.arguments)
4547
if check.contradicted is not None:
4648
continue
47-
findings.append(_reached(advisory, site.target.symbol, site.location, function, check))
49+
findings.append(_reached(advisory, site.target.symbol, site.location, function, check, entry))
4850
# Reading ``request.form.get`` reads ``request.form``: one finding per entry
4951
# point and function, where the function first reads it.
5052
reported: set[tuple[Advisory, SymbolId]] = set()
5153
for read in graph.reads(function):
5254
for symbol in _read_through(read.symbol):
53-
for advisory in affected.get(symbol, ()):
54-
entry = advisory.entry_point(symbol)
55+
for advisory, entry in affected_entries(symbol, affected, suffixes):
5556
if entry is None or not entry.read or (advisory, symbol) in reported:
5657
continue
5758
reported.add((advisory, symbol))
5859
check = check_conditions(entry, None)
59-
findings.append(_reached(advisory, symbol, read.location, function, check))
60+
findings.append(_reached(advisory, symbol, read.location, function, check, entry))
6061
for call in ctx.templates.calls:
61-
for advisory in affected.get(call.symbol, ()):
62-
check = check_conditions(advisory.entry_point(call.symbol), None)
63-
findings.append(_reached(advisory, call.symbol, call.span, None, check))
62+
for advisory, entry in affected_entries(call.symbol, affected, suffixes):
63+
check = check_conditions(entry, None)
64+
findings.append(_reached(advisory, call.symbol, call.span, None, check, entry))
6465
return findings
6566

6667

@@ -73,7 +74,12 @@ def _read_through(symbol: SymbolId) -> Iterator[SymbolId]:
7374

7475

7576
def _reached(
76-
advisory: Advisory, symbol: SymbolId, span: SourceSpan, function: str | None, check: ConditionCheck
77+
advisory: Advisory,
78+
symbol: SymbolId,
79+
span: SourceSpan,
80+
function: str | None,
81+
check: ConditionCheck,
82+
entry: AdvisoryEntryPoint | None,
7783
) -> Finding:
7884
return Finding(
7985
rule_id="reachable-vulnerability",
@@ -85,5 +91,5 @@ def _reached(
8591
confidence=Confidence.HIGH,
8692
span=span,
8793
function=function,
88-
metadata=evidence(advisory, symbol, "reachable", check),
94+
metadata=evidence(advisory, symbol, "reachable", check, entry),
8995
)

‎src/coretrace_python/bundled/dependency/vulnerable_dependency/vulnerable_dependency.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77

88
from coretrace_python.analysis import AnyAnalysis
99
from coretrace_python.dependency import DependencyAnalysis
10-
from coretrace_python.dependency.correlation import affected_symbols, check_conditions, ruled_out
10+
from coretrace_python.dependency.correlation import (
11+
affected_entries,
12+
affected_symbols,
13+
check_conditions,
14+
ruled_out,
15+
suffix_index,
16+
)
1117
from coretrace_python.findings import Confidence, Finding
1218
from coretrace_python.interprocedural import CallGraphAnalysis, ExternalSymbol
1319
from coretrace_python.plugins import ProjectContext, ProjectPlugin
@@ -57,15 +63,16 @@ def _ruled_out(ctx: ProjectContext) -> dict[str, list[str]]:
5763
conditions: the evidence that the requirement, imported, is not reached there."""
5864

5965
affected = affected_symbols(ctx.dependencies, ctx.advisories)
66+
suffixes = suffix_index(affected, ctx.modules)
6067
excluded: dict[str, list[str]] = {}
6168
for module in sorted(ctx.modules):
6269
graph = ctx.call_graph(module)
6370
for function in graph.functions:
6471
for site in graph.sites(function):
6572
if not isinstance(site.target, ExternalSymbol):
6673
continue
67-
for advisory in affected.get(site.target.symbol, ()):
68-
check = check_conditions(advisory.entry_point(site.target.symbol), site.arguments)
74+
for advisory, entry in affected_entries(site.target.symbol, affected, suffixes):
75+
check = check_conditions(entry, site.arguments)
6976
if check.contradicted is not None:
7077
excluded.setdefault(advisory.id, []).append(ruled_out(module, site, check))
7178
return excluded

‎src/coretrace_python/dependency/advisories.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
that a project keeps at its root as ``advisories.json`` or passes with ``--advisories``.
77
OSV records name no affected APIs, so imported advisories feed the requirement checks
88
and the SBOM; a file completed by hand with ``affected_symbols``, ``entry_points`` and
9-
their ``conditions`` also feeds the reachability and correlation checks.
9+
their ``conditions`` and ``suffixes`` also feeds the reachability and correlation checks.
1010
"""
1111

1212
from __future__ import annotations
@@ -161,6 +161,8 @@ def _entry_point_entry(entry_point: AdvisoryEntryPoint) -> dict[str, Any]:
161161
}
162162
if entry_point.read:
163163
entry["read"] = True
164+
if entry_point.suffixes:
165+
entry["suffixes"] = list(entry_point.suffixes)
164166
if entry_point.attacker_arguments:
165167
entry["attacker_arguments"] = [_attacker_argument_entry(a) for a in entry_point.attacker_arguments]
166168
return entry
@@ -228,9 +230,20 @@ def _entry_point(entry: Mapping[str, Any]) -> AdvisoryEntryPoint:
228230
tuple(_condition(c) for c in entry.get("conditions") or []),
229231
read,
230232
tuple(_attacker_argument(a) for a in entry.get("attacker_arguments") or []),
233+
tuple(_suffix(s) for s in entry.get("suffixes") or []),
231234
)
232235

233236

237+
def _suffix(value: Any) -> str:
238+
if not isinstance(value, str):
239+
raise TypeError(f"an entry point suffix must be a string, got {value!r}")
240+
parts = value.split(".")
241+
# A single name (``annotate``) would match every project method of that name.
242+
if len(parts) < 2 or not all(p.isidentifier() for p in parts):
243+
raise ValueError(f"an entry point suffix is at least two dot-separated names, got {value!r}")
244+
return value
245+
246+
234247
def _attacker_argument(entry: Mapping[str, Any]) -> AttackerArgument:
235248
argument, position, variadic = entry.get("argument"), entry.get("position"), entry.get("variadic", False)
236249
if argument is not None and not isinstance(argument, str):

‎src/coretrace_python/dependency/correlation.py‎

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from coretrace_python.findings.refutation import Status, Verdict, Verdicts
2424
from coretrace_python.interprocedural import Arguments, CallSite, ExternalSymbol
2525
from coretrace_python.semantic.symbols import SymbolId
26-
from coretrace_python.taint import KEYWORD_NAMES, Sink, TaintFlow, TaintKind
26+
from coretrace_python.taint import KEYWORD_NAMES, Sink, SuffixSink, TaintFlow, TaintKind
2727
from coretrace_python.taint.urls import UrlProof, redirects_disabled
2828

2929
Affected = Mapping[SymbolId, tuple[Advisory, ...]]
@@ -49,6 +49,56 @@ def advisory_sinks(affected: Affected) -> tuple[Sink, ...]:
4949
return tuple(Sink(symbol, TaintKind.ADVISORY) for symbol in affected)
5050

5151

52+
@dataclass(frozen=True)
53+
class SuffixIndex:
54+
"""The curated entry-point suffixes of the matching advisories, and the project's
55+
module prefixes: a derived symbol matches a suffix on a dot boundary, as a
56+
``SuffixSink`` does, and only when it belongs to a project module — another
57+
package's ``objects.annotate`` stays out."""
58+
59+
entries: tuple[tuple[str, Advisory, AdvisoryEntryPoint], ...] = ()
60+
prefixes: tuple[str, ...] = ()
61+
62+
def matches(self, symbol: SymbolId) -> tuple[tuple[Advisory, AdvisoryEntryPoint], ...]:
63+
name = symbol.canonical_name
64+
found = tuple((a, e) for tail, a, e in self.entries if name.endswith(tail))
65+
return found if found and name.startswith(self.prefixes) else ()
66+
67+
68+
def suffix_index(affected: Affected, modules: Iterable[str]) -> SuffixIndex:
69+
advisories = {a: None for found in affected.values() for a in found}
70+
entries = tuple((f".{s}", a, e) for a in advisories for e in a.entry_points for s in e.suffixes)
71+
return SuffixIndex(entries, tuple(f"python.{m}." for m in sorted(set(modules))))
72+
73+
74+
def affected_entries(
75+
symbol: SymbolId, affected: Affected, suffixes: SuffixIndex | None = None
76+
) -> tuple[tuple[Advisory, AdvisoryEntryPoint | None], ...]:
77+
"""The advisories reached through ``symbol``, each with the entry point it is reached
78+
by: the advisories affecting the exact symbol, then those a suffix matches."""
79+
80+
found: list[tuple[Advisory, AdvisoryEntryPoint | None]] = [
81+
(a, a.entry_point(symbol)) for a in affected.get(symbol, ())
82+
]
83+
if suffixes is not None:
84+
seen = {(a.id, a.package, None if e is None else e.symbol) for a, e in found}
85+
for advisory, entry in suffixes.matches(symbol):
86+
key = (advisory.id, advisory.package, entry.symbol)
87+
if key not in seen:
88+
seen.add(key)
89+
found.append((advisory, entry))
90+
return tuple(found)
91+
92+
93+
def advisory_suffix_sinks(affected: Affected) -> tuple[SuffixSink, ...]:
94+
"""A suffix sink per curated entry-point suffix, so the taint engine tracks flows
95+
into the derived symbols; ``correlate`` then keeps only the project's."""
96+
97+
advisories = {a: None for found in affected.values() for a in found}
98+
suffixes = {s: None for a in advisories for e in a.entry_points for s in e.suffixes}
99+
return tuple(SuffixSink(s, TaintKind.ADVISORY) for s in suffixes)
100+
101+
52102
@dataclass(frozen=True)
53103
class ConditionCheck:
54104
"""What one call tells of an entry point's conditions: those it meets, those left to
@@ -145,13 +195,21 @@ def ruled_out(module: str, site: CallSite, check: ConditionCheck) -> str:
145195
return f"{module}:{site.location.start_line} {site.target.symbol}({passed})"
146196

147197

148-
def evidence(advisory: Advisory, symbol: SymbolId, level: str, check: ConditionCheck) -> dict[str, str]:
198+
def evidence(
199+
advisory: Advisory,
200+
symbol: SymbolId,
201+
level: str,
202+
check: ConditionCheck,
203+
entry: AdvisoryEntryPoint | None = None,
204+
) -> dict[str, str]:
149205
"""What a finding keeps of the advisory for ``symbol``: the level of evidence
150206
established, how the symbol relates to the vulnerability, and its conditions — those
151-
the call meets and those left to review."""
207+
the call meets and those left to review. ``entry`` names the entry point ``symbol``
208+
was matched by when it is not the advisory's own symbol, as a suffix match derives
209+
it from the project; the metadata then keeps both."""
152210

153211
metadata = {"advisory": advisory.id, "package": advisory.package, "symbol": str(symbol), "level": level}
154-
entry = advisory.entry_point(symbol)
212+
entry = entry if entry is not None else advisory.entry_point(symbol)
155213
if entry is None:
156214
metadata["justification"] = DIRECT
157215
return metadata
@@ -172,6 +230,7 @@ def correlate(
172230
verdicts: Verdicts | None,
173231
affected: Affected,
174232
urls: Mapping[TaintFlow, UrlProof] | None = None,
233+
suffixes: SuffixIndex | None = None,
175234
) -> tuple[Finding, ...]:
176235
"""Exploitable-vulnerability findings for the non-refuted ADVISORY flows of a function,
177236
one per advisory the sink is affected by; ``urls`` holds what each flow's URL proves."""
@@ -184,25 +243,30 @@ def correlate(
184243
if verdict is not None and verdict.status is Status.REFUTED:
185244
continue
186245
hotspot = verdict is not None and verdict.status is Status.HOTSPOT
187-
for advisory in affected.get(flow.sink.symbol, ()):
188-
entry = advisory.entry_point(flow.sink.symbol)
246+
for advisory, entry in affected_entries(flow.sink.symbol, affected, suffixes):
189247
if entry is not None and not any(entry.exploitable_through(p, k) for p, k in flow.passed_as):
190248
continue
191249
check = check_conditions(entry, flow.sink_arguments, (urls or {}).get(flow), flow.passed_as)
192250
if check.contradicted is None:
193-
findings.append(_exploitable(function, flow, advisory, verdict, hotspot, check))
251+
findings.append(_exploitable(function, flow, advisory, verdict, hotspot, check, entry))
194252
return tuple(findings)
195253

196254

197255
def _exploitable(
198-
function: str, flow: TaintFlow, advisory: Advisory, verdict: Verdict | None, hotspot: bool, check: ConditionCheck
256+
function: str,
257+
flow: TaintFlow,
258+
advisory: Advisory,
259+
verdict: Verdict | None,
260+
hotspot: bool,
261+
check: ConditionCheck,
262+
entry: AdvisoryEntryPoint | None,
199263
) -> Finding:
200264
message = (
201265
f"{advisory.id}: {flow.source.label} input reaches {flow.sink.symbol}, affected in "
202266
f"the required {advisory.package} {advisory.vulnerable}: {advisory.summary}"
203267
)
204268
metadata = {
205-
**evidence(advisory, flow.sink.symbol, "exploitable", check),
269+
**evidence(advisory, flow.sink.symbol, "exploitable", check, entry),
206270
"source": str(flow.source.symbol),
207271
"source_label": flow.source.label,
208272
"verdict": "hotspot" if hotspot else "vulnerability",

‎src/coretrace_python/dependency/graph.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,19 @@ class AdvisoryEntryPoint:
207207
An entry point is reached by a call, or — when ``read`` is set, for an attribute whose
208208
getter runs the affected code, such as a lazily parsed request body — by any read of
209209
it or of an attribute of it. When it names ``attacker_arguments``, attacker input
210-
exploits it only through one of them; otherwise through any argument."""
210+
exploits it only through one of them; otherwise through any argument.
211+
212+
A suffix names the tail of a derived project symbol through which the entry point is
213+
reached when the engine cannot type the receiver: ``Item.objects.annotate`` derives
214+
``python.app.models.Item.objects.annotate``, which ``objects.annotate`` matches.
215+
Suffixes match on dot boundaries, and only symbols of the project's own modules."""
211216

212217
symbol: SymbolId
213218
justification: str
214219
conditions: tuple[Condition, ...] = ()
215220
read: bool = False
216221
attacker_arguments: tuple[AttackerArgument, ...] = ()
222+
suffixes: tuple[str, ...] = ()
217223

218224
def exploitable_through(self, position: int | None, keyword: str | None) -> bool:
219225
"""Whether attacker input in the argument at ``position`` or ``keyword`` can

0 commit comments

Comments
 (0)