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
//! This parses the output of npm-audit
//!
//! [npm-audit](https://docs.npmjs.com/cli/v7/commands/npm-audit)
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::process::Command;
use std::str::from_utf8;
use tracing::{debug, warn};
/// This is used to return the data from audit()
/// but not used for parsing since we can not easily tell
/// serde how to decide which to use and the untagged union
/// error messages are not great
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase", untagged)]
pub enum NpmAuditData {
    /// audit report version 1 (npm 6 or below)
    Version1(NpmAuditDataV1),
    /// audit report version 2 (npm 8)
    Version2(NpmAuditDataV2),
}
/// audit report version 1
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NpmAuditDataV1 {
    /// UUID identitying the run of npm-audit
    ///
    /// only included in some versions of npm
    pub run_id: Option<String>,
    /// actions to perform to fix vulnerabilities
    pub actions: Vec<Action>,
    /// advisories by id
    pub advisories: BTreeMap<String, Advisory>,
    /// list of muted packages
    ///
    /// only included in some versions of npm audit
    pub muted: Option<Vec<String>>,
    /// vulnerability and dependency counts
    pub metadata: MetadataV1,
}
/// helper to parse module paths
pub fn deserialize_module_path<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let s = String::deserialize(deserializer)?;
    Ok(s.split('>').map(|s| s.to_string()).collect())
}
/// helper to serialize module paths
pub fn serialize_module_path<S>(xs: &[String], serializer: S) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    let s = xs.join(">");
    s.serialize(serializer)
}
/// helper to parse Vec of module paths
pub fn deserialize_module_path_vec<'de, D>(deserializer: D) -> Result<Vec<Vec<String>>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let xs = <Vec<String>>::deserialize(deserializer)?;
    Ok(xs
        .into_iter()
        .map(|x| x.split('>').map(|s| s.to_string()).collect())
        .collect())
}
/// helper to serialize Vec of module paths
pub fn serialize_module_path_vec<S>(xxs: &[Vec<String>], serializer: S) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    let v: Vec<String> = xxs.iter().map(|xs| xs.join(">")).collect();
    v.serialize(serializer)
}
/// helper to parse created in the correct format
/// (default time serde implementation seems to use a different format)
pub fn deserialize_rfc3339<'de, D>(deserializer: D) -> Result<time::OffsetDateTime, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let s = String::deserialize(deserializer)?;
    time::OffsetDateTime::parse(&s, &time::format_description::well_known::Rfc3339)
        .map_err(serde::de::Error::custom)
}
/// helper to serialize created in the correct format
/// (default time serde implementation seems to use a different format)
pub fn serialize_rfc3339<S>(t: &time::OffsetDateTime, serializer: S) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    let s = t
        .format(&time::format_description::well_known::Rfc3339)
        .map_err(serde::ser::Error::custom)?;
    s.serialize(serializer)
}
/// helper to parse updated and deleted in the correct format
/// (default time serde implementation seems to use a different format)
pub fn deserialize_optional_rfc3339<'de, D>(
    deserializer: D,
) -> Result<Option<time::OffsetDateTime>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let s = <Option<String> as Deserialize<'de>>::deserialize(deserializer)?;
    if let Some(s) = s {
        Ok(Some(
            time::OffsetDateTime::parse(&s, &time::format_description::well_known::Rfc3339)
                .map_err(serde::de::Error::custom)?,
        ))
    } else {
        Ok(None)
    }
}
/// helper to serialize updated and deleted in the correct format
/// (default time serde implementation seems to use a different format)
pub fn serialize_optional_rfc3339<S>(
    t: &Option<time::OffsetDateTime>,
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    if let Some(t) = t {
        let s = t
            .format(&time::format_description::well_known::Rfc3339)
            .map_err(serde::ser::Error::custom)?;
        s.serialize(serializer)
    } else {
        let n: Option<String> = None;
        n.serialize(serializer)
    }
}
/// advisory in report version 1
///
/// there is a field metadata in the output here but since I could not find
/// information on its structure it is not parsed (was always null for me)
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Advisory {
    /// numeric id
    pub id: u64,
    /// human readable title
    pub title: String,
    /// where was the module affected by this advisory found in the dependency
    /// tree
    pub findings: Vec<Finding>,
    /// which versions of the affected module are vulnerable
    pub vulnerable_versions: Option<String>,
    /// name of the affected node module
    pub module_name: Option<String>,
    /// how severe is the issue
    pub severity: Severity,
    /// GitHub advisory Id
    pub github_advisory_id: Option<String>,
    /// CVE numbers
    pub cves: Option<Vec<String>>,
    /// if this advisory is public
    pub access: String,
    /// which versions of the affected package are patched
    pub patched_versions: Option<String>,
    /// a human readable recommendation on how to fix this
    pub recommendation: String,
    /// a CWE (common weakness enumeration) identifier
    pub cwe: Option<Vec<String>>,
    /// who found this security issue
    pub found_by: Option<String>,
    /// who reported this security issue
    pub reported_by: Option<String>,
    /// when was this advisory created
    #[serde(
        serialize_with = "serialize_rfc3339",
        deserialize_with = "deserialize_rfc3339"
    )]
    pub created: time::OffsetDateTime,
    /// when was this advisory last updated
    #[serde(
        serialize_with = "serialize_optional_rfc3339",
        deserialize_with = "deserialize_optional_rfc3339"
    )]
    pub updated: Option<time::OffsetDateTime>,
    /// when was this deleted
    #[serde(
        serialize_with = "serialize_optional_rfc3339",
        deserialize_with = "deserialize_optional_rfc3339"
    )]
    pub deleted: Option<time::OffsetDateTime>,
    /// external references, all in one String, with newlines
    pub references: Option<String>,
    /// npm advisory id
    pub npm_advisory_id: Option<String>,
    /// human-readable description
    pub overview: String,
    /// URL to learn more
    pub url: String,
}
/// findings in advisory in report version 1
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Finding {
    /// dependency version found
    version: String,
    /// paths from current module to dependency
    #[serde(
        serialize_with = "serialize_module_path_vec",
        deserialize_with = "deserialize_module_path_vec"
    )]
    paths: Vec<Vec<String>>,
}
/// audit report version 2
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NpmAuditDataV2 {
    /// version of the audit report
    ///
    /// not all versions of npm produce this field
    pub audit_report_version: Option<u32>,
    /// Vulnerabilities found in dependencies
    pub vulnerabilities: BTreeMap<String, VulnerablePackage>,
    /// vulnerability and dependency counts
    pub metadata: MetadataV2,
}
/// Actions to perform to fix security issues
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase", tag = "action")]
pub enum Action {
    /// install a new package
    #[serde(rename_all = "camelCase")]
    Install {
        /// which advisories will this action resolve
        resolves: Vec<Resolves>,
        /// which package do we need to install
        module: String,
        /// how deep in our dependency tree is this package
        depth: Option<u32>,
        /// which version of the package do we need to install
        target: String,
        /// is this a major version
        is_major: bool,
    },
    /// update a package
    #[serde(rename_all = "camelCase")]
    Update {
        /// which advisories will this action resolve
        resolves: Vec<Resolves>,
        /// which package do we need to update
        module: String,
        /// how deep in our dependency tree is this package
        depth: Option<u32>,
        /// which version of the package do we need to update to
        target: String,
    },
    /// review code using a package
    #[serde(rename_all = "camelCase")]
    Review {
        /// which advisories will this action resolve
        resolves: Vec<Resolves>,
        /// which package do we need to review
        module: String,
        /// how deep in our dependency tree is this package
        depth: Option<u32>,
    },
}
/// Which advisories are resolved by an action
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Resolves {
    /// advisory id
    pub id: u64,
    /// path of depedencies from current module to affected module
    #[serde(
        serialize_with = "serialize_module_path",
        deserialize_with = "deserialize_module_path"
    )]
    pub path: Vec<String>,
    /// is this due to a dev dependency of the current package
    pub dev: bool,
    /// is this due to an optional dependency of the current package
    pub optional: bool,
    /// is this due to a bundled dependency of the current package
    pub bundled: bool,
}
/// Severity of vulnerabilities
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Severity {
    /// no need to take action
    None,
    /// just informational
    Info,
    /// low severity
    Low,
    /// moderate severity
    Moderate,
    /// high severity
    High,
    /// critical severity
    Critical,
}
/// The details for a single vulnerable package
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VulnerablePackage {
    /// Package name
    pub name: String,
    /// The severity of the vulnerabilities
    pub severity: Severity,
    /// is this a direct dependency
    pub is_direct: bool,
    /// the vulnerabilities that make this a vulnerable package
    pub via: Vec<Vulnerability>,
    /// not sure what htis means
    pub effects: Vec<String>,
    /// affected version range
    pub range: String,
    /// not sure what this means
    pub nodes: Vec<String>,
    /// is there a fix available
    pub fix_available: Fix,
}
/// a single vulnerability
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase", untagged)]
pub enum Vulnerability {
    /// some vulnerabilities in the via list are only a name
    NameOnly(String),
    /// and some contain full details
    Full {
        /// numeric id, not sure what it means
        source: u64,
        /// the name of the vulnerability, or if none exists the vulnerable package
        name: String,
        /// the name of the dependency which is vulnerable
        dependency: String,
        /// the human readable title of the vulnerability
        title: String,
        /// an URL explaining the vulnerability
        url: String,
        /// the severity of this vulnerability
        severity: Severity,
        /// the affected version range
        range: String,
    },
}
/// a single fix
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
pub enum Fix {
    /// some packages only indicate whether a fix is available or not
    BoolOnly(bool),
    /// others provide more details
    #[serde(rename_all = "camelCase")]
    Full {
        /// the fixed package name
        name: String,
        /// the fixed package version
        version: String,
        /// is this a semver major update
        is_sem_ver_major: bool,
    },
}
/// The vulnerability and dependency counts returned by npm-audit in report
/// version 1
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MetadataV1 {
    /// Vulnerability counts (without total)
    pub vulnerabilities: VulnerabilityCountsV1,
    /// Number of production dependencies
    pub dependencies: u32,
    /// Number of development dependencies
    pub dev_dependencies: u32,
    /// Number of optional dependencies
    pub optional_dependencies: u32,
    /// Total number of dependencies
    pub total_dependencies: u32,
}
/// The vulnerability and dependency counts returned by npm-audit in report
/// version 2
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MetadataV2 {
    /// Vulnerability counts
    pub vulnerabilities: VulnerabilityCountsV2,
    /// Dependency counts
    pub dependencies: DependencyCounts,
}
/// The vulnerability and dependency counts returned by npm-audit in report
/// version 1
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct VulnerabilityCountsV1 {
    /// Number of info level vulnerabilities
    pub info: u32,
    /// Number of low level vulnerabilities
    pub low: u32,
    /// Number of moderate level vulnerabilities
    pub moderate: u32,
    /// Number of high level vulnerabilities
    pub high: u32,
    /// Number of critical level vulnerabilities
    pub critical: u32,
}
/// The vulnerability and dependency counts returned by npm-audit in report
/// version 2
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct VulnerabilityCountsV2 {
    /// Number of total vulnerabilities
    pub total: u32,
    /// Number of info level vulnerabilities
    pub info: u32,
    /// Number of low level vulnerabilities
    pub low: u32,
    /// Number of moderate level vulnerabilities
    pub moderate: u32,
    /// Number of high level vulnerabilities
    pub high: u32,
    /// Number of critical level vulnerabilities
    pub critical: u32,
}
/// The vulnerability and dependency counts returned by npm-audit
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DependencyCounts {
    /// Total number of dependencies
    pub total: u32,
    /// Number of production dependencies
    pub prod: u32,
    /// Number of development dependencies
    pub dev: u32,
    /// Number of optional dependencies
    pub optional: u32,
    /// Number of peer dependencies
    ///
    /// see <https://nodejs.org/es/blog/npm/peer-dependencies/>
    pub peer: u32,
    /// Number of optional peer dependencies
    pub peer_optional: u32,
}
/// What the exit code indicated about required updates
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum IndicatedUpdateRequirement {
    /// No update is required
    UpToDate,
    /// An update is required
    UpdateRequired,
}
impl std::fmt::Display for IndicatedUpdateRequirement {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            IndicatedUpdateRequirement::UpToDate => {
                write!(f, "up-to-date")
            }
            IndicatedUpdateRequirement::UpdateRequired => {
                write!(f, "update-required")
            }
        }
    }
}
/// main entry point for the npm-audit call
pub fn audit() -> Result<(IndicatedUpdateRequirement, NpmAuditData), crate::Error> {
    let mut version_cmd = Command::new("npm");
    version_cmd.args(["--version"]);
    let version_output = version_cmd.output()?;
    let version = from_utf8(&version_output.stdout)?.trim();
    debug!("Got version string {} from npm --version", version);
    let report_format = match versions::Versioning::new(version) {
        Some(version) => {
            debug!("Got version {} from npm --version", version);
            let audit_report_change = versions::Versioning::new("7.0.0").unwrap();
            if version < audit_report_change {
                debug!(
                    "Dealing with npm before version {}, using report format 1",
                    audit_report_change
                );
                1
            } else {
                debug!(
                    "Dealing with npm version {} or above, using report format 2",
                    audit_report_change
                );
                2
            }
        }
        None => {
            // if --version already fails I do not have high hopes for
            // parsing anything but we might as well assume we are dealing with a
            // newer version since audit only appeared in npm version 6
            debug!("Could not parse npm version, defaulting to report format 2");
            2
        }
    };
    debug!("Using report format {}", report_format);
    let mut cmd = Command::new("npm");
    cmd.args(["audit", "--json"]);
    let output = cmd.output()?;
    if !output.status.success() {
        warn!(
            "npm audit did not return with a successful exit code: {}",
            output.status
        );
        debug!("stdout:\n{}", from_utf8(&output.stdout)?);
        if !output.stderr.is_empty() {
            warn!("stderr:\n{}", from_utf8(&output.stderr)?);
        }
    }
    let update_requirement = if output.status.success() {
        IndicatedUpdateRequirement::UpToDate
    } else {
        IndicatedUpdateRequirement::UpdateRequired
    };
    let json_str = from_utf8(&output.stdout)?;
    let jd = &mut serde_json::Deserializer::from_str(json_str);
    let data: NpmAuditData = match report_format {
        1 => NpmAuditData::Version1(serde_path_to_error::deserialize::<_, NpmAuditDataV1>(jd)?),
        2 => NpmAuditData::Version2(serde_path_to_error::deserialize::<_, NpmAuditDataV2>(jd)?),
        _ => {
            panic!("Unknown report version")
        }
    };
    Ok((update_requirement, data))
}
#[cfg(test)]
mod test {
    use super::*;
    use crate::Error;
    use tracing_test::traced_test;
    /// this test requires a package.json and package-lock.json in the main crate
    /// directory (working dir of the tests)
    #[traced_test]
    #[test]
    fn test_run_npm_audit() -> Result<(), Error> {
        audit()?;
        Ok(())
    }
}