Files
ResearchMethods_Project/validation/README.md
T
Joren 033e5d0ea3 Add Bandit and Semgrep per-sample comparison exports
- Run Semgrep p/python on original and obfuscated samples
- Add raw Bandit/Semgrep findings and per-sample target-category flags
- Add SPSS-ready tool-by-condition long file and summary
- Document tool mappings, coverage limits, and obfuscation effects
2026-11-10 20:33:23 +01:00

5.9 KiB
Raw Blame History

Validation artifacts

This directory contains reproducible checks and the executed results used to validate the obfuscated dataset condition.

Automated pipeline test

The test starts a local mock OpenAI-compatible API and runs the real run_sast_eval.py CLI against four benchmark samples. It covers:

  • valid JSON
  • fenced JSON
  • malformed JSON followed by a repair request
  • a 429 retry
  • SQLite persistence
  • CSV export
  • --report without API calls
  • dry-run without an API key

Run it with the standard library only:

python3 -m unittest validation/test_pipeline.py -v

Current result: 3 tests passed.

Bandit comparison

Bandit was installed in a uv virtual environment:

uv venv --python 3.14 .venv
uv pip install --python .venv/bin/python -r requirements-dev.txt

Commands:

.venv/bin/bandit -r Samples -f json -o validation/bandit/samples.json
.venv/bin/bandit -r Samples_Obfuscated_v2 -f json -o validation/bandit/obfuscated_v2.json

Result:

  • Original: 89 findings
  • Obfuscated v2: 86 findings
  • 86 findings identical by file, rule, severity, and confidence
  • No new findings
  • The only difference is 3 B608 findings in BenchmarkTest00192.py, BenchmarkTest00284.py, and BenchmarkTest00286.py

B608 is a string-pattern rule for SQL-looking text. The obfuscator replaces SQL strings with generic msg tokens, so those findings disappear. The SQL sink and data flow remain. With B608 skipped, both directories produce exactly 86/86 identical findings.

See bandit_comparison.txt for the full report and bandit/*.json for raw Bandit output.

Semgrep comparison

Semgrep 1.185.0 was run with the p/python ruleset:

.venv/bin/semgrep scan --config p/python --json \
  --output validation/semgrep/samples.json Samples
.venv/bin/semgrep scan --config p/python --json \
  --output validation/semgrep/obfuscated_v2.json Samples_Obfuscated_v2

Result:

  • Original: 27 findings
  • Obfuscated v2: 24 findings
  • Three findings disappear after obfuscation:
    • request-data-write (CWE-93) in BenchmarkTest00472 — out of scope
    • avoid-insecure-deserialization (CWE-502) in BenchmarkTest00508 — false positive
    • path-traversal-open (CWE-22) in BenchmarkTest01196 — true positive

Semgrep’s path-traversal finding is lost because the obfuscator changes the identifier/request structure the pattern depends on. Raw JSON is in semgrep/.

SAST tool comparison

export_sast_tools.py combines the Bandit and Semgrep findings into per-sample and SPSS-ready files under sast_tools/:

  • findings.csv — all raw findings
  • tools_long.csv — 800 rows: tool × condition × sample
  • tools_summary.csv — aggregate metrics
  • sast_tools.sps — SPSS import/analysis syntax
  • codebook.md
python3 validation/export_sast_tools.py

See sast_tools/README.md for the mapping rules and results.

DeepSeek comparison

Two runs were executed with opencode-go/deepseek-v4.1-flash and prompts/sast_v1.txt:

Metric Original Obfuscated v2
TP 46 46
FP 13 18
TN 98 93
FN 0 0
Accuracy 91.7% 88.5%
Precision 78.0% 71.9%
Recall 100% 100%
Category accuracy (TP) 100% 100%
CWE accuracy (TP) 100% 100%
Total tokens 381,410 472,990
API calls 201 202

Paired McNemar test on the 157 in-scope samples: p = 0.125, so the +5 false-positive difference is not statistically significant in this single run.

Caveat: the original run used --workers 4 and the obfuscated run used --workers 1, so wall-clock times are not comparable. See deepseek_comparison.md for the per-category breakdown, new false positives, and the recommended replication.

Raw per-sample outputs are in deepseek/*.csv; deepseek/summary.csv contains the comparison table in machine-readable form.

SPSS exports

export_spss.py reads the two DeepSeek runs from results/sast_runs.sqlite3 and writes importable files to validation/spss/:

  • sast_long.csv — 400 rows (200 samples × 2 conditions)
  • sast_wide.csv — 200 paired rows
  • sast_summary.csv — aggregate metrics
  • sast_paired.csv — McNemar counts and exact p-value
  • codebook.csv / codebook.md
  • sast_import.sps / sast_wide.sps / sast_summary.sps
python3 validation/export_spss.py

See spss/README.md for import instructions and suggested analyses.

Contents

validation/
├── test_pipeline.py                 # mock-API end-to-end test
├── export_spss.py                   # generate SPSS-ready LLM CSVs/syntax
├── export_sast_tools.py             # generate Bandit/Semgrep CSVs/syntax
├── bandit_comparison.txt            # human-readable Bandit diff
├── bandit/
│   ├── samples.json                 # raw Bandit JSON, original
│   └── obfuscated_v2.json           # raw Bandit JSON, obfuscated
├── semgrep/
│   ├── samples.json                 # raw Semgrep JSON, original
│   └── obfuscated_v2.json           # raw Semgrep JSON, obfuscated
├── deepseek_comparison.md           # original vs obfuscated analysis
├── deepseek/
│   ├── deepseek-original.csv        # per-sample original results
│   ├── deepseek-v4.1-flash.csv      # per-sample obfuscated results
│   └── summary.csv                  # comparison metrics
├── sast_tools/
│   ├── findings.csv
│   ├── bandit_findings.csv
│   ├── semgrep_findings.csv
│   ├── tools_long.csv
│   ├── tools_summary.csv
│   ├── codebook.md
│   ├── sast_tools.sps
│   └── README.md
└── spss/
    ├── sast_long.csv
    ├── sast_wide.csv
    ├── sast_summary.csv
    ├── sast_paired.csv
    ├── codebook.csv
    ├── codebook.md
    ├── sast_import.sps
    ├── sast_wide.sps
    ├── sast_summary.sps
    └── README.md