opi 0.13.4

Operations Interface — a project control center for your terminal
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
//! Lockfile audit through `osv-scanner`.
//!
//! Runs *alongside* the native audits in [`crate::audit`], not instead of
//! them: the package managers know their own registries and `cargo audit` knows
//! RustSec, while `osv-scanner` reads the lockfiles directly against the OSV
//! database. It is an additional source, so a finding the native audit already
//! reported for the same package is dropped here ([`without_known`]) and the
//! two never show the same package twice. Where a native audit is missing or
//! failed, everything OSV found stays.
//!
//! The JSON is the documented `--format json` output of osv-scanner v2: the
//! `results[].packages[]` tree, with a CVSS score in `groups[].max_severity`.
//! The result is the same [`Advisory`] shape the native audits produce.

use std::collections::BTreeMap;
use std::path::Path;
use std::process::Command;

use serde::Deserialize;

use crate::audit::{Advisory, AuditError, Severity};

/// One vulnerable package, with the ecosystem OSV filed it under (`npm`,
/// `crates.io`, …) so it can be matched against the native audits.
#[derive(Debug, Clone, PartialEq)]
pub struct Finding {
    pub ecosystem: String,
    pub name: String,
    /// `module` reads `name version`: unlike the native audits, the lockfile
    /// says exactly which version is installed.
    pub advisory: Advisory,
}

#[derive(Debug, Deserialize)]
struct Report {
    #[serde(default)]
    results: Vec<Source>,
}

#[derive(Debug, Deserialize)]
struct Source {
    #[serde(default)]
    packages: Vec<Entry>,
}

#[derive(Debug, Deserialize)]
struct Entry {
    package: Package,
    #[serde(default)]
    vulnerabilities: Vec<Vulnerability>,
    #[serde(default)]
    groups: Vec<Group>,
}

#[derive(Debug, Deserialize)]
struct Package {
    name: String,
    #[serde(default)]
    version: String,
    #[serde(default)]
    ecosystem: String,
}

#[derive(Debug, Deserialize)]
struct Vulnerability {
    id: String,
    #[serde(default)]
    affected: Vec<Affected>,
    #[serde(default)]
    database_specific: DatabaseSpecific,
}

#[derive(Debug, Default, Deserialize)]
struct DatabaseSpecific {
    #[serde(default)]
    severity: Option<String>,
}

#[derive(Debug, Deserialize)]
struct Affected {
    #[serde(default)]
    package: Option<AffectedPackage>,
    #[serde(default)]
    ranges: Vec<Range>,
}

#[derive(Debug, Deserialize)]
struct AffectedPackage {
    #[serde(default)]
    name: String,
}

#[derive(Debug, Deserialize)]
struct Range {
    #[serde(default)]
    events: Vec<Event>,
}

#[derive(Debug, Deserialize)]
struct Event {
    #[serde(default)]
    fixed: Option<String>,
}

#[derive(Debug, Deserialize)]
struct Group {
    #[serde(default)]
    ids: Vec<String>,
    /// A CVSS score as text, e.g. `"7.5"`; empty when the advisory has none.
    #[serde(default)]
    max_severity: String,
}

/// Runs `osv-scanner` over `root` and reads what it found.
///
/// [`AuditError::NotInstalled`] when the binary is missing, so the caller can
/// say so rather than print an empty section.
pub fn run(root: &Path) -> Result<Vec<Finding>, AuditError> {
    let output = match Command::new("osv-scanner")
        .args(["scan", "--format", "json", "-r"])
        .arg(root)
        .output()
    {
        Ok(output) => output,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
            return Err(AuditError::NotInstalled);
        }
        Err(error) => {
            return Err(AuditError::Failed(format!(
                "could not run osv-scanner: {error}"
            )));
        }
    };

    let text = String::from_utf8_lossy(&output.stdout);
    if text.trim().is_empty() {
        // Exit 128 is osv-scanner's "found nothing to scan": no lockfile is
        // not a failure, and not a finding either.
        if output.status.code() == Some(128) {
            return Ok(Vec::new());
        }
        let reason = String::from_utf8_lossy(&output.stderr)
            .lines()
            .last()
            .unwrap_or("no output")
            .trim()
            .to_owned();
        return Err(AuditError::Failed(format!("osv-scanner: {reason}")));
    }

    // Exit 1 means findings, so the output is read whatever the status.
    read(&text).map_err(|error| AuditError::Failed(format!("could not read osv-scanner: {error}")))
}

