Files
ResearchMethods_Project/validation/README.md
T

250 lines
8.4 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`; git-tracked copies
are in `spss_all/raw/`. Comments-removed runs use
`results/*-comments-removed.csv`.
## Comments-only condition
`obfuscator_comments_only.py` creates `Samples_CommentsRemoved` by removing
only comments and docstrings. Identifiers, strings, routes, templates, and
formatting are preserved. Per-file statistics are recorded in
`comments_removed_report.json`.
```bash
python3 obfuscator_comments_only.py -i Samples -o Samples_CommentsRemoved \
--report validation/comments_removed_report.json
```
Bandit and Semgrep outputs are `validation/{bandit,semgrep}/comments_removed.json`;
LLM outputs are in `results/*-comments-removed.csv` and copied to
`spss_all/raw/`.
## SPSS exports
`export_spss_all.py` combines the traditional SAST tools (Bandit and Semgrep),
DeepSeek V4.1 Flash (open-weight, evaluated through a hosted endpoint), and
Gemini 4 Argon (frontier hosted) into one SPSS-ready dataset covering all three
conditions. The script reads the tool exports from
`sast_tools/tools_long.csv` and the LLM runs from the SQLite databases in
`results/`, then writes to `validation/spss_all/`:
- `sast_all_long.csv` / `.sav` — 2,400 rows (4 detectors × 3 conditions × 200 samples)
- `sast_all_wide.csv` / `.sav` — 200 paired rows
- `sast_all_summary.csv` / `.sav` — 12 aggregate rows
- `sast_all_category.csv` / `.sav` — 168 per-category rows
- `sast_all_paired.csv` — 12 within-detector condition-pair McNemar tests
- `sast_all_between.csv` — 18 between-detector McNemar tests
- `codebook.csv` / `codebook.md`
- `.sps` import syntax for the CSV files
```bash
python3 validation/export_spss_all.py
uv run --python 3.13 --with pandas --with pyreadstat python validation/export_sav.py
```
See `spss_all/codebook.md` for variable codes and import instructions.
## Contents
```text
validation/
├── test_pipeline.py # mock-API end-to-end test
├── export_spss.py # shared SPSS helpers (legacy DeepSeek export)
├── export_spss_all.py # combined SAST + LLM SPSS exports
├── export_sav.py # convert SPSS CSVs to native .sav
├── 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
│ └── comments_removed.json # raw Bandit JSON, comments removed
├── semgrep/
│ ├── samples.json # raw Semgrep JSON, original
│ ├── obfuscated_v2.json # raw Semgrep JSON, obfuscated
│ └── comments_removed.json # raw Semgrep JSON, comments removed
├── deepseek_comparison.md # v1 pilot analysis (historical)
├── sast_tools/
│ ├── findings.csv
│ ├── bandit_findings.csv
│ ├── semgrep_findings.csv
│ ├── tools_long.csv
│ ├── tools_summary.csv
│ ├── codebook.md
│ ├── sast_tools.sps
│ └── README.md
└── spss_all/
├── sast_all_long.csv / .sav
├── sast_all_wide.csv / .sav
├── sast_all_summary.csv / .sav
├── sast_all_category.csv / .sav
├── sast_all_paired.csv
├── sast_all_between.csv
├── codebook.csv / .md
├── sast_all_import.sps
├── sast_all_wide.sps
├── sast_all_summary.sps
├── sast_all_category.sps
└── raw/
├── README.md
├── llm_*.csv
└── sast_*.csv
```