zuit-report 0.1.0

Output formatters (JSON, SARIF, Markdown, terminal) for zuit.
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
//! SARIF 2.1.0 formatter for zuit reports.
//!
//! Produces a single *merged run* โ€” all findings from all analyzers are
//! combined into one `runs[0]` entry.  This is a deliberate v1 choice: the
//! `ARCH_SPEC` ยง7.2 notes that per-analyzer runs are *configurable*, but v1
//! ships with the simpler merged variant.  A future version can make the
//! strategy selectable via [`crate::RenderOptions`].
//!
//! ## Output structure
//!
//! ```json
//! {
//!   "$schema": "https://json.schemastore.org/sarif-2.1.0.json",
//!   "version": "2.1.0",
//!   "runs": [
//!     {
//!       "tool": {
//!         "driver": {
//!           "name": "zuit",
//!           "version": "0.1.0",
//!           "informationUri": "https://github.com/shubhamkaushal/zuit",
//!           "rules": [ /* deduplicated, sorted by id */ ]
//!         }
//!       },
//!       "results": [ /* one per finding, sorted by (uri, line, col, ruleId) */ ]
//!     }
//!   ]
//! }
//! ```
//!
//! ## Severity โ†’ level mapping
//!
//! | zuit Severity | SARIF level  |
//! |-------------------|--------------|
//! | Critical          | `"error"`    |
//! | High              | `"error"`    |
//! | Medium            | `"warning"`  |
//! | Low               | `"note"`     |
//! | Info              | `"note"`     |
//!
//! ## Taxonomy references
//!
//! CWE and OWASP identifiers carried in findings are emitted as `taxa`
//! references on each result.  Each `taxa` entry uses the `toolComponent`
//! reference `{ "name": "CWE" }` or `{ "name": "OWASP" }` so that SARIF
//! consumers can correlate entries without requiring a full taxonomy
//! `toolComponentReference` to be registered in the run.

use std::collections::BTreeMap;

use zuit_core::analyzer::Severity;
use zuit_core::engine::Report;
use zuit_core::finding::Finding;
use serde_json::{Value, json};

use crate::ReportError;

/// URL of the SARIF 2.1.0 JSON schema, used in the `$schema` field.
const SARIF_SCHEMA: &str = "https://json.schemastore.org/sarif-2.1.0.json";

/// SARIF version string.
const SARIF_VERSION: &str = "2.1.0";

/// The tool's canonical name.
const TOOL_NAME: &str = "zuit";

/// Public URL for the tool, used in `tool.driver.informationUri`.
const TOOL_INFORMATION_URI: &str = "https://github.com/shubhamkaushal/zuit";

/// Maps a zuit [`Severity`] to a SARIF `level` string.
///
/// | Severity         | SARIF level  |
/// |------------------|--------------|
/// | `Critical`       | `"error"`    |
/// | `High`           | `"error"`    |
/// | `Medium`         | `"warning"`  |
/// | `Low`            | `"note"`     |
/// | `Info`           | `"note"`     |
fn severity_to_level(severity: Severity) -> &'static str {
    match severity {
        Severity::Critical | Severity::High => "error",
        Severity::Medium => "warning",
        Severity::Low | Severity::Info => "note",
    }
}

/// Converts a file-system path (any platform) to a POSIX-style URI string.
///
/// On Windows paths use `\` as a separator; SARIF `artifactLocation.uri`
/// must use `/`.  This function replaces every `\` with `/`.
fn path_to_uri(path: &std::path::Path) -> String {
    path.to_string_lossy().replace('\\', "/")
}

/// Builds a deduplicated, sorted list of SARIF rule descriptors from findings.
///
/// Each unique `rule_id` across all findings becomes one entry.  When multiple
/// findings share the same rule, the *highest* severity among them is used for
/// `defaultConfiguration.level` so that the rule descriptor reflects its most
/// impactful occurrence.
///
/// Rules are sorted by `id` for deterministic output.
fn build_rules(findings: &[Finding]) -> Vec<Value> {
    // Map rule_id โ†’ highest severity seen.
    let mut rule_map: BTreeMap<&str, Severity> = BTreeMap::new();
    for f in findings {
        let entry = rule_map.entry(f.rule_id.as_str()).or_insert(f.severity);
        if f.severity > *entry {
            *entry = f.severity;
        }
    }

    rule_map
        .into_iter()
        .map(|(id, severity)| {
            json!({
                "id": id,
                "name": id,
                "shortDescription": {
                    "text": id
                },
                "defaultConfiguration": {
                    "level": severity_to_level(severity)
                }
            })
        })
        .collect()
}

