nyx-scanner 0.6.0

A multi-language static analysis tool for detecting security vulnerabilities
Documentation
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
//! Finding serialization and output routing.
//!
//! Serializes [`crate::commands::scan::Diag`] values to console, JSON, or
//! SARIF based on the requested format. `PATTERN_DESCRIPTIONS` is a
//! lazily-built map from pattern ID to human-readable description, populated
//! from all language registries on first access. `sarif_base_id` normalizes
//! source-location-suffixed finding IDs (like `"taint-unsanitised-flow (source 12:3)"`)
//! to the canonical SARIF rule ID form.

use crate::commands::scan::Diag;
use crate::patterns::{self, Severity};
use once_cell::sync::Lazy;
use serde_json::{Value, json};
use std::collections::HashMap;
use std::path::Path;

/// Lazily-built global map: pattern ID → description from all language registries.
static PATTERN_DESCRIPTIONS: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
    let mut map = HashMap::new();
    for lang in &[
        "rust",
        "c",
        "cpp",
        "java",
        "go",
        "php",
        "python",
        "ruby",
        "javascript",
        "typescript",
    ] {
        for p in patterns::load(lang) {
            map.entry(p.id).or_insert(p.description);
        }
    }
    map
});

/// CFG rule descriptions for rules not in the pattern registry.
fn cfg_rule_description(id: &str) -> Option<&'static str> {
    match id {
        "cfg-unguarded-sink" => Some("Dangerous sink reachable without prior guard or sanitizer"),
        "cfg-unreachable-sink" => Some("Sink in unreachable code"),
        "cfg-auth-gap" => Some("Entry-point handler reaches sink without authentication check"),
        "cfg-error-fallthrough" => {
            Some("Error check does not terminate; dangerous call follows on error path")
        }
        "cfg-resource-leak" => Some("Resource acquired but not released on all exit paths"),
        "cfg-lock-not-released" => Some("Lock acquired but not released on all exit paths"),
        "state-use-after-close" => Some("Variable used after its resource handle was closed"),
        "state-double-close" => Some("Resource handle closed more than once"),
        "state-resource-leak" => Some("Resource acquired but never closed"),
        "state-resource-leak-possible" => Some("Resource may not be closed on all paths"),
        "state-unauthed-access" => Some("Sensitive operation reached without authentication"),
        _ => None,
    }
}

/// Normalise a finding's id to the base SARIF rule id.
///
/// Findings carry source-location-suffixed ids like
/// `"taint-unsanitised-flow (source 12:3)"` so identical (source, sink)
/// pairs can be deduped, but SARIF wants a single rule per category.
/// Cap-specific taint rule classes (e.g. `taint-data-exfiltration`) are
/// preserved as distinct bases so consumers can filter on them rather than
/// folding everything into `taint-unsanitised-flow`.
fn sarif_base_id(id: &str) -> &str {
    if id.starts_with("taint-data-exfiltration") {
        "taint-data-exfiltration"
    } else if id.starts_with("taint-") {
        "taint-unsanitised-flow"
    } else {
        id
    }
}

/// Look up a human-readable description for any rule ID.
fn rule_description(id: &str) -> &str {
    // Strip taint-specific suffix for lookup (e.g. "taint-unsanitised-flow:foo.rs:42" → base)
    let base_id = sarif_base_id(id);

    if let Some(desc) = PATTERN_DESCRIPTIONS.get(base_id) {
        return desc;
    }
    if let Some(desc) = cfg_rule_description(base_id) {
        return desc;
    }
    match base_id {
        "taint-unsanitised-flow" => "Unsanitised data flows from source to sink",
        "taint-data-exfiltration" => {
            "Sensitive data flows into the payload of an outbound network request"
        }
        _ => id,
    }
}

fn severity_to_level(sev: Severity) -> &'static str {
    match sev {
        Severity::High => "error",
        Severity::Medium => "warning",
        Severity::Low => "note",
    }
}

