Skip to content

Commit 2e2321e

Browse files
committed
feat: resolve project-returned objects and annotated parameters, add deserialization, redirect and CLI models
1 parent c95a4b9 commit 2e2321e

15 files changed

Lines changed: 231 additions & 36 deletions

File tree

‎README.md‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,19 @@ memory.
9696

9797
`--plugins` is a directory searched recursively for `plugin.toml` manifests and may be
9898
repeated. The repository ships a first set under `plugins/`: security models for the
99-
standard library, Flask, FastAPI, Django, SQLAlchemy and the Requests and httpx clients (`models/`), taint detectors for SQL
100-
injection, command injection, path traversal, SSRF, XSS and plaintext credential storage
101-
(`security/`), and syntactic
99+
standard library, Flask, FastAPI, Django, SQLAlchemy, the Requests and httpx clients, and the
100+
click and Typer command-line frameworks (`models/`), taint detectors for SQL
101+
injection, command injection, path traversal, SSRF, XSS, insecure deserialization, open
102+
redirects and plaintext credential storage (`security/`), and syntactic
102103
detectors for `eval`/`exec`, weak hashes, `app.run(debug=True)` and HTTP client calls
103104
without a timeout (`syntax/`). Route handlers of Flask and
104105
FastAPI receive their parameters as HTTP input, whether the application is created
105106
directly (`app = Flask(__name__)`) or by a project factory whose summary returns it
106107
(`app = create_app()`); `request.args` and its siblings are HTTP
107-
sources. A parameter named `password` or the like is a credential wherever it appears: stored
108+
sources; the parameters of a click or Typer command are `argv` input. SQL sinks read the
109+
statement only, so a tainted value in the parameter tuple of a parameterised query is not an
110+
injection, and an annotated parameter denotes its class, so `db: Session = Depends(get_db)`
111+
gives `db.execute` its sink. A parameter named `password` or the like is a credential wherever it appears: stored
108112
in a database without passing through a hashing function it is a
109113
`plaintext-credential-storage` finding at medium confidence, since a name is a hint.
110114
Django views are undecorated, so a parameter annotated with a request class

