Skip to content

Building 70 Projects ranging from beginner to advanced so anyone can — learn from, build upon, use as a reference, or even copy directly. Owned by my cybersecurity company 👇

3.5 KB
Raw Blame
// ©AngelaMos | 2026
// severity.rs

pub const CASR_EXPLOITABLE: &str = "EXPLOITABLE";
pub const CASR_PROBABLY_EXPLOITABLE: &str = "PROBABLY_EXPLOITABLE";
pub const CASR_NOT_EXPLOITABLE: &str = "NOT_EXPLOITABLE";
pub const CASR_UNDEFINED: &str = "UNDEFINED";

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Severity {
    Exploitable,
    ProbablyExploitable,
    NotExploitable,
    Undefined,
}

impl Severity {
    #[must_use]
    pub fn from_casr(severity: &str) -> Option<Self> {
        match severity {
            CASR_EXPLOITABLE => Some(Self::Exploitable),
            CASR_PROBABLY_EXPLOITABLE => Some(Self::ProbablyExploitable),
            CASR_NOT_EXPLOITABLE => Some(Self::NotExploitable),
            CASR_UNDEFINED => Some(Self::Undefined),
            _ => None,
        }
    }

    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Exploitable => CASR_EXPLOITABLE,
            Self::ProbablyExploitable => CASR_PROBABLY_EXPLOITABLE,
            Self::NotExploitable => CASR_NOT_EXPLOITABLE,
            Self::Undefined => CASR_UNDEFINED,
        }
    }
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use libcasr::execution_class::CLASSES;

    use super::*;

    const TAXONOMY_SIZE: usize = 75;
    const EXPLOITABLE_CLASSES: usize = 18;
    const PROBABLY_EXPLOITABLE_CLASSES: usize = 22;
    const NOT_EXPLOITABLE_CLASSES: usize = 34;
    const UNDEFINED_CLASSES: usize = 1;

    #[test]
    fn every_severity_the_pinned_taxonomy_can_produce_has_a_typed_equivalent() {
        let unmapped: Vec<String> = CLASSES
            .iter()
            .filter(|(severity, _, _, _)| Severity::from_casr(severity).is_none())
            .map(|(severity, class, _, _)| format!("{class} -> {severity}"))
            .collect();
        assert!(
            unmapped.is_empty(),
            "libcasr types severity as a String, so a new tier upstream is never a compile error and would silently fall out of triage: {unmapped:?}"
        );
    }

    #[test]
    fn the_pinned_taxonomy_still_has_the_shape_this_crate_was_written_against() {
        let mut tiers: BTreeMap<&str, usize> = BTreeMap::new();
        for (severity, _, _, _) in CLASSES {
            *tiers.entry(severity).or_default() += 1;
        }
        assert_eq!(CLASSES.len(), TAXONOMY_SIZE);
        assert_eq!(tiers.len(), 4);
        assert_eq!(tiers[CASR_EXPLOITABLE], EXPLOITABLE_CLASSES);
        assert_eq!(
            tiers[CASR_PROBABLY_EXPLOITABLE],
            PROBABLY_EXPLOITABLE_CLASSES
        );
        assert_eq!(tiers[CASR_NOT_EXPLOITABLE], NOT_EXPLOITABLE_CLASSES);
        assert_eq!(
            tiers[CASR_UNDEFINED], UNDEFINED_CLASSES,
            "the taxonomy is pinned at =2.13.1, so this can only move by a deliberate version bump, and a bump must be re-censused before it ships"
        );
    }

    #[test]
    fn a_severity_string_outside_the_taxonomy_is_refused_rather_than_guessed() {
        assert_eq!(Severity::from_casr("VERY_EXPLOITABLE"), None);
        assert_eq!(Severity::from_casr(""), None);
        assert_eq!(Severity::from_casr("exploitable"), None);
    }

    #[test]
    fn the_typed_severity_round_trips_through_its_taxonomy_string() {
        for severity in [
            Severity::Exploitable,
            Severity::ProbablyExploitable,
            Severity::NotExploitable,
            Severity::Undefined,
        ] {
            assert_eq!(Severity::from_casr(severity.as_str()), Some(severity));
        }
    }
}