/// Build a SARIF 2.1.0 JSON value from a list of diagnostics.
pub fn build_sarif(diags: &[Diag], scan_root: &Path) -> Value {
    // Deduplicate rule IDs and build rules array.
    let mut rule_ids: Vec<String> = Vec::new();
    let mut rule_index_map: HashMap<String, usize> = HashMap::new();

    for d in diags {
        let base = sarif_base_id(&d.id).to_string();
        if !rule_index_map.contains_key(&base) {
            let idx = rule_ids.len();
            rule_index_map.insert(base.clone(), idx);
            rule_ids.push(base);
        }
    }

    let rules: Vec<Value> = rule_ids
        .iter()
        .map(|id| {
            json!({
                "id": id,
                "shortDescription": { "text": rule_description(id) },
            })
        })
        .collect();

    let results: Vec<Value> = diags
        .iter()
        .map(|d| {
            let base = sarif_base_id(&d.id);
            let rule_index = rule_index_map[base];

            // Make path relative to scan root. Fall back to a deterministic
            // sentinel instead of the absolute path, SARIF must not leak
            // home-directory or host-specific prefixes.
            let uri = match Path::new(&d.path).strip_prefix(scan_root) {
                Ok(p) => p.to_string_lossy().to_string(),
                Err(_) => {
                    tracing::warn!(
                        path = %d.path,
                        scan_root = %scan_root.display(),
                        "SARIF: finding path is outside scan root; redacting"
                    );
                    "<out-of-root>".to_string()
                }
            };

            // Prefer the per-finding message (e.g. from state analysis) over the generic rule description.
            let msg_text = d
                .message
                .as_deref()
                .unwrap_or_else(|| rule_description(base));

            let mut result = json!({
                "ruleId": base,
                "ruleIndex": rule_index,
                "level": severity_to_level(d.severity),
                "message": { "text": msg_text },
                "locations": [{
                    "physicalLocation": {
                        "artifactLocation": { "uri": uri },
                        "region": {
                            "startLine": d.line,
                            "startColumn": d.col
                        }
                    }
                }]
            });

            // Emit SARIF `codeFlows` when the finding carries structured flow
            // steps.  Each step becomes a `threadFlows[0].locations[]` entry,
            // the SARIF-idiomatic encoding for data-flow paths; the primary
            // `locations[0]` above already names the true sink.
            if let Some(ev) = d.evidence.as_ref()
                && !ev.flow_steps.is_empty()
            {
                let thread_locations: Vec<Value> = ev
                    .flow_steps
                    .iter()
                    .map(|step| {
                        let step_uri = Path::new(&step.file)
                            .strip_prefix(scan_root)
                            .map(|p| p.to_string_lossy().to_string())
                            .unwrap_or_else(|_| step.file.clone());
                        let mut loc = json!({
                            "location": {
                                "physicalLocation": {
                                    "artifactLocation": { "uri": step_uri },
                                    "region": {
                                        "startLine": step.line,
                                        "startColumn": step.col
                                    }
                                },
                                "message": { "text": step.kind.to_string() }
                            }
                        });
                        if let Some(ref snippet) = step.snippet {
                            loc["location"]["physicalLocation"]["region"]["snippet"] =
                                json!({ "text": snippet });
                        }
                        loc
                    })
                    .collect();
                result["codeFlows"] = json!([{
                    "threadFlows": [{ "locations": thread_locations }]
                }]);
            }

            // Build properties object
            let mut props = serde_json::Map::new();
            props.insert("category".into(), json!(d.category.to_string()));
            if let Some(conf) = d.confidence {
                props.insert("confidence".into(), json!(conf.to_string()));
            }

            // `DATA_EXFIL` findings carry the destination object-literal
            // field the leak reached (`body` / `headers` / `json`); surface
            // it so SARIF consumers can pivot per-destination without
            // reparsing the message.
            if let Some(field) = d
                .evidence
                .as_ref()
                .and_then(|ev| ev.data_exfil_field.as_deref())
            {
                props.insert("data_exfil_field".into(), json!(field));
            }

            // Alternative-path cross-references.  When the dedup pass
            // at `taint::analyse_file` preserves both a validated and
            // an unvalidated flow for the same `(body, sink, source)`,
            // or two flows that differ on the traversed intermediate
            // variables, each finding carries its own stable ID plus
            // the IDs of its siblings.  SARIF consumers can follow the
            // links via `properties.finding_id` and
            // `properties.relatedFindings`.
            if !d.finding_id.is_empty() {
                props.insert("finding_id".into(), json!(d.finding_id));
            }
            if !d.alternative_finding_ids.is_empty() {
                props.insert("relatedFindings".into(), json!(d.alternative_finding_ids));
            }

            // Engine provenance notes, surface any cap-hit / lowering
            // bail / timeout signals recorded by the analysis engine so
            // downstream consumers can tell "nothing found" from "engine
            // stopped looking".
            //
            // Three properties are emitted together:
            //   * `engine_notes`      , raw list of {kind, ...} entries
            //   * `confidence_capped` , true iff any non-informational
            //                            note is present (back-compat
            //                            boolean; drives legacy dashboards)
            //   * `loss_direction`    , worst `LossDirection` across
            //                            the list ("under-report",
            //                            "over-report", "bail").  Absent
            //                            when only informational notes
            //                            are attached.
            if let Some(engine_notes) = d.evidence.as_ref().and_then(|ev| {
                if ev.engine_notes.is_empty() {
                    None
                } else {
                    Some(&ev.engine_notes)
                }
            }) {
                props.insert(
                    "engine_notes".into(),
                    serde_json::to_value(engine_notes).unwrap_or(Value::Null),
                );
                props.insert(
                    "confidence_capped".into(),
                    json!(
                        engine_notes
                            .iter()
                            .any(crate::engine_notes::EngineNote::lowers_confidence)
                    ),
                );
                if let Some(dir) = crate::engine_notes::worst_direction(engine_notes) {
                    props.insert("loss_direction".into(), json!(dir.tag()));
                }
            }

            // Add rollup data if present
            if let Some(ref rollup) = d.rollup {
                props.insert(
                    "rollup".into(),
                    json!({
                        "count": rollup.count,
                    }),
                );

                // Add rollup occurrences as relatedLocations
                let related: Vec<Value> = rollup
                    .occurrences
                    .iter()
                    .enumerate()
                    .map(|(idx, loc)| {
                        json!({
                            "id": idx,
                            "physicalLocation": {
                                "artifactLocation": { "uri": &uri },
                                "region": {
                                    "startLine": loc.line,
                                    "startColumn": loc.col
                                }
                            }
                        })
                    })
                    .collect();
                if !related.is_empty() {
                    result["relatedLocations"] = json!(related);
                }
            }

            result["properties"] = Value::Object(props);

            result
        })
        .collect();

    json!({
        "$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json",
        "version": "2.1.0",
        "runs": [{
            "tool": {
                "driver": {
                    "name": "nyx",
                    "version": env!("CARGO_PKG_VERSION"),
                    "informationUri": env!("CARGO_PKG_HOMEPAGE"),
                    "rules": rules
                }
            },
            "results": results
        }]
    })
}

