npm-utils 0.6.1

Pure-Rust npm toolkit: resolve, download, install/ci, add/remove/upgrade, search, SBOM (CycloneDX/SPDX), and vulnerability audit (npm + OSV) — no Node.
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
//! The OSV (osv.dev) advisory source.
//!
//! A batched `POST /v1/querybatch` returns vulnerability ids per query (positionally, one result
//! list per queried component); each id is then hydrated with `GET /v1/vulns/{id}` for the full
//! record — structured `affected` ranges, aliases (CVE/GHSA), and severity. The endpoint rejects
//! more than `QUERYBATCH_LIMIT` queries per request (`400 "Too many queries."`), so larger
//! component sets are sent as pages. OSV records span many packages and ecosystems, so a record
//! is only relevant when one of its `affected` entries is the npm package we asked about; that
//! entry's SEMVER `events` are turned into a `>=`/`<` range string the shared
//! [`Range`] matcher can post-filter.

use std::collections::HashMap;

use serde_json::{json, Value};

use super::{Advisory, AdvisorySource, Severity};
use crate::download;
use crate::package_json::spec::Range;
use crate::sbom::Component;

const QUERYBATCH_URL: &str = "https://api.osv.dev/v1/querybatch";
const VULN_URL_BASE: &str = "https://api.osv.dev/v1/vulns";

/// The most queries OSV accepts per `querybatch` request — one more and the endpoint answers
/// `400 {"code":3,"message":"Too many queries."}` (verified empirically), which the audit would
/// misreport as an unreachable source. Bigger component sets are paged at this size.
const QUERYBATCH_LIMIT: usize = 1000;

/// Queries the public OSV database (osv.dev).
pub struct OsvSource;

impl AdvisorySource for OsvSource {
    fn name(&self) -> &'static str {
        "osv"
    }

    fn query(&self, components: &[Component]) -> crate::Result<Vec<Advisory>> {
        if components.is_empty() {
            return Ok(Vec::new());
        }
        // Page the batch at OSV's query cap; each page's positional results are read against
        // that page's slice, so the pairing survives the split.
        let mut wanted: Vec<(String, String, String)> = Vec::new(); // (name, version, vuln id)
        for page in components.chunks(QUERYBATCH_LIMIT) {
            let raw = serde_json::to_vec(&querybatch_body(page))?;
            let Some(resp) =
                download::post_json(QUERYBATCH_URL, &raw, None, Some("application/json"))
            else {
                // Unreachable endpoint (or a rejected request): report it as an error so
                // `run_audit` records OSV as failed rather than treating it as "no
                // vulnerabilities".
                return Err(
                    "OSV querybatch endpoint unreachable or returned no usable data".into(),
                );
            };
            wanted.extend(wanted_ids(&resp, page));
        }

        // Hydrate each distinct id once (a record can apply to several queried packages).
        let mut records: HashMap<String, Option<Value>> = HashMap::new();
        let mut out = Vec::new();
        for (name, version, id) in wanted {
            let record = records.entry(id.clone()).or_insert_with(|| hydrate(&id));
            match record {
                Some(record) => {
                    if let Some(advisory) = parse_osv_vuln(record, &name, &version) {
                        out.push(advisory);
                    }
                }
                None => crate::warn::warn(&format!(
                    "OSV record {id} could not be fetched; audit results may be incomplete"
                )),
            }
        }
        Ok(out)
    }
}

/// The `(name, version, vuln id)` triples a querybatch response names: `results` is positional —
/// `results[i]` holds the vuln ids for `queried[i]`, so `queried` must be exactly the slice the
/// request was built from. Each component's version rides along so a confirmed hit can fall back
/// to an exact `=version` range.
fn wanted_ids(resp: &Value, queried: &[Component]) -> Vec<(String, String, String)> {
    let mut out = Vec::new();
    if let Some(results) = resp.get("results").and_then(Value::as_array) {
        for (i, result) in results.iter().enumerate() {
            let Some(component) = queried.get(i) else {
                continue;
            };
            let Some(vulns) = result.get("vulns").and_then(Value::as_array) else {
                continue;
            };
            for v in vulns {
                if let Some(id) = v.get("id").and_then(Value::as_str) {
                    out.push((
                        component.name.clone(),
                        component.version.clone(),
                        id.to_string(),
                    ));
                }
            }
        }
    }
    out
}

/// The querybatch body: one `{ package, version }` query per component, in order.
fn querybatch_body(components: &[Component]) -> Value {
    let queries: Vec<Value> = components
        .iter()
        .map(|c| {
            json!({
                "package": { "name": c.name, "ecosystem": "npm" },
                "version": c.version,
            })
        })
        .collect();
    json!({ "queries": queries })
}

