//! Findings from outside the scan pipeline: entered by hand, or imported from //! a tool's report (SARIF 2.1.0, OWASP ZAP JSON). //! //! A DAST run or a penetration test is done by a person, not by the agent. Its //! results still belong next to the scan's findings, with the same fields, so //! the customer sees one list and the evidence pack one set. pub mod sarif; pub mod zap; use compliance_core::models::{Finding, ScanType, Severity}; use serde::Deserialize; use crate::pipeline::dedup; /// Longest excerpt of evidence (a request or response) kept on a finding. const MAX_EVIDENCE_CHARS: usize = 4_000; /// What a finding from a manual test says, whether typed into the form or /// read from a report. #[derive(Debug, Clone, Deserialize, PartialEq, utoipa::ToSchema)] pub struct ManualFinding { /// What was found. pub title: String, /// The finding in detail. #[serde(default)] pub description: String, /// `info`, `low`, `medium`, `high` or `critical`. #[schema(value_type = String, example = "high")] pub severity: Severity, /// `CWE-79`, or just `79`. #[serde(default)] pub cwe: Option, /// The CVSS base score. #[serde(default)] pub cvss_score: Option, /// Where it was found: a URL, an endpoint, a file. #[serde(default)] pub location: Option, /// A request or response excerpt that shows it. #[serde(default)] pub evidence: Option, /// How to reproduce it. #[serde(default)] pub reproduction: Option, /// How to fix it. #[serde(default)] pub remediation: Option, /// The tool's rule or plugin id, for an imported finding. #[serde(default)] pub rule_id: Option, } /// Where a finding came from, for the stored record. #[derive(Debug, Clone, Copy)] pub struct Provenance<'a> { /// The target the finding belongs to. pub target_id: &'a str, /// `Dast` or `Pentest`. pub scan_type: ScanType, /// The tool that found it, or `manual`. pub scanner: &'a str, /// The user who entered or imported it. pub reported_by: &'a str, } impl ManualFinding { /// The finding as stored. Its fingerprint covers the target, tool, rule, /// title and location, so importing the same report twice adds nothing. pub fn into_finding(self, from: Provenance<'_>) -> Finding { let cwe = self.cwe.as_deref().and_then(normalise_cwe); let fingerprint = dedup::compute_fingerprint(&[ from.target_id, from.scanner, &from.scan_type.to_string(), self.rule_id.as_deref().unwrap_or_default(), &self.title, self.location.as_deref().unwrap_or_default(), ]); let mut finding = Finding::new( from.target_id.to_string(), fingerprint, from.scanner.to_string(), from.scan_type, self.title, self.description, self.severity, ); finding.control_refs = cwe.as_deref().map(control_refs).unwrap_or_default(); finding.cwe = cwe; finding.cvss_score = self.cvss_score.filter(|s| (0.0..=10.0).contains(s)); finding.file_path = self.location.filter(|l| !l.trim().is_empty()); finding.code_snippet = self.evidence.map(|e| truncate(&e, MAX_EVIDENCE_CHARS)); finding.reproduction = self.reproduction.filter(|r| !r.trim().is_empty()); finding.remediation = self.remediation.filter(|r| !r.trim().is_empty()); finding.rule_id = self.rule_id; finding.reported_by = Some(from.reported_by.to_string()); finding } } /// `CWE-79` from `79`, `cwe-79` or `CWE-79`; `None` for anything else. pub fn normalise_cwe(raw: &str) -> Option { let digits = raw.trim().to_ascii_uppercase(); let digits = digits.strip_prefix("CWE-").unwrap_or(&digits); (!digits.is_empty() && digits.chars().all(|c| c.is_ascii_digit()) && digits != "0") .then(|| format!("CWE-{digits}")) } /// The CRA controls a verified weakness is evidence for (see /// [`control_map::ControlMap::controls_for_cwe`]). pub(crate) fn control_refs(cwe: &str) -> Vec { control_map::ControlMap::cra() .map(|map| { map.controls_for_cwe(cwe) .into_iter() .map(|c| c.control.clone()) .collect() }) .unwrap_or_default() } /// `text` cut to at most `max` characters, on a character boundary. fn truncate(text: &str, max: usize) -> String { match text.char_indices().nth(max) { Some((cut, _)) => format!("{}…", &text[..cut]), None => text.to_string(), } } /// A severity from a CVSS-style score (0–10). pub fn severity_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::Medium, s if s > 0.0 => Severity::Low, _ => Severity::Info, } } #[cfg(test)] mod tests { use super::*; fn provenance() -> Provenance<'static> { Provenance { target_id: "t1", scan_type: ScanType::Pentest, scanner: "manual", reported_by: "u-1", } } fn finding(title: &str, location: Option<&str>) -> ManualFinding { ManualFinding { title: title.to_string(), description: "d".to_string(), severity: Severity::High, cwe: Some("79".to_string()), cvss_score: Some(7.5), location: location.map(String::from), evidence: Some("x".repeat(MAX_EVIDENCE_CHARS + 10)), reproduction: Some(" ".to_string()), remediation: None, rule_id: None, } } #[test] fn a_manual_finding_is_stored_like_a_scanned_one() { let f = finding("Reflected XSS", Some("https://device.local/login")).into_finding(provenance()); assert_eq!(f.scan_type, ScanType::Pentest); assert_eq!(f.cwe.as_deref(), Some("CWE-79")); assert_eq!(f.file_path.as_deref(), Some("https://device.local/login")); assert_eq!(f.reported_by.as_deref(), Some("u-1")); assert_eq!(f.reproduction, None, "blank text is not stored"); assert!(f .code_snippet .is_some_and(|e| e.chars().count() == MAX_EVIDENCE_CHARS + 1)); assert!(!f.control_refs.is_empty(), "CWE-79 maps to a CRA control"); } #[test] fn the_same_finding_twice_has_one_fingerprint() { let a = finding("XSS", Some("/a")).into_finding(provenance()); let b = finding("XSS", Some("/a")).into_finding(provenance()); let c = finding("XSS", Some("/b")).into_finding(provenance()); assert_eq!(a.fingerprint, b.fingerprint); assert_ne!(a.fingerprint, c.fingerprint); } #[test] fn cwes_and_scores_are_read_leniently() { assert_eq!(normalise_cwe("cwe-89").as_deref(), Some("CWE-89")); assert_eq!(normalise_cwe(" 22 ").as_deref(), Some("CWE-22")); assert_eq!(normalise_cwe("0"), None); assert_eq!(normalise_cwe("CWE-"), None); assert_eq!(normalise_cwe("injection"), None); assert_eq!(severity_from_score(9.8), Severity::Critical); assert_eq!(severity_from_score(5.0), Severity::Medium); assert_eq!(severity_from_score(0.0), Severity::Info); } }