~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/vuln-notif

0
0
main
297 lines · 7.9 KB Raw History
//! Affected version planner tests: OSV-shaped advisories evaluated against a
//! source version catalog, with no fabricated versions.

use std::cmp::Ordering;

use vuln_notif::release::planner::{
    Advisory, AffectedPackage, AffectedRange, CatalogVersion, LexicalCompare, RangeEvent,
    VersionCompare, VersionPlan, plan_versions,
};

const RHEL_ADVISORY: &str = include_str!("fixtures/planner/rhel_advisory.json");
const RHEL_CATALOG: &str = include_str!("fixtures/planner/catalog_rhel.json");
const NO_MATCH: &str = include_str!("fixtures/planner/no_match.json");

const PROJECT: &str = "red-hat-enterprise-linux";
const PURL: &str = "pkg:rpm/redhat/enterprise_linux";

fn catalog() -> Vec<CatalogVersion> {
    serde_json::from_str(RHEL_CATALOG).expect("catalog fixture")
}

fn versions(plan: &VersionPlan) -> Vec<&str> {
    plan.versions
        .iter()
        .map(|version| version.version.as_str())
        .collect()
}

fn introduced(version: &str) -> RangeEvent {
    RangeEvent {
        introduced: Some(version.to_owned()),
        ..RangeEvent::default()
    }
}

fn fixed(version: &str) -> RangeEvent {
    RangeEvent {
        fixed: Some(version.to_owned()),
        ..RangeEvent::default()
    }
}

fn last_affected(version: &str) -> RangeEvent {
    RangeEvent {
        last_affected: Some(version.to_owned()),
        ..RangeEvent::default()
    }
}

fn advisory_with(
    id: &str,
    aliases: &[&str],
    events: Vec<RangeEvent>,
    versions: &[&str],
) -> Advisory {
    let ranges = if events.is_empty() {
        Vec::new()
    } else {
        vec![AffectedRange {
            kind: "ECOSYSTEM".to_owned(),
            events,
        }]
    };
    Advisory {
        id: id.to_owned(),
        aliases: aliases.iter().map(|alias| (*alias).to_owned()).collect(),
        affected: vec![AffectedPackage {
            ecosystem: "Red Hat".to_owned(),
            name: "enterprise_linux".to_owned(),
            purl: Some(PURL.to_owned()),
            ranges,
            versions: versions
                .iter()
                .map(|version| (*version).to_owned())
                .collect(),
        }],
        ..Advisory::default()
    }
}

#[test]
fn lexical_comparator_is_a_test_scheme() {
    assert_eq!(LexicalCompare.scheme(), "lexical");
    assert_eq!(LexicalCompare.compare("9.4", "9.5"), Ordering::Less);
}

#[test]
fn rhel_range_plans_affected_catalog_versions() {
    let advisory: Advisory = serde_json::from_str(RHEL_ADVISORY).expect("advisory fixture");
    // The CVE comes from the advisory alias, not from the OSV/RHSA id.
    let plan = plan_versions(
        "CVE-2024-0001",
        PROJECT,
        PURL,
        &catalog(),
        &[advisory],
        &LexicalCompare,
    );

    assert_eq!(plan.project_slug, PROJECT);
    assert_eq!(plan.purl, PURL);
    assert!(plan.skipped_reason.is_none());
    assert_eq!(versions(&plan), ["9.0", "9.1", "9.2", "9.3"]);
    assert!(plan.versions.iter().all(|version| version.affected));
    assert!(
        plan.versions
            .iter()
            .all(|version| version.fixed_in.as_deref() == Some("9.4"))
    );
    assert_eq!(plan.affected_ranges, [">= 9.0, < 9.4"]);
    assert!(plan.version_labels.is_empty());
}

#[test]
fn last_affected_is_inclusive() {
    let advisory = advisory_with(
        "OSV-2024-0002",
        &["CVE-2024-0002"],
        vec![introduced("9.1"), last_affected("9.3")],
        &[],
    );
    let plan = plan_versions(
        "CVE-2024-0002",
        PROJECT,
        PURL,
        &catalog(),
        &[advisory],
        &LexicalCompare,
    );

    assert_eq!(versions(&plan), ["9.1", "9.2", "9.3"]);
    assert_eq!(plan.affected_ranges, [">= 9.1, <= 9.3"]);
    assert!(
        plan.versions
            .iter()
            .all(|version| version.fixed_in.is_none())
    );
}

