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
mod entries;
mod index;
mod query;
pub use self::query::Query;
use self::{entries::Entries, index::Index};
use crate::{
advisory::{self, Advisory},
collection::Collection,
error::Error,
fs,
vulnerability::Vulnerability,
Lockfile,
};
use std::path::Path;
#[cfg(feature = "git")]
use crate::repository::git;
pub type Iter<'a> = std::slice::Iter<'a, Advisory>;
#[derive(Debug)]
pub struct Database {
advisories: Entries,
rust_index: Index,
crate_index: Index,
#[cfg(feature = "git")]
latest_commit: Option<git::Commit>,
}
impl Database {
pub fn open(path: &Path) -> Result<Self, Error> {
let mut advisory_paths = vec![];
for collection in Collection::all() {
let collection_path = path.join(collection.as_str());
if let Ok(collection_entry) = fs::read_dir(&collection_path) {
for dir_entry in collection_entry {
let dir_entry = dir_entry?;
if !dir_entry.file_type()?.is_dir() {
continue;
}
for advisory_entry in fs::read_dir(dir_entry.path())? {
let advisory_path = advisory_entry?.path();
let file_name = advisory_path.file_name().and_then(|f| f.to_str());
if file_name.map_or(false, |f| f.starts_with('.')) {
continue;
}
advisory_paths.push(advisory_path);
}
}
}
}
let mut advisories = Entries::new();
let mut rust_index = Index::new();
let mut crate_index = Index::new();
for path in &advisory_paths {
if let Some(slot) = advisories.load_file(path)? {
let advisory = advisories.get(slot).unwrap();
match advisory.metadata.collection.unwrap() {
Collection::Crates => {
crate_index.insert(&advisory.metadata.package, slot);
}
Collection::Rust => {
rust_index.insert(&advisory.metadata.package, slot);
}
}
}
}
Ok(Self {
advisories,
crate_index,
rust_index,
#[cfg(feature = "git")]
latest_commit: None,
})
}
#[cfg(feature = "git")]
pub fn load_from_repo(repo: &git::Repository) -> Result<Self, Error> {
let mut db = Self::open(repo.path())?;
db.latest_commit = Some(repo.latest_commit()?);
Ok(db)
}
#[cfg(feature = "git")]
pub fn fetch() -> Result<Self, Error> {
git::Repository::fetch_default_repo().and_then(|repo| Self::load_from_repo(&repo))
}
pub fn get(&self, id: &advisory::Id) -> Option<&Advisory> {
self.advisories.find_by_id(id)
}
pub fn query(&self, query: &Query) -> Vec<&Advisory> {
if let Some(name) = &query.package_name {
if let Some(collection) = query.collection {
return match collection {
Collection::Crates => self.crate_index.get(name),
Collection::Rust => self.rust_index.get(name),
}
.map(|slots| {
slots
.map(|slot| self.advisories.get(*slot).unwrap())
.filter(|advisory| query.matches(advisory))
.collect()
})
.unwrap_or_else(Vec::new);
}
}
self.iter()
.filter(|advisory| query.matches(advisory))
.collect()
}
pub fn query_vulnerabilities(&self, lockfile: &Lockfile, query: &Query) -> Vec<Vulnerability> {
let mut vulns = vec![];
for package in &lockfile.packages {
let advisories = self.query(&query.clone().package(package));
vulns.extend(
advisories
.iter()
.map(|advisory| Vulnerability::new(advisory, package)),
);
}
vulns
}
pub fn vulnerabilities(&self, lockfile: &Lockfile) -> Vec<Vulnerability> {
self.query_vulnerabilities(lockfile, &Query::crate_scope())
}
pub fn iter(&self) -> Iter<'_> {
self.advisories.iter()
}
#[cfg(feature = "git")]
pub fn latest_commit(&self) -> Option<&git::Commit> {
self.latest_commit.as_ref()
}
}
impl IntoIterator for Database {
type Item = Advisory;
type IntoIter = std::vec::IntoIter<Advisory>;
fn into_iter(self) -> Self::IntoIter {
self.advisories.into_iter()
}
}