Debian Codegraph @

src/v4/metric/base/pr.rs

raw symbols SCIP
1 //! Privileges Required (PR)
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 /// Privileges Required (PR) - CVSS v4.0 Base Metric Group
11 ///
12 /// Described in CVSS v4.0 Specification: Section 2.1.4
13 ///
14 /// > This metric describes the level of privileges an attacker must possess
15 /// > prior to successfully exploiting the vulnerability. The method by which
16 /// > the attacker obtains privileged credentials prior to the attack (e.g.,
17 /// > free trial accounts), is outside the scope of this metric. Generally,
18 /// > self-service provisioned accounts do not constitute a privilege
19 /// > requirement if the attacker can grant themselves privileges as part of the
20 /// > attack.
21 #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
22 pub enum PrivilegesRequired {
23 /// High (H)
24 ///
25 /// > The attacker requires privileges that provide significant (e.g.,
26 /// > administrative) control over the vulnerable system allowing full
27 /// > access to the vulnerable system’s settings and files.
28 High,
29
30 /// Low (L)
31 ///
32 /// > The attacker requires privileges that provide basic capabilities that
33 /// > are typically limited to settings and resources owned by a single
34 /// > low-privileged user. Alternatively, an attacker with Low privileges
35 /// > has the ability to access only non-sensitive resources.
36 Low,
37
38 /// None (N)
39 ///
40 /// > The attacker is unauthenticated prior to attack, and therefore does
41 /// > not require any access to settings or files of the vulnerable system
42 /// > to carry out an attack.
43 None,
44 }
45
46 impl Default for PrivilegesRequired {
47 fn default() -> Self {
48 Self::None
49 }
50 }
51
52 impl Metric for PrivilegesRequired {
53 const TYPE: MetricType = MetricType::PR;
54
55 fn as_str(self) -> &'static str {
56 match self {
57 PrivilegesRequired::None => "N",
58 PrivilegesRequired::Low => "L",
59 PrivilegesRequired::High => "H",
60 }
61 }
62 }
63
64 impl fmt::Display for PrivilegesRequired {
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 PrivilegesRequired {
71 type Err = Error;
72
73 fn from_str(s: &str) -> Result<Self> {
74 match s {
75 "N" => Ok(PrivilegesRequired::None),
76 "L" => Ok(PrivilegesRequired::Low),
77 "H" => Ok(PrivilegesRequired::High),
78 _ => Err(Error::InvalidMetricV4 {
79 metric_type: Self::TYPE,
80 value: s.to_owned(),
81 }),
82 }
83 }
84 }
85
86 #[cfg(feature = "std")]
87 pub(crate) mod merge {
88 use super::*;
89 use crate::{
90 Error,
91 v4::{
92 MetricType,
93 metric::{MetricLevel, environmental::ModifiedPrivilegesRequired},
94 },
95 };
96 use alloc::borrow::ToOwned;
97 use core::str::FromStr;
98
99 /// Result of the merging of the base and modified metrics.
100 ///
101 /// Used in scoring.
102 #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
103 pub(crate) enum MergedPrivilegesRequired {
104 High,
105 Low,
106 None,
107 }
108
109 impl Default for MergedPrivilegesRequired {
110 fn default() -> Self {
111 Self::None
112 }
113 }
114
115 impl FromStr for MergedPrivilegesRequired {
116 type Err = Error;
117
118 fn from_str(s: &str) -> Result<Self> {
119 match s {
120 "H" => Ok(MergedPrivilegesRequired::High),
121 "L" => Ok(MergedPrivilegesRequired::Low),
122 "N" => Ok(MergedPrivilegesRequired::None),
123 _ => Err(Error::InvalidMetricV4 {
124 metric_type: MetricType::PR,
125 value: s.to_owned(),
126 }),
127 }
128 }
129 }
130
131 impl MetricLevel for MergedPrivilegesRequired {
132 fn level(self) -> f64 {
133 // PR_levels = {"N": 0.0, "L": 0.1, "H": 0.2}
134 match self {
135 Self::High => 0.2,
136 Self::Low => 0.1,
137 Self::None => 0.0,
138 }
139 }
140 }
141
142 impl PrivilegesRequired {
143 pub(crate) fn merge(
144 self,
145 value: Option<ModifiedPrivilegesRequired>,
146 ) -> MergedPrivilegesRequired {
147 match value {
148 Some(ModifiedPrivilegesRequired::NotDefined) | None => match self {
149 Self::High => MergedPrivilegesRequired::High,
150 Self::Low => MergedPrivilegesRequired::Low,
151 Self::None => MergedPrivilegesRequired::None,
152 },
153 Some(ModifiedPrivilegesRequired::High) => MergedPrivilegesRequired::High,
154 Some(ModifiedPrivilegesRequired::Low) => MergedPrivilegesRequired::Low,
155 Some(ModifiedPrivilegesRequired::None) => MergedPrivilegesRequired::None,
156 }
157 }
158 }
159 }