File size: 13,001 Bytes
b06a783
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
"""Is query text in Python source built from constants, or from values that arrive at run time?

`analyse(source)` finds every call that hands query text to a database (`.run`, `.execute`,
`.execute_query`, `.executemany`, `Query(...)`) and classifies the text argument:

    clean          only string constants, constants joined together, or lookups in a constant
                   table (an allowlist); placeholders generated from a count ("?" * len(ids))
    interpolated   an f-string, `+`, `%`, `.format` or `.join` that folds a run-time value into the text
    unknown        cannot tell (the text comes from a parameter or an unresolved call)

Wrapping a value in int()/float()/bool() before formatting it is reported as clean with
`numeric: true`: it cannot inject, and the cases do not grade it.

The analysis is flow-insensitive: a name is as dirty as its dirtiest assignment in the function.
Unit tests: test_py_params.py.
"""
from __future__ import annotations

import ast

SINKS = {"run", "execute", "execute_query", "executemany", "executescript", "execute_read", "execute_write"}
ORDER = {"clean": 0, "numeric": 1, "unknown": 2, "interpolated": 3}
PASS_THROUGH = {"strip", "lstrip", "rstrip", "upper", "lower", "dedent", "removeprefix", "removesuffix", "copy",
                "keys", "values", "items", "splitlines", "title"}


def worst(*levels: str) -> str:
    return max(levels, key=ORDER.__getitem__) if levels else "clean"


def _build(level: str) -> str:
    """Folding a non-constant into text makes the text interpolated."""
    return "interpolated" if level in ("unknown", "interpolated") else level


class _Scope:
    def __init__(self, module_env: dict, fn_returns: dict):
        self.env = dict(module_env)
        self.fn_returns = fn_returns

    def classify(self, node) -> str:
        env = self.env
        if node is None:
            return "clean"
        if isinstance(node, ast.Constant):
            return "clean"
        if isinstance(node, ast.Name):
            return env.get(node.id, "unknown")
        if isinstance(node, ast.JoinedStr):
            parts = [self.classify(v.value) for v in node.values if isinstance(v, ast.FormattedValue)]
            return _build(worst(*parts)) if parts else "clean"
        if isinstance(node, ast.FormattedValue):
            return self.classify(node.value)
        if isinstance(node, ast.BinOp):
            left, right = self.classify(node.left), self.classify(node.right)
            if isinstance(node.op, ast.Mult):  # "?" * len(ids), ["?"] * n
                return worst(*(x for x in (left, right) if x != "numeric")) if "numeric" in (left, right) else worst(left, right)
            if isinstance(node.op, (ast.Add, ast.Mod)):
                return _build(worst(left, right))
            return worst(left, right)
        if isinstance(node, (ast.List, ast.Tuple, ast.Set)):
            return worst(*(self.classify(e) for e in node.elts))
        if isinstance(node, ast.Dict):
            return worst(*(self.classify(v) for v in node.values))
        if isinstance(node, ast.IfExp):
            return worst(self.classify(node.body), self.classify(node.orelse))
        if isinstance(node, ast.BoolOp):
            return worst(*(self.classify(v) for v in node.values))
        if isinstance(node, ast.Starred):
            return self.classify(node.value)
        if isinstance(node, ast.Subscript):
            base = self.classify(node.value)
            if base == "clean":  # lookup in a constant table: the result is one of its constants
                return "clean"
            return base
        if isinstance(node, (ast.ListComp, ast.GeneratorExp, ast.SetComp)):
            saved = dict(env)
            for gen in node.generators:
                level = self.classify(gen.iter)
                if isinstance(gen.iter, ast.Call) and isinstance(gen.iter.func, ast.Name) and gen.iter.func.id in ("range", "enumerate"):
                    level = "numeric"
                for name in ast.walk(gen.target):
                    if isinstance(name, ast.Name):
                        env[name.id] = level
            result = self.classify(node.elt)
            self.env = saved
            return result
        if isinstance(node, ast.Call):
            return self._call(node)
        if isinstance(node, ast.Attribute):
            if isinstance(node.value, ast.Name) and node.attr.isupper():
                return self.env.get(node.attr, "clean" if node.value.id in ("self", "cls") else "unknown")
            return "unknown"
        return "unknown"

    def _call(self, node: ast.Call) -> str:
        f = node.func
        args = [self.classify(a) for a in node.args] + [self.classify(k.value) for k in node.keywords]
        if isinstance(f, ast.Name):
            if f.id in ("int", "float", "bool", "len", "abs", "round", "min", "max"):
                return "numeric"
            if f.id in ("str", "list", "tuple", "sorted", "set", "dict", "repr"):
                return worst(*args)
            if f.id in self.fn_returns:
                return self.fn_returns[f.id]
            return "unknown"
        if isinstance(f, ast.Attribute):
            base = self.classify(f.value)
            if f.attr == "join":
                return _build(worst(base, *args)) if worst(base, *args) != "numeric" else "clean"
            if f.attr == "format":
                return _build(worst(base, *args))
            if f.attr == "get":      # TABLE.get(key, DEFAULT): one of the table's constants or the default
                return worst(base, *args[1:]) if base == "clean" else worst(base, *args)
            if f.attr == "replace":
                return _build(worst(base, *args)) if worst(*args) != "clean" else base
            if f.attr in PASS_THROUGH:
                return worst(base, *args)
            if isinstance(f.value, ast.Name) and f.value.id == "textwrap":
                return worst(*args)
        return "unknown"


