about summary refs log tree commit diff
path: root/test/fuzz_bnw.py
blob: d76da4f0998241289180c261c043c8d568091b22 (plain)
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
#!/usr/bin/env python3
"""Exhaustive small-payload fuzzing of the BNW GN2 entry point.

Generates small (7-sample) datasets across the input space -- sizes,
data types, special characters, missing data, separators -- posts each
to /bn_genenet.php and classifies the response:

  PREPARED    prepared page rendered (input accepted)
  REJECTED    clear validation error page ("could not be used")
  FAILED      structure-learning failure page (after clicking through)
  PHP-FATAL   PHP fatal/warning text leaked into the page
  HTTP-ERR    non-200 response
  OTHER       anything else (investigate!)

Usage: BASE_URL=http://127.0.0.1:8881 python3 fuzz_bnw.py
"""
import itertools
import os
import random
import sys
import time
import urllib.error
import urllib.parse
import urllib.request

BASE_URL = os.environ.get("BASE_URL", "http://127.0.0.1:8881").rstrip("/")

def post_gn2(payload, key, timeout=120):
    data = urllib.parse.urlencode({"My_Gennenet": payload,
                                   "My_key": key}).encode()
    req = urllib.request.Request(BASE_URL + "/bn_genenet.php", data=data,
                                 headers={"User-Agent": "bnw-fuzz/1.0"})
    try:
        r = urllib.request.urlopen(req, timeout=timeout)
        return r.status, r.read().decode(errors="replace")
    except urllib.error.HTTPError as e:
        return e.code, e.read().decode(errors="replace")
    except Exception as e:
        return -1, str(e)

def run_learning(key, timeout=300):
    """click through: progress -> execute -> layout; classify the end"""
    def get(path):
        req = urllib.request.Request(f"{BASE_URL}{path}",
                                     headers={"User-Agent": "bnw-fuzz/1.0"})
        try:
            r = urllib.request.urlopen(req, timeout=timeout)
            return r.status, r.read().decode(errors="replace")
        except urllib.error.HTTPError as e:
            return e.code, e.read().decode(errors="replace")
        except Exception as e:
            return -1, str(e)
    get(f"/executionprogress_default.php?My_key={key}")
    st, body = get(f"/execute_bn_gom.php?My_key={key}")
    if st != 200:
        return f"HTTP-ERR {st}", body[:200]
    if "Structure learning FAILED" in body:
        # informative failure page (works as designed) -- but still a
        # learning failure for a payload that passed validation
        return "FAILED", body
    if "Fatal error" in body or "Uncaught" in body:
        return "PHP-FATAL", body
    st2, body2 = get(f"/layout_svg_no.php?My_key={key}")
    if st2 == 200 and "<svg" in body2:
        return "OK-NETWORK", ""
    return "OTHER", body2[:400]

def classify(status, body):
    if status != 200:
        return f"HTTP-ERR {status}"
    if "Fatal error" in body or "Uncaught" in body or "Warning:" in body.split("var data1")[0]:
        return "PHP-NOISE"
    if "could not be used" in body:
        return "REJECTED"
    if "Perform Bayesian network modeling" in body:
        return "PREPARED"
    return "OTHER"