fn hydrate(id: &str) -> Option<Value> {
    let bytes = download::fetch(&format!("{VULN_URL_BASE}/{id}")).ok()?;
    serde_json::from_slice::<Value>(&bytes).ok()
}

/// Parse a hydrated OSV record into an [`Advisory`] for the npm package `want_name` at
/// `want_version`, or `None` when the record has no `affected` entry for that npm package. Severity
/// is read from `database_specific.severity` (a bucket word); the CVSS vector, when present, is
/// carried for display but not scored — an advisory with only a CVSS vector therefore has an unknown
/// severity, which the audit treats conservatively (it still trips `--audit-level`).
///
/// The vulnerable range is the matching entry's SEMVER events (or its explicit `versions` list).
/// When no range can be reconstructed but the querybatch already confirmed `want_version` is
/// affected, it falls back to an exact `=want_version` so a confirmed hit is never dropped.
pub fn parse_osv_vuln(record: &Value, want_name: &str, want_version: &str) -> Option<Advisory> {
    let id = record.get("id").and_then(Value::as_str)?.to_string();
    let affected = record.get("affected").and_then(Value::as_array)?;
    let entry = affected.iter().find(|a| {
        let pkg = a.get("package");
        pkg.and_then(|p| p.get("ecosystem")).and_then(Value::as_str) == Some("npm")
            && pkg.and_then(|p| p.get("name")).and_then(Value::as_str) == Some(want_name)
    })?;
    // Prefer the record's own affected range, but only when it actually covers the version the
    // querybatch confirmed as affected. Otherwise fall back to an exact `=version`: OSV already told
    // us this version is affected, so a range we can't reconstruct (ECOSYSTEM-only or unparseable)
    // must not turn a confirmed hit into a false negative.
    let vulnerable_range = osv_range_string(entry)
        .filter(|r| range_covers(r, want_version))
        .unwrap_or_else(|| format!("={want_version}"));

    let database_specific = record.get("database_specific");
    let severity = database_specific
        .and_then(|d| d.get("severity"))
        .and_then(Value::as_str)
        .and_then(Severity::from_str_loose);
    let cvss_vector = record
        .get("severity")
        .and_then(Value::as_array)
        .and_then(|arr| {
            arr.iter()
                .find_map(|s| s.get("score").and_then(Value::as_str))
        })
        .map(str::to_string);
    let url = record
        .get("references")
        .and_then(Value::as_array)
        .and_then(|refs| {
            refs.iter()
                .find_map(|r| r.get("url").and_then(Value::as_str))
        })
        .map(str::to_string)
        .or_else(|| Some(format!("https://osv.dev/vulnerability/{id}")));

    Some(Advisory {
        source: "osv",
        id,
        aliases: string_array(record.get("aliases")),
        package: want_name.to_string(),
        vulnerable_range,
        severity,
        title: record
            .get("summary")
            .or_else(|| record.get("details"))
            .and_then(Value::as_str)
            .unwrap_or_default()
            .to_string(),
        url,
        cwe: string_array(database_specific.and_then(|d| d.get("cwe_ids"))),
        cvss_score: None,
        cvss_vector,
        matched_version: String::new(),
    })
}

/// Turn an `affected` entry into an npm-style range string. SEMVER ranges are preferred: within a
/// range the events are an ordered sequence — an `introduced` opens an interval (`>=A`, or nothing
/// for `"0"`), a `fixed`/`last_affected` closes it (`<B` / `<=B`); an interval still open at the end
/// is open-ended. Intervals are ANDed within a range and ORed (`||`) across ranges — so e.g.
/// `[introduced:0, fixed:4.17.12]` → `<4.17.12`. When no SEMVER range is expressed, the explicit
/// affected `versions` list is used instead (each as `=v`). `None` if neither is present.
fn osv_range_string(entry: &Value) -> Option<String> {
    let mut alternatives: Vec<String> = Vec::new();
    if let Some(ranges) = entry.get("ranges").and_then(Value::as_array) {
        for r in ranges {
            if r.get("type").and_then(Value::as_str) != Some("SEMVER") {
                continue;
            }
            let Some(events) = r.get("events").and_then(Value::as_array) else {
                continue;
            };
            let mut lower: Option<String> = None;
            let mut open = false;
            for e in events {
                if let Some(introduced) = e.get("introduced").and_then(Value::as_str) {
                    lower = (introduced != "0").then(|| introduced.to_string());
                    open = true;
                } else if let Some(fixed) = e.get("fixed").and_then(Value::as_str) {
                    alternatives.push(interval(lower.as_deref(), Some(("<", fixed))));
                    lower = None;
                    open = false;
                } else if let Some(last) = e.get("last_affected").and_then(Value::as_str) {
                    alternatives.push(interval(lower.as_deref(), Some(("<=", last))));
                    lower = None;
                    open = false;
                }
            }
            if open {
                alternatives.push(interval(lower.as_deref(), None));
            }
        }
    }
    // Fall back to the explicit affected `versions` list (e.g. an ECOSYSTEM-only advisory that
    // carries no SEMVER range): each exact version becomes an `=v` alternative.
    if alternatives.is_empty() {
        if let Some(versions) = entry.get("versions").and_then(Value::as_array) {
            for v in versions.iter().filter_map(Value::as_str) {
                alternatives.push(format!("={v}"));
            }
        }
    }
    (!alternatives.is_empty()).then(|| alternatives.join(" || "))
}