def _assignments(body_nodes, scope: _Scope, params: set[str]) -> None:
    """Fill scope.env for one function (or the module) by iterating to a fixed point."""
    for p in params:
        scope.env[p] = "unknown"
    stores: list[tuple[str, object, str]] = []  # (name, value node, how)
    for node in body_nodes:
        for sub in ast.walk(node):
            if isinstance(sub, (ast.FunctionDef, ast.AsyncFunctionDef, ast.Lambda)) and sub is not node:
                a = sub.args
                for arg in a.args + a.kwonlyargs + a.posonlyargs + ([a.vararg] if a.vararg else []) + ([a.kwarg] if a.kwarg else []):
                    scope.env.setdefault(arg.arg, "unknown")
            if isinstance(sub, ast.Assign):
                for target in sub.targets:
                    if isinstance(target, ast.Name):
                        stores.append((target.id, sub.value, "set"))
                    elif isinstance(target, (ast.Tuple, ast.List)):
                        values = sub.value.elts if isinstance(sub.value, (ast.Tuple, ast.List)) and len(sub.value.elts) == len(target.elts) else None
                        for k, elt in enumerate(target.elts):
                            if isinstance(elt, ast.Name):
                                stores.append((elt.id, values[k] if values else sub.value, "set"))
                    elif isinstance(target, ast.Subscript) and isinstance(target.value, ast.Name):
                        stores.append((target.value.id, sub.value, "item"))
            elif isinstance(sub, ast.AnnAssign) and isinstance(sub.target, ast.Name) and sub.value is not None:
                stores.append((sub.target.id, sub.value, "set"))
            elif isinstance(sub, ast.AugAssign) and isinstance(sub.target, ast.Name):
                stores.append((sub.target.id, sub.value, "aug"))
            elif isinstance(sub, ast.NamedExpr) and isinstance(sub.target, ast.Name):
                stores.append((sub.target.id, sub.value, "set"))
            elif isinstance(sub, (ast.For, ast.AsyncFor)):
                for name in ast.walk(sub.target):
                    if isinstance(name, ast.Name):
                        stores.append((name.id, sub.iter, "set"))
            elif isinstance(sub, ast.Call) and isinstance(sub.func, ast.Attribute) and isinstance(sub.func.value, ast.Name) \
                    and sub.func.attr in ("append", "extend", "add", "insert", "update") and sub.args:
                stores.append((sub.func.value.id, sub.args[-1], "item"))
            elif isinstance(sub, (ast.With, ast.AsyncWith)):
                for item in sub.items:
                    if isinstance(item.optional_vars, ast.Name):
                        scope.env[item.optional_vars.id] = "unknown"
    assigned = {name for name, _, how in stores if how == "set"}
    current = {name: "clean" for name in assigned - params}
    scope.env.update(current)
    for _ in range(6):
        changed = False
        for name, value, how in stores:
            level = scope.classify(value)
            if how == "aug" and isinstance(value, (ast.JoinedStr, ast.BinOp, ast.Call, ast.Name)):
                level = _build(level) if level != "clean" else level
            new = worst(scope.env.get(name, "clean"), level)
            if scope.env.get(name) != new:
                scope.env[name] = new
                changed = True
        if not changed:
            break


