Skip to content

Commit c9b0716

Browse files
authored
Merge pull request #89 from CoreTrace/feat/tornado-bottle-models
Add Tornado and Bottle models and resolve inherited handler attributes
2 parents ed53092 + fe58a53 commit c9b0716

15 files changed

Lines changed: 318 additions & 33 deletions

File tree

‎docs/usage.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ report's per-file detail tell "no findings" from "nothing analysed".
7979

8080
Taint follows values between functions and across files through function summaries,
8181
into objects (containers, attributes, instances of the project's own classes) and through
82-
closures. Flask, FastAPI, Django and aiohttp route handlers, class-based views and registered
83-
URL patterns receive HTTP input; click and Typer commands receive `argv` input; `input()`,
82+
closures. Flask, FastAPI, Django, aiohttp, Tornado and Bottle route handlers, class-based views
83+
and registered URL patterns receive HTTP input, and a method a handler class inherits
84+
from its framework base, `self.get_argument` or `self.request`, denotes the base's; click and Typer commands receive `argv` input; `input()`,
8485
`sys.argv`, environment variables, the output of local processes and the responses of
8586
HTTP clients are further sources. The first three are operator-controlled: a command-line
8687
tool opens the paths it is given, a command or a path built from the environment is the
@@ -309,9 +310,9 @@ real 45 000-line project is exercised on every change.
309310

310311
## Plugins
311312

312-
The package ships 28 plugins, loaded by default. Security models for the standard library
313+
The package ships 30 plugins, loaded by default. Security models for the standard library
313314
and the supported frameworks: `python-stdlib-models`, `flask-models`, `fastapi-models`,
314-
`django-models`, `aiohttp-models`, `sqlalchemy-models`, `db-driver-models` (aiopg,
315+
`django-models`, `aiohttp-models`, `tornado-models`, `bottle-models`, `sqlalchemy-models`, `db-driver-models` (aiopg,
315316
asyncpg, psycopg2, PyMySQL), `http-client-models`, `credential-models` and `cli-models`. Taint detectors: `command-injection`, `sql-injection`, `path-traversal`,
316317
`ssrf`, `xss`, `insecure-deserialization`, `open-redirect` and `plaintext-credentials`.
317318
Dangerous API detectors: `dangerous-eval`, `weak-crypto`, `flask-debug` and
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""Bottle security models: ``@route``, ``@get`` and the methods of a ``Bottle`` application
2+
are entry points, ``bottle.request`` is the HTTP source, ``redirect``, ``static_file``,
3+
``template`` and ``HTTPResponse`` are sinks, ``html_escape`` a sanitizer."""
4+
5+
from __future__ import annotations
6+
7+
from typing import ClassVar
8+
9+
from coretrace_python.plugins import ModelPlugin
10+
from coretrace_python.semantic.symbols import SymbolId
11+
from coretrace_python.taint import EntryPoint, Model, Sanitizer, Sink, Source, TaintKind
12+
13+
_METHODS = ("route", "get", "post", "put", "delete", "patch")
14+
_REQUEST_ATTRIBUTES = (
15+
"query", "forms", "params", "json", "body", "headers", "cookies", "GET", "POST", "files",
16+
"url", "path", "fullpath", "query_string", "get_cookie", "get_header",
17+
)
18+
_FIRST_ONLY = lambda kind: ((kind, (0,)),)
19+
20+
21+
def _sym(path: str) -> SymbolId:
22+
return SymbolId(f"python.{path}")
23+
24+
25+
class BottleModels(ModelPlugin):
26+
name: ClassVar[str] = "bottle-models"
27+
models: ClassVar[tuple[Model, ...]] = (
28+
*(EntryPoint(_sym(f"bottle.{method}"), "http") for method in _METHODS),
29+
*(EntryPoint(_sym(f"bottle.Bottle.{method}"), "http") for method in _METHODS),
30+
*(Source(_sym(f"bottle.request.{attribute}"), "http") for attribute in _REQUEST_ATTRIBUTES),
31+
Sink(_sym("bottle.redirect"), TaintKind.REDIRECT, _FIRST_ONLY(TaintKind.REDIRECT)),
32+
# ``static_file(filename, root)``: the root is the application's own.
33+
Sink(_sym("bottle.static_file"), TaintKind.PATH, _FIRST_ONLY(TaintKind.PATH)),
34+
# ``template(source_or_name, **variables)``: a tainted template is injection, the
35+
# variables are escaped by the engine.
36+
Sink(_sym("bottle.template"), TaintKind.HTML, _FIRST_ONLY(TaintKind.HTML)),
37+
Sink(_sym("bottle.HTTPResponse"), TaintKind.HTML),
38+
Sanitizer(_sym("bottle.html_escape"), TaintKind.HTML),
39+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name = "bottle-models"
2+
version = "1.0.0"
3+
plugin_api = ">=1,<2"
4+
requires = []
5+
provides = ["model.http-sources", "model.bottle-routes"]
6+
7+
[entrypoint]
8+
module = "bottle_models"
9+
class = "BottleModels"

‎src/coretrace_python/bundled/models/django/django_models.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
RouteRegistrar,
2121
Sanitizer,
2222
Sink,
23+
Source,
2324
SuffixSink,
2425
TaintKind,
2526
TypedParameter,
@@ -98,6 +99,8 @@ class DjangoModels(ModelPlugin):
9899
models: ClassVar[tuple[Model, ...]] = (
99100
*(TypedParameter(_sym(cls), "http") for cls in _REQUEST_CLASSES),
100101
*(EntryPoint(_sym(base), "http") for base in _VIEW_BASES),
102+
# ``self.request`` in a class-based view: the attribute is inherited from the base.
103+
*(Source(_sym(f"{base}.request"), "http") for base in _VIEW_BASES),
101104
*(EntryPoint(_sym(decorator), "http") for decorator in _VIEW_DECORATORS),
102105
Sink(_sym("django.db.connection.cursor.execute"), TaintKind.SQL | TaintKind.CREDENTIAL, ((TaintKind.SQL, (0,)),)),
103106
Sink(_sym("django.db.connection.cursor.executemany"), TaintKind.SQL | TaintKind.CREDENTIAL, ((TaintKind.SQL, (0,)),)),
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name = "tornado-models"
2+
version = "1.0.0"
3+
plugin_api = ">=1,<2"
4+
requires = []
5+
provides = ["model.http-sources", "model.tornado-handlers", "model.ssrf-sinks"]
6+
7+
[entrypoint]
8+
module = "tornado_models"
9+
class = "TornadoModels"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Tornado security models: ``RequestHandler`` subclasses are entry points whose inherited
2+
request methods are HTTP sources and whose ``write``, ``finish`` and ``redirect`` are
3+
sinks; the HTTP clients are SSRF sinks with untrusted responses."""
4+
5+
from __future__ import annotations
6+
7+
from typing import ClassVar
8+
9+
from coretrace_python.plugins import ModelPlugin
10+
from coretrace_python.semantic.symbols import SymbolId
11+
from coretrace_python.taint import EntryPoint, Model, Sanitizer, Sink, Source, TaintKind
12+
13+
_HANDLERS = ("tornado.web.RequestHandler", "tornado.websocket.WebSocketHandler")
14+
_REQUEST_METHODS = (
15+
"get_argument", "get_arguments", "get_query_argument", "get_query_arguments",
16+
"get_body_argument", "get_body_arguments", "get_cookie", "request", "path_args", "path_kwargs",
17+
)
18+
_CLIENTS = ("tornado.httpclient.AsyncHTTPClient.fetch", "tornado.httpclient.HTTPClient.fetch")
19+
_TARGET_ONLY = ((TaintKind.REDIRECT, (0,)),)
20+
21+
22+
def _sym(path: str) -> SymbolId:
23+
return SymbolId(f"python.{path}")
24+
25+
26+
class TornadoModels(ModelPlugin):
27+
name: ClassVar[str] = "tornado-models"
28+
models: ClassVar[tuple[Model, ...]] = (
29+
*(EntryPoint(_sym(handler), "http") for handler in _HANDLERS),
30+
*(Source(_sym(f"{handler}.{method}"), "http") for handler in _HANDLERS for method in _REQUEST_METHODS),
31+
*(Sink(_sym(f"{handler}.write"), TaintKind.HTML) for handler in _HANDLERS),
32+
*(Sink(_sym(f"{handler}.finish"), TaintKind.HTML) for handler in _HANDLERS),
33+
*(Sink(_sym(f"{handler}.redirect"), TaintKind.REDIRECT, _TARGET_ONLY) for handler in _HANDLERS),
34+
*(Sink(_sym(client), TaintKind.SSRF) for client in _CLIENTS),
35+
*(Source(_sym(client), "http-response") for client in _CLIENTS),
36+
Sanitizer(_sym("tornado.escape.xhtml_escape"), TaintKind.HTML),
37+
)

‎src/coretrace_python/cache.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from coretrace_python.semantic.symbols import SymbolId
3838
from coretrace_python.source import SourceId, SourceSpan
3939

40-
CACHE_FORMAT = 3
40+
CACHE_FORMAT = 4
4141

4242

4343
@dataclass(frozen=True)
@@ -252,6 +252,7 @@ def _encode_summary(summary: FunctionSummary) -> dict[str, Any]:
252252
{"name": w.name, "dependencies": sorted(w.dependencies), "externals": sorted(str(s) for s in w.externals)}
253253
for w in summary.nonlocal_writes
254254
],
255+
"static": summary.static,
255256
}
256257

