shipsafe 0.2.0

AI-Powered Pre-Deploy Security Gate
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
//! AI triage: send scan findings (with surrounding code context) to the
//! Claude API and classify each one as a true positive, a false positive, or
//! uncertain. False positives stay in the report — annotated with the model's
//! reasoning — but are excluded from the `--fail-on` gate, so noisy findings
//! stop breaking builds without ever being silently hidden.
//!
//! Opt-in only (`ai.triage: true` or `--ai-triage`) and BYOK: the request is
//! sent directly to the Anthropic API with the user's own ANTHROPIC_API_KEY.
//! Any failure (missing key, network, refusal) degrades gracefully: the scan
//! result is simply left untriaged and the gate behaves exactly as without AI.

use crate::config::Config;
use crate::scanners::{Finding, ScanResults};
use anyhow::{anyhow, bail, Context, Result};
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::time::Duration;

pub const API_KEY_ENV: &str = "ANTHROPIC_API_KEY";
const API_URL: &str = "https://api.anthropic.com/v1/messages";
const ANTHROPIC_VERSION: &str = "2023-06-01";
/// 1 initial try + 2 retries on rate limits / server errors.
const MAX_ATTEMPTS: u32 = 3;
/// Lines of code context included before and after each finding line.
const CONTEXT_LINES: usize = 12;
/// Findings descriptions are clipped to keep the request small.
const MAX_DESCRIPTION_CHARS: usize = 400;

/// Triage verdict attached to a finding.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Verdict {
    /// Likely exploitable / real — keeps gating the build.
    TruePositive,
    /// Not exploitable in this context — annotated, excluded from the gate.
    FalsePositive,
    /// The model could not decide — treated like a true positive (fail safe).
    Uncertain,
}

impl Verdict {
    pub fn label(&self, lang: &str) -> &'static str {
        if lang == "ja" {
            match self {
                Verdict::TruePositive => "要対応",
                Verdict::FalsePositive => "誤検知",
                Verdict::Uncertain => "要確認",
            }
        } else {
            match self {
                Verdict::TruePositive => "true positive",
                Verdict::FalsePositive => "false positive",
                Verdict::Uncertain => "uncertain",
            }
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TriageConfidence {
    Low,
    Medium,
    High,
}

impl TriageConfidence {
    pub fn label(&self, lang: &str) -> &'static str {
        if lang == "ja" {
            match self {
                TriageConfidence::Low => "",
                TriageConfidence::Medium => "",
                TriageConfidence::High => "",
            }
        } else {
            match self {
                TriageConfidence::Low => "low",
                TriageConfidence::Medium => "medium",
                TriageConfidence::High => "high",
            }
        }
    }
}

/// AI triage result carried on a `Finding` and serialized into JSON / SARIF
/// reports, so every verdict is auditable.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Triage {
    pub verdict: Verdict,
    pub confidence: TriageConfidence,
    /// Short explanation of the verdict, in the configured output language.
    pub reason: String,
    /// Model that produced the verdict.
    pub model: String,
}

/// Counts reported after a triage run.
#[derive(Debug, Default)]
pub struct TriageSummary {
    pub triaged: usize,
    pub true_positives: usize,
    pub false_positives: usize,
    pub uncertain: usize,
    /// Findings beyond `ai.max-findings` that were left untriaged.
    pub skipped: usize,
}

/// Run AI triage over the scan results, annotating findings in place.
///
/// Errors are returned to the caller, which warns and continues — triage must
/// never break the gate.
pub async fn run(
    results: &mut ScanResults,
    scan_path: &Path,
    config: &Config,
) -> Result<TriageSummary> {
    if results.findings.is_empty() {
        return Ok(TriageSummary::default());
    }

    let api_key = std::env::var(API_KEY_ENV)
        .ok()
        .filter(|k| !k.trim().is_empty())
        .ok_or_else(|| anyhow!("{} is not set", API_KEY_ENV))?;

    // Severity-descending order; cap at ai.max-findings to bound cost.
    let mut order: Vec<usize> = (0..results.findings.len()).collect();
    order.sort_by(|a, b| {
        results.findings[*b]
            .severity
            .cmp(&results.findings[*a].severity)
    });
    let selected: Vec<usize> = order.into_iter().take(config.ai.max_findings).collect();
    let skipped = results.findings.len() - selected.len();

    let prompt = build_prompt(&results.findings, &selected, scan_path);
    let body = build_request_body(&config.ai.model, &config.lang, &prompt);

    let response = send_with_retries(&api_key, &body, config.ai.timeout_seconds).await?;
    let verdicts = parse_response(&response)?;

    let mut summary = TriageSummary {
        skipped,
        ..TriageSummary::default()
    };
    for v in verdicts {
        let Some(&finding_idx) = selected.get(v.index) else {
            tracing::warn!("triage verdict for unknown finding index {}", v.index);
            continue;
        };
        match v.verdict {
            Verdict::TruePositive => summary.true_positives += 1,
            Verdict::FalsePositive => summary.false_positives += 1,
            Verdict::Uncertain => summary.uncertain += 1,
        }
        summary.triaged += 1;
        results.findings[finding_idx].ai_triage = Some(Triage {
            verdict: v.verdict,
            confidence: v.confidence,
            reason: v.reason,
            model: config.ai.model.clone(),
        });
    }
    Ok(summary)
}

/// Per-finding verdict as returned by the model (indexes into the list of
/// findings that were sent, not into `results.findings`).
#[derive(Debug, Deserialize)]
struct RawVerdict {
    index: usize,
    verdict: Verdict,
    confidence: TriageConfidence,
    reason: String,
}

#[derive(Debug, Deserialize)]
struct RawVerdicts {
    verdicts: Vec<RawVerdict>,
}

fn system_prompt(lang: &str) -> String {
    let reason_lang = if lang == "ja" { "Japanese" } else { "English" };
    format!(
        "You are the triage engine of ShipSafe, a pre-deploy security gate. You receive \
         findings from SAST, dependency (SCA), and secret scanners, each with surrounding \
         code context. Classify every finding:\n\
         - true_positive: plausibly real and reachable in this code.\n\
         - false_positive: clearly not exploitable here — e.g. test/fixture/example code, \
         a placeholder or documented sample value, dead code, input that is already \
         sanitized or constant, or a finding that misreads the code.\n\
         - uncertain: not enough context to decide.\n\
         Be conservative: a false_positive verdict removes the finding from the build \
         gate, so only use it when the evidence in the provided context is clear. When \
         in doubt, answer uncertain. Never invent code that is not shown.\n\
         For each finding give a one-sentence reason in {reason_lang}, written for the \
         developer who will review the report."
    )
}

/// JSON schema the model's answer must conform to (structured outputs).
fn output_schema() -> serde_json::Value {
    serde_json::json!({
        "type": "object",
        "properties": {
            "verdicts": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "index": { "type": "integer", "description": "Finding index as given in the input" },
                        "verdict": { "type": "string", "enum": ["true_positive", "false_positive", "uncertain"] },
                        "confidence": { "type": "string", "enum": ["low", "medium", "high"] },
                        "reason": { "type": "string", "description": "One-sentence justification" }
                    },
                    "required": ["index", "verdict", "confidence", "reason"],
                    "additionalProperties": false
                }
            }
        },
        "required": ["verdicts"],
        "additionalProperties": false
    })
}

fn build_request_body(model: &str, lang: &str, prompt: &str) -> serde_json::Value {
    serde_json::json!({
        "model": model,
        "max_tokens": 16000,
        "thinking": { "type": "adaptive" },
        "system": system_prompt(lang),
        "messages": [ { "role": "user", "content": prompt } ],
        "output_config": { "format": { "type": "json_schema", "schema": output_schema() } }
    })
}

/// Render the selected findings (with code context) into the user prompt.
fn build_prompt(findings: &[Finding], selected: &[usize], scan_path: &Path) -> String {
    let mut prompt = String::with_capacity(selected.len() * 1024);
    prompt.push_str(
        "Triage the following security findings. Reply with one verdict per finding, \
         keyed by its index.\n",
    );
    for (sent_idx, &finding_idx) in selected.iter().enumerate() {
        let f = &findings[finding_idx];
        let mut description = f.description.clone();
        if description.len() > MAX_DESCRIPTION_CHARS {
            let mut cut = MAX_DESCRIPTION_CHARS;
            while !description.is_char_boundary(cut) {
                cut -= 1;
            }
            description.truncate(cut);
            description.push('');
        }
        prompt.push_str(&format!(
            "\n## Finding {sent_idx}\n- rule: {} (scanner: {})\n- severity: {}\n- title: {}\n",
            f.id, f.scanner, f.severity, f.title
        ));
        if !description.is_empty() {
            prompt.push_str(&format!("- description: {description}\n"));
        }
        let location = match f.line {
            Some(line) => format!("{}:{}", f.file, line),
            None => f.file.clone(),
        };
        prompt.push_str(&format!("- location: {location}\n"));
        if let Some(snippet) = code_context(scan_path, f) {
            prompt.push_str(&format!("- code context:\n```\n{snippet}```\n"));
        }
    }
    prompt
}