/// Builds the `taxa` array for a single result from its CWE and OWASP entries.
///
/// Returns `None` when both `cwe` and `owasp` are empty, so the field can be
/// omitted from the result object entirely.
fn build_taxa(cwe: &[String], owasp: &[String]) -> Option<Value> {
    if cwe.is_empty() && owasp.is_empty() {
        return None;
    }

    let mut taxa: Vec<Value> = Vec::with_capacity(cwe.len() + owasp.len());

    for id in cwe {
        taxa.push(json!({
            "toolComponent": { "name": "CWE" },
            "id": id
        }));
    }

    for id in owasp {
        taxa.push(json!({
            "toolComponent": { "name": "OWASP" },
            "id": id
        }));
    }

    Some(Value::Array(taxa))
}

/// Converts a single [`Finding`] into a SARIF result object.
fn finding_to_result(finding: &Finding) -> Value {
    let uri = path_to_uri(&finding.location.file);
    let region = json!({
        "startLine":   finding.location.start.line,
        "startColumn": finding.location.start.column,
        "endLine":     finding.location.end.line,
        "endColumn":   finding.location.end.column,
    });

    let mut result = json!({
        "ruleId": finding.rule_id,
        "level":  severity_to_level(finding.severity),
        "message": { "text": finding.message },
        "locations": [{
            "physicalLocation": {
                "artifactLocation": { "uri": uri },
                "region": region
            }
        }]
    });

    // Emit `fixes` only when a suggestion is present.
    if let Some(suggestion) = &finding.suggestion {
        result["fixes"] = json!([{
            "description": { "text": suggestion }
        }]);
    }

    // Emit `taxa` only when CWE or OWASP entries are present.
    if let Some(taxa) = build_taxa(&finding.cwe, &finding.owasp) {
        result["taxa"] = taxa;
    }

    result
}

/// Sort key for SARIF results: `(uri, startLine, startColumn, ruleId)`.
///
/// This matches the `ARCH_SPEC` determinism requirement while being meaningful
/// for SARIF consumers that process results in document order.
fn result_sort_key(result: &Value) -> (String, u64, u64, String) {
    let uri = result["locations"][0]["physicalLocation"]["artifactLocation"]["uri"]
        .as_str()
        .unwrap_or("")
        .to_string();
    let line = result["locations"][0]["physicalLocation"]["region"]["startLine"]
        .as_u64()
        .unwrap_or(0);
    let col = result["locations"][0]["physicalLocation"]["region"]["startColumn"]
        .as_u64()
        .unwrap_or(0);
    let rule = result["ruleId"].as_str().unwrap_or("").to_string();
    (uri, line, col, rule)
}

/// Renders `report` as a SARIF 2.1.0 JSON string.
///
/// The output is a single merged run containing all findings from all
/// analyzers.  This is the v1 strategy; see the module-level documentation
/// for the full rationale.
///
/// ## Output determinism
///
/// - `tool.driver.rules` is sorted by rule `id`.
/// - `results` is sorted by `(artifactLocation.uri, startLine, startColumn, ruleId)`.
///
/// ## Empty reports
///
/// An empty `Report` (no findings) produces valid SARIF with empty `rules`
/// and `results` arrays.
///
/// # Errors
///
/// Returns [`ReportError::Serialize`] if `serde_json` serialization fails
/// (practically infallible for well-formed data, but propagated for
/// correctness).
pub fn render_sarif(report: &Report) -> Result<String, ReportError> {
    let rules = build_rules(&report.findings);

    let mut results: Vec<Value> = report.findings.iter().map(finding_to_result).collect();
    results.sort_by_key(result_sort_key);

    let sarif = json!({
        "$schema": SARIF_SCHEMA,
        "version": SARIF_VERSION,
        "runs": [{
            "tool": {
                "driver": {
                    "name": TOOL_NAME,
                    "version": env!("CARGO_PKG_VERSION"),
                    "informationUri": TOOL_INFORMATION_URI,
                    "rules": rules
                }
            },
            "results": results
        }]
    });

    Ok(serde_json::to_string_pretty(&sarif)?)
}

#[cfg(test)]
mod tests {
    use super::*;
    use zuit_core::analyzer::{Dimension, Severity};
    use zuit_core::engine::{Report, RunStats};
    use zuit_core::id::AnalyzerId;
    use zuit_core::score::aggregate_dimension_score;
    use zuit_core::span::{ByteOffset, LineCol, Location, Span};
    use std::collections::BTreeMap;
    use std::path::PathBuf;