/// What the native audits already reported, as `(ecosystem, package name)`;
/// OSV's ecosystem names (`npm`, `crates.io`).
pub fn without_known(found: Vec<Finding>, known: &[(&str, String)]) -> Vec<Finding> {
    found
        .into_iter()
        .filter(|finding| {
            !known
                .iter()
                .any(|(ecosystem, name)| *ecosystem == finding.ecosystem && *name == finding.name)
        })
        .collect()
}

/// One finding per package and version, at the worst severity among its
/// vulnerabilities, worst first.
fn read(text: &str) -> Result<Vec<Finding>, serde_json::Error> {
    let report: Report = serde_json::from_str(text)?;
    let mut found = BTreeMap::new();

    for entry in report
        .results
        .into_iter()
        .flat_map(|source| source.packages)
    {
        if entry.vulnerabilities.is_empty() {
            continue;
        }
        let severity = entry
            .vulnerabilities
            .iter()
            .map(|vulnerability| severity_of(vulnerability, &entry.groups))
            .min()
            .unwrap_or(Severity::Info);

        // A remedy only where all vulnerabilities agree on one fixed version;
        // with several, the highest is not something to guess at without
        // comparing versions per ecosystem.
        let mut fixes: Vec<&str> = entry
            .vulnerabilities
            .iter()
            .flat_map(|vulnerability| fixed_in(vulnerability, &entry.package.name))
            .collect();
        fixes.sort_unstable();
        fixes.dedup();
        let patched = match fixes.as_slice() {
            [only] => Some(format!(">={only}")),
            _ => None,
        };

        let module = format!("{} {}", entry.package.name, entry.package.version);
        found.insert(
            (entry.package.ecosystem.clone(), module.clone()),
            Finding {
                ecosystem: entry.package.ecosystem,
                name: entry.package.name,
                advisory: Advisory {
                    module,
                    severity,
                    patched,
                },
            },
        );
    }

    let mut found: Vec<Finding> = found.into_values().collect();
    found.sort_by(|a, b| {
        a.advisory
            .severity
            .cmp(&b.advisory.severity)
            .then_with(|| a.advisory.module.cmp(&b.advisory.module))
    });
    Ok(found)
}

/// The group's CVSS score where there is one, else the database's own label.
/// A vulnerability with neither is [`Severity::Info`]: not guessed upwards, and
/// not failing a run on a number nobody gave.
fn severity_of(vulnerability: &Vulnerability, groups: &[Group]) -> Severity {
    groups
        .iter()
        .find(|group| group.ids.contains(&vulnerability.id))
        .and_then(|group| group.max_severity.parse::<f64>().ok())
        .map(from_score)
        .or_else(|| {
            vulnerability
                .database_specific
                .severity
                .as_deref()
                .and_then(from_label)
        })
        .unwrap_or(Severity::Info)
}

/// The CVSS v3 qualitative bands.
fn from_score(score: f64) -> Severity {
    match score {
        s if s >= 9.0 => Severity::Critical,
        s if s >= 7.0 => Severity::High,
        s if s >= 4.0 => Severity::Moderate,
        s if s > 0.0 => Severity::Low,
        _ => Severity::Info,
    }
}

fn from_label(label: &str) -> Option<Severity> {
    match label.to_ascii_lowercase().as_str() {
        "critical" => Some(Severity::Critical),
        "high" => Some(Severity::High),
        "moderate" | "medium" => Some(Severity::Moderate),
        "low" => Some(Severity::Low),
        _ => None,
    }
}

