Debian Codegraph @

src/v4/metric/base/ui.rs

raw symbols SCIP
1 //! User Interaction (UI)
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 /// User Interaction (UI) - CVSS v4.0 Base Metric Group
11 ///
12 /// Described in CVSS v4.0 Specification: Section 2.1.5
13 ///
14 /// > This metric captures the requirement for a human user, other than the
15 /// > attacker, to participate in the successful compromise of the vulnerable
16 /// > system. This metric determines whether the vulnerability can be exploited
17 /// > solely at the will of the attacker, or whether a separate user (or
18 /// > user-initiated process) must participate in some manner. The resulting
19 /// > score is greatest when no user interaction is required.
20 #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
21 pub enum UserInteraction {
22 /// Active (A)
23 ///
24 /// > Successful exploitation of this vulnerability requires a targeted user
25 /// > to perform specific, conscious interactions with the vulnerable system
26 /// > and the attacker’s payload, or the user’s interactions would actively
27 /// > subvert protection mechanisms which would lead to exploitation of the
28 /// > vulnerability. Examples include: importing a file into a vulnerable
29 /// > system in a specific manner placing files into a specific directory
30 /// > prior to executing code submitting a specific string into a web
31 /// > application (e.g. reflected or self XSS) dismiss or accept prompts or
32 /// > security warnings prior to taking an action (e.g. opening/editing a
33 /// > file, connecting a device).
34 Active,
35
36 /// Passive (P)
37 ///
38 /// > Successful exploitation of this vulnerability requires limited
39 /// > interaction by the targeted user with the vulnerable system and the
40 /// > attacker’s payload. These interactions would be considered involuntary
41 /// > and do not require that the user actively subvert protections built
42 /// > into the vulnerable system. Examples include: utilizing a website that
43 /// > has been modified to display malicious content when the page is
44 /// > rendered (most stored XSS or CSRF) running an application that calls a
45 /// > malicious binary that has been planted on the system using an
46 /// > application which generates traffic over an untrusted or compromised
47 /// > network (vulnerabilities requiring an on-path attacker)
48 Passive,
49
50 /// None (N)
51 ///
52 /// > The vulnerable system can be exploited without interaction from any
53 /// > human user, other than the attacker. Examples include: a remote
54 /// > attacker is able to send packets to a target system a locally
55 /// > authenticated attacker executes code to elevate privileges
56 None,
57 }
58
59 impl Default for UserInteraction {
60 fn default() -> Self {
61 Self::None
62 }
63 }
64
65 impl Metric for UserInteraction {
66 const TYPE: MetricType = MetricType::UI;
67
68 fn as_str(self) -> &'static str {
69 match self {
70 UserInteraction::None => "N",
71 UserInteraction::Passive => "P",
72 UserInteraction::Active => "A",
73 }
74 }
75 }
76
77 impl fmt::Display for UserInteraction {
78 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79 write!(f, "{}:{}", Self::name(), self.as_str())
80 }
81 }
82
83 impl FromStr for UserInteraction {
84 type Err = Error;
85
86 fn from_str(s: &str) -> Result<Self> {
87 match s {
88 "N" => Ok(UserInteraction::None),
89 "P" => Ok(UserInteraction::Passive),
90 "A" => Ok(UserInteraction::Active),
91 _ => Err(Error::InvalidMetricV4 {
92 metric_type: Self::TYPE,
93 value: s.to_owned(),
94 }),
95 }
96 }
97 }
98
99 #[cfg(feature = "std")]
100 pub(crate) mod merge {
101 use super::*;
102 use crate::{
103 Error,
104 v4::{
105 MetricType,
106 metric::{MetricLevel, environmental::ModifiedUserInteraction},
107 },
108 };
109 use alloc::borrow::ToOwned;
110 use core::str::FromStr;
111
112 /// Result of the merging of the base and modified metrics.
113 ///
114 /// Used in scoring.
115 #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
116 pub(crate) enum MergedUserInteraction {
117 Active,
118 Passive,
119 None,
120 }
121
122 impl Default for MergedUserInteraction {
123 fn default() -> Self {
124 Self::None
125 }
126 }
127
128 impl FromStr for MergedUserInteraction {
129 type Err = Error;
130
131 fn from_str(s: &str) -> Result<Self> {
132 match s {
133 "A" => Ok(MergedUserInteraction::Active),
134 "P" => Ok(MergedUserInteraction::Passive),
135 "N" => Ok(MergedUserInteraction::None),
136 _ => Err(Error::InvalidMetricV4 {
137 metric_type: MetricType::UI,
138 value: s.to_owned(),
139 }),
140 }
141 }
142 }
143
144 impl MetricLevel for MergedUserInteraction {
145 fn level(self) -> f64 {
146 // UI_levels = {"N": 0.0, "P": 0.1, "A": 0.2}
147 match self {
148 Self::Active => 0.2,
149 Self::Passive => 0.1,
150 Self::None => 0.0,
151 }
152 }
153 }
154
155 impl UserInteraction {
156 pub(crate) fn merge(self, value: Option<ModifiedUserInteraction>) -> MergedUserInteraction {
157 match value {
158 Some(ModifiedUserInteraction::NotDefined) | None => match self {
159 Self::Passive => MergedUserInteraction::Passive,
160 Self::Active => MergedUserInteraction::Active,
161 Self::None => MergedUserInteraction::None,
162 },
163 Some(ModifiedUserInteraction::Passive) => MergedUserInteraction::Passive,
164 Some(ModifiedUserInteraction::Active) => MergedUserInteraction::Active,
165 Some(ModifiedUserInteraction::None) => MergedUserInteraction::None,
166 }
167 }
168 }
169 }