/// Read ±CONTEXT_LINES around the finding line, with line numbers and a
/// `>` marker on the flagged line. Returns None when the file or line is
/// unavailable (e.g. SCA findings on lockfiles are judged without context).
fn code_context(scan_path: &Path, finding: &Finding) -> Option<String> {
    let line = finding.line? as usize;
    if finding.file.is_empty() {
        return None;
    }
    let path = Path::new(&finding.file);
    let resolved = if path.is_absolute() {
        path.to_path_buf()
    } else {
        scan_path.join(path)
    };
    let content = std::fs::read_to_string(&resolved).ok()?;
    let lines: Vec<&str> = content.lines().collect();
    if line == 0 || line > lines.len() {
        return None;
    }
    let start = line.saturating_sub(CONTEXT_LINES + 1);
    let end = (line + CONTEXT_LINES).min(lines.len());
    let mut snippet = String::new();
    for (i, text) in lines[start..end].iter().enumerate() {
        let n = start + i + 1;
        let marker = if n == line { ">" } else { " " };
        snippet.push_str(&format!("{marker}{n:>5} | {text}\n"));
    }
    Some(snippet)
}

/// POST to the Messages API, retrying rate limits and server errors with
/// backoff. Other HTTP errors fail immediately.
async fn send_with_retries(
    api_key: &str,
    body: &serde_json::Value,
    timeout_seconds: u64,
) -> Result<serde_json::Value> {
    let client = reqwest::Client::builder()
        .timeout(Duration::from_secs(timeout_seconds))
        .build()
        .context("failed to build HTTP client")?;

    let mut last_error = None;
    for attempt in 1..=MAX_ATTEMPTS {
        let result = client
            .post(API_URL)
            .header("x-api-key", api_key)
            .header("anthropic-version", ANTHROPIC_VERSION)
            .header("content-type", "application/json")
            .json(body)
            .send()
            .await;

        match result {
            Ok(response) => {
                let status = response.status();
                if status.is_success() {
                    return response.json().await.context("invalid JSON from the API");
                }
                let text = response.text().await.unwrap_or_default();
                let first = text.lines().next().unwrap_or("").to_string();
                let retryable = status.as_u16() == 429 || status.is_server_error();
                last_error = Some(anyhow!("API returned {status}: {first}"));
                if !retryable {
                    break;
                }
            }
            Err(e) => {
                last_error = Some(anyhow!(e).context("request to the Anthropic API failed"));
            }
        }
        if attempt < MAX_ATTEMPTS {
            tokio::time::sleep(Duration::from_millis(1000 * u64::from(attempt))).await;
        }
    }
    Err(last_error.unwrap_or_else(|| anyhow!("triage request failed")))
}

/// Extract the verdicts from a Messages API response.
fn parse_response(response: &serde_json::Value) -> Result<Vec<RawVerdict>> {
    if response.get("stop_reason").and_then(|s| s.as_str()) == Some("refusal") {
        bail!("the model declined to triage this scan (stop_reason: refusal)");
    }
    if response.get("stop_reason").and_then(|s| s.as_str()) == Some("max_tokens") {
        bail!("triage response was truncated (stop_reason: max_tokens)");
    }
    let text = response
        .get("content")
        .and_then(|c| c.as_array())
        .and_then(|blocks| {
            blocks
                .iter()
                .find(|b| b.get("type").and_then(|t| t.as_str()) == Some("text"))
        })
        .and_then(|b| b.get("text"))
        .and_then(|t| t.as_str())
        .ok_or_else(|| anyhow!("no text block in the API response"))?;
    let parsed: RawVerdicts =
        serde_json::from_str(text).context("triage response did not match the expected schema")?;
    Ok(parsed.verdicts)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::scanners::Severity;

    fn finding(id: &str, severity: Severity, file: &str, line: Option<u32>) -> Finding {
        Finding {
            id: id.into(),
            scanner: "sast".into(),
            severity,
            title: id.into(),
            description: "desc".into(),
            file: file.into(),
            line,
            cwe: None,
            cve: None,
            fix_suggestion: None,
            ai_triage: None,
        }
    }

    #[test]
    fn test_parse_response_extracts_verdicts() {
        let response = serde_json::json!({
            "stop_reason": "end_turn",
            "content": [
                { "type": "thinking", "thinking": "..." },
                { "type": "text", "text": r#"{"verdicts":[
                    {"index":0,"verdict":"false_positive","confidence":"high","reason":"test fixture"},
                    {"index":1,"verdict":"true_positive","confidence":"medium","reason":"reachable"}
                ]}"# }
            ]
        });
        let verdicts = parse_response(&response).unwrap();
        assert_eq!(verdicts.len(), 2);
        assert_eq!(verdicts[0].verdict, Verdict::FalsePositive);
        assert_eq!(verdicts[0].confidence, TriageConfidence::High);
        assert_eq!(verdicts[1].verdict, Verdict::TruePositive);
    }

    #[test]
    fn test_parse_response_refusal_is_error() {
        let response = serde_json::json!({ "stop_reason": "refusal", "content": [] });
        let err = parse_response(&response).unwrap_err();
        assert!(err.to_string().contains("refusal"));
    }

    #[test]
    fn test_parse_response_truncated_is_error() {
        let response = serde_json::json!({ "stop_reason": "max_tokens", "content": [] });
        assert!(parse_response(&response).is_err());
    }

    #[test]
    fn test_parse_response_invalid_json_is_error() {
        let response = serde_json::json!({
            "stop_reason": "end_turn",
            "content": [ { "type": "text", "text": "not json" } ]
        });
        assert!(parse_response(&response).is_err());
    }

    #[test]
    fn test_build_prompt_includes_findings_and_indexes() {
        let findings = vec![
            finding("sql-injection", Severity::Critical, "app.py", Some(3)),
            finding("xss", Severity::Medium, "web.js", None),
        ];
        let prompt = build_prompt(&findings, &[0, 1], Path::new("/nonexistent"));
        assert!(prompt.contains("## Finding 0"));
        assert!(prompt.contains("sql-injection"));
        assert!(prompt.contains("app.py:3"));
        assert!(prompt.contains("## Finding 1"));
        assert!(prompt.contains("web.js"));
    }

    #[test]
    fn test_build_prompt_clips_long_descriptions() {
        let mut f = finding("rule", Severity::Low, "a.py", Some(1));
        f.description = "x".repeat(2000);
        let prompt = build_prompt(std::slice::from_ref(&f), &[0], Path::new("/nonexistent"));
        assert!(prompt.contains(''));
        assert!(prompt.len() < 2000);
    }

    #[test]
    fn test_code_context_marks_finding_line() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("app.py");
        let body: String = (1..=40).map(|i| format!("line {i}\n")).collect();
        std::fs::write(&file, body).unwrap();

        let f = finding("rule", Severity::High, "app.py", Some(20));
        let snippet = code_context(dir.path(), &f).unwrap();
        assert!(snippet.contains(">   20 | line 20"));
        assert!(snippet.contains("    8 | line 8"));
        assert!(snippet.contains("   32 | line 32"));
        assert!(!snippet.contains("line 7\n"));
    }

    #[test]
    fn test_code_context_missing_file_is_none() {
        let f = finding("rule", Severity::High, "missing.py", Some(1));
        assert!(code_context(Path::new("/nonexistent-shipsafe"), &f).is_none());
    }

    #[test]
    fn test_code_context_out_of_range_line_is_none() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("a.py"), "one line\n").unwrap();
        let f = finding("rule", Severity::High, "a.py", Some(99));
        assert!(code_context(dir.path(), &f).is_none());
    }

    #[test]
    fn test_request_body_shape() {
        let body = build_request_body("claude-opus-4-8", "ja", "PROMPT");
        assert_eq!(body["model"], "claude-opus-4-8");
        assert_eq!(body["thinking"]["type"], "adaptive");
        assert_eq!(body["output_config"]["format"]["type"], "json_schema");
        assert_eq!(body["messages"][0]["content"], "PROMPT");
        assert!(body["system"].as_str().unwrap().contains("Japanese"));
        let schema = &body["output_config"]["format"]["schema"];
        assert_eq!(schema["properties"]["verdicts"]["type"], "array");
    }

    #[tokio::test]
    async fn test_run_without_api_key_fails_gracefully() {
        // Ensure the variable is unset for this test.
        std::env::remove_var(API_KEY_ENV);
        let mut results = ScanResults::new();
        results
            .findings
            .push(finding("rule", Severity::High, "a.py", Some(1)));
        results.recalculate_summary();

        let config = Config::default();
        let err = run(&mut results, Path::new("."), &config)
            .await
            .unwrap_err();
        assert!(err.to_string().contains(API_KEY_ENV));
        assert!(results.findings[0].ai_triage.is_none());
    }
}