Debian Codegraph @

src/v3/base/pr.rs

raw symbols SCIP
1 //! Privileges Required (PR)
2
3 use crate::{Metric, MetricType, error::Error};
4 use alloc::borrow::ToOwned;
5 use core::{fmt, str::FromStr};
6
7 /// Privileges Required (PR) - CVSS v3.1 Base Metric Group
8 ///
9 /// Described in CVSS v3.1 Specification: Section 2.1.3:
10 /// <https://www.first.org/cvss/specification-document#t6>
11 ///
12 /// > This metric describes the level of privileges an attacker must possess
13 /// > *before* successfully exploiting the vulnerability. The Base Score is
14 /// > greatest if no privileges are required.
15 #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
16 pub enum PrivilegesRequired {
17 /// High (H)
18 ///
19 /// > The attacker requires privileges that provide significant
20 /// > (e.g., administrative) control over the vulnerable component allowing
21 /// > access to component-wide settings and files.
22 High,
23
24 /// Low (L)
25 ///
26 /// > The attacker requires privileges that provide basic user capabilities
27 /// > that could normally affect only settings and files owned by a user.
28 /// > Alternatively, an attacker with Low privileges has the ability to
29 /// > access only non-sensitive resources.
30 Low,
31
32 /// None (N)
33 ///
34 /// > The attacker is unauthorized prior to attack, and therefore does not
35 /// > require any access to settings or files of the vulnerable system
36 /// > to carry out an attack.
37 None,
38 }
39
40 impl PrivilegesRequired {
41 /// Score when accounting for scope change
42 pub fn scoped_score(self, scope_change: bool) -> f64 {
43 match self {
44 PrivilegesRequired::High => {
45 if scope_change {
46 0.50
47 } else {
48 0.27
49 }
50 }
51 PrivilegesRequired::Low => {
52 if scope_change {
53 0.68
54 } else {
55 0.62
56 }
57 }
58 PrivilegesRequired::None => 0.85,
59 }
60 }
61 }
62
63 impl Metric for PrivilegesRequired {
64 const TYPE: MetricType = MetricType::PR;
65
66 fn score(self) -> f64 {
67 self.scoped_score(false)
68 }
69
70 fn as_str(self) -> &'static str {
71 match self {
72 PrivilegesRequired::High => "H",
73 PrivilegesRequired::Low => "L",
74 PrivilegesRequired::None => "N",
75 }
76 }
77 }
78
79 impl fmt::Display for PrivilegesRequired {
80 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81 write!(f, "{}:{}", Self::name(), self.as_str())
82 }
83 }
84
85 impl FromStr for PrivilegesRequired {
86 type Err = Error;
87
88 fn from_str(s: &str) -> Result<Self, Error> {
89 match s {
90 "H" => Ok(PrivilegesRequired::High),
91 "L" => Ok(PrivilegesRequired::Low),
92 "N" => Ok(PrivilegesRequired::None),
93 _ => Err(Error::InvalidMetric {
94 metric_type: Self::TYPE,
95 value: s.to_owned(),
96 }),
97 }
98 }
99 }