// ─────────────────────────────────────────────────────────────────────────────
//  Tests
// ─────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::commands::scan::{Diag, Location, RollupData};
    use crate::patterns::{FindingCategory, Severity};

    fn make_diag(id: &str, severity: Severity) -> Diag {
        Diag {
            path: "/scan_root/src/main.rs".into(),
            line: 10,
            col: 5,
            severity,
            id: id.into(),
            category: FindingCategory::Security,
            path_validated: false,
            guard_kind: None,
            message: None,
            labels: vec![],
            confidence: None,
            evidence: None,
            rank_score: None,
            rank_reason: None,
            suppressed: false,
            suppression: None,
            rollup: None,
            finding_id: String::new(),
            alternative_finding_ids: Vec::new(),
        }
    }

    // ── severity_to_level ──────────────────────────────────────────────────

    #[test]
    fn severity_to_level_high_is_error() {
        assert_eq!(severity_to_level(Severity::High), "error");
    }

    #[test]
    fn severity_to_level_medium_is_warning() {
        assert_eq!(severity_to_level(Severity::Medium), "warning");
    }

    #[test]
    fn severity_to_level_low_is_note() {
        assert_eq!(severity_to_level(Severity::Low), "note");
    }

    // ── cfg_rule_description ───────────────────────────────────────────────

    #[test]
    fn cfg_rule_description_known_ids() {
        let cases = [
            ("cfg-unguarded-sink", "without prior guard"),
            ("cfg-unreachable-sink", "unreachable"),
            ("cfg-auth-gap", "authentication"),
            ("cfg-error-fallthrough", "dangerous call follows"),
            ("cfg-resource-leak", "not released"),
            ("cfg-lock-not-released", "Lock acquired"),
            (
                "state-use-after-close",
                "after its resource handle was closed",
            ),
            ("state-double-close", "more than once"),
            ("state-resource-leak", "never closed"),
            ("state-resource-leak-possible", "may not be closed"),
            ("state-unauthed-access", "without authentication"),
        ];
        for (id, fragment) in cases {
            let desc = cfg_rule_description(id).unwrap_or_else(|| panic!("no desc for {id}"));
            assert!(
                desc.contains(fragment),
                "Description for '{id}' should contain '{fragment}', got: {desc}"
            );
        }
    }

    #[test]
    fn cfg_rule_description_unknown_id_returns_none() {
        assert!(cfg_rule_description("unknown-rule-xyz").is_none());
        assert!(cfg_rule_description("").is_none());
    }

    // ── rule_description ──────────────────────────────────────────────────

    #[test]
    fn rule_description_taint_prefix_returns_fallback() {
        // Any taint-* ID without a registered pattern description falls back
        // to the hardcoded message.
        let desc = rule_description("taint-unsanitised-flow");
        assert!(
            desc.contains("Unsanitised"),
            "expected taint fallback, got: {desc}"
        );
    }

    #[test]
    fn rule_description_taint_with_suffix_normalises_to_base() {
        // IDs like "taint-unsanitised-flow:foo.rs:42" are stripped to base.
        let desc = rule_description("taint-unsanitised-flow:foo.rs:42");
        assert!(
            desc.contains("Unsanitised"),
            "expected taint fallback, got: {desc}"
        );
    }

    #[test]
    fn rule_description_cfg_known_id_returns_description() {
        let desc = rule_description("cfg-auth-gap");
        assert!(
            desc.contains("authentication"),
            "expected cfg-auth-gap description, got: {desc}"
        );
    }

    #[test]
    fn rule_description_unknown_returns_id_itself() {
        let id = "totally-unknown-rule-zzzz";
        let desc = rule_description(id);
        assert_eq!(desc, id, "unknown rule ID should be returned as-is");
    }

    // ── build_sarif ───────────────────────────────────────────────────────

    #[test]
    fn build_sarif_empty_diags_produces_valid_structure() {
        let sarif = build_sarif(&[], Path::new("/scan_root"));
        assert_eq!(sarif["version"], "2.1.0");
        assert!(sarif["runs"].is_array());
        let run = &sarif["runs"][0];
        assert_eq!(run["tool"]["driver"]["name"], "nyx");
        assert_eq!(run["results"].as_array().unwrap().len(), 0);
        assert_eq!(run["tool"]["driver"]["rules"].as_array().unwrap().len(), 0);
    }

    #[test]
    fn build_sarif_single_diag_has_correct_fields() {
        let diag = make_diag("rs.security.sql-injection", Severity::High);
        let sarif = build_sarif(&[diag], Path::new("/scan_root"));

        let results = sarif["runs"][0]["results"].as_array().unwrap();
        assert_eq!(results.len(), 1);

        let result = &results[0];
        assert_eq!(result["ruleId"], "rs.security.sql-injection");
        assert_eq!(result["level"], "error");

        let loc = &result["locations"][0]["physicalLocation"];
        assert_eq!(loc["region"]["startLine"], 10);
        assert_eq!(loc["region"]["startColumn"], 5);
        // Path should be relative to scan_root
        let uri = loc["artifactLocation"]["uri"].as_str().unwrap();
        assert!(
            !uri.starts_with("/scan_root"),
            "URI should be relative, got: {uri}"
        );
        assert!(uri.contains("main.rs"));
    }

    #[test]
    fn build_sarif_severity_mapping() {
        let diags = vec![
            make_diag("rule-high", Severity::High),
            make_diag("rule-medium", Severity::Medium),
            make_diag("rule-low", Severity::Low),
        ];
        let sarif = build_sarif(&diags, Path::new("/"));
        let results = sarif["runs"][0]["results"].as_array().unwrap();
        assert_eq!(results[0]["level"], "error");
        assert_eq!(results[1]["level"], "warning");
        assert_eq!(results[2]["level"], "note");
    }

    #[test]
    fn build_sarif_taint_ids_normalised_to_base() {
        let mut diag = make_diag("taint-unsanitised-flow", Severity::High);
        diag.path = "/scan_root/src/main.rs".into();
        let sarif = build_sarif(&[diag], Path::new("/scan_root"));

        let results = sarif["runs"][0]["results"].as_array().unwrap();
        // ruleId should be the base ID, not the suffixed version
        assert_eq!(results[0]["ruleId"], "taint-unsanitised-flow");

        let rules = sarif["runs"][0]["tool"]["driver"]["rules"]
            .as_array()
            .unwrap();
        // Only one rule entry for the base ID
        assert_eq!(rules.len(), 1);
        assert_eq!(rules[0]["id"], "taint-unsanitised-flow");
    }

    #[test]
    fn build_sarif_duplicate_rule_ids_deduplicated() {
        // Two findings with the same rule ID should produce only one rules entry.
        let d1 = make_diag("rs.security.sqli", Severity::High);
        let d2 = make_diag("rs.security.sqli", Severity::Medium);
        let sarif = build_sarif(&[d1, d2], Path::new("/"));
        let rules = sarif["runs"][0]["tool"]["driver"]["rules"]
            .as_array()
            .unwrap();
        assert_eq!(rules.len(), 1, "duplicate rule IDs should be deduplicated");
        let results = sarif["runs"][0]["results"].as_array().unwrap();
        assert_eq!(results.len(), 2);
        // Both results reference ruleIndex 0
        assert_eq!(results[0]["ruleIndex"], 0);
        assert_eq!(results[1]["ruleIndex"], 0);
    }

    #[test]
    fn build_sarif_message_override_from_diag() {
        let mut diag = make_diag("state-resource-leak", Severity::Medium);
        diag.message = Some("Custom message from state analysis".into());
        let sarif = build_sarif(&[diag], Path::new("/scan_root"));
        let result = &sarif["runs"][0]["results"][0];
        assert_eq!(
            result["message"]["text"],
            "Custom message from state analysis"
        );
    }

    #[test]
    fn build_sarif_uses_rule_description_when_no_message() {
        let diag = make_diag("cfg-auth-gap", Severity::High);
        let sarif = build_sarif(&[diag], Path::new("/scan_root"));
        let result = &sarif["runs"][0]["results"][0];
        let msg = result["message"]["text"].as_str().unwrap();
        assert!(
            msg.contains("authentication"),
            "should use cfg-auth-gap description, got: {msg}"
        );
    }

    #[test]
    fn build_sarif_rollup_produces_related_locations() {
        let mut diag = make_diag("rs.quality.unwrap", Severity::Low);
        diag.rollup = Some(RollupData {
            count: 3,
            occurrences: vec![Location { line: 5, col: 1 }, Location { line: 12, col: 3 }],
        });
        let sarif = build_sarif(&[diag], Path::new("/scan_root"));
        let result = &sarif["runs"][0]["results"][0];

        // Properties should include rollup count
        let props = &result["properties"];
        assert_eq!(props["rollup"]["count"], 3);

        // relatedLocations should have 2 entries
        let related = result["relatedLocations"].as_array().unwrap();
        assert_eq!(related.len(), 2);
        assert_eq!(related[0]["physicalLocation"]["region"]["startLine"], 5);
        assert_eq!(related[1]["physicalLocation"]["region"]["startLine"], 12);
    }

    #[test]
    fn build_sarif_no_rollup_no_related_locations() {
        let diag = make_diag("rs.security.sql-injection", Severity::High);
        let sarif = build_sarif(&[diag], Path::new("/scan_root"));
        let result = &sarif["runs"][0]["results"][0];
        // relatedLocations key should not be present when there's no rollup
        assert!(
            result.get("relatedLocations").is_none(),
            "relatedLocations should be absent without rollup"
        );
    }

    #[test]
    fn build_sarif_path_relative_to_scan_root() {
        let mut diag = make_diag("rule-x", Severity::High);
        diag.path = "/workspace/src/lib.rs".into();
        let sarif = build_sarif(&[diag], Path::new("/workspace"));
        let uri =
            sarif["runs"][0]["results"][0]["locations"][0]["physicalLocation"]["artifactLocation"]
                ["uri"]
                .as_str()
                .unwrap();
        assert_eq!(uri, "src/lib.rs");
    }

    #[test]
    fn build_sarif_path_outside_scan_root_is_redacted() {
        // Absolute host paths leak home-directory information, SARIF must
        // substitute a deterministic token when a finding falls outside the
        // scan root.
        let mut diag = make_diag("rule-x", Severity::High);
        diag.path = "/other/place/file.rs".into();
        let sarif = build_sarif(&[diag], Path::new("/workspace"));
        let uri =
            sarif["runs"][0]["results"][0]["locations"][0]["physicalLocation"]["artifactLocation"]
                ["uri"]
                .as_str()
                .unwrap();
        assert_eq!(uri, "<out-of-root>");
    }

    #[test]
    fn build_sarif_confidence_in_properties() {
        let mut diag = make_diag("rule-conf", Severity::High);
        diag.confidence = Some(crate::evidence::Confidence::High);
        let sarif = build_sarif(&[diag], Path::new("/scan_root"));
        let props = &sarif["runs"][0]["results"][0]["properties"];
        let conf = props["confidence"].as_str().unwrap();
        assert_eq!(conf, "High");
    }

    #[test]
    fn build_sarif_category_in_properties() {
        let mut diag = make_diag("rule-cat", Severity::Medium);
        diag.category = FindingCategory::Reliability;
        let sarif = build_sarif(&[diag], Path::new("/scan_root"));
        let props = &sarif["runs"][0]["results"][0]["properties"];
        assert_eq!(props["category"], "Reliability");
    }

    #[test]
    fn build_sarif_schema_and_version_fields_present() {
        let sarif = build_sarif(&[], Path::new("/"));
        assert!(
            sarif["$schema"].as_str().unwrap().contains("sarif"),
            "schema should be a SARIF schema URL"
        );
        assert_eq!(sarif["version"], "2.1.0");
    }

    #[test]
    fn build_sarif_multiple_distinct_rules_indexed_in_order() {
        let d1 = make_diag("rule-alpha", Severity::High);
        let d2 = make_diag("rule-beta", Severity::Medium);
        let d3 = make_diag("rule-gamma", Severity::Low);
        let sarif = build_sarif(&[d1, d2, d3], Path::new("/"));
        let rules = sarif["runs"][0]["tool"]["driver"]["rules"]
            .as_array()
            .unwrap();
        assert_eq!(rules.len(), 3);
        assert_eq!(rules[0]["id"], "rule-alpha");
        assert_eq!(rules[1]["id"], "rule-beta");
        assert_eq!(rules[2]["id"], "rule-gamma");

        let results = sarif["runs"][0]["results"].as_array().unwrap();
        assert_eq!(results[0]["ruleIndex"], 0);
        assert_eq!(results[1]["ruleIndex"], 1);
        assert_eq!(results[2]["ruleIndex"], 2);
    }
}