fn fixed_in<'a>(vulnerability: &'a Vulnerability, name: &str) -> impl Iterator<Item = &'a str> {
    let name = name.to_owned();
    vulnerability
        .affected
        .iter()
        .filter(move |affected| {
            affected
                .package
                .as_ref()
                .is_some_and(|package| package.name == name)
        })
        .flat_map(|affected| &affected.ranges)
        .flat_map(|range| &range.events)
        .filter_map(|event| event.fixed.as_deref())
}

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

    /// Written to osv-scanner v2's documented `--format json` schema and cut
    /// down to the keys read (no osv-scanner was available to record one):
    /// an npm lockfile with lodash (two vulnerabilities, one fix) and a package
    /// with only a database label, plus a Cargo.lock with one scored crate.
    const SAMPLE: &str = r#"{
      "results": [
        {
          "source": { "path": "/app/package-lock.json", "type": "lockfile" },
          "packages": [
            {
              "package": { "name": "lodash", "version": "4.17.15", "ecosystem": "npm" },
              "vulnerabilities": [
                {
                  "id": "GHSA-35jh-r3h4-6jhm",
                  "affected": [{
                    "package": { "name": "lodash", "ecosystem": "npm" },
                    "ranges": [{ "type": "SEMVER",
                      "events": [{ "introduced": "0" }, { "fixed": "4.17.21" }] }]
                  }],
                  "database_specific": { "severity": "HIGH" }
                },
                {
                  "id": "GHSA-29mw-wpgm-hmr9",
                  "affected": [{
                    "package": { "name": "lodash", "ecosystem": "npm" },
                    "ranges": [{ "type": "SEMVER",
                      "events": [{ "introduced": "0" }, { "fixed": "4.17.21" }] }]
                  }],
                  "database_specific": { "severity": "MODERATE" }
                }
              ],
              "groups": [
                { "ids": ["GHSA-35jh-r3h4-6jhm"], "max_severity": "7.2" },
                { "ids": ["GHSA-29mw-wpgm-hmr9"], "max_severity": "5.3" }
              ]
            },
            {
              "package": { "name": "minimist", "version": "1.2.0", "ecosystem": "npm" },
              "vulnerabilities": [
                { "id": "GHSA-xvch-5gv4-984h", "database_specific": { "severity": "CRITICAL" } }
              ],
              "groups": [{ "ids": ["GHSA-xvch-5gv4-984h"], "max_severity": "" }]
            }
          ]
        },
        {
          "source": { "path": "/app/Cargo.lock", "type": "lockfile" },
          "packages": [
            {
              "package": { "name": "quick-xml", "version": "0.38.4", "ecosystem": "crates.io" },
              "vulnerabilities": [{ "id": "RUSTSEC-2026-0195" }],
              "groups": [{ "ids": ["RUSTSEC-2026-0195"], "max_severity": "9.8" }]
            }
          ]
        }
      ]
    }"#;

    #[test]
    fn one_finding_per_package_at_its_worst_severity() {
        let found = read(SAMPLE).expect("parse");
        let lodash = found.iter().find(|f| f.name == "lodash").expect("lodash");
        assert_eq!(found.len(), 3);
        assert_eq!(lodash.advisory.module, "lodash 4.17.15");
        assert_eq!(lodash.advisory.severity, Severity::High);
        assert_eq!(lodash.ecosystem, "npm");
    }

    #[test]
    fn worst_first_and_cargo_is_read_too() {
        let found = read(SAMPLE).expect("parse");
        let names: Vec<&str> = found.iter().map(|f| f.name.as_str()).collect();
        // quick-xml scores 9.8, minimist falls back to its CRITICAL label.
        assert_eq!(names, ["minimist", "quick-xml", "lodash"]);
        assert_eq!(found[1].ecosystem, "crates.io");
        assert_eq!(found[1].advisory.severity, Severity::Critical);
    }

    #[test]
    fn a_single_agreed_fix_becomes_the_remedy_and_none_otherwise() {
        let found = read(SAMPLE).expect("parse");
        let lodash = found.iter().find(|f| f.name == "lodash").unwrap();
        assert_eq!(lodash.advisory.patched.as_deref(), Some(">=4.17.21"));
        let minimist = found.iter().find(|f| f.name == "minimist").unwrap();
        assert_eq!(minimist.advisory.patched, None);
    }

    #[test]
    fn packages_without_vulnerabilities_and_empty_reports_yield_nothing() {
        assert!(read(r#"{"results":[]}"#).unwrap().is_empty());
        assert!(read("{}").unwrap().is_empty());
        let clean = r#"{"results":[{"packages":[{"package":
            {"name":"a","version":"1","ecosystem":"npm"}}]}]}"#;
        assert!(read(clean).unwrap().is_empty());
    }

    #[test]
    fn what_the_native_audits_reported_is_dropped_per_ecosystem() {
        let found = read(SAMPLE).expect("parse");
        let known = [
            ("npm", "lodash".to_owned()),
            ("npm", "quick-xml".to_owned()),
        ];
        let rest = without_known(found, &known);
        let names: Vec<&str> = rest.iter().map(|f| f.name.as_str()).collect();
        // quick-xml is a crate, so an npm package of that name does not hide it.
        assert_eq!(names, ["minimist", "quick-xml"]);
    }

    #[test]
    fn scores_map_to_the_cvss_bands() {
        assert_eq!(from_score(9.0), Severity::Critical);
        assert_eq!(from_score(7.0), Severity::High);
        assert_eq!(from_score(4.0), Severity::Moderate);
        assert_eq!(from_score(0.1), Severity::Low);
        assert_eq!(from_score(0.0), Severity::Info);
    }
}