Files
ResearchMethods_Project/validation/README.md
T
Joren 26da3173e9 Switch runner to 14-category CWE prompt and strict scoring
- Add sast_v2 prompt covering all BenchmarkPython CWEs
- Accept boolean vulnerable plus CWE and derive category from CWE
- Score flag accuracy separately from strict flag+CWE accuracy
- Report wrong-category true positives explicitly
- Run DeepSeek v2 on all 200 original and obfuscated samples
- Extend LLM and SAST exports with strict correctness and CWE fields
2026-11-10 22:58:36 +01:00

224 lines
7.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 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:
```bash
python3 -m unittest validation/test_pipeline.py -v
```
Current result: **3 tests passed**.
## Bandit comparison
Bandit was installed in a `uv` virtual environment:
```bash
uv venv --python 3.14 .venv
uv pip install --python .venv/bin/python -r requirements-dev.txt
```
Commands:
```bash
.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:
```bash
.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`
```bash
python3 validation/export_sast_tools.py
```
See `sast_tools/README.md` for the mapping rules and results.
## DeepSeek comparison
### v2 prompt: all fourteen categories
Two runs were executed with `opencode-go/deepseek-v4.1-flash` and
`prompts/sast_v2.txt`, covering all 200 samples:
| Metric | Original | Obfuscated v2 |
|---|---:|---:|
| TP | 65 | 67 |
| FP | 14 | 18 |
| TN | 119 | 115 |
| FN | 2 | 0 |
| Accuracy | 92.0% | 91.0% |
| Strict accuracy (flag + CWE) | 92.0% | 91.0% |
| Precision | 82.3% | 78.8% |
| Recall | 97.0% | 100% |
| FPR | 10.5% | 13.5% |
| Balanced accuracy | 93.2% | 93.2% |
| F1 | 89.0% | 88.2% |
| MCC | 0.835 | 0.826 |
| Category accuracy (TP) | 100% | 100% |
| CWE accuracy (TP) | 100% | 100% |
| Wrong-category TPs | 0 | 0 |
| Total tokens | 378,040 | 505,773 |
| API calls | 200 | 200 |
Paired McNemar test on all 200 samples: **p = 0.774**. The small
original-vs-obfuscated difference is not statistically significant. The
original run missed two `hash` samples; the obfuscated run detected all 67
vulnerable samples but produced four more false positives.
**Strict accuracy** requires the vulnerable/safe flag to be correct and, for
vulnerable samples, the predicted CWE to match the benchmark CWE. A vulnerable
sample flagged vulnerable with the wrong CWE is a detection true positive but
not a strict-correct classification; `wrong_category_tp` counts these. There
were zero in both v2 runs, so strict accuracy equals flag accuracy here.
### v1 prompt: nine categories (pilot)
The earlier pilot used `prompts/sast_v1.txt` and scored only the 157 samples in
its nine target categories:
| 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% |
| Total tokens | 381,410 | 472,990 |
| API calls | 201 | 202 |
Paired McNemar on the 157 in-scope samples: **p = 0.125**. This pilot is kept for
comparison but is superseded by the v2 all-category runs.
Raw per-sample outputs are in `results/deepseek-v2-*.csv` and the older
`deepseek/*.csv` pilot files. The SPSS exports use the v2 runs by default.
## 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`
```bash
python3 validation/export_spss.py
```
See `spss/README.md` for import instructions and suggested analyses.
## Contents
```text
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-v2-original.csv # v2, all 200, original
│ ├── deepseek-v2-obfuscated.csv # v2, all 200, obfuscated
│ ├── summary_v2.csv # v2 comparison metrics
│ ├── deepseek-original.csv # v1 pilot, original
│ ├── deepseek-v4.1-flash.csv # v1 pilot, obfuscated
│ └── summary.csv # v1 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
```