Debian Codegraph @

src/v4/metric/base/at.rs

raw symbols SCIP
1 //! Attack Requirements (AT)
2
3 use crate::{
4 Error, Result,
5 v4::metric::{Metric, MetricType},
6 };
7 use alloc::borrow::ToOwned;
8 use core::{fmt, str::FromStr};
9
10 /// Attack Requirements (AT) - CVSS v4.0 Base Metric Group
11 ///
12 /// Described in CVSS v4.0 Specification: Section 2.1.3
13 ///
14 /// > This metric captures the prerequisite **deployment and execution
15 /// > conditions or variables** of the vulnerable system that enable the attack.
16 /// > These differ from security-enhancing techniques/technologies (ref _Attack
17 /// > Complexity_) as the primary purpose of these conditions is **not** to
18 /// > explicitly mitigate attacks, but rather, emerge naturally as a consequence
19 /// > of the deployment and execution of the vulnerable system. If the attacker
20 /// > does not take action to overcome these conditions, the attack may succeed
21 /// > only occasionally or not succeed at all.
22 #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
23 pub enum AttackRequirements {
24 /// Present (P)
25 ///
26 /// > The successful attack depends on the presence of specific deployment
27 /// > and execution conditions of the vulnerable system that enable the
28 /// > attack. These include: A **race condition** must be won to
29 /// > successfully exploit the vulnerability. The successfulness of the
30 /// > attack is conditioned on execution conditions that are not under full
31 /// > control of the attacker. The attack may need to be launched multiple
32 /// > times against a single target before being successful. Network
33 /// > injection. The attacker must inject themselves into the logical
34 /// > network path between the target and the resource requested by the
35 /// > victim (e.g. vulnerabilities requiring an on-path attacker).
36 Present,
37
38 /// None (N)
39 ///
40 /// > The successful attack does not depend on the deployment and execution
41 /// > conditions of the vulnerable system. The attacker can expect to be
42 /// > able to reach the vulnerability and execute the exploit under all or
43 /// > most instances of the vulnerability.
44 None,
45 }
46
47 impl Default for AttackRequirements {
48 fn default() -> Self {
49 Self::None
50 }
51 }
52
53 impl Metric for AttackRequirements {
54 const TYPE: MetricType = MetricType::AT;
55
56 fn as_str(self) -> &'static str {
57 match self {
58 AttackRequirements::Present => "P",
59 AttackRequirements::None => "N",
60 }
61 }
62 }
63
64 impl fmt::Display for AttackRequirements {
65 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 write!(f, "{}:{}", Self::name(), self.as_str())
67 }
68 }
69
70 impl FromStr for AttackRequirements {
71 type Err = Error;
72
73 fn from_str(s: &str) -> Result<Self> {
74 match s {
75 "P" => Ok(AttackRequirements::Present),
76 "N" => Ok(AttackRequirements::None),
77 _ => Err(Error::InvalidMetricV4 {
78 metric_type: Self::TYPE,
79 value: s.to_owned(),
80 }),
81 }
82 }
83 }
84
85 #[cfg(feature = "std")]
86 pub(crate) mod merge {
87 use super::*;
88 use crate::{
89 Error,
90 v4::{
91 MetricType,
92 metric::{MetricLevel, environmental::ModifiedAttackRequirements},
93 },
94 };
95 use alloc::borrow::ToOwned;
96 use core::str::FromStr;
97
98 /// Result of the merging of the base and modified metrics.
99 ///
100 /// Used in scoring.
101 #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
102 pub(crate) enum MergedAttackRequirements {
103 Present,
104 None,
105 }
106
107 impl Default for MergedAttackRequirements {
108 fn default() -> Self {
109 Self::None
110 }
111 }
112
113 impl FromStr for MergedAttackRequirements {
114 type Err = Error;
115
116 fn from_str(s: &str) -> Result<Self> {
117 match s {
118 "P" => Ok(MergedAttackRequirements::Present),
119 "N" => Ok(MergedAttackRequirements::None),
120 _ => Err(Error::InvalidMetricV4 {
121 metric_type: MetricType::AT,
122 value: s.to_owned(),
123 }),
124 }
125 }
126 }
127
128 impl MetricLevel for MergedAttackRequirements {
129 fn level(self) -> f64 {
130 // AT_levels = {'N': 0.0, 'P': 0.1}
131 match self {
132 Self::Present => 0.1,
133 Self::None => 0.0,
134 }
135 }
136 }
137
138 impl AttackRequirements {
139 pub(crate) fn merge(
140 self,
141 value: Option<ModifiedAttackRequirements>,
142 ) -> MergedAttackRequirements {
143 match value {
144 Some(ModifiedAttackRequirements::NotDefined) | None => match self {
145 Self::Present => MergedAttackRequirements::Present,
146 Self::None => MergedAttackRequirements::None,
147 },
148 Some(ModifiedAttackRequirements::Present) => MergedAttackRequirements::Present,
149 Some(ModifiedAttackRequirements::None) => MergedAttackRequirements::None,
150 }
151 }
152 }
153 }