# ---------------------------------------------------------------- cases
def cases():
    random.seed(42)
    C = []

    def payload(names, rows, sep=";", eol=""):
        head = ",".join("_" + n for n in names)
        body = sep.join(",".join(str(v) for v in row) for row in rows)
        return f"{head}{sep}{body}{eol}"

    # --- 1. size grid: variables x samples (7 samples default) --------
    for nv in [2, 3, 5, 7, 10, 15, 20]:
        for ns in [7]:
            rows = [[round(random.gauss(0, 1), 3) for _ in range(nv)]
                    for _ in range(ns)]
            C.append((f"size {nv}v x {ns}s", payload([f"v{i}" for i in range(nv)], rows)))
    # samples varied, 3 variables
    for ns in [2, 3, 4, 7, 12, 30, 100]:
        rows = [[round(random.gauss(0, 1), 3) for _ in range(3)]
                for _ in range(ns)]
        C.append((f"size 3v x {ns}s", payload(["a", "b", "c"], rows)))

    # --- 2. data types -------------------------------------------------
    vals = {
        "ints":          [1, 2, 3, 4, 5, 6, 7],
        "neg-floats":    [-1.5, -2.3, 0.1, 4.7, -0.001, 3.14, -7.7],
        "scientific":    [1e-5, 2.5e3, -3e-2, 1e10, 5e-1, 2e2, -1e3],
        "big":           [1e15, -1e15, 0.0, 1e6, -1e6, 123456.789, 42],
        "zeros":         [0, 0, 0, 1, 0, 0, 0],
        "tiny-variance": [1.0001, 1.0002, 1.0003, 1.0004, 1.0005, 1.0006, 1.0007],
        "all-same":      [5.0] * 7,
        "mixed-int-float": [1, 2.5, 3, 4.5, 5, 6.5, 7],
        "trailing-zeros": ["1.10", "2.20", "3.30", "4.40", "5.50", "6.60", "7.70"],
        "leading-plus":  ["+1.5", "+2.5", "+3.5", "+4.5", "+5.5", "+6.5", "+7.5"],
        "no-leading-zero": [".5", ".25", ".75", ".125", ".625", ".875", ".375"],
    }
    for name, col in vals.items():
        rows = [[col[i], col[(i + 3) % 7], col[(i + 5) % 7]] for i in range(7)]
        C.append((f"datatype {name}", payload(["x", "y", "z"], rows)))

    # --- 3. missing data -------------------------------------------------
    base = [[float(7 * r + c) for c in range(3)] for r in range(7)]
    # scattered NA
    rows = [r[:] for r in base]; rows[2][1] = "NA"; rows[5][0] = "NA"
    C.append(("NA scattered (2 cells)", payload(["a", "b", "c"], rows)))
    # one all-NA row (prep removes it)
    rows = [r[:] for r in base]; rows[3] = ["NA"] * 3
    C.append(("one all-NA row (6 left)", payload(["a", "b", "c"], rows)))
    # one all-NA column (prep removes the variable?)
    rows = [r[:] for r in base]
    for r in rows: r[1] = "NA"
    C.append(("one all-NA column", payload(["a", "b", "c"], rows)))
    # many NAs
    rows = [r[:] for r in base]
    for i, r in enumerate(rows): r[i % 3] = "NA"
    C.append(("NA in every row", payload(["a", "b", "c"], rows)))
    # NA variants
    rows = [r[:] for r in base]; rows[0] = ["na", "N/A", "nan"]
    C.append(("NA spelling variants", payload(["a", "b", "c"], rows)))
    # empty cells
    rows = [r[:] for r in base]; rows[1][2] = ""
    C.append(("empty cell", payload(["a", "b", "c"], rows)))

    # --- 4. names / special characters (7 samples, 3 vars) ---------------
    col = [[1, 2, 3, 4, 5, 6, 7][i:i + 1][0] for i in range(7)]
    colv = [[float(r) for r in range(7)], [float(r * 2) for r in range(7)],
            [float(r * 3) for r in range(7)]]
    name_sets = {
        "spaces":        ["Arf1 mouse", "Hiat1 rat", "Cdp1 worm"],
        "multi-spaces":  ["A  B", "C   D", "E    F"],
        "colons":        ["Chr11: 59.2", "Chr13: 118.3", "Chr5: 3.1"],
        "parens":        ["ADP-rib (verified)", "factor 1 (yes)", "X (maybe)"],
        "percent":       ["%gc", "a%b", "c%d"],
        "quotes":        ['a"b', "c'd", 'e"f'],
        "backslash":     ["a\\b", "c\\d", "e\\f"],
        "slash":         ["a/b", "c/d", "e/f"],
        "unicode":       ["mõus", "ra†", "wørm"],
        "leading-digits": ["1abc", "2def", "3ghi"],
        "very-long":     ["X" * 200, "Y" * 200, "Z" * 200],
        "single-chars":  ["a", "b", "c"],
        "dots":          ["a.b", "c.d", "e.f"],
        "dashes":        ["a-b", "c-d", "e-f"],
        "underscore":    ["a_b", "c_d", "e_f"],
        "numeric-names": ["123", "456", "789"],
        "empty-name":    ["", "b", "c"],
        "duplicates-2":  ["dup", "other", "dup"],
        "duplicates-3":  ["dup", "dup", "dup"],
        "mixed-case":    ["Arf1", "ARF1", "arf1"],
    }
    for name, names in name_sets.items():
        rows = [[colv[j][i] for j in range(3)] for i in range(7)]
        C.append((f"names {name}", payload(names, rows)))

    # --- 5. separators / structure ---------------------------------------
    good = payload(["a", "b", "c"],
                   [[float(7 * r + c) for c in range(3)] for r in range(7)])
    C.append(("CRLF line endings", good.replace("\n", "\r\n")))
    C.append(("CR-only line endings", good.replace("\n", "\r")))
    C.append(("double semicolons", good.replace(";", ";;")))
    C.append(("trailing semicolon+newline", good + ";\n"))
    C.append(("semicolon+newline per row",
              payload(["a", "b", "c"],
                      [[float(7 * r + c) for c in range(3)] for r in range(7)],
                      eol=";\n")))
    C.append(("no trailing semicolon", good.rstrip(";")))
    C.append(("spaces after commas", good.replace(",", ", ")))
    C.append(("leading whitespace rows", good.replace(";", ";\n ")))
    C.append(("tabs already in payload", good.replace(",", "\t")))
    C.append(("trailing tab per row", good.replace(";", "\t;")))
    C.append(("BOM prefix", "" + good))
    C.append(("blank lines between rows", good.replace(";", ";\n\n")))

    # --- 6. degenerate / should-reject ------------------------------------
    C.append(("empty string", ""))
    C.append(("only semicolons", ";;;"))
    C.append(("only commas", ",,,,"))
    C.append(("header only", "_a,_b,_c;"))
    C.append(("one variable", "_a;1;2;3;4;5;6;7"))
    C.append(("one sample", "_a,_b;1,2"))
    C.append(("numeric header", "1,2,3;4,5,6;7,8,9"))
    C.append(("ragged short row", "_a,_b,_c;1,2,3;4,5;7,8,9;1,2,3;4,5,6;7,8,9;1,2,3"))
    C.append(("ragged long row", "_a,_b,_c;1,2,3;4,5,6,7;7,8,9;1,2,3;4,5,6;7,8,9;1,2,3"))
    C.append(("non-numeric cell", "_a,_b,_c;1,2,3;4,x,6;7,8,9;1,2,3;4,5,6;7,8,9;1,2,3"))
    C.append(("all NA rows", "_a,_b,_c;NA,NA,NA;NA,NA,NA"))
    C.append(("huge payload 50v x 7s",
              payload([f"g{i}" for i in range(50)],
                      [[round(random.gauss(0, 1), 3) for _ in range(50)]
                       for _ in range(7)])))
    return C

def main():
    do_learning = os.environ.get("FUZZ_LEARNING", "0") == "1"
    only = sys.argv[1] if len(sys.argv) > 1 else None
    results = {}
    details = {}
    n = 0
    for name, pl in cases():
        if only and only not in name:
            continue
        n += 1
        key = f"f{n % 100:02d}"
        st, body = post_gn2(pl, key)
        cat = classify(st, body)
        if cat == "PREPARED" and do_learning:
            cat, extra = run_learning(key)
            if cat not in ("OK-NETWORK", "FAILED"):
                details[name] = extra
        results.setdefault(cat, []).append(name)
        if cat not in ("PREPARED", "REJECTED", "OK-NETWORK", "FAILED"):
            details[name] = f"status={st} " + body[:300]
        print(f"{cat:<12} {name}", flush=True)
    print("\n=== SUMMARY ===")
    for cat in sorted(results):
        print(f"{cat:<12} {len(results[cat]):3d}  {', '.join(results[cat][:6])}{' ...' if len(results[cat])>6 else ''}")
    if details:
        print("\n=== DETAILS (unexpected) ===")
        for k, v in details.items():
            print(f"--- {k}\n{v}\n")

if __name__ == "__main__":
    main()