Repository navigation
Expand file tree
/
Copy pathfuzzer.py
More file actions
1447 lines (1262 loc) · 74.7 KB
/
Copy pathfuzzer.py
File metadata and controls
1447 lines (1262 loc) · 74.7 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import inspect
import os
import subprocess
import shutil
import json
import asyncio
import time
import logging
import glob
from .llm import LLMHandler, SeedProvenanceError, _no_llm_sanitizers, _flag
from .client import MCPInspector
from . import sanitizers
from .runners import PythonRunner, JSRunner
from .config import FuzzerConfig
from .reporters import CompositeVulnerabilityReporter, MongoVulnerabilityReporter, ConsoleVulnerabilityReporter
from .input_parser import FuzzInputParser
from .file_manager import FuzzFileManager
from .engine import AtherisFuzzingEngine
from .metrics import CoverageTimelineCollector
from .runners.base import BaseRunner
logger = logging.getLogger(__name__)
def _keep_corpus() -> bool:
"""Opt-in corpus persistence across runs of the same tool (TOFU_KEEP_CORPUS)."""
return os.environ.get("TOFU_KEEP_CORPUS", "").strip().lower() not in ("", "0", "false", "no")
def _no_seeds() -> bool:
"""TOFU_NO_SEEDS (ablation): start libFuzzer from an EMPTY corpus -- no LLM seeds,
no fallback seeds. Inputs are then schema-conforming values built from raw bytes
by input_parser (FuzzedDataProvider path)."""
return _flag("TOFU_NO_SEEDS")
def _oracle_shadow() -> bool:
"""TOFU_ORACLE_SHADOW (ablation): also evaluate the input sanitizers on FAILED
executions and log what WOULD have fired without the silent-success gate. Logged
only (ORACLE_SHADOW_UNGATED), never reported -- the run's findings are unchanged."""
return _flag("TOFU_ORACLE_SHADOW")
_STR_CONTRACT_CACHE = {}
def _takes_a_string(fn) -> bool:
"""Is this sanitizer's contract (str, ...) -> ...?
_check_string_sanitizers used to call EVERY selected sanitizer with a plain
string, whatever its declared parameter type. Measured 2026-09-10:
check_process_and_fd_leaks(leaked_pids: set[int]) -> tuple[bool, str]
called with '/var/folders/.../tmp32djxuif.xlsx' returns
(True, 'Leaked background subprocess PIDs detected ... <that path>')
-- a non-empty string is a truthy "set of leaked pids". Combined with the
tuple-truthiness bug above, this fired check_file_access,
check_unhandled_promise_rejection and check_process_and_fd_leaks on EVERY
string argument of EVERY input: 295 identical alerts in one 90s run, all
three names from BASE_MANDATORY_SANITIZERS, on the benign input
{'filePath': '<temp>.xlsx', 'sheetName': 'Sheet1', 'cell': 'A1'} that ToFu
itself constructed. Not one LLM-selected sanitizer fired.
Unannotated parameters are ALLOWED through: most of the genuine string
sanitizers predate the annotations, and excluding them would silently
disable the real checks -- the precise failure this codebase keeps having.
Only a parameter annotated as something other than `str` is skipped.
The caller passes ONE positional argument, so a sanitizer with a second
required positional parameter is not a string contract either:
check_reflected_output(tool_input, tool_output) raised TypeError and killed
the fuzz target after 3 execs once every sanitizer was selected (ablation
arm allsan, PSU PC 2026-09-28). It is evaluated on its own evidence-gated
path in _run_sanitizers, so excluding it here loses nothing.
"""
cached = _STR_CONTRACT_CACHE.get(fn)
if cached is None:
try:
params = list(inspect.signature(fn).parameters.values())
if not params:
cached = False
else:
ann = params[0].annotation
P = inspect.Parameter
extra_required = any(
p.default is P.empty and p.kind in (P.POSITIONAL_ONLY, P.POSITIONAL_OR_KEYWORD)
for p in params[1:])
cached = (ann is P.empty or ann is str or ann == "str") and not extra_required
except (TypeError, ValueError):
cached = True
_STR_CONTRACT_CACHE[fn] = cached
return cached
def _descendants(root_pid, ps_map: dict) -> set:
"""PIDs descended from root_pid, per a _ps_snapshot() map.
Replaces a GLOBAL "which pids are new" diff. That diff saw every process the
whole machine started during the execution window, including the OTHER
worker's MCP server: with --workers 2 the campaign reported
"Process leak/leftover detected: PID 38747 (node)" on four separate tools
that spawn nothing (nazmul.todo item 40).
The old code DID filter by ancestry, and that filter is correct in
isolation -- but it was guarded by
if leaked_pids and self.current_runner and self.current_runner.process:
so whenever the runner had no live `process` the filter was SKIPPED and the
unfiltered global diff was returned. It FAILED OPEN. Computing descendants
directly cannot fail that way: with no root there are no descendants, so
the answer is empty rather than everything.
"""
if not root_pid:
return set()
children = {}
for pid, entry in ps_map.items():
children.setdefault(entry[0], []).append(pid)
out, stack = set(), list(children.get(root_pid, ()))
while stack:
pid = stack.pop()
if pid in out:
continue
out.add(pid)
stack.extend(children.get(pid, ()))
return out
def _runner_pid(runner):
"""PID of the runner's server process, or None."""
return getattr(getattr(runner, "process", None), "pid", None)
_PROC_SANITIZERS = {"check_process_and_fd_leaks", "check_terminal_dos_and_leak"}
def _ps_snapshot() -> dict:
"""pid -> (ppid, comm) for every process, portable across Linux and macOS.
The code this replaces read os.listdir("/proc") directly. THERE IS NO /proc
ON macOS -- the eval machine. Every read raised FileNotFoundError into a
bare `except Exception: pass`, so before_pids and after_pids were always
empty, leaked_pids was always empty, and check_process_and_fd_leaks /
check_terminal_dos_and_leak could NEVER fire on the platform the campaign
actually runs on. Silently: no error, no warning, just a sanitizer family
that reports nothing forever. Verified 2026-09-10:
os.listdir('/proc') -> FileNotFoundError [Errno 2] on Darwin-arm64
"""
if os.path.isdir("/proc"):
out = {}
for x in os.listdir("/proc"):
if not x.isdigit():
continue
try:
with open(f"/proc/{x}/stat") as f:
stat = f.read()
# comm sits in parens and may itself contain spaces or parens,
# so split on the LAST ')'.
r = stat.rindex(")")
out[int(x)] = (int(stat[r + 2:].split()[1]), stat[stat.index("(") + 1:r])
except Exception:
continue
return out
# macOS / BSD
try:
res = subprocess.run(["ps", "-Ao", "pid=,ppid=,comm="],
capture_output=True, timeout=10)
out = {}
for line in res.stdout.decode(errors="ignore").splitlines():
parts = line.split(None, 2)
if len(parts) < 2:
continue
try:
out[int(parts[0])] = (int(parts[1]),
(parts[2].strip() if len(parts) > 2 else ""))
except ValueError:
continue
return out
except Exception:
return {}
class MCPDifferentialFuzzer:
def __init__(self, command: str, args: list, target_tool: str, api_key: str,
corpus_dir: str = "./fuzz_corpus", override_rules: dict = None,
file_backed_params: dict = None, config: FuzzerConfig = None,
reporter = None, metadata_provider = None, runner_factory = None,
predefined_seeds: list = None, dict_file: str = None):
self.command = command
self.args = args
self.target_tool = target_tool
self.corpus_dir = corpus_dir
self.llm = LLMHandler(api_key=api_key)
self.override_rules = override_rules or {}
self.file_backed_params = file_backed_params or {}
self.config = config or FuzzerConfig()
self.predefined_seeds = predefined_seeds or []
self.dict_file = dict_file
self.target_tool_desc = ""
# Inject metadata provider
if metadata_provider is None:
from .client import MCPInspector
self.inspector = MCPInspector(command=command, args=args)
else:
self.inspector = metadata_provider
# Inject runner factory
if runner_factory is None:
self.runner_factory = self._default_runner_factory
else:
self.runner_factory = runner_factory
if reporter is None:
# The Mongo sink was unconditional while FuzzerConfig.export_mongo
# (default False) was ignored. With no mongod running, EVERY finding
# cost a 2s serverSelectionTimeoutMS: the 2026-09-10 appium run spent
# ~124s of its 540.4s wall clock on 62 connection-refused retries,
# which silently corrupts the per-run time budget the campaign
# accounts in. Console only unless the config asks for Mongo.
# This changes where findings are LOGGED, never what is found.
sinks = [ConsoleVulnerabilityReporter()]
if getattr(self.config, "export_mongo", False):
sinks.append(MongoVulnerabilityReporter())
self.reporter = CompositeVulnerabilityReporter(sinks)
else:
self.reporter = reporter
self.current_runner = None
self.current_schema = None
self.selected_sanitizer_names = []
self.selected_sanitizer_rules = {}
self.silent_bug_found = False
self.target_tool_name = ""
self.exec_count = 0
# EXECUTABILITY ACCOUNTING. A tool whose every execution returns
# isError/JSON-RPC-error never really ran, so no sanitizer can fire and
# the run reports a healthy "0 findings". Measured 2026-09-10 on
# api7/apisix-mcp::delete_resource: every input returned
# "Status: undefined\nMessage: connect ECONNREFUSED 127.0.0.1:9180"
# because the tool is an Admin-API client and no APISIX is running. All
# 32 apisix tools are in that state, and libFuzzer's cov: is flat by
# construction because there is exactly one reachable path.
# "Ran and found nothing" and "never actually ran" must not look alike.
self.exec_success = 0
self.exec_total = 0
self.exec_error_counts = {}
self._failfast_after = int(os.environ.get("TOFU_FAILFAST_EXECS", "2000"))
self.start_fuzz_time = None
# Instantiated helpers
self.file_manager = FuzzFileManager(self.file_backed_params)
self.input_parser = None
# Metrics collection
if getattr(self.config, "enable_metrics", True):
self.metrics_collector = CoverageTimelineCollector(
output_dir=getattr(self.config, "metrics_dir", "metrics"),
export_csv=getattr(self.config, "export_csv", True),
export_mongo=getattr(self.config, "export_mongo", False),
sample_interval_sec=getattr(self.config, "metrics_sample_interval_sec", 60.0)
)
else:
self.metrics_collector = None
def _default_runner_factory(self, tool: str) -> BaseRunner:
is_js = (
any(arg.endswith(".js") for arg in self.args) or
any(js_cmd in self.command for js_cmd in ("node", "npx", "npm", "yarn", "pnpm"))
)
if is_js:
from .runners import JSRunner
return JSRunner(self.command, self.args, tool,
custom_env=getattr(self.config, "target_env", None),
cwd=getattr(self.config, "target_cwd", None))
else:
from .runners import PythonRunner
# BUG 21 for the Python arm too: the target's own directory, so the
# runner can find its venv (.venv/) and src/ layout.
return PythonRunner(self.command, self.args, tool,
cwd=getattr(self.config, "target_cwd", None),
custom_env=getattr(self.config, "target_env", None))
def test_one_input(self, data: bytes) -> None:
"""The callback executed by Atheris for each input permutation."""
if self.current_runner is None or self.current_schema is None:
return
self.exec_count += 1
if self.start_fuzz_time is None:
self.start_fuzz_time = time.time()
if self.metrics_collector:
self.metrics_collector.record_execution_progress(self.exec_count)
# 1. Sequence mode. OFF BY DEFAULT -- it used to default to 2.
#
# FuzzerConfig has no `sequence_steps` field, so the getattr default of
# 2 always won and EVERY corpus entry was sliced into fragments before
# parsing. Measured 2026-09-10 on a real LLM-generated seed:
# original : {"filePath": "predefined_seed:test_file.xlsx", "sheetName": "Sheet1", "cell": "A1"}
# chunk 0: {"filePath": "predefined_seed:test_file.x -> not JSON
# chunk 1: lsx", "sheetName": "Sheet1", "cell": "A1" -> not JSON
# chunk 2: } -> not JSON
# None of the three parses, so input_parser fell through to
# FuzzedDataProvider on every fragment and the tool received garbage:
# EXEC_ERROR n=1304 detail='MCP error -32600: Invalid cell address: '
# EXEC_SUMMARY tool=get_cell n_exec=512 n_success=0
#
# This silently defeated the JSON-first parse order (audit Fix 3), the
# restored seed_bank/, LLM seed generation and the seed validation --
# all of which exist to deliver a COMPLETE argument object. A seed can
# be perfect and still never arrive.
#
# Splitting a serialised argument object at an arbitrary byte offset is
# not a sequence of tool calls; it is corruption. If stateful multi-call
# testing is wanted it needs its own representation, not a byte slice.
# Opt in explicitly with FuzzerConfig.sequence_steps.
sequence_steps = getattr(self.config, "sequence_steps", 1) if hasattr(self, "config") else 1
# Split data if sequence mode is active
data_chunks = [data[i:i+len(data)//sequence_steps] for i in range(0, len(data), max(1, len(data)//sequence_steps))] if sequence_steps > 1 else [data]
for step_idx, chunk in enumerate(data_chunks):
# 1. Parse Input using delegated parser helper
generated_arguments, parsed_successfully = self.input_parser.parse(chunk)
# 2. Prepare File-Backed Parameters using delegated file manager helper
generated_arguments, created_temp_files = self.file_manager.prepare_file_params(generated_arguments)
# 3. Execute and Measure
res, elapsed, leaked_files, leaked_pids = self._execute_and_time(generated_arguments, created_temp_files)
# Counted per EXECUTION. self.exec_count increments once per
# test_one_input, but sequence mode can run several executions
# inside one call, so the two were on different bases: a log showed
# n_exec=512 alongside an error bucket of n=1304.
self.exec_total += 1
if getattr(res, "success", False):
self.exec_success += 1
else: # noqa: E501
# Bucket by the error's shape, not its full text, so one
# recurring cause does not become thousands of distinct keys.
key = (str(getattr(res, "error", "") or "unknown"))[:120]
self.exec_error_counts[key] = self.exec_error_counts.get(key, 0) + 1
# EMIT PERIODICALLY, NOT AT THE END. libFuzzer terminates the
# process itself when -max_total_time expires, so everything after
# engine.fuzz() is dead code: "[FUZZING METRICS]" and "Fuzzing
# session completed" appear in ZERO of the logs this repo has ever
# produced, and libFuzzer's own closing "Done ... runs" line is lost
# with them (the stderr reader is a daemon thread and the pipe is
# not drained on a hard exit). A summary that only prints at the end
# is a summary that never prints. run_eval takes the LAST one.
if self.exec_count == 1 or self.exec_count % 512 == 0:
self._emit_exec_summary()
# FAIL FAST. A tool that has failed every one of its first N
# executions is not going to start working, and the remaining
# budget buys nothing: 143 of 173 configs are in that state here
# (no APISIX on :9180, no graphlit credential, no n8n backend), and
# at ~450s each that is ~9h of a campaign spent proving the same
# ECONNREFUSED over and over. Ending the run keeps the evidence
# (EXEC_SUMMARY / EXEC_ERROR / NOT_EXECUTABLE are already in the
# log) and drops the cost.
#
# The threshold is deliberately generous: the deterministic seeds
# alone cannot execute excel-reader (they cannot guess a sheet
# name) while the LLM seeds can, so a small sample would discard a
# target that actually works. Set TOFU_FAILFAST_EXECS=0 to disable.
if (self._failfast_after
and self.exec_total >= self._failfast_after
and self.exec_success == 0):
self._emit_exec_summary()
logger.error(
f"FAILFAST tool={self.target_tool_name} -- stopping after "
f"{self.exec_total} executions, none of which succeeded. The "
f"remaining budget cannot change this. This is NOT a clean run.")
sys.exit(0)
# 4. Run Sanitizers
sanitizer_triggered, triggered_by, err_details = self._run_sanitizers(
generated_arguments, res, elapsed, leaked_files, leaked_pids
)
# 5. Handle Violations
if sanitizer_triggered:
self._handle_violation(generated_arguments, res.result if hasattr(res, 'result') else res, triggered_by, f"[Sequence Step {step_idx+1}/{len(data_chunks)}] {err_details}")
break
def _emit_exec_summary(self) -> None:
"""One greppable line per tool. eval/run_eval.py parses the LAST one."""
n = self.exec_total or self.exec_count
rate = (self.exec_success / n) if n else 0.0
logger.info(f"EXEC_SUMMARY tool={self.target_tool_name} n_exec={n} "
f"n_success={self.exec_success} success_rate={rate:.3f}")
for err, n in sorted(self.exec_error_counts.items(), key=lambda kv: -kv[1])[:3]:
logger.info(f"EXEC_ERROR n={n} detail={err!r}")
if (self.exec_total or self.exec_count) >= 64 and self.exec_success == 0:
logger.error(
f"NOT_EXECUTABLE tool={self.target_tool_name} -- all {n} "
f"executions returned an error, so NO sanitizer could fire and coverage "
f"cannot vary. This run is NOT evidence that the tool is clean. Most "
f"common cause: the tool is a client for a backend/credential that is "
f"not present in this environment.")
def _handle_violation(self, inputs: dict, result: any, triggered_by: list[str], err_details: str) -> None:
"""Logs the triggered security exception and marks bug found without stopping the session."""
self.reporter.report(
self.target_tool_name,
inputs,
result,
triggered_by,
err_details
)
# Findings-over-time timeline (opt-in via TOFU_FINDINGS_TIMELINE; no-op
# otherwise). Records time-to-bug for the 24h deep campaign / RQ4.
if self.metrics_collector is not None:
self.metrics_collector.record_finding(
self.target_tool_name, inputs, triggered_by, err_details
)
self.silent_bug_found = True
def _execute_and_time(self, generated_arguments: dict, created_temp_files: list[str]) -> tuple[any, float, set[str], set[int]]:
"""Executes the runner, measures duration, and detects leaked temporary files."""
import tempfile as tf_mod
temp_dir = tf_mod.gettempdir()
try:
before_files = set(glob.glob(os.path.join(temp_dir, "*")))
except Exception:
before_files = set()
before_pids = set()
ps_before = {}
# Gate on EITHER consumer. _check_process_leak fires for
# check_process_and_fd_leaks OR check_terminal_dos_and_leak, but the
# snapshot was only taken for the latter -- and the former is one of the
# 10 BASE_MANDATORY_SANITIZERS, i.e. always on. So a mandatory sanitizer
# could never fire: leaked_pids was empty whenever the opt-in one was
# not also selected.
if _PROC_SANITIZERS & set(self.selected_sanitizer_names):
ps_before = _ps_snapshot()
# Descendants of THIS run's server only -- never a global pid set.
before_pids = _descendants(_runner_pid(self.current_runner), ps_before)
if not ps_before and not getattr(self, "_ps_warned", False):
self._ps_warned = True
logger.error(
"check_terminal_dos_and_leak is selected but the process "
"snapshot is EMPTY, so process-leak detection cannot fire. "
"Do not read a clean run as evidence of no leak.")
start_time = time.time()
try:
res = self.current_runner.execute(generated_arguments)
finally:
self.file_manager.clean_temp_files(created_temp_files)
elapsed = time.time() - start_time
try:
after_files = set(glob.glob(os.path.join(temp_dir, "*")))
except Exception:
after_files = set()
leaked_files = {f for f in (after_files - before_files) if not f.endswith((".cpuprofile", ".cpuprofile.json")) and "systemd-private-" not in f}
after_pids = set()
ps_after = {}
if _PROC_SANITIZERS & set(self.selected_sanitizer_names):
ps_after = _ps_snapshot()
after_pids = _descendants(_runner_pid(self.current_runner), ps_after)
# Both sides are already restricted to descendants of this run's own
# server, so the difference is exactly "processes this tool call left
# behind". No ancestry re-filter is needed, and nothing another worker
# starts can appear here.
leaked_pids = after_pids - before_pids
self._last_ps = ps_after
return res, elapsed, leaked_files, leaked_pids
def _run_sanitizers(self, generated_arguments: dict, res: any, elapsed: float, leaked_files: set[str], leaked_pids: set[int]) -> tuple[bool, list[str], str]:
"""Runs the fuzzer's active security sanitizers on input/execution details."""
triggered_by = []
err_details = ""
# 1. Check temp file leaks
leak_triggered, leak_err = self._check_temp_file_leak(leaked_files)
if leak_triggered:
triggered_by.append("check_temp_file_leak")
err_details += leak_err
# 2. Check process leaks / terminal DoS
if "check_process_and_fd_leaks" in self.selected_sanitizer_names or "check_terminal_dos_and_leak" in self.selected_sanitizer_names:
proc_leak_triggered, proc_leak_err = self._check_process_leak(leaked_pids)
if proc_leak_triggered:
triggered_by.append("check_process_and_fd_leaks")
err_details += proc_leak_err
# 3. Check resource exhaustion / timeout (e.g. ReDoS)
timeout_triggered, timeout_err = self._check_resource_exhaustion(elapsed, res)
if timeout_triggered:
triggered_by.append("check_resource_exhaustion")
err_details += timeout_err
# 4. Check unexpected library/runtime crashes
crash_triggered, crash_err = self._check_unexpected_crash(res)
if crash_triggered:
triggered_by.append("check_unexpected_crash")
err_details += crash_err
# 4.c Run Introspection Sanitizers: check file system accesses, deserialization, command execution, SQL queries, file uploads, code injection, SSRF, and temp leaks
from .sanitizers import (
check_unhandled_promise_rejection, check_file_access, check_introspection_path_traversal,
check_untrusted_deserialization, check_introspection_command_injection, check_introspection_sql_injection,
check_unrestricted_file_upload, check_introspection_code_injection, check_introspection_ssrf,
check_introspection_temp_file_leak, check_introspection_log_injection, check_introspection_sensitive_data_logging,
check_introspection_ssl_verification, check_introspection_sensitive_file_access,
check_introspection_unrestricted_ip_binding, check_introspection_redos,
check_introspection_blocking_event_loop_call, check_introspection_hardcoded_credentials,
check_introspection_weak_hash, check_introspection_weak_prng, check_introspection_nonce_reuse,
check_introspection_unverified_code_download, check_introspection_xxe, check_introspection_data_amplification,
check_introspection_argument_injection, check_introspection_csv_formula_injection,
check_introspection_symlink_following, check_introspection_insecure_temp_permissions,
check_introspection_sensitive_error_leak, check_introspection_reachable_assertion,
check_introspection_cleartext_sensitive_storage, check_introspection_cleartext_sensitive_transmission,
check_introspection_failed_init_non_exit, check_introspection_incomplete_cleanup,
check_introspection_unchecked_loop_condition, check_introspection_memory_access_violation,
check_introspection_divide_by_zero, check_introspection_insecure_file_permissions,
check_introspection_deadlock, check_introspection_file_descriptor_leak,
check_introspection_prototype_pollution, check_introspection_external_config_control,
check_introspection_format_string, check_introspection_type_confusion,
check_introspection_cross_session_data_leak, check_introspection_unnecessary_root_privileges,
check_introspection_exfil, check_reflected_output
)
# Item 78: this sanitizer greps the STRINGIFIED response for
# substrings like "SyntaxError:" -- it has no way to tell an actually
# unhandled exception from a well-formed, successful reply whose own
# descriptive text happens to quote one (measured 2026-09-12 on
# bsv-mcp::app_explorer_data: the tool caught a JSON.parse error from
# an upstream API and returned it as a normal text response,
# n_exec_success=1/1 -- not a crash). A caught exception surfaced in
# a successful reply's text is not what this sanitizer claims to
# detect. Same trade as bug 20: skip when the call was not itself
# reported as a failure, trading recall for soundness.
if "check_unhandled_promise_rejection" in self.selected_sanitizer_names and not res.success:
unhandled_triggered, unhandled_err = check_unhandled_promise_rejection(res.result if hasattr(res, 'result') else res)
if unhandled_triggered:
triggered_by.append("check_unhandled_promise_rejection")
err_details += unhandled_err
prev_session_data = getattr(res, "prev_session_data", "")
current_out = str(getattr(res, "result", ""))
if "check_introspection_cross_session_data_leak" in self.selected_sanitizer_names or (prev_session_data and current_out):
cs_triggered, cs_err = check_introspection_cross_session_data_leak(prev_session_data, current_out)
if cs_triggered:
triggered_by.append("check_introspection_cross_session_data_leak")
err_details += cs_err
# Reflected-output / XSS (CWE-79/116): dangerous input payload echoed unescaped in
# output. Evidence-gated on there being any output to reflect into.
if "check_reflected_output" in self.selected_sanitizer_names or current_out:
refl_triggered, refl_err = check_reflected_output(generated_arguments, current_out)
if refl_triggered:
triggered_by.append("check_reflected_output")
err_details += refl_err
error_string = str(getattr(res, "error", ""))
if "check_introspection_type_confusion" in self.selected_sanitizer_names or error_string:
tc_triggered, tc_err = check_introspection_type_confusion(error_string)
if tc_triggered:
triggered_by.append("check_introspection_type_confusion")
err_details += tc_err
error_string = str(getattr(res, "error", ""))
logs_list = getattr(res, "logs", [])
if "check_introspection_format_string" in self.selected_sanitizer_names or error_string or logs_list:
fmt_triggered, fmt_err = check_introspection_format_string(error_string, logs_list)
if fmt_triggered:
triggered_by.append("check_introspection_format_string")
err_details += fmt_err
mod_env = getattr(res, "modified_env_vars", [])
if "check_introspection_external_config_control" in self.selected_sanitizer_names or mod_env:
env_triggered, env_err = check_introspection_external_config_control(mod_env)
if env_triggered:
triggered_by.append("check_introspection_external_config_control")
err_details += env_err
# Check prototype pollution in test input / payload
input_args = getattr(res, "input_args", None)
if "check_introspection_prototype_pollution" in self.selected_sanitizer_names or input_args:
proto_triggered, proto_err = check_introspection_prototype_pollution(input_args)
if proto_triggered:
triggered_by.append("check_introspection_prototype_pollution")
err_details += proto_err
leaked_fds = getattr(res, "leaked_fds", [])
if "check_introspection_file_descriptor_leak" in self.selected_sanitizer_names or leaked_fds:
fd_triggered, fd_err = check_introspection_file_descriptor_leak(len(leaked_fds), leaked_fds)
if fd_triggered:
triggered_by.append("check_introspection_file_descriptor_leak")
err_details += fd_err
is_timeout = getattr(res, "timed_out", False)
active_locks = getattr(res, "active_locks_count", 0)
if "check_introspection_deadlock" in self.selected_sanitizer_names or is_timeout:
dead_triggered, dead_err = check_introspection_deadlock(is_timeout, active_locks)
if dead_triggered:
triggered_by.append("check_introspection_deadlock")
err_details += dead_err
exit_code = getattr(res, "exit_code", 0)
error_string = str(getattr(res, "error", ""))
if "check_introspection_divide_by_zero" in self.selected_sanitizer_names or error_string:
div_triggered, div_err = check_introspection_divide_by_zero(error_string)
if div_triggered:
triggered_by.append("check_introspection_divide_by_zero")
err_details += div_err
if "check_introspection_memory_access_violation" in self.selected_sanitizer_names or exit_code or error_string:
mem_triggered, mem_err = check_introspection_memory_access_violation(exit_code, error_string)
if mem_triggered:
triggered_by.append("check_introspection_memory_access_violation")
err_details += mem_err
loop_count = getattr(res, "loop_iterations", 0)
if "check_introspection_unchecked_loop_condition" in self.selected_sanitizer_names or loop_count:
loop_triggered, loop_err = check_introspection_unchecked_loop_condition(loop_count)
if loop_triggered:
triggered_by.append("check_introspection_unchecked_loop_condition")
err_details += loop_err
logged_msgs = getattr(res, "logged_messages", [])
is_success = getattr(res, "success", True)
if "check_introspection_failed_init_non_exit" in self.selected_sanitizer_names or logged_msgs:
init_triggered, init_err = check_introspection_failed_init_non_exit(is_success, logged_msgs)
if init_triggered:
triggered_by.append("check_introspection_failed_init_non_exit")
err_details += init_err
leaked_temps = getattr(res, "leaked_temp_files", [])
if "check_introspection_incomplete_cleanup" in self.selected_sanitizer_names or leaked_temps:
clean_triggered, clean_err = check_introspection_incomplete_cleanup(leaked_temps)
if clean_triggered:
triggered_by.append("check_introspection_incomplete_cleanup")
err_details += clean_err
net_conns = getattr(res, "network_connections", [])
if "check_introspection_cleartext_sensitive_transmission" in self.selected_sanitizer_names or net_conns:
trans_triggered, trans_err = check_introspection_cleartext_sensitive_transmission(net_conns)
if trans_triggered:
triggered_by.append("check_introspection_cleartext_sensitive_transmission")
err_details += trans_err
# Check errors / stack traces for sensitive disclosures (CWE-209) and reachable assertion DoS (CWE-617)
error_string = str(getattr(res, "error", ""))
if "check_introspection_sensitive_error_leak" in self.selected_sanitizer_names or error_string:
leak_triggered, leak_err = check_introspection_sensitive_error_leak(error_string)
if leak_triggered:
triggered_by.append("check_introspection_sensitive_error_leak")
err_details += leak_err
if "check_introspection_reachable_assertion" in self.selected_sanitizer_names or error_string:
assert_triggered, assert_err = check_introspection_reachable_assertion(error_string)
if assert_triggered:
triggered_by.append("check_introspection_reachable_assertion")
err_details += assert_err
accessed_files = getattr(res, "accessed_files", [])
if "check_introspection_symlink_following" in self.selected_sanitizer_names or accessed_files:
sym_triggered, sym_err = check_introspection_symlink_following(accessed_files)
if sym_triggered:
triggered_by.append("check_introspection_symlink_following")
err_details += sym_err
# Exfiltration correlation (CWE-201): sensitive file READ + outbound network SEND in
# the same call. Evidence-gated on BOTH a file access and a network connection.
if "check_introspection_exfil" in self.selected_sanitizer_names or (accessed_files and net_conns):
exfil_triggered, exfil_err = check_introspection_exfil(accessed_files, net_conns)
if exfil_triggered:
triggered_by.append("check_introspection_exfil")
err_details += exfil_err
written_files = getattr(res, "written_files", [])
if "check_introspection_insecure_file_permissions" in self.selected_sanitizer_names or written_files:
file_perm_triggered, file_perm_err = check_introspection_insecure_file_permissions(written_files)
if file_perm_triggered:
triggered_by.append("check_introspection_insecure_file_permissions")
err_details += file_perm_err
if "check_introspection_cleartext_sensitive_storage" in self.selected_sanitizer_names or written_files:
store_triggered, store_err = check_introspection_cleartext_sensitive_storage(written_files)
if store_triggered:
triggered_by.append("check_introspection_cleartext_sensitive_storage")
err_details += store_err
if "check_introspection_insecure_temp_permissions" in self.selected_sanitizer_names or written_files:
perm_triggered, perm_err = check_introspection_insecure_temp_permissions(written_files)
if perm_triggered:
triggered_by.append("check_introspection_insecure_temp_permissions")
err_details += perm_err
if "check_introspection_csv_formula_injection" in self.selected_sanitizer_names or written_files:
csv_triggered, csv_err = check_introspection_csv_formula_injection(written_files)
if csv_triggered:
triggered_by.append("check_introspection_csv_formula_injection")
err_details += csv_err
executed_cmds = getattr(res, "executed_commands", [])
if "check_introspection_unnecessary_root_privileges" in self.selected_sanitizer_names or executed_cmds:
root_triggered, root_err = check_introspection_unnecessary_root_privileges(executed_cmds)
if root_triggered:
triggered_by.append("check_introspection_unnecessary_root_privileges")
err_details += root_err
if "check_introspection_argument_injection" in self.selected_sanitizer_names or executed_cmds:
arg_triggered, arg_err = check_introspection_argument_injection(executed_cmds)
if arg_triggered:
triggered_by.append("check_introspection_argument_injection")
err_details += arg_err
xxe_ops = getattr(res, "xxe_calls", [])
if "check_introspection_xxe" in self.selected_sanitizer_names or xxe_ops:
xxe_triggered, xxe_err = check_introspection_xxe(xxe_ops)
if xxe_triggered:
triggered_by.append("check_introspection_xxe")
err_details += xxe_err
amp_ops = getattr(res, "amplification_calls", [])
if "check_introspection_data_amplification" in self.selected_sanitizer_names or amp_ops:
amp_triggered, amp_err = check_introspection_data_amplification(amp_ops)
if amp_triggered:
triggered_by.append("check_introspection_data_amplification")
err_details += amp_err
unverified_dl = getattr(res, "unverified_downloads", [])
if "check_introspection_unverified_code_download" in self.selected_sanitizer_names or unverified_dl:
dl_triggered, dl_err = check_introspection_unverified_code_download(unverified_dl)
if dl_triggered:
triggered_by.append("check_introspection_unverified_code_download")
err_details += dl_err
reused_nonces = getattr(res, "reused_nonces", [])
if "check_introspection_nonce_reuse" in self.selected_sanitizer_names or reused_nonces:
nonce_triggered, nonce_err = check_introspection_nonce_reuse(reused_nonces)
if nonce_triggered:
triggered_by.append("check_introspection_nonce_reuse")
err_details += nonce_err
weak_hashes = getattr(res, "weak_hash_calls", [])
if "check_introspection_weak_hash" in self.selected_sanitizer_names or weak_hashes:
hash_triggered, hash_err = check_introspection_weak_hash(weak_hashes)
if hash_triggered:
triggered_by.append("check_introspection_weak_hash")
err_details += hash_err
weak_prngs = getattr(res, "weak_prng_calls", [])
if "check_introspection_weak_prng" in self.selected_sanitizer_names or weak_prngs:
prng_triggered, prng_err = check_introspection_weak_prng(weak_prngs)
if prng_triggered:
triggered_by.append("check_introspection_weak_prng")
err_details += prng_err
# Check logged messages and result string for hardcoded credentials (CWE-798)
logged_msgs = getattr(res, "logged_messages", [])
runtime_strings = [str(res.result)] + [m.get("message", "") for m in logged_msgs if isinstance(m, dict)]
if "check_introspection_hardcoded_credentials" in self.selected_sanitizer_names or runtime_strings:
cred_triggered, cred_err = check_introspection_hardcoded_credentials(runtime_strings)
if cred_triggered:
triggered_by.append("check_introspection_hardcoded_credentials")
err_details += cred_err
blocking_ops = getattr(res, "blocking_calls", [])
if "check_introspection_blocking_event_loop_call" in self.selected_sanitizer_names or blocking_ops:
block_triggered, block_err = check_introspection_blocking_event_loop_call(blocking_ops)
if block_triggered:
triggered_by.append("check_introspection_blocking_event_loop_call")
err_details += block_err
slow_regex = getattr(res, "slow_regex_calls", [])
if "check_introspection_redos" in self.selected_sanitizer_names or slow_regex:
redos_triggered, redos_err = check_introspection_redos(slow_regex)
if redos_triggered:
triggered_by.append("check_introspection_redos")
err_details += redos_err
bound_addrs = getattr(res, "bound_addresses", [])
if "check_introspection_unrestricted_ip_binding" in self.selected_sanitizer_names or bound_addrs:
bind_triggered, bind_err = check_introspection_unrestricted_ip_binding(bound_addrs)
if bind_triggered:
triggered_by.append("check_introspection_unrestricted_ip_binding")
err_details += bind_err
acc_files = getattr(res, "accessed_files", [])
if "check_introspection_sensitive_file_access" in self.selected_sanitizer_names or acc_files:
sens_file_triggered, sens_file_err = check_introspection_sensitive_file_access(acc_files)
if sens_file_triggered:
triggered_by.append("check_introspection_sensitive_file_access")
err_details += sens_file_err
unverified_ssl = getattr(res, "unverified_ssl_calls", [])
if "check_introspection_ssl_verification" in self.selected_sanitizer_names or unverified_ssl:
ssl_triggered, ssl_err = check_introspection_ssl_verification(unverified_ssl)
if ssl_triggered:
triggered_by.append("check_introspection_ssl_verification")
err_details += ssl_err
logged_msgs = getattr(res, "logged_messages", [])
if "check_introspection_log_injection" in self.selected_sanitizer_names or logged_msgs:
log_inj_triggered, log_inj_err = check_introspection_log_injection(logged_msgs)
if log_inj_triggered:
triggered_by.append("check_introspection_log_injection")
err_details += log_inj_err
if "check_introspection_sensitive_data_logging" in self.selected_sanitizer_names or logged_msgs:
sens_triggered, sens_err = check_introspection_sensitive_data_logging(logged_msgs)
if sens_triggered:
triggered_by.append("check_introspection_sensitive_data_logging")
err_details += sens_err
leaked_tmp = getattr(res, "leaked_temp_files", [])
if "check_introspection_temp_file_leak" in self.selected_sanitizer_names or leaked_tmp:
temp_triggered, temp_err = check_introspection_temp_file_leak(leaked_tmp)
if temp_triggered:
triggered_by.append("check_introspection_temp_file_leak")
err_details += temp_err
net_calls = getattr(res, "network_connections", [])
if "check_introspection_ssrf" in self.selected_sanitizer_names or net_calls:
ssrf_triggered, ssrf_err = check_introspection_ssrf(net_calls, generated_arguments)
if ssrf_triggered:
triggered_by.append("check_introspection_ssrf")
err_details += ssrf_err
code_calls = getattr(res, "evaluated_code_calls", [])
if "check_introspection_code_injection" in self.selected_sanitizer_names and code_calls:
code_triggered, code_err = check_introspection_code_injection(code_calls)
if code_triggered:
triggered_by.append("check_introspection_code_injection")
err_details += code_err
written_calls = getattr(res, "written_files", [])
if "check_unrestricted_file_upload" in self.selected_sanitizer_names and written_calls:
upload_triggered, upload_err = check_unrestricted_file_upload(written_calls)
if upload_triggered:
triggered_by.append("check_unrestricted_file_upload")
err_details += upload_err
sql_calls = getattr(res, "sql_queries", [])
if "check_introspection_sql_injection" in self.selected_sanitizer_names and sql_calls:
sql_triggered, sql_err = check_introspection_sql_injection(sql_calls)
if sql_triggered:
triggered_by.append("check_introspection_sql_injection")
err_details += sql_err
cmd_calls = getattr(res, "executed_commands", [])
if "check_introspection_command_injection" in self.selected_sanitizer_names and cmd_calls:
cmd_triggered, cmd_err = check_introspection_command_injection(cmd_calls)
if cmd_triggered:
triggered_by.append("check_introspection_command_injection")
err_details += cmd_err
deser_calls = getattr(res, "deserialization_calls", [])
if "check_untrusted_deserialization" in self.selected_sanitizer_names and deser_calls:
deser_triggered, deser_err = check_untrusted_deserialization(deser_calls)
if deser_triggered:
triggered_by.append("check_untrusted_deserialization")
err_details += deser_err
accessed_files = getattr(res, "accessed_files", [])
if "check_file_access" in self.selected_sanitizer_names or True: # always active or if selected
file_access_triggered, file_access_err = check_file_access(accessed_files)
if file_access_triggered:
triggered_by.append("check_file_access")
err_details += file_access_err
if "check_introspection_path_traversal" in self.selected_sanitizer_names:
# An escape must be judged against the TARGET's workspace. Falling
# back to os.getcwd() measured against ToFu's repo root (bug 21).
allowed_dir = (getattr(self.config, "allowed_workspace_dir", None)
or getattr(self.config, "target_cwd", None)
or os.getcwd())
pt_triggered, pt_err = check_introspection_path_traversal(accessed_files, allowed_base_dir=allowed_dir)
if pt_triggered:
triggered_by.append("check_introspection_path_traversal")
err_details += pt_err
if "check_strict_url_security_contract" in self.selected_sanitizer_names:
url_sec_triggered, url_sec_err = sanitizers.check_strict_url_security_contract(generated_arguments, res)
if url_sec_triggered:
triggered_by.append("check_strict_url_security_contract")
err_details += url_sec_err
if "check_strict_har_security_contract" in self.selected_sanitizer_names:
har_sec_triggered, har_sec_err = sanitizers.check_strict_har_security_contract(generated_arguments, res)
if har_sec_triggered:
triggered_by.append("check_strict_har_security_contract")
err_details += har_sec_err
if "check_strict_markitdown_security_contract" in self.selected_sanitizer_names:
md_sec_triggered, md_sec_err = sanitizers.check_strict_markitdown_security_contract(generated_arguments, res)
if md_sec_triggered:
triggered_by.append("check_strict_markitdown_security_contract")
err_details += md_sec_err
# 5. Check standard string-based sanitizers
string_triggered, string_triggered_by, string_err = self._check_string_sanitizers(generated_arguments, res)
if string_triggered:
for s_name in string_triggered_by:
if s_name not in triggered_by:
triggered_by.append(s_name)
err_details += string_err
if _oracle_shadow() and not getattr(res, "success", False):
self._log_shadow_ungated(generated_arguments, res)
sanitizer_triggered = bool(triggered_by)
return sanitizer_triggered, triggered_by, err_details
def _check_temp_file_leak(self, leaked_files: set[str]) -> tuple[bool, str]:
"""Returns True and diagnostic message if temporary files were leaked."""
if "check_temp_file_leak" in self.selected_sanitizer_names and leaked_files:
err = f"Temp file leak detected: {list(leaked_files)}. "
for f in leaked_files:
try:
if os.path.isdir(f):
shutil.rmtree(f)
else:
os.remove(f)
except Exception:
pass
return True, err
return False, ""
def _check_process_leak(self, leaked_pids: set[int]) -> tuple[bool, str]:
"""Returns True and diagnostic message if background processes were leaked."""
if ("check_process_and_fd_leaks" in self.selected_sanitizer_names or "check_terminal_dos_and_leak" in self.selected_sanitizer_names) and leaked_pids:
details = []
snap = getattr(self, "_last_ps", None) or _ps_snapshot()
for pid in leaked_pids:
info = snap.get(pid)
details.append(f"PID {pid} ({info[1]})" if info and info[1] else f"PID {pid}")
err = f"Process leak/leftover detected: {', '.join(details)}. "
return True, err
return False, ""
# Transport-level failures. When the runner cannot speak to the server at
# all, `elapsed` measures OUR socket timeout, not the tool's execution.
_TRANSPORT_FAILURES = ("socket error", "socket communication error",
"is not running", "failed to restart",
"broken pipe", "connection reset", "eof")
def _is_transport_failure(self, res) -> bool:
if res is None:
return True
if getattr(res, "success", True):
return False
err = (getattr(res, "error", "") or "").lower()
return any(k in err for k in self._TRANSPORT_FAILURES)
def _check_resource_exhaustion(self, elapsed: float, res=None) -> tuple[bool, str]:
"""Returns True and diagnostic message if execution duration exceeds config limits.
A TRANSPORT FAILURE IS NOT A TIMING MEASUREMENT. js_runner sets
sock.settimeout(5.0) while timeout_limit defaults to 2.0, so before this
guard every timed-out read scored elapsed~5.0 > 2.0 and was reported as
resource exhaustion -- with Tool Output None, because nothing ran. The
runner then restarts the server, whose boot time makes the NEXT read
time out too, so one dead server produced a cascade of identical
"findings" (measured: 21 on mcp-appium-gestures::generate-swipe-code,
gpt41-full run 2026-09-11, all check_resource_exhaustion, all 5.0-5.2s).
A genuine hang also surfaces as a socket timeout, so this guard trades
recall for soundness: it cannot distinguish "tool hung" from "server
died". Separating them needs a liveness probe after the timeout --
nazmul.todo item 39, still open.
"""
if "check_resource_exhaustion" not in self.selected_sanitizer_names:
return False, ""
if self._is_transport_failure(res):
return False, ""
if elapsed > self.config.timeout_limit:
err = f"Resource exhaustion/Timeout detected: Execution took {elapsed:.2f} seconds (Limit: {self.config.timeout_limit}s). "
return True, err
return False, ""
def _check_unexpected_crash(self, res: any) -> tuple[bool, str]:
"""Returns True and diagnostic message if an unhandled library exception is captured."""
if not res.success and res.error:
if any(crash_name in res.error for crash_name in self.config.unexpected_crashes):
err = f"Unexpected library/runtime crash: {res.error}. "
return True, err
return False, ""
def _log_shadow_ungated(self, generated_arguments: dict, res: any) -> None:
"""Oracle ablation: which input sanitizers would fire on this FAILED execution
if the silent-success gate were removed. One line per new signature per tool."""
try: