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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
use super::date::{YEAR_MAX, YEAR_MIN};
use crate::error::{Error, ErrorKind};
use serde::{de::Error as DeError, Deserialize, Deserializer, Serialize, Serializer};
use std::{
fmt::{self, Display},
str::FromStr,
};
pub const PLACEHOLDER: &str = "RUSTSEC-0000-0000";
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct Id {
kind: Kind,
year: Option<u32>,
string: String,
}
impl Id {
pub fn as_str(&self) -> &str {
self.string.as_ref()
}
pub fn kind(&self) -> Kind {
self.kind
}
pub fn is_placeholder(&self) -> bool {
self.string == PLACEHOLDER
}
pub fn is_rustsec(&self) -> bool {
self.kind == Kind::RUSTSEC
}
pub fn is_cve(&self) -> bool {
self.kind == Kind::CVE
}
pub fn is_ghsa(&self) -> bool {
self.kind == Kind::GHSA
}
pub fn is_other(&self) -> bool {
self.kind == Kind::Other
}
pub fn year(&self) -> Option<u32> {
self.year
}
pub fn numerical_part(&self) -> Option<u32> {
if self.is_placeholder() {
return None;
}
self.string
.split('-')
.last()
.and_then(|s| str::parse(s).ok())
}
pub fn url(&self) -> Option<String> {
match self.kind {
Kind::RUSTSEC => {
if self.is_placeholder() {
None
} else {
Some(format!("https://rustsec.org/advisories/{}", &self.string))
}
}
Kind::CVE => Some(format!(
"https://cve.mitre.org/cgi-bin/cvename.cgi?name={}",
&self.string
)),
Kind::TALOS => Some(format!(
"https://www.talosintelligence.com/reports/{}",
&self.string
)),
_ => None,
}
}
}
impl AsRef<str> for Id {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Default for Id {
fn default() -> Id {
Id {
kind: Kind::RUSTSEC,
year: None,
string: PLACEHOLDER.into(),
}
}
}
impl Display for Id {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.string.fmt(f)
}
}
impl FromStr for Id {
type Err = Error;
fn from_str(advisory_id: &str) -> Result<Self, Error> {
if advisory_id == PLACEHOLDER {
return Ok(Id::default());
}
let kind = Kind::detect(advisory_id);
let year = match kind {
Kind::RUSTSEC | Kind::CVE | Kind::TALOS => Some(parse_year(advisory_id)?),
_ => None,
};
Ok(Self {
kind,
year,
string: advisory_id.into(),
})
}
}
impl Into<String> for Id {
fn into(self) -> String {
self.string
}
}
impl Serialize for Id {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&self.string)
}
}
impl<'de> Deserialize<'de> for Id {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Self::from_str(&String::deserialize(deserializer)?)
.map_err(|e| D::Error::custom(format!("{}", e)))
}
}
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Kind {
RUSTSEC,
CVE,
GHSA,
TALOS,
Other,
}
impl Kind {
pub fn detect(string: &str) -> Self {
if string.starts_with("RUSTSEC-") {
Kind::RUSTSEC
} else if string.starts_with("CVE-") {
Kind::CVE
} else if string.starts_with("TALOS-") {
Kind::TALOS
} else if string.starts_with("GHSA-") {
Kind::GHSA
} else {
Kind::Other
}
}
}
fn parse_year(advisory_id: &str) -> Result<u32, Error> {
let mut parts = advisory_id.split('-');
parts.next().unwrap();
let year = match parts.next().unwrap().parse::<u32>() {
Ok(n) => match n {
YEAR_MIN..=YEAR_MAX => n,
_ => fail!(
ErrorKind::Parse,
"out-of-range year in advisory ID: {}",
advisory_id
),
},
_ => fail!(
ErrorKind::Parse,
"malformed year in advisory ID: {}",
advisory_id
),
};
if let Some(num) = parts.next() {
if num.parse::<u32>().is_err() {
fail!(ErrorKind::Parse, "malformed advisory ID: {}", advisory_id);
}
} else {
fail!(ErrorKind::Parse, "incomplete advisory ID: {}", advisory_id);
}
if parts.next().is_some() {
fail!(ErrorKind::Parse, "malformed advisory ID: {}", advisory_id);
}
Ok(year)
}
#[cfg(test)]
mod tests {
use super::{Id, Kind, PLACEHOLDER};
const EXAMPLE_RUSTSEC_ID: &str = "RUSTSEC-2018-0001";
const EXAMPLE_CVE_ID: &str = "CVE-2017-1000168";
const EXAMPLE_GHSA_ID: &str = "GHSA-4mmc-49vf-jmcp";
const EXAMPLE_TALOS_ID: &str = "TALOS-2017-0468";
const EXAMPLE_UNKNOWN_ID: &str = "Anonymous-42";
#[test]
fn rustsec_id_test() {
let rustsec_id = EXAMPLE_RUSTSEC_ID.parse::<Id>().unwrap();
assert!(rustsec_id.is_rustsec());
assert_eq!(rustsec_id.year().unwrap(), 2018);
assert_eq!(
rustsec_id.url().unwrap(),
"https://rustsec.org/advisories/RUSTSEC-2018-0001"
);
assert_eq!(rustsec_id.numerical_part().unwrap(), 0001);
}
#[test]
fn rustsec_0000_0000_test() {
let rustsec_id = PLACEHOLDER.parse::<Id>().unwrap();
assert!(rustsec_id.is_rustsec());
assert!(rustsec_id.year().is_none());
assert!(rustsec_id.url().is_none());
assert!(rustsec_id.numerical_part().is_none());
}
#[test]
fn cve_id_test() {
let cve_id = EXAMPLE_CVE_ID.parse::<Id>().unwrap();
assert!(cve_id.is_cve());
assert_eq!(cve_id.year().unwrap(), 2017);
assert_eq!(
cve_id.url().unwrap(),
"https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-1000168"
);
assert_eq!(cve_id.numerical_part().unwrap(), 1000168);
}
#[test]
fn ghsa_id_test() {
let ghsa_id = EXAMPLE_GHSA_ID.parse::<Id>().unwrap();
assert!(ghsa_id.is_ghsa());
assert!(ghsa_id.year().is_none());
assert!(ghsa_id.url().is_none());
assert!(ghsa_id.numerical_part().is_none());
}
#[test]
fn talos_id_test() {
let talos_id = EXAMPLE_TALOS_ID.parse::<Id>().unwrap();
assert_eq!(talos_id.kind(), Kind::TALOS);
assert_eq!(talos_id.year().unwrap(), 2017);
assert_eq!(
talos_id.url().unwrap(),
"https://www.talosintelligence.com/reports/TALOS-2017-0468"
);
assert_eq!(talos_id.numerical_part().unwrap(), 0468);
}
#[test]
fn other_id_test() {
let other_id = EXAMPLE_UNKNOWN_ID.parse::<Id>().unwrap();
assert!(other_id.is_other());
assert!(other_id.year().is_none());
assert!(other_id.url().is_none());
assert_eq!(other_id.numerical_part().unwrap(), 42);
}
}