File size: 5,583 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
"""Static fallback for JavaScript driver code: is Cypher text built from run-time values?

Used only when Node is not available to run the code against the stand-in driver (drivergrade.call_node
is the primary check). `analyse(source)` looks at every template literal and string concatenation that
contains Cypher keywords:

    clean          no substitutions, or only constants / lookups in a constant table (an allowlist)
    interpolated   a substitution or `+` operand that is a plain identifier or member chain holding a
                   run-time value (`${sort}`, `${req.query.dir}`, `'...' + name`)
    unknown        anything else (a conditional expression, a function call)

Unit tests: test_js_params.py.
"""
from __future__ import annotations

import re

CYPHER = re.compile(r"\b(MATCH|RETURN|MERGE|WHERE|ORDER BY|DETACH|DELETE|UNWIND|LIMIT)\b")
MEMBER = r"[A-Za-z_$][\w$]*(?:\s*\??\.\s*[A-Za-z_$][\w$]*)*"


def scan(source: str) -> tuple[str, list[dict]]:
    """Returns (code with every string/template replaced by a numbered placeholder ⟦n⟧, literals).
    Each literal: {"kind": "string" | "template", "text": str, "subs": [expr, ...]}."""
    out, lits, i, n = [], [], 0, len(source)
    while i < n:
        c = source[i]
        if source.startswith("//", i):
            j = source.find("\n", i)
            i = n if j == -1 else j
        elif source.startswith("/*", i):
            j = source.find("*/", i + 2)
            i = n if j == -1 else j + 2
        elif c in "'\"":
            j = i + 1
            while j < n and source[j] != c:
                j += 2 if source[j] == "\\" else 1
            lits.append({"kind": "string", "text": source[i + 1:j], "subs": []})
            out.append(f"⟦{len(lits) - 1}⟧")
            i = j + 1
        elif c == "`":
            j, text, subs = i + 1, [], []
            while j < n and source[j] != "`":
                if source[j] == "\\":
                    text.append(source[j:j + 2])
                    j += 2
                elif source.startswith("${", j):
                    depth, k = 1, j + 2
                    while k < n and depth:
                        depth += source[k] == "{"
                        depth -= source[k] == "}"
                        k += 1
                    subs.append(source[j + 2:k - 1].strip())
                    text.append(" ⟨sub⟩ ")
                    j = k
                else:
                    text.append(source[j])
                    j += 1
            lits.append({"kind": "template", "text": "".join(text), "subs": subs})
            out.append(f"⟦{len(lits) - 1}⟧")
            i = j + 1
        else:
            out.append(c)
            i += 1
    return "".join(out), lits


def analyse(source: str) -> dict:
    code, lits = scan(source)
    constants, tables = set(), set()
    for m in re.finditer(r"\b(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*=\s*([^;\n]+)", code):
        name, rhs = m.group(1), m.group(2).strip()
        lit = re.fullmatch(r"⟦(\d+)⟧", rhs)
        if lit and not lits[int(lit.group(1))]["subs"]:
            constants.add(name)
        elif re.match(r"(Object\.freeze\(\s*)?[{\[]|new (Map|Set)\(", rhs):
            tables.add(name)

    def roots(expr: str) -> set[str]:
        expr = re.sub(r"⟦\d+⟧", " ", expr)
        expr = re.sub(r"\[[^\]]*\]", " ", expr)                      # what a table is indexed with does not matter
        expr = re.sub(r"\.\s*(get|has)\s*\([^)]*\)", " ", expr)
        return {m.group(1) for m in re.finditer(r"(?<![\w$.])([A-Za-z_$][\w$]*)", expr)} - {"undefined", "null", "true", "false"}

    derived = set()
    for _ in range(3):
        for m in re.finditer(r"\b(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*=\s*([^;\n]+)", code):
            r = roots(m.group(2))
            if r and r <= (constants | tables | derived) and (r & tables or r & derived):
                derived.add(m.group(1))
    clean_names = constants | derived

    defs = {m.group(1): m.group(2).strip()
            for m in re.finditer(r"\b(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*=\s*([^;\n]+)", code)}

    def classify(expr: str, depth: int = 0) -> str:
        expr = re.sub(r"\s+", " ", scan(expr)[0]).strip()
        r = roots(expr)
        if not r or r <= clean_names or (r <= (clean_names | tables) and re.search(r"[\[.]", expr)):
            return "clean"
        if re.fullmatch(MEMBER, expr):
            if expr in defs and depth < 4:      # a local name: as dirty as what it was assigned
                return classify(defs[expr], depth + 1)
            return "interpolated"
        return "unknown"

    findings = []
    for idx, lit in enumerate(lits):
        if not CYPHER.search(lit["text"]):
            continue
        verdicts = [classify(s) for s in lit["subs"]]
        for m in re.finditer(rf"⟦{idx}⟧\s*\+\s*({MEMBER})|({MEMBER})\s*\+\s*⟦{idx}⟧", code):
            operand = m.group(1) or m.group(2)
            if not re.fullmatch(r"⟦\d+⟧", operand):
                verdicts.append(classify(operand))
        level = "interpolated" if "interpolated" in verdicts else "unknown" if "unknown" in verdicts else "clean"
        findings.append({"text": lit["text"][:80].strip(), "subs": lit["subs"], "verdict": level})
    if not findings:
        verdict = "no-queries"
    elif any(f["verdict"] == "interpolated" for f in findings):
        verdict = "interpolated"
    elif all(f["verdict"] == "clean" for f in findings):
        verdict = "clean"
    else:
        verdict = "unknown"
    return {"verdict": verdict, "findings": findings}