|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Fuzz testing: feeds random/mutated inputs to the MCP server and CLI |
| 5 | +# to find crashes, hangs, and memory errors. Runs for a limited time. |
| 6 | +# |
| 7 | +# Usage: scripts/security-fuzz-random.sh <binary-path> [duration_seconds] |
| 8 | + |
| 9 | +BINARY="${1:?usage: security-fuzz-random.sh <binary-path> [duration_seconds]}" |
| 10 | +DURATION="${2:-60}" |
| 11 | + |
| 12 | +if [[ ! -f "$BINARY" ]]; then |
| 13 | + echo "FAIL: binary not found: $BINARY" |
| 14 | + exit 1 |
| 15 | +fi |
| 16 | + |
| 17 | +echo "=== Fuzz Testing ($DURATION seconds) ===" |
| 18 | + |
| 19 | +FUZZ_TMPDIR=$(mktemp -d) |
| 20 | +trap 'rm -rf "$FUZZ_TMPDIR"' EXIT |
| 21 | + |
| 22 | +CRASHES=0 |
| 23 | +ITERATIONS=0 |
| 24 | +END_TIME=$((SECONDS + DURATION)) |
| 25 | + |
| 26 | +# Portable timeout |
| 27 | +run_with_timeout() { |
| 28 | + local secs="$1"; shift |
| 29 | + if command -v timeout &>/dev/null; then |
| 30 | + timeout "$secs" "$@" || true |
| 31 | + else |
| 32 | + perl -e "alarm($secs); exec @ARGV" -- "$@" || true |
| 33 | + fi |
| 34 | +} |
| 35 | + |
| 36 | +# ── Phase 1: Random JSON-RPC mutations ─────────────────────── |
| 37 | +echo "" |
| 38 | +echo "--- Phase 1: Random JSON-RPC mutations ---" |
| 39 | + |
| 40 | +while [ $SECONDS -lt $END_TIME ]; do |
| 41 | + ITERATIONS=$((ITERATIONS + 1)) |
| 42 | + |
| 43 | + # Generate a random mutated JSON-RPC payload |
| 44 | + PAYLOAD=$(python3 -c " |
| 45 | +import random, string, json |
| 46 | +
|
| 47 | +# Base valid request |
| 48 | +base = { |
| 49 | + 'jsonrpc': '2.0', |
| 50 | + 'id': random.randint(-999999, 999999), |
| 51 | + 'method': random.choice([ |
| 52 | + 'initialize', 'tools/call', 'tools/list', |
| 53 | + random.choice(string.ascii_letters) * random.randint(1, 100), |
| 54 | + '', |
| 55 | + ]), |
| 56 | +} |
| 57 | +
|
| 58 | +# Random mutations |
| 59 | +mutation = random.randint(0, 10) |
| 60 | +if mutation == 0: |
| 61 | + # Valid tool call with random args |
| 62 | + base['params'] = {'name': 'search_graph', 'arguments': { |
| 63 | + 'name_pattern': ''.join(random.choices(string.printable, k=random.randint(0, 500))) |
| 64 | + }} |
| 65 | +elif mutation == 1: |
| 66 | + # Huge nested object |
| 67 | + base['params'] = {'a': {'b': {'c': {'d': {'e': 'deep'}}}}} |
| 68 | +elif mutation == 2: |
| 69 | + # Random bytes as method |
| 70 | + base['method'] = ''.join(random.choices(string.printable, k=random.randint(1, 1000))) |
| 71 | +elif mutation == 3: |
| 72 | + # Null fields |
| 73 | + base['method'] = None |
| 74 | + base['id'] = None |
| 75 | +elif mutation == 4: |
| 76 | + # Array instead of object |
| 77 | + base = [1, 2, 3] |
| 78 | +elif mutation == 5: |
| 79 | + # Empty |
| 80 | + base = {} |
| 81 | +elif mutation == 6: |
| 82 | + # Random Cypher query |
| 83 | + base['params'] = {'name': 'query_graph', 'arguments': { |
| 84 | + 'query': ''.join(random.choices(string.printable, k=random.randint(1, 200))) |
| 85 | + }} |
| 86 | +elif mutation == 7: |
| 87 | + # Very long string values |
| 88 | + base['params'] = {'name': 'search_graph', 'arguments': { |
| 89 | + 'name_pattern': 'A' * random.randint(10000, 100000) |
| 90 | + }} |
| 91 | +elif mutation == 8: |
| 92 | + # Unicode/binary-like content |
| 93 | + base['params'] = {'name': 'search_code', 'arguments': { |
| 94 | + 'pattern': ''.join(chr(random.randint(0, 0xFFFF)) for _ in range(100)) |
| 95 | + }} |
| 96 | +elif mutation == 9: |
| 97 | + # Random tool name |
| 98 | + base['params'] = {'name': ''.join(random.choices(string.ascii_letters, k=50)), 'arguments': {}} |
| 99 | +else: |
| 100 | + # Completely random JSON |
| 101 | + base = ''.join(random.choices(string.printable, k=random.randint(1, 500))) |
| 102 | +
|
| 103 | +try: |
| 104 | + print(json.dumps(base)) |
| 105 | +except: |
| 106 | + print(json.dumps({'garbage': True})) |
| 107 | +" 2>/dev/null || echo '{}') |
| 108 | + |
| 109 | + # Build session: init + mutated payload |
| 110 | + INIT='{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"fuzz","version":"1.0"}}}' |
| 111 | + printf '%s\n%s\n' "$INIT" "$PAYLOAD" > "$FUZZ_TMPDIR/input.jsonl" |
| 112 | + |
| 113 | + # Run and check for crashes (not exit code — we expect errors, just not crashes) |
| 114 | + run_with_timeout 5 "$BINARY" < "$FUZZ_TMPDIR/input.jsonl" > /dev/null 2>&1 |
| 115 | + EC=$? |
| 116 | + |
| 117 | + # 139 = SIGSEGV, 134 = SIGABRT, 136 = SIGFPE — real crashes |
| 118 | + if [ $EC -eq 139 ] || [ $EC -eq 134 ] || [ $EC -eq 136 ]; then |
| 119 | + echo "CRASH: exit code $EC on iteration $ITERATIONS" |
| 120 | + echo "Payload: $PAYLOAD" |
| 121 | + CRASHES=$((CRASHES + 1)) |
| 122 | + fi |
| 123 | +done |
| 124 | + |
| 125 | +echo "Phase 1: $ITERATIONS iterations, $CRASHES crashes" |
| 126 | + |
| 127 | +# ── Phase 2: Random Cypher queries via CLI ─────────────────── |
| 128 | +echo "" |
| 129 | +echo "--- Phase 2: Random Cypher queries via CLI ---" |
| 130 | + |
| 131 | +CLI_ITERATIONS=0 |
| 132 | +CLI_END=$((SECONDS + 10)) |
| 133 | + |
| 134 | +while [ $SECONDS -lt $CLI_END ]; do |
| 135 | + CLI_ITERATIONS=$((CLI_ITERATIONS + 1)) |
| 136 | + |
| 137 | + QUERY=$(python3 -c " |
| 138 | +import random, string |
| 139 | +parts = ['MATCH', 'WHERE', 'RETURN', '(', ')', '-[', ']->', '<-[', ']-', |
| 140 | + 'n', 'r', '.name', '.label', '=', '\"', \"'\", ';', '--', 'DROP', |
| 141 | + 'DELETE', 'ATTACH', 'DETACH', '*', 'COUNT', 'LIMIT', 'ORDER BY'] |
| 142 | +q = ' '.join(random.choices(parts, k=random.randint(1, 20))) |
| 143 | +# Sometimes add random garbage |
| 144 | +if random.random() > 0.5: |
| 145 | + q += ''.join(random.choices(string.printable, k=random.randint(1, 100))) |
| 146 | +print(q) |
| 147 | +" 2>/dev/null || echo "MATCH (n) RETURN n") |
| 148 | + |
| 149 | + run_with_timeout 3 "$BINARY" cli query_graph "{\"query\":\"$QUERY\"}" > /dev/null 2>&1 |
| 150 | + EC=$? |
| 151 | + |
| 152 | + if [ $EC -eq 139 ] || [ $EC -eq 134 ] || [ $EC -eq 136 ]; then |
| 153 | + echo "CRASH: exit code $EC on Cypher iteration $CLI_ITERATIONS" |
| 154 | + echo "Query: $QUERY" |
| 155 | + CRASHES=$((CRASHES + 1)) |
| 156 | + fi |
| 157 | +done |
| 158 | + |
| 159 | +echo "Phase 2: $CLI_ITERATIONS iterations, $CRASHES total crashes" |
| 160 | + |
| 161 | +# ── Summary ────────────────────────────────────────────────── |
| 162 | +echo "" |
| 163 | +if [ $CRASHES -gt 0 ]; then |
| 164 | + echo "=== FUZZ TESTING FAILED: $CRASHES crash(es) found ===" |
| 165 | + exit 1 |
| 166 | +fi |
| 167 | + |
| 168 | +echo "=== Fuzz testing passed: $((ITERATIONS + CLI_ITERATIONS)) iterations, 0 crashes ===" |
0 commit comments