# Security audit (Phase 3 Step 2.7)

The mechanics of the conditional security audit that runs inside the Phase 3 review window and merges at Step 3.0. The phase doc carries the trigger and the merge; the rest is here.

## Trigger (deterministic, no new classifier)

Run the audit when any of these holds:

- Step 1.75 scored `security_path` on a file in the diff (the same signal that already forces `full` review at Step 1.77).
- The base branch is a release branch.
- The run is the standalone `/multi-agent:security-review` command, which dispatches straight to this step.

There is no `--audit` flag: the trigger is the diff, not a word the user has to remember. When none of these holds, the audit does not run, no `security-audit-$ITERATION.json` is written, and the Step 3.0 merge and the review-decision `--source` are no-ops.

## Threat model first

The auditor reads `.pipeline/threat-model.md` if Phase 1 or a prior step wrote it, and produces it (four sections) if absent. Contract: `$HOME/.claude/multi-agent-refs/threat-model.md`. It is keyed to repo+branch and mirrored to `state.threatModel` so a resume reuses it rather than re-deriving it.

## Dispatch

One `Agent(subagent_type: "security-auditor")` on the same capped diff the reviewers saw, plus the threat model and the resolved `${CRITERIA}` block:

```
Agent(subagent_type: "security-auditor", prompt: "<threat-model + capped diff + ${CRITERIA}>")
```

Persona contract: `~/.claude/agents/security-auditor.md`.

## Output is reviewer-shaped, and that is the point

The auditor returns one object conforming to `$HOME/.claude/schemas/reviewer-output.schema.json`, whose `findings[]` each carry the `security` envelope of `security-finding.schema.json`: OWASP category, CWE, CVSS vector + band, evidence, counterevidence, confidence, closure, assumptions, remediation, and the before/after fix; the object also carries `securityCoverage[]`. Severity is the reviewer enum, derived from the CVSS band:

| CVSS band | severity |
| --------- | -------- |
| critical / high | blocking |
| medium | important |
| low / none | suggestion |

Compute `security.cvss.baseScore` + `band` with the toolkit `mcp__multi-agent__security_cvss_score` tool, not by hand, so the score cannot drift from the vector.

## Validate, persist, merge

Validate with the same gate protocol as a reviewer  -  the exit code decides, not the LLM turn:

```bash
printf '%s' "$SECURITY_AUDIT_JSON" | node "$HOME/.claude/scripts/validate-reviewer.mjs" -
```

On validator failure: one self-correction rework, then HALT the phase (identical to the reviewer output-contract gate).

Then check that every cited location exists. The schema cannot know whether `src/api/users.ts:42` is real; this gate opens the files:

```bash
printf '%s' "$SECURITY_AUDIT_JSON" | node "$HOME/.claude/scripts/security-evidence-gate.mjs" --findings - --root "$WORKTREE"
```

Exit 1 lists each citation that does not resolve (missing file, line past the end, a `remediationDiff.before` snippet not in the file, a `ruled_out` control that is not there). Send that list back to the auditor once; a finding whose citation still does not resolve is dropped and named in the Phase 3 output as "evidence did not resolve". Exit 2 is a gate error, never a pass.

Outside a pipeline worktree (a standalone `/multi-agent:security-review`, or a host where the pipeline scripts are not installed), `mcp__multi-agent__security_evidence_check` is the host-independent equivalent: `findings` (the array, an object holding `findings`, or an absolute JSON path), `root` (absolute repository root) and optional `diff` (unified diff text or an absolute `.diff` path, which also requires each non-`repo` finding to sit inside a changed hunk). `ok: false` with its `failures[]` is handled exactly like exit 1 above. When both are available, the script is the gate and the tool is not called a second time.

Persist the object to `state.reviewIterations[<iteration>].securityAudit` and write it to a file, which Step 3.0 merges and `review-decision-gate.mjs` reads as one independent `--source`:

```bash
printf '%s' "$SECURITY_AUDIT_JSON" > "$WORKTREE/.pipeline/security-audit-$ITERATION.json"
```

Because the output is reviewer-shaped, the Step 3.0 merge appends its `findings[]` alongside the test-integrity findings. A `blocking` security finding then reaches triage, and a triage-accepted blocker blocks Phase 4  -  the "critical security items block the commit" contract is wired here, not merely stated.

## Proof state, coverage and duplicates

- **Open proof gaps stay visible.** A finding with `closure: open_proof_gap` goes through triage like any other, and the Phase 3 summary lists every one of them with the unproven link, whatever triage decided. It is never dropped because it is unproven.
- **Coverage.** `securityCoverage[]` from the audit is persisted with the findings and summarised in the Phase 3 output as counts per outcome. Every `needs_follow_up` row is listed by surface and risk area.
- **One finding per root cause.** Before triage, two security findings are merged when they share all four of: root cause, component, attack vector and fix. The merged finding keeps the higher CVSS band, every cited location, and both evidence texts. Findings that share three of the four stay separate.

## SARIF export

Optional output. `mcp__multi-agent__security_sarif_export` turns the persisted audit into SARIF 2.1.0 for code-scanning views: one rule per `ruleId` (else CWE), level from the CVSS band, the cited file and line as the physical location. Pass `findings` as the absolute path of `.pipeline/security-audit-$ITERATION.json`, optional `toolName`, and an absolute `outPath` (its directory must exist) to write the `.sarif` file instead of returning it; the tool never overwrites, so use a new name per run (`<repo>/.pipeline/security-findings-<timestamp>.sarif`). Nothing in the gate depends on it.

## Store-compliance cross-reference

On store-relevant diffs the auditor also cites the Apple ITMS / Google Play catalog rule on the finding via `ruleId` + `criteriaSource` (`apple-archive-compliance` / `google-play-compliance`). This is annotation on the same finding, not a second pass; the device-level `store-ready` Gate under `/multi-agent:test` stays separate.