def analyse(source: str) -> dict:
    """Returns {"verdict": clean|interpolated|unknown|no-queries|syntax-error, "sinks": [...]}."""
    try:
        tree = ast.parse(source)
    except SyntaxError as exc:
        return {"verdict": "syntax-error", "sinks": [], "error": str(exc)}
    module_scope = _Scope({}, {})
    top = [n for n in tree.body if not isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef))]
    class_level = [s for n in tree.body if isinstance(n, ast.ClassDef) for s in n.body
                   if not isinstance(s, (ast.FunctionDef, ast.AsyncFunctionDef))]
    _assignments(top + class_level, module_scope, set())
    module_env = dict(module_scope.env)

    functions = [n for n in ast.walk(tree) if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef))]
    nested = {id(sub) for fn in functions for sub in ast.walk(fn)
              if sub is not fn and isinstance(sub, (ast.FunctionDef, ast.AsyncFunctionDef))}
    units = [fn for fn in functions if id(fn) not in nested]

    def fn_params(fn) -> set[str]:
        a = fn.args
        return {x.arg for x in a.args + a.kwonlyargs + a.posonlyargs} | ({a.vararg.arg} if a.vararg else set()) | (
            {a.kwarg.arg} if a.kwarg else set())

    fn_returns: dict[str, str] = {}
    for _ in range(3):  # helpers that build fragments: classify what they return
        for fn in units:
            scope = _Scope(module_env, fn_returns)
            _assignments(fn.body, scope, fn_params(fn))
            levels = [scope.classify(r.value) for r in ast.walk(fn) if isinstance(r, ast.Return) and r.value is not None]
            fn_returns[fn.name] = worst(*levels) if levels else "unknown"

    sinks = []

    def scan(nodes, scope: _Scope, where: str):
        for node in nodes:
            for sub in ast.walk(node):
                if not isinstance(sub, ast.Call):
                    continue
                is_sink = (isinstance(sub.func, ast.Attribute) and sub.func.attr in SINKS) or (
                    isinstance(sub.func, ast.Name) and sub.func.id == "Query")
                if not is_sink or not sub.args:
                    continue
                arg = sub.args[0]
                if isinstance(arg, (ast.Lambda, ast.Name)) and isinstance(sub.func, ast.Attribute) \
                        and sub.func.attr in ("execute_read", "execute_write") :
                    continue  # a transaction function, not query text
                level = scope.classify(arg)
                verdict = "clean" if level in ("clean", "numeric") else level
                sinks.append({"line": sub.lineno, "in": where, "call": getattr(sub.func, "attr", "Query"),
                              "verdict": verdict, "numeric": level == "numeric",
                              "has_parameters": len(sub.args) > 1 or bool(sub.keywords)})

    for fn in units:
        scope = _Scope(module_env, fn_returns)
        _assignments(fn.body, scope, fn_params(fn))
        scan(fn.body, scope, fn.name)
    scan(top, _Scope(module_env, fn_returns), "<module>")

    if not sinks:
        verdict = "no-queries"
    elif any(s["verdict"] == "interpolated" for s in sinks):
        verdict = "interpolated"
    elif all(s["verdict"] == "clean" for s in sinks):
        verdict = "clean"
    else:
        verdict = "unknown"
    return {"verdict": verdict, "sinks": sinks}


def same_code(a: str, b: str) -> bool:
    """True when two sources are the same program: comments, blank lines and formatting are ignored."""
    try:
        return ast.dump(ast.parse(a)) == ast.dump(ast.parse(b))
    except SyntaxError:
        return False