‎plugins/models/cli/cli_models.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""Command-line entry points: the parameters of click and Typer commands are argv input."""
2+
3+
from __future__ import annotations
4+
5+
from typing import ClassVar
6+
7+
from coretrace_python.plugins import ModelPlugin
8+
from coretrace_python.semantic.symbols import SymbolId
9+
from coretrace_python.taint import EntryPoint, Model, TaintKind
10+
11+
# A command-line tool is expected to open the paths it is given: argv input carries every
12+
# kind but PATH.
13+
ARGV_KINDS = TaintKind.ALL & ~TaintKind.PATH
14+
15+
_COMMANDS = (
16+
"click.command",
17+
"click.group",
18+
"click.command.command",
19+
"click.group.command",
20+
"click.group.group",
21+
"click.Group.command",
22+
"typer.Typer.command",
23+
"typer.Typer.callback",
24+
)
25+
26+
27+
def _sym(path: str) -> SymbolId:
28+
return SymbolId(f"python.{path}")
29+
30+
31+
class CliModels(ModelPlugin):
32+
name: ClassVar[str] = "cli-models"
33+
models: ClassVar[tuple[Model, ...]] = tuple(
34+
EntryPoint(_sym(command), "argv", ARGV_KINDS) for command in _COMMANDS
35+
)

‎plugins/models/cli/plugin.toml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name = "cli-models"
2+
version = "1.0.0"
3+
plugin_api = ">=1,<2"
4+
requires = []
5+
provides = ["model.cli-sources"]
6+
7+
[entrypoint]
8+
module = "cli_models"
9+
class = "CliModels"

‎plugins/models/django/django_models.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ class DjangoModels(ModelPlugin):
8686
*(TypedParameter(_sym(cls), "http") for cls in _REQUEST_CLASSES),
8787
*(EntryPoint(_sym(base), "http") for base in _VIEW_BASES),
8888
*(EntryPoint(_sym(decorator), "http") for decorator in _VIEW_DECORATORS),
89-
Sink(_sym("django.db.connection.cursor.execute"), TaintKind.SQL | TaintKind.CREDENTIAL),
90-
Sink(_sym("django.db.connection.cursor.executemany"), TaintKind.SQL | TaintKind.CREDENTIAL),
89+
Sink(_sym("django.db.connection.cursor.execute"), TaintKind.SQL | TaintKind.CREDENTIAL, ((TaintKind.SQL, (0,)),)),
90+
Sink(_sym("django.db.connection.cursor.executemany"), TaintKind.SQL | TaintKind.CREDENTIAL, ((TaintKind.SQL, (0,)),)),
9191
Sink(_sym("django.db.models.expressions.RawSQL"), TaintKind.SQL | TaintKind.CREDENTIAL),
9292
Sink(_sym("django.db.models.Manager.raw"), TaintKind.SQL | TaintKind.CREDENTIAL),
9393
Sink(_sym("django.db.models.QuerySet.raw"), TaintKind.SQL | TaintKind.CREDENTIAL),
@@ -97,6 +97,9 @@ class DjangoModels(ModelPlugin):
9797
Sink(_sym("django.http.response.HttpResponse"), TaintKind.HTML),
9898
Sink(_sym("django.template.Template"), TaintKind.HTML),
9999
Sink(_sym("django.http.FileResponse"), TaintKind.PATH),
100+
Sink(_sym("django.shortcuts.redirect"), TaintKind.REDIRECT),
101+
Sink(_sym("django.http.HttpResponseRedirect"), TaintKind.REDIRECT),
102+
Sink(_sym("django.http.HttpResponsePermanentRedirect"), TaintKind.REDIRECT),
100103
Sanitizer(_sym("django.utils.html.escape"), TaintKind.HTML),
101104
Sanitizer(_sym("django.utils.html.conditional_escape"), TaintKind.HTML),
102105
*(AuthorizationGuard(_sym(decorator), label) for decorator, label in _AUTHORIZATION_DECORATORS),

‎plugins/models/fastapi/fastapi_models.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ class FastApiModels(ModelPlugin):
2222
*(EntryPoint(_sym(f"fastapi.APIRouter.{method}"), "http") for method in _METHODS),
2323
Sink(_sym("fastapi.responses.HTMLResponse"), TaintKind.HTML),
2424
Sink(_sym("fastapi.responses.FileResponse"), TaintKind.PATH),
25+
Sink(_sym("fastapi.responses.RedirectResponse"), TaintKind.REDIRECT),
26+
Sink(_sym("starlette.responses.RedirectResponse"), TaintKind.REDIRECT),
2527
)

‎plugins/models/flask/flask_models.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class FlaskModels(ModelPlugin):
3737
Sink(_sym("flask.Response"), TaintKind.HTML),
3838
Sink(_sym("flask.Markup"), TaintKind.HTML),
3939
Sink(_sym("flask.send_file"), TaintKind.PATH),
40+
Sink(_sym("flask.redirect"), TaintKind.REDIRECT),
41+
Sink(_sym("werkzeug.utils.redirect"), TaintKind.REDIRECT),
4042
Sink(_sym("flask.request.files.save"), TaintKind.PATH),
4143
Sanitizer(_sym("werkzeug.utils.secure_filename"), TaintKind.PATH),
4244
Sanitizer(_sym("flask.escape"), TaintKind.HTML),

‎plugins/models/python_stdlib/python_stdlib.py‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class PythonStdlibModels(ModelPlugin):
1818
models: ClassVar[tuple[Model, ...]] = (
1919
Source(_sym("builtins.input"), "stdin"),
2020
Source(_sym("sys.stdin"), "stdin"),
21-
Source(_sym("sys.argv"), "argv"),
21+
# A command-line tool is expected to open the paths it is given.
22+
Source(_sym("sys.argv"), "argv", TaintKind.ALL & ~TaintKind.PATH),
2223
Source(_sym("os.environ"), "environment"),
2324
Source(_sym("subprocess.run.stdout"), "process-output"),
2425
Source(_sym("subprocess.check_output"), "process-output"),
@@ -40,12 +41,27 @@ class PythonStdlibModels(ModelPlugin):
4041
Sink(_sym("shutil.rmtree"), TaintKind.PATH),
4142
Sink(_sym("urllib.request.urlopen"), TaintKind.SSRF),
4243
*(
43-
Sink(_sym(f"sqlite3.connect{cursor}.{method}"), TaintKind.SQL | TaintKind.CREDENTIAL)
44+
Sink(
45+
_sym(f"sqlite3.connect{cursor}.{method}"),
46+
TaintKind.SQL | TaintKind.CREDENTIAL,
47+
((TaintKind.SQL, (0,)),),
48+
)
4449
for cursor in ("", ".cursor")
4550
for method in ("execute", "executemany", "executescript")
4651
),
4752
Sanitizer(_sym("shlex.quote"), TaintKind.COMMAND),
4853
Sanitizer(_sym("html.escape"), TaintKind.HTML),
54+
Sink(_sym("pickle.loads"), TaintKind.DESERIALIZATION),
55+
Sink(_sym("pickle.load"), TaintKind.DESERIALIZATION),
56+
Sink(_sym("pickle.Unpickler"), TaintKind.DESERIALIZATION),
57+
Sink(_sym("marshal.loads"), TaintKind.DESERIALIZATION),
58+
Sink(_sym("marshal.load"), TaintKind.DESERIALIZATION),
59+
Sink(_sym("shelve.open"), TaintKind.DESERIALIZATION),
60+
Sink(_sym("dill.loads"), TaintKind.DESERIALIZATION),
61+
Sink(_sym("jsonpickle.decode"), TaintKind.DESERIALIZATION),
62+
Sink(_sym("yaml.load"), TaintKind.DESERIALIZATION),
63+
Sink(_sym("yaml.unsafe_load"), TaintKind.DESERIALIZATION),
64+
Sink(_sym("yaml.full_load"), TaintKind.DESERIALIZATION),
4965
Sanitizer(_sym("os.path.basename"), TaintKind.PATH),
5066
Validator(_sym("re.fullmatch"), argument=1),
5167
Validator(_sym("re.compile.fullmatch")),

‎plugins/models/sqlalchemy/sqlalchemy_models.py‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from coretrace_python.plugins import ModelPlugin
1313
from coretrace_python.semantic.symbols import SymbolId
14-
from coretrace_python.taint import Model, Sink, TaintKind
14+
from coretrace_python.taint import Model, Sanitizer, Sink, TaintKind
1515

1616
_EXECUTORS = (
1717
"sqlalchemy.create_engine",
@@ -25,14 +25,23 @@
2525
)
2626

2727

28+
_TEXT_MODULES = ("sqlalchemy", "sqlalchemy.sql", "sqlalchemy.sql.expression")
29+
30+
2831
def _sym(path: str) -> SymbolId:
2932
return SymbolId(f"python.{path}")
3033

3134

3235
class SqlAlchemyModels(ModelPlugin):
3336
name: ClassVar[str] = "sqlalchemy-models"
34-
models: ClassVar[tuple[Model, ...]] = tuple(
35-
Sink(_sym(f"{executor}.{method}"), TaintKind.SQL | TaintKind.CREDENTIAL)
36-
for executor in _EXECUTORS
37-
for method in ("execute", "exec_driver_sql", "scalar", "scalars")
37+
models: ClassVar[tuple[Model, ...]] = (
38+
*(
39+
Sink(_sym(f"{executor}.{method}"), TaintKind.SQL | TaintKind.CREDENTIAL, ((TaintKind.SQL, (0,)),))
40+
for executor in _EXECUTORS
41+
for method in ("execute", "exec_driver_sql", "scalar", "scalars")
42+
),
43+
# ``text()`` is where SQL text is committed: a sink, and its result carries no
44+
# further SQL taint so the ``execute`` that follows does not report it twice.
45+
*(Sink(_sym(f"{module}.text"), TaintKind.SQL) for module in _TEXT_MODULES),
46+
*(Sanitizer(_sym(f"{module}.text"), TaintKind.SQL) for module in _TEXT_MODULES),
3847
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Insecure deserialization: attacker-controlled input reaching a insecure deserialization sink."""
2+
3+
from __future__ import annotations
4+
5+
from typing import ClassVar
6+
7+
from coretrace_python.findings import Severity
8+
from coretrace_python.plugins import TaintDetector
9+
from coretrace_python.taint import TaintKind
10+
11+
12+
class InsecureDeserializationPlugin(TaintDetector):
13+
name: ClassVar[str] = "insecure-deserialization"
14+
rule_id: ClassVar[str] = "insecure-deserialization"
15+
kind: ClassVar[TaintKind] = TaintKind.DESERIALIZATION
16+
severity: ClassVar[Severity] = Severity.CRITICAL
17+
title: ClassVar[str] = "Insecure deserialization"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name = "insecure-deserialization"
2+
version = "1.0.0"
3+
plugin_api = ">=1,<2"
4+
requires = ["taint.flows", "findings.refutation"]
5+
provides = ["vulnerability.insecure-deserialization"]
6+
7+
[entrypoint]
8+
module = "insecure_deserialization"
9+
class = "InsecureDeserializationPlugin"

0 commit comments

Comments
 (0)