1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use std::collections::HashMap;
use std::fmt;
pub const METRIC_DELIM: char = ':';
pub const VECTOR_DELIM: char = '/';
pub trait CVSSScore {
fn base_score(&self) -> Score;
fn temporal_score(&self) -> Score;
fn environmental_score(&self) -> Score;
fn impact_score(&self) -> Score;
fn expoitability_score(&self) -> Score;
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Debug, Default, PartialEq, PartialOrd)]
pub struct Score(f64);
impl Score {
pub fn value(self) -> f64 {
self.0
}
pub fn severity(self) -> Severity {
Severity::from_score(self)
}
}
impl From<f64> for Score {
fn from(score: f64) -> Score {
Score(score)
}
}
impl From<Score> for f64 {
fn from(score: Score) -> f64 {
score.value()
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub enum Severity {
None,
Low,
Medium,
High,
Critical,
}
impl Severity {
fn from(s: f64) -> Severity {
match s {
s if s < 0.1 => Severity::None,
s if s >= 0.1 && s < 4.0 => Severity::Low,
s if s >= 4.0 && s < 7.0 => Severity::Medium,
s if s >= 7.0 && s < 9.0 => Severity::High,
s if s >= 9.0 => Severity::Critical,
_ => Severity::Critical,
}
}
pub fn from_score(s: Score) -> Severity {
Severity::from(s.value())
}
}
impl fmt::Display for Severity {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
match self {
Severity::None => "None",
Severity::Low => "Low",
Severity::Medium => "Medium",
Severity::High => "High",
Severity::Critical => "Critical",
}
)
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ParseError {
MalformedVector,
Missing,
Duplicated,
IncorrectValue,
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "CVSS parse error")
}
}
pub trait AsStr {
fn as_str(&self) -> &str;
}
pub trait Optional {
fn is_undefined(&self) -> bool;
}
pub trait NumValue {
fn num_value(&self) -> f64 {
0.0
}
fn num_value_scoped(&self, _scope_change: bool) -> f64 {
0.0
}
}
pub fn append_metric<T: AsStr>(vector: &mut String, metric: &str, value: &T) {
if !vector.is_empty() {
vector.push(VECTOR_DELIM);
}
vector.push_str(metric);
vector.push(METRIC_DELIM);
vector.push_str(value.as_str());
}
pub fn append_metric_optional<T: AsStr + Optional>(vector: &mut String, metric: &str, value: &T) {
if !value.is_undefined() {
append_metric(vector, metric, value);
}
}
pub fn parse_metrics(cvss_str: &str) -> Result<HashMap<&str, &str>, ParseError> {
let mut parsed = HashMap::new();
for vector_part in cvss_str.split(VECTOR_DELIM) {
let mut metric_parts = vector_part.split(METRIC_DELIM);
let metric = metric_parts
.next()
.ok_or_else(|| ParseError::MalformedVector)?;
let value = metric_parts
.next()
.ok_or_else(|| ParseError::MalformedVector)?;
if metric_parts.next().is_some() {
return Err(ParseError::MalformedVector);
}
if parsed.contains_key(metric) {
return Err(ParseError::Duplicated);
}
parsed.insert(metric, value);
}
Ok(parsed)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_severity_from_score() {
assert_eq!(Severity::from_score(Score(0.0)), Severity::None);
assert_eq!(Severity::from_score(Score(3.9)), Severity::Low);
assert_eq!(Severity::from_score(Score(3.95)), Severity::Low);
assert_eq!(Severity::from_score(Score(4.0)), Severity::Medium);
assert_eq!(Severity::from_score(Score(4.01)), Severity::Medium);
assert_eq!(Severity::from_score(Score(7.89)), Severity::High);
assert_eq!(Severity::from_score(Score(9.12)), Severity::Critical);
assert_eq!(Severity::from_score(Score(102.3)), Severity::Critical);
}
#[test]
fn test_score_severity() {
assert_eq!(Score(4.5).severity(), Severity::Medium);
}
}