|
| 1 | +use std::collections::HashSet; |
| 2 | +use crate::csaf::csaf2_1::schema::CategoryOfTheRemediation; |
| 3 | +use crate::csaf::getter_traits::{CsafTrait, ProductStatusTrait, RemediationTrait, VulnerabilityTrait}; |
| 4 | +use crate::csaf::validation::ValidationError; |
| 5 | + |
| 6 | +/// Remediation categories that conflict with the product status "not affected". |
| 7 | +const NOT_AFFECTED_CONFLICTS: &[CategoryOfTheRemediation] = &[ |
| 8 | + CategoryOfTheRemediation::Workaround, |
| 9 | + CategoryOfTheRemediation::Mitigation, |
| 10 | + CategoryOfTheRemediation::VendorFix, |
| 11 | + CategoryOfTheRemediation::NoneAvailable, |
| 12 | +]; |
| 13 | + |
| 14 | +/// Remediation categories that conflict with "fixed" product statuses. |
| 15 | +const FIXED_CONFLICTS: &[CategoryOfTheRemediation] = &[ |
| 16 | + CategoryOfTheRemediation::NoneAvailable, |
| 17 | + CategoryOfTheRemediation::FixPlanned, |
| 18 | + CategoryOfTheRemediation::NoFixPlanned, |
| 19 | + CategoryOfTheRemediation::VendorFix, |
| 20 | + CategoryOfTheRemediation::Mitigation, |
| 21 | + CategoryOfTheRemediation::Workaround, |
| 22 | +]; |
| 23 | + |
| 24 | +pub fn test_6_1_36_status_group_contradicting_remediation_categories( |
| 25 | + doc: &impl CsafTrait, |
| 26 | +) -> Result<(), ValidationError> { |
| 27 | + for (v_i, v) in doc.get_vulnerabilities().iter().enumerate() { |
| 28 | + if let Some(product_status) = v.get_product_status() { |
| 29 | + // Collect Product IDs that may cause conflicts |
| 30 | + let affected_products = product_status.get_all_affected(); |
| 31 | + let not_affected_products = match product_status.get_known_not_affected() { |
| 32 | + Some(products) => products.into_iter().collect(), |
| 33 | + None => HashSet::new(), |
| 34 | + }; |
| 35 | + let fixed_products = product_status.get_all_fixed(); |
| 36 | + // Iterate over remediations |
| 37 | + for (r_i, r) in v.get_remediations().iter().enumerate() { |
| 38 | + // Only handle Remediations having product IDs associated |
| 39 | + if let Some(product_ids) = r.get_all_product_ids(doc) { |
| 40 | + // Category of current remediation |
| 41 | + let cat = r.get_category(); |
| 42 | + // Iterate over product IDs |
| 43 | + for p in product_ids { |
| 44 | + if affected_products.contains(&p) && cat == CategoryOfTheRemediation::OptionalPatch { |
| 45 | + return Err(ValidationError { |
| 46 | + message: format!( |
| 47 | + "Product {} is listed as affected but has conflicting remediation category {}", |
| 48 | + p, |
| 49 | + cat |
| 50 | + ), |
| 51 | + instance_path: format!("/vulnerabilities/{}/remediations/{}", v_i, r_i), |
| 52 | + }); |
| 53 | + } |
| 54 | + if not_affected_products.contains(&p) && NOT_AFFECTED_CONFLICTS.contains(&cat) { |
| 55 | + return Err(ValidationError { |
| 56 | + message: format!( |
| 57 | + "Product {} is listed as not affected but has conflicting remediation category {}", |
| 58 | + p, |
| 59 | + cat |
| 60 | + ), |
| 61 | + instance_path: format!("/vulnerabilities/{}/remediations/{}", v_i, r_i), |
| 62 | + }); |
| 63 | + } |
| 64 | + if fixed_products.contains(&p) && FIXED_CONFLICTS.contains(&cat) { |
| 65 | + return Err(ValidationError { |
| 66 | + message: format!( |
| 67 | + "Product {} is listed as fixed but has conflicting remediation category {}", |
| 68 | + p, |
| 69 | + cat |
| 70 | + ), |
| 71 | + instance_path: format!("/vulnerabilities/{}/remediations/{}", v_i, r_i), |
| 72 | + }); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + Ok(()) |
| 80 | +} |
| 81 | + |
| 82 | +#[cfg(test)] |
| 83 | +mod tests { |
| 84 | + use crate::csaf::csaf2_1::loader::load_document; |
| 85 | + use crate::csaf::validation::ValidationError; |
| 86 | + use crate::csaf::validations::test_6_1_36::test_6_1_36_status_group_contradicting_remediation_categories; |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn test_test_6_1_36() { |
| 90 | + for x in ["11", "12", "13"].iter() { |
| 91 | + let doc = load_document(format!("../csaf/csaf_2.1/test/validator/data/mandatory/oasis_csaf_tc-csaf_2_1-2024-6-1-36-{}.json", x).as_str()).unwrap(); |
| 92 | + assert_eq!( |
| 93 | + Ok(()), |
| 94 | + test_6_1_36_status_group_contradicting_remediation_categories(&doc) |
| 95 | + ) |
| 96 | + } |
| 97 | + for (x, err) in [ |
| 98 | + ("01", ValidationError { |
| 99 | + message: "Product CSAFPID-9080700 is listed as not affected but has conflicting remediation category vendor_fix".to_string(), |
| 100 | + instance_path: "/vulnerabilities/0/remediations/0".to_string() |
| 101 | + }), |
| 102 | + ("02", ValidationError { |
| 103 | + message: "Product CSAFPID-9080703 is listed as fixed but has conflicting remediation category none_available".to_string(), |
| 104 | + instance_path: "/vulnerabilities/0/remediations/0".to_string() |
| 105 | + }), |
| 106 | + ("03", ValidationError { |
| 107 | + message: "Product CSAFPID-9080700 is listed as affected but has conflicting remediation category optional_patch".to_string(), |
| 108 | + instance_path: "/vulnerabilities/0/remediations/0".to_string(), |
| 109 | + }), |
| 110 | + ].iter() { |
| 111 | + let doc = load_document(format!("../csaf/csaf_2.1/test/validator/data/mandatory/oasis_csaf_tc-csaf_2_1-2024-6-1-36-{}.json", x).as_str()).unwrap(); |
| 112 | + assert_eq!( |
| 113 | + Err(err.clone()), |
| 114 | + test_6_1_36_status_group_contradicting_remediation_categories(&doc) |
| 115 | + ) |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments