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
320
321
322
323
324
325
326
327
328
329
330
331
//! Advisory linter: ensure advisories are well-formed according to the
//! currently valid set of fields.
//!
//! This is run in CI at the time advisories are submitted.
use super::{Advisory, Category, Informational};
use chrono::Datelike;
use std::{fmt, fs, path::Path};
/// Lint information about a particular advisory
#[derive(Debug)]
pub struct Linter {
/// Advisory being linted
advisory: Advisory,
/// Errors detected during linting
errors: Vec<Error>,
}
impl Linter {
/// Lint the advisory TOML file located at the given path
pub fn lint_file<P: AsRef<Path>>(path: P) -> Result<Self, crate::Error> {
let path = path.as_ref();
let toml = fs::read_to_string(path).map_err(|e| {
format_err!(
crate::ErrorKind::Io,
"couldn't open {}: {}",
path.display(),
e
)
})?;
Self::lint_string(&toml)
}
/// Lint the given advisory TOML string
pub fn lint_string(s: &str) -> Result<Self, crate::Error> {
// Ensure the advisory parses according to the normal parser first
let advisory = s.parse::<Advisory>()?;
// Get a raw TOML value representing the document for linting
let toml_value = s.parse::<toml::Value>()?;
let mut linter = Self {
advisory,
errors: vec![],
};
linter.lint_advisory(&toml_value);
Ok(linter)
}
/// Get the parsed advisory
pub fn advisory(&self) -> &Advisory {
&self.advisory
}
/// Get the errors that occurred during linting
pub fn errors(&self) -> &[Error] {
self.errors.as_slice()
}
/// Lint the provided TOML value as the toplevel table of an advisory
fn lint_advisory(&mut self, advisory: &toml::Value) {
if let Some(table) = advisory.as_table() {
for (key, value) in table {
match key.as_str() {
"advisory" => self.lint_metadata(value),
"versions" => self.lint_versions(value),
"affected" => self.lint_affected(value),
_ => self.errors.push(Error {
kind: ErrorKind::key(key),
section: None,
message: None,
}),
}
}
} else {
self.errors.push(Error {
kind: ErrorKind::Malformed,
section: None,
message: Some("expected table"),
});
}
}
/// Lint the `[advisory]` metadata section
fn lint_metadata(&mut self, metadata: &toml::Value) {
let mut year = None;
if let Some(table) = metadata.as_table() {
for (key, value) in table {
match key.as_str() {
"id" => {
if self.advisory.metadata.id.is_other() {
self.errors.push(Error {
kind: ErrorKind::value("id", value.to_string()),
section: Some("advisory"),
message: Some("unknown advisory ID type"),
});
} else if let Some(y1) = self.advisory.metadata.id.year() {
if let Some(y2) = year {
if y1 != y2 {
self.errors.push(Error {
kind: ErrorKind::value("id", value.to_string()),
section: Some("advisory"),
message: Some("year in advisory ID does not match date"),
});
}
} else {
year = Some(y1);
}
}
}
"categories" => {
for category in &self.advisory.metadata.categories {
if let Category::Other(other) = category {
self.errors.push(Error {
kind: ErrorKind::value("category", other.to_string()),
section: Some("advisory"),
message: Some("unknown category"),
});
}
}
}
"collection" => self.errors.push(Error {
kind: ErrorKind::Malformed,
section: Some("advisory"),
message: Some("collection shouldn't be explicit; inferred by location"),
}),
"informational" => {
if let Some(Informational::Other(other)) =
&self.advisory.metadata.informational
{
self.errors.push(Error {
kind: ErrorKind::value("informational", other.to_string()),
section: Some("advisory"),
message: Some("unknown informational advisory type"),
});
}
}
"url" => {
if let Some(url) = value.as_str() {
if !url.starts_with("https://") {
self.errors.push(Error {
kind: ErrorKind::value("url", value.to_string()),
section: Some("advisory"),
message: Some("URL must start with https://"),
});
}
}
}
"date" => {
let y1 =
self.advisory.metadata.date.to_chrono_date().unwrap().year() as u32;
if let Some(y2) = year {
if y1 != y2 {
self.errors.push(Error {
kind: ErrorKind::value("date", value.to_string()),
section: Some("advisory"),
message: Some("year in advisory ID does not match date"),
});
}
} else {
year = Some(y1);
}
}
"patched_versions" | "unaffected_versions" => (), // TODO(tarcieri): deprecate
"aliases" | "cvss" | "keywords" | "obsolete" | "package" | "references"
| "title" | "description" => (),
_ => self.errors.push(Error {
kind: ErrorKind::key(key),
section: Some("advisory"),
message: None,
}),
}
}
} else {
self.errors.push(Error {
kind: ErrorKind::Malformed,
section: Some("advisory"),
message: Some("expected table"),
});
}
}
/// Lint the `[versions]` section of an advisory
fn lint_versions(&mut self, versions: &toml::Value) {
if let Some(table) = versions.as_table() {
for (key, _) in table {
match key.as_str() {
"patched" | "unaffected" => (),
_ => self.errors.push(Error {
kind: ErrorKind::key(key),
section: Some("versions"),
message: None,
}),
}
}
}
}
/// Lint the `[affected]` section of an advisory
fn lint_affected(&mut self, affected: &toml::Value) {
if let Some(table) = affected.as_table() {
for (key, _) in table {
match key.as_str() {
"functions" => {
for function in self.advisory.affected.as_ref().unwrap().functions.keys() {
if function.segments()[0].as_str()
!= self.advisory.metadata.package.as_str()
{
self.errors.push(Error {
kind: ErrorKind::value("functions", function.to_string()),
section: Some("affected"),
message: Some("function path must start with crate name"),
});
}
}
}
"arch" | "os" => (),
_ => self.errors.push(Error {
kind: ErrorKind::key(key),
section: Some("affected"),
message: None,
}),
}
}
}
}
}
/// Lint errors
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Error {
/// Kind of error
kind: ErrorKind,
/// Section of the advisory where the error occurred
section: Option<&'static str>,
/// Message about why it's invalid
message: Option<&'static str>,
}
impl Error {
/// Get the kind of error
pub fn kind(&self) -> &ErrorKind {
&self.kind
}
/// Get the section of the advisory where the error occurred
pub fn section(&self) -> Option<&str> {
self.section.as_ref().map(AsRef::as_ref)
}
/// Get an optional message about the lint failure
pub fn message(&self) -> Option<&str> {
self.message.as_ref().map(AsRef::as_ref)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", &self.kind)?;
if let Some(section) = &self.section {
write!(f, " in [{}]", section)?;
} else {
write!(f, " in toplevel")?;
}
if let Some(msg) = &self.message {
write!(f, ": {}", msg)?
}
Ok(())
}
}
/// Lint errors
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ErrorKind {
/// Advisory is structurally malformed
Malformed,
/// Unknown key
InvalidKey {
/// Name of the key
name: String,
},
/// Unknown value
InvalidValue {
/// Name of the key
name: String,
/// Invalid value
value: String,
},
}
impl ErrorKind {
/// Invalid key
pub fn key(name: &str) -> Self {
ErrorKind::InvalidKey {
name: name.to_owned(),
}
}
/// Invalid value
pub fn value(name: &str, value: impl Into<String>) -> Self {
ErrorKind::InvalidValue {
name: name.to_owned(),
value: value.into(),
}
}
}
impl fmt::Display for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ErrorKind::Malformed => write!(f, "malformed content"),
ErrorKind::InvalidKey { name } => write!(f, "invalid key `{}`", name),
ErrorKind::InvalidValue { name, value } => {
write!(f, "invalid value `{}` for key `{}`", value, name)
}
}
}
}