/// Whether `range` (npm grammar) contains `version` — used to decide whether a synthesized OSV range
/// can be trusted for the querybatch-confirmed version.
fn range_covers(range: &str, version: &str) -> bool {
    match (Range::parse(range), semver::Version::parse(version)) {
        (Ok(r), Ok(v)) => r.matches(&v),
        _ => false,
    }
}

/// One affected interval as comparators: a lower `>=A` (when bounded) ANDed with an upper `<B`/`<=B`
/// (when present). An interval with neither bound is "all versions" (`*`).
fn interval(lower: Option<&str>, upper: Option<(&str, &str)>) -> String {
    let mut parts = Vec::new();
    if let Some(l) = lower {
        parts.push(format!(">={l}"));
    }
    if let Some((op, v)) = upper {
        parts.push(format!("{op}{v}"));
    }
    if parts.is_empty() {
        "*".to_string()
    } else {
        parts.join(" ")
    }
}

fn string_array(v: Option<&Value>) -> Vec<String> {
    v.and_then(Value::as_array)
        .map(|a| {
            a.iter()
                .filter_map(Value::as_str)
                .map(str::to_string)
                .collect()
        })
        .unwrap_or_default()
}

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

    fn component(name: &str, version: &str) -> Component {
        Component {
            name: name.into(),
            version: version.into(),
            purl: format!("pkg:npm/{name}@{version}"),
            license: None,
            resolved: None,
            integrity: None,
        }
    }

    #[test]
    fn querybatch_pages_split_at_the_limit() {
        // 1001 components → two pages of 1000 + 1 queries; one more query per request and OSV
        // answers 400 "Too many queries." (the bug that made a 1077-package audit report OSV as
        // unreachable).
        let components: Vec<Component> = (0..=QUERYBATCH_LIMIT)
            .map(|i| component(&format!("pkg{i}"), "1.0.0"))
            .collect();
        let query_counts: Vec<usize> = components
            .chunks(QUERYBATCH_LIMIT)
            .map(|page| querybatch_body(page)["queries"].as_array().unwrap().len())
            .collect();
        assert_eq!(query_counts, [QUERYBATCH_LIMIT, 1]);
    }

    #[test]
    fn wanted_ids_maps_results_to_the_queried_page_positionally() {
        // results[i] pairs with queried[i] — of the queried PAGE, not any global index.
        let queried = [component("a", "1.0.0"), component("b", "2.0.0")];
        let resp = json!({ "results": [
            { "vulns": [{ "id": "GHSA-aaaa" }, { "id": "GHSA-bbbb" }] },
            {},
        ]});
        let wanted = wanted_ids(&resp, &queried);
        assert_eq!(
            wanted,
            [
                (
                    "a".to_string(),
                    "1.0.0".to_string(),
                    "GHSA-aaaa".to_string()
                ),
                (
                    "a".to_string(),
                    "1.0.0".to_string(),
                    "GHSA-bbbb".to_string()
                ),
            ]
        );
        // A malformed response (no `results`) yields nothing rather than panicking.
        assert!(wanted_ids(&json!({}), &queried).is_empty());
    }

    #[test]
    fn osv_range_from_simple_introduced_zero_fixed() {
        let entry = json!({
            "package": { "name": "lodash", "ecosystem": "npm" },
            "ranges": [{ "type": "SEMVER", "events": [{ "introduced": "0" }, { "fixed": "4.17.12" }] }]
        });
        assert_eq!(osv_range_string(&entry).as_deref(), Some("<4.17.12"));
    }

    #[test]
    fn osv_range_from_bounded_and_multi_interval() {
        let bounded = json!({ "ranges": [{ "type": "SEMVER",
            "events": [{ "introduced": "1.0.0" }, { "fixed": "1.5.0" }] }] });
        assert_eq!(
            osv_range_string(&bounded).as_deref(),
            Some(">=1.0.0 <1.5.0")
        );

        let multi = json!({ "ranges": [{ "type": "SEMVER", "events": [
            { "introduced": "1.0.0" }, { "fixed": "1.2.0" },
            { "introduced": "2.0.0" }, { "fixed": "2.2.0" }
        ] }] });
        assert_eq!(
            osv_range_string(&multi).as_deref(),
            Some(">=1.0.0 <1.2.0 || >=2.0.0 <2.2.0")
        );

        let open =
            json!({ "ranges": [{ "type": "SEMVER", "events": [{ "introduced": "3.0.0" }] }] });
        assert_eq!(osv_range_string(&open).as_deref(), Some(">=3.0.0"));
    }

    #[test]
    fn osv_range_falls_back_to_explicit_versions_list() {
        // No SEMVER range (an ECOSYSTEM range is skipped) but an explicit affected versions list.
        let entry = json!({
            "ranges": [{ "type": "ECOSYSTEM", "events": [{ "introduced": "0" }] }],
            "versions": ["1.2.3", "1.2.4"]
        });
        assert_eq!(
            osv_range_string(&entry).as_deref(),
            Some("=1.2.3 || =1.2.4")
        );
    }

    #[test]
    fn parse_osv_vuln_matches_npm_package_only() {
        let record = json!({
            "id": "GHSA-jf85-cpcp-j695",
            "summary": "Prototype Pollution in lodash",
            "aliases": ["CVE-2019-10744"],
            "database_specific": { "severity": "CRITICAL", "cwe_ids": ["CWE-1321", "CWE-20"] },
            "references": [{ "type": "ADVISORY", "url": "https://nvd.nist.gov/vuln/detail/CVE-2019-10744" }],
            "affected": [
                { "package": { "name": "lodash", "ecosystem": "npm" },
                  "ranges": [{ "type": "SEMVER", "events": [{ "introduced": "0" }, { "fixed": "4.17.12" }] }] },
                { "package": { "name": "lodash-rails", "ecosystem": "RubyGems" },
                  "ranges": [{ "type": "ECOSYSTEM", "events": [{ "introduced": "0" }, { "fixed": "4.17.12" }] }] }
            ]
        });
        let adv = parse_osv_vuln(&record, "lodash", "4.17.11").expect("npm lodash affected");
        assert_eq!(adv.source, "osv");
        assert_eq!(adv.id, "GHSA-jf85-cpcp-j695");
        assert_eq!(adv.aliases, vec!["CVE-2019-10744"]);
        assert_eq!(adv.severity, Some(Severity::Critical));
        assert_eq!(adv.vulnerable_range, "<4.17.12");
        assert_eq!(adv.cwe, vec!["CWE-1321", "CWE-20"]);
        assert_eq!(
            adv.url.as_deref(),
            Some("https://nvd.nist.gov/vuln/detail/CVE-2019-10744")
        );

        // The RubyGems ecosystem and unrelated names are ignored.
        assert!(parse_osv_vuln(&record, "lodash-rails", "4.17.11").is_none());
        assert!(parse_osv_vuln(&record, "express", "1.0.0").is_none());
    }

    #[test]
    fn parse_osv_vuln_keeps_a_confirmed_hit_without_a_semver_range() {
        // An npm entry with no SEMVER range and no versions list, and no severity bucket (only a
        // CVSS vector): the querybatch-confirmed version must still be kept — with an exact fallback
        // range and an unknown (None) severity that the audit treats conservatively.
        let record = json!({
            "id": "GHSA-xxxx-yyyy-zzzz",
            "summary": "Something in demo",
            "severity": [{ "type": "CVSS_V3", "score": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H" }],
            "affected": [
                { "package": { "name": "demo", "ecosystem": "npm" },
                  "ranges": [{ "type": "ECOSYSTEM", "events": [{ "introduced": "0" }] }] }
            ]
        });
        let adv = parse_osv_vuln(&record, "demo", "1.2.3").expect("confirmed hit is kept");
        assert_eq!(adv.vulnerable_range, "=1.2.3");
        assert_eq!(adv.severity, None);
        assert!(adv.cvss_vector.is_some());
    }
}