257258

@@ -279,6 +280,7 @@ def _decode_summary(data: Mapping[str, Any]) -> FunctionSummary:
279280
)
280281
for w in data["nonlocal_writes"]
281282
),
283+
bool(data["static"]),
282284
)
283285

284286

‎src/coretrace_python/interprocedural/callgraph.py‎

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,23 +158,43 @@ def resolve_targets(
158158
classes: Mapping[str, frozenset[str]] | None = None,
159159
owner: str | None = None,
160160
typed: Mapping[Value, str] | None = None,
161+
bases: Mapping[str, SymbolId] | None = None,
161162
) -> tuple[dict[Value, Target], dict[Value, SymbolId]]:
162163
"""Map every callee value of ``function`` to its target, and every symbol value.
163164
``nested`` names the functions defined inside this one (``outer.inner``);
164165
``classes`` maps the module's classes to their methods, ``owner`` is the class of a
165166
method and ``typed`` the parameters annotated with a module class. ``App(x)`` is a
166-
call to ``App.__init__``, ``app.run()`` and ``self.run()`` calls to ``App.run``."""
167+
call to ``App.__init__``, ``app.run()`` and ``self.run()`` calls to ``App.run``.
168+
``bases`` maps a module class to the symbol of its base: an attribute the class does
169+
not define is inherited, so ``self.get_argument`` in a ``RequestHandler`` subclass
170+
denotes ``tornado.web.RequestHandler.get_argument``."""
167171

168172
module = scopes.module_scope
169-
symbols = derive_symbols(function, parameters)
170-
targets: dict[Value, Target] = {
171-
value: ExternalSymbol(symbol) for value, symbol in symbols.items()
172-
}
173173
classes = classes or {}
174174
instance_of: dict[Value, str] = dict(typed or {})
175175
if owner is not None and function.parameters:
176176
instance_of[function.parameters[0]] = owner
177177
defs = {i.result: i for block in function.blocks for i in block.instructions if i.result}
178+
for block in function.blocks:
179+
for instruction in block.instructions:
180+
if isinstance(instruction, Call):
181+
callee = defs.get(instruction.callee)
182+
if isinstance(callee, Global) and callee.name in classes:
183+
instance_of[instruction.result] = callee.name
184+
inherited: dict[Value, SymbolId] = dict(parameters or {})
185+
for block in function.blocks:
186+
for instruction in block.instructions:
187+
if isinstance(instruction, GetAttr) and instruction.object in instance_of:
188+
if instruction.object in inherited:
189+
continue # an annotated parameter denotes its own class
190+
class_name = instance_of[instruction.object]
191+
base = (bases or {}).get(class_name)
192+
if base is not None and instruction.attribute not in classes.get(class_name, ()):
193+
inherited[instruction.result] = base.attribute(instruction.attribute)
194+
symbols = derive_symbols(function, inherited)
195+
targets: dict[Value, Target] = {
196+
value: ExternalSymbol(symbol) for value, symbol in symbols.items()
197+
}
178198
for block in function.blocks:
179199
for instruction in block.instructions:
180200
if isinstance(instruction, MakeFunction):
@@ -189,17 +209,29 @@ def resolve_targets(
189209
targets[instruction.result] = KnownFunction(instruction.name)
190210
elif binding.kind is BindingKind.CLASS and "__init__" in classes.get(instruction.name, ()):
191211
targets[instruction.result] = KnownFunction(f"{instruction.name}.__init__")
192-
elif isinstance(instruction, Call):
193-
callee = defs.get(instruction.callee)
194-
if isinstance(callee, Global) and callee.name in classes:
195-
instance_of[instruction.result] = callee.name
196212
elif isinstance(instruction, GetAttr) and instruction.object in instance_of:
197213
method = f"{instance_of[instruction.object]}.{instruction.attribute}"
198214
if method in nested:
199215
targets[instruction.result] = KnownFunction(method)
216+
elif isinstance(instruction, GetAttr):
217+
# ``Student.create(...)``: a static or class method called on the class.
218+
origin = defs.get(instruction.object)
219+
if isinstance(origin, Global) and origin.name in classes:
220+
method = f"{origin.name}.{instruction.attribute}"
221+
if method in nested:
222+
targets[instruction.result] = KnownFunction(method)
200223
return targets, symbols
201224

202225

226+
def _is_staticmethod(function: nodes.Function) -> bool:
227+
for decorator in function.decorators:
228+
if isinstance(decorator, nodes.Name) and decorator.identifier == "staticmethod":
229+
return True
230+
if isinstance(decorator, nodes.Attribute) and decorator.name == "staticmethod":
231+
return True
232+
return False
233+
234+
203235
def _annotated(
204236
function: nodes.Function, ssa: FunctionIR, scopes: ScopeTable, table: SymbolTable
205237
) -> dict[Value, SymbolId]:
@@ -257,6 +289,14 @@ def compute(cls, ctx: AnalysisContext) -> CallGraph:
257289
for statement in ctx.module.body
258290
if isinstance(statement, nodes.Class)
259291
}
292+
bases: dict[str, SymbolId] = {}
293+
for statement in ctx.module.body:
294+
if isinstance(statement, nodes.Class):
295+
for base_expression in statement.bases:
296+
symbol = table.resolve_expression(scopes.module_scope.id, base_expression)
297+
if symbol is not None:
298+
bases[statement.name] = symbol
299+
break
260300
sites: dict[str, tuple[CallSite, ...]] = {}
261301
symbols: dict[str, Mapping[Value, SymbolId]] = {}
262302
unsupported: set[str] = set()
@@ -268,6 +308,8 @@ def compute(cls, ctx: AnalysisContext) -> CallGraph:
268308
sites[name] = ()
269309
continue
270310
owner = name.rsplit(".", 1)[0] if "." in name and name.rsplit(".", 1)[0] in classes else None
311+
if owner is not None and _is_staticmethod(function):
312+
owner = None # a static method has no receiver
271313
targets, symbols[name] = resolve_targets(
272314
ssa,
273315
scopes,
@@ -277,6 +319,7 @@ def compute(cls, ctx: AnalysisContext) -> CallGraph:
277319
classes,
278320
owner,
279321
_typed_with_module_classes(function, ssa, classes),
322+
bases,
280323
)
281324
found: list[CallSite] = []
282325
for block in ssa.blocks:

‎src/coretrace_python/interprocedural/summaries.py‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
ExternalSymbol,
3232
KnownFunction,
3333
UnknownTarget,
34+
_is_staticmethod,
3435
)
3536
from coretrace_python.ir.model import (
3637
BasicBlock,
@@ -125,6 +126,8 @@ class FunctionSummary:
125126
mutations: tuple[Mutation, ...] = ()
126127
side_effects: frozenset[str] = frozenset()
127128
nonlocal_writes: tuple[NonlocalWrite, ...] = ()
129+
# A ``@staticmethod``: ``Class.method(...)`` passes no receiver.
130+
static: bool = False
128131

129132

130133
class SummaryIndex:
@@ -322,7 +325,7 @@ def call(self, call: Call, state: dict[Key, Dep]) -> Dep:
322325
if project is None:
323326
project = self.project.get(target.symbol.attribute("__init__"))
324327
if project is not None:
325-
bound = self.receiver(call, project.name)
328+
bound = () if project.static else self.receiver(call, project.name)
326329
arguments = (*(self.deep(r, state) for r in bound), *arguments)
327330
return self.known(project, arguments, keywords, everything, call, state, bound)
328331
self.record(
@@ -337,9 +340,10 @@ def call(self, call: Call, state: dict[Key, Dep]) -> Dep:
337340
made = self.defs.get(call.callee)
338341
if isinstance(made, MakeFunction):
339342
arguments = (*arguments, *(self.deep(v, state) for v in made.captured))
340-
bound = self.receiver(call, target.name)
343+
callee_summary = self.table[target.name]
344+
bound = () if callee_summary.static else self.receiver(call, target.name)
341345
arguments = (*(self.deep(r, state) for r in bound), *arguments)
342-
return self.known(self.table[target.name], arguments, keywords, everything, call, state, bound)
346+
return self.known(callee_summary, arguments, keywords, everything, call, state, bound)
343347
assert isinstance(target, UnknownTarget)
344348
return everything | state.get(call.callee, EMPTY)
345349

@@ -357,7 +361,7 @@ def nonlocal_result(self, instruction: NonlocalResult, state: dict[Key, Dep]) ->
357361
made = self.defs.get(call.callee)
358362
if isinstance(made, MakeFunction):
359363
arguments = (*arguments, *(self.deep(v, state) for v in made.captured))
360-
bound = self.receiver(call, target.name)
364+
bound = () if callee.static else self.receiver(call, target.name)
361365
arguments = (*(self.deep(r, state) for r in bound), *arguments)
362366
keywords = EMPTY
363367
for value in (*call.starred, *(v for _, v in call.keywords)):
@@ -447,6 +451,7 @@ def summarize(
447451
table: Mapping[str, FunctionSummary],
448452
project: Mapping[SymbolId, FunctionSummary] | None = None,
449453
heap: HeapFacts | None = None,
454+
static: bool = False,
450455
) -> FunctionSummary:
451456
problem = _DependenceProblem(name, function, graph, table, project, heap)
452457
solution = solve(problem, cfg)
@@ -479,6 +484,7 @@ def summarize(
479484
for name, deps in sorted(problem.nonlocal_stores.items())
480485
if deps.parameters or deps.externals
481486
),
487+
static=static,
482488
)
483489

484490

@@ -509,7 +515,9 @@ def compute(cls, ctx: AnalysisContext) -> SummaryTable:
509515
while changed:
510516
changed = False
511517
for name, (ssa, cfg, heap) in supported.items():
512-
updated = summarize(name, ssa, cfg, graph, table, project, heap)
518+
updated = summarize(
519+
name, ssa, cfg, graph, table, project, heap, _is_staticmethod(graph.definitions[name])
520+
)
513521
if updated != table[name]:
514522
table[name] = updated
515523
changed = True

0 commit comments

Comments
 (0)