#[test]
fn exact_versions_list_matches_catalog() {
    let advisory = advisory_with(
        "OSV-2024-0003",
        &["CVE-2024-0003"],
        Vec::new(),
        &["9.1", "9.3"],
    );
    let plan = plan_versions(
        "CVE-2024-0003",
        PROJECT,
        PURL,
        &catalog(),
        &[advisory],
        &LexicalCompare,
    );

    assert_eq!(versions(&plan), ["9.1", "9.3"]);
    assert!(plan.affected_ranges.is_empty());
    assert!(plan.version_labels.is_empty());
}

#[test]
fn ranges_missing_from_catalog_emit_labels_not_versions() {
    // The fixed version 9.6 has no catalog record; affected records stop at
    // the catalog edge and the endpoint becomes a version label.
    let partial = advisory_with(
        "OSV-2024-0004",
        &["CVE-2024-0004"],
        vec![introduced("9.0"), fixed("9.6")],
        &[],
    );
    let plan = plan_versions(
        "CVE-2024-0004",
        PROJECT,
        PURL,
        &catalog(),
        &[partial],
        &LexicalCompare,
    );
    assert_eq!(versions(&plan), ["9.0", "9.1", "9.2", "9.3", "9.4", "9.5"]);
    assert_eq!(plan.version_labels, ["9.6"]);
    assert_eq!(plan.affected_ranges, [">= 9.0, < 9.6"]);
    assert!(plan.skipped_reason.is_none());

    // A range entirely outside the catalog plans no versions.
    let outside = advisory_with(
        "OSV-2024-0005",
        &["CVE-2024-0005"],
        vec![introduced("9.7"), fixed("9.8")],
        &[],
    );
    let plan = plan_versions(
        "CVE-2024-0005",
        PROJECT,
        PURL,
        &catalog(),
        &[outside],
        &LexicalCompare,
    );
    assert!(plan.versions.is_empty());
    assert_eq!(plan.version_labels, ["9.7", "9.8"]);
    assert_eq!(plan.affected_ranges, [">= 9.7, < 9.8"]);
    assert!(plan.skipped_reason.is_none());
}

#[test]
fn no_osv_match_skips_without_versions() {
    let advisories: Vec<Advisory> = serde_json::from_str(NO_MATCH).expect("no-match fixture");
    let plan = plan_versions(
        "CVE-2024-0001",
        PROJECT,
        PURL,
        &catalog(),
        &advisories,
        &LexicalCompare,
    );
    assert_eq!(plan.skipped_reason.as_deref(), Some("no_osv_match"));
    assert!(plan.versions.is_empty());
    assert!(plan.version_labels.is_empty());
    assert!(plan.affected_ranges.is_empty());

    // NVD-only CVE: there is no advisory at all, so nothing is inferred.
    let plan = plan_versions(
        "CVE-2024-0001",
        PROJECT,
        PURL,
        &catalog(),
        &[],
        &LexicalCompare,
    );
    assert_eq!(plan.skipped_reason.as_deref(), Some("no_osv_match"));
    assert!(plan.versions.is_empty());
}

#[test]
fn plan_is_deterministic_regardless_of_advisory_order() {
    let first = advisory_with(
        "OSV-2024-0006",
        &["CVE-2024-0006"],
        vec![introduced("9.0"), fixed("9.2")],
        &[],
    );
    let second = advisory_with(
        "OSV-2024-0007",
        &["CVE-2024-0006"],
        vec![introduced("9.2"), fixed("9.5")],
        &[],
    );
    let forward = plan_versions(
        "CVE-2024-0006",
        PROJECT,
        PURL,
        &catalog(),
        &[first.clone(), first.clone(), second.clone()],
        &LexicalCompare,
    );
    let backward = plan_versions(
        "CVE-2024-0006",
        PROJECT,
        PURL,
        &catalog(),
        &[second, first],
        &LexicalCompare,
    );

    assert_eq!(forward, backward);
    assert_eq!(versions(&forward), ["9.0", "9.1", "9.2", "9.3", "9.4"]);
    assert_eq!(forward.affected_ranges, [">= 9.0, < 9.2", ">= 9.2, < 9.5"]);
    assert!(forward.version_labels.is_empty());
}

#[test]
fn deprecated_catalog_versions_are_still_planned() {
    let mut catalog = catalog();
    catalog
        .iter_mut()
        .find(|entry| entry.raw == "9.1")
        .expect("fixture has 9.1")
        .deprecated = true;
    let advisory = advisory_with(
        "OSV-2024-0008",
        &["CVE-2024-0008"],
        vec![introduced("9.0"), fixed("9.2")],
        &[],
    );
    let plan = plan_versions(
        "CVE-2024-0008",
        PROJECT,
        PURL,
        &catalog,
        &[advisory],
        &LexicalCompare,
    );

    assert_eq!(versions(&plan), ["9.0", "9.1"]);
    assert!(plan.versions.iter().any(|version| version.raw == "9.1"));
}