    fn empty_report() -> Report {
        let mut scores = BTreeMap::new();
        for dim in [
            Dimension::Maintainability,
            Dimension::Security,
            Dimension::Complexity,
            Dimension::Documentation,
            Dimension::TestSmell,
        ] {
            scores.insert(dim, aggregate_dimension_score(&[], 1.0));
        }
        Report {
            schema_version: 1,
            findings: vec![],
            scores,
            stats: RunStats {
                files_scanned: 0,
                parse_failures: 0,
                elapsed_ms: 0,
                suppressed: 0,
                cache_hits: 0,
            },
        }
    }

    // ------------------------------------------------------------------
    // Unit tests: severity โ†’ level mapping
    // ------------------------------------------------------------------

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

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

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

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

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

    // ------------------------------------------------------------------
    // Unit tests: path โ†’ URI conversion
    // ------------------------------------------------------------------

    #[test]
    fn posix_path_unchanged() {
        let p = std::path::Path::new("src/auth.rs");
        assert_eq!(path_to_uri(p), "src/auth.rs");
    }

    #[cfg(windows)]
    #[test]
    fn windows_path_converted_to_forward_slash() {
        let p = std::path::Path::new("src\\auth.rs");
        assert_eq!(path_to_uri(p), "src/auth.rs");
    }

    // ------------------------------------------------------------------
    // Unit tests: empty report edge case
    // ------------------------------------------------------------------

    #[test]
    fn empty_report_produces_valid_sarif() {
        let report = empty_report();
        let output = render_sarif(&report).expect("empty report must not fail");
        let v: serde_json::Value = serde_json::from_str(&output).expect("must be valid JSON");
        assert_eq!(v["version"], "2.1.0");
        assert_eq!(v["$schema"], SARIF_SCHEMA);
        let runs = v["runs"].as_array().unwrap();
        assert_eq!(runs.len(), 1);
        assert!(runs[0]["results"].as_array().unwrap().is_empty());
        assert!(
            runs[0]["tool"]["driver"]["rules"]
                .as_array()
                .unwrap()
                .is_empty()
        );
    }

    // ------------------------------------------------------------------
    // Unit tests: taxa helpers
    // ------------------------------------------------------------------

    #[test]
    fn build_taxa_returns_none_when_empty() {
        assert!(build_taxa(&[], &[]).is_none());
    }

    #[test]
    fn build_taxa_includes_cwe_and_owasp() {
        let taxa = build_taxa(&["CWE-798".to_string()], &["A07:2021".to_string()])
            .expect("taxa must be Some");
        let arr = taxa.as_array().unwrap();
        assert_eq!(arr.len(), 2);
        assert_eq!(arr[0]["toolComponent"]["name"], "CWE");
        assert_eq!(arr[0]["id"], "CWE-798");
        assert_eq!(arr[1]["toolComponent"]["name"], "OWASP");
        assert_eq!(arr[1]["id"], "A07:2021");
    }

    // ------------------------------------------------------------------
    // Unit test: finding with suggestion emits fixes
    // ------------------------------------------------------------------

    #[test]
    fn finding_with_suggestion_emits_fixes() {
        let finding = zuit_core::finding::Finding {
            analyzer: AnalyzerId::new("test"),
            dimension: Dimension::Security,
            rule_id: "SEC001".to_string(),
            severity: Severity::High,
            message: "secret found".to_string(),
            location: Location {
                file: PathBuf::from("src/lib.rs"),
                span: Span::new(ByteOffset(0), ByteOffset(10)),
                start: LineCol::new(1, 1),
                end: LineCol::new(1, 11),
            },
            suggestion: Some("use env vars".to_string()),
            references: vec![],
            cwe: vec![],
            owasp: vec![],
        };
        let result = finding_to_result(&finding);
        let fixes = result["fixes"].as_array().expect("fixes must be present");
        assert_eq!(fixes[0]["description"]["text"], "use env vars");
    }

    #[test]
    fn finding_without_suggestion_omits_fixes() {
        let finding = zuit_core::finding::Finding {
            analyzer: AnalyzerId::new("test"),
            dimension: Dimension::Security,
            rule_id: "SEC001".to_string(),
            severity: Severity::High,
            message: "secret found".to_string(),
            location: Location {
                file: PathBuf::from("src/lib.rs"),
                span: Span::new(ByteOffset(0), ByteOffset(10)),
                start: LineCol::new(1, 1),
                end: LineCol::new(1, 11),
            },
            suggestion: None,
            references: vec![],
            cwe: vec![],
            owasp: vec![],
        };
        let result = finding_to_result(&finding);
        assert!(
            result.get("fixes").is_none(),
            "fixes must be absent when suggestion is None"
        );
    }
}