-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
mod.rs
170 lines (154 loc) · 5.71 KB
/
mod.rs
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
use crate::parser::structs::ScannedDependency;
use console::{style, Term};
use once_cell::sync::Lazy;
use std::{collections::HashMap, io, process::exit};
static CONS: Lazy<Term> = Lazy::new(Term::stdout);
pub struct Progress {
// this progress info only contains progress info about the found vulns.
pub count: usize,
current_displayed: usize,
}
impl Progress {
pub fn new() -> Progress {
Progress {
count: 0,
current_displayed: 0,
}
}
pub fn display(&mut self) {
if self.count > 1 {
let _ = CONS.clear_last_lines(1);
}
if self.count > self.current_displayed {
let _ = CONS.write_line(
format!(
"Found {} vulnerabilities so far",
style(self.count).bold().bright().red()
)
.as_str(),
);
self.current_displayed = self.count;
}
}
pub fn count_one(&mut self) {
self.count += 1;
}
pub fn end(&mut self) {
let _ = CONS.clear_last_lines(1);
}
}
pub fn display_queried(
collected: &Vec<ScannedDependency>,
imports_info: &mut HashMap<String, String>,
) {
// --- displaying query result starts here ---
for dep in collected {
let _ = CONS.write_line(
format!(
"|-| {} [{}]{:^5}",
style(dep.name.as_str()).bold().bright().yellow(),
style(dep.version.as_str()).bold().dim(),
style(" -> Found vulnerabilities!").bold().bright().red()
)
.as_str(),
);
} // displays all the deps where vuln has been found
// remove the the deps with vulns from import_info so what remains is the safe deps, which we can display as safe
for d in collected.iter() {
imports_info.remove(d.name.as_str());
}
for (k, v) in imports_info.iter() {
let _ = CONS.write_line(
format!(
"|-| {} [{}]{}",
style(k.as_str()).bold().bright().yellow(),
style(v.as_str()).bold().dim(),
style(" -> No vulnerabilities found.")
.bold()
.bright()
.green()
)
.as_str(),
);
} // display the safe deps
let _ = display_summary(&collected);
}
pub fn display_summary(collected: &Vec<ScannedDependency>) -> io::Result<()> {
// thing is, collected only has vulnerable dependencies, if theres a case where no vulns have been found, it will just skip this entire thing.
if !collected.is_empty() {
// --- summary starts here ---
CONS.write_line(&format!(
"{}",
style("SUMMARY").bold().yellow().underlined()
))?;
for v in collected {
for vuln in &v.vuln.vulns {
// DEPENDENCY
let name = format!(
"Dependency: {}",
style(v.name.clone()).bold().bright().red()
);
CONS.write_line(name.as_str())?;
CONS.flush()?;
// ID
let id = format!("ID: {}", style(vuln.id.as_str()).bold().bright().yellow());
CONS.write_line(id.as_str())?;
CONS.flush()?;
// DETAILS
let details = format!("Details: {}", style(vuln.details.as_str()).italic());
CONS.write_line(details.as_str())?;
CONS.flush()?;
// VERSIONS AFFECTED from ... to
let vers: Vec<Vec<String>> = vuln
.affected
.iter()
.map(|affected| {
vec![
{
if let Some(v) = &affected.versions {
v.first().unwrap().to_string()
} else {
"This version".to_string()
}
},
{
if let Some(v) = &affected.versions {
v.last().unwrap().to_string()
} else {
"Unknown".to_string()
}
},
]
})
.collect();
// let vers: Vec<Vec<String>> = vuln.affected.iter().map(|affected| {vec![affected.versions.first().unwrap().to_string(), affected.versions.last().unwrap().to_string()]}).collect();
let version = format!(
"Versions affected: {} to {}",
style(
vers.first()
.expect("No version found affected")
.first()
.unwrap()
)
.dim()
.underlined(),
style(
vers.last()
.expect("No version found affected")
.last()
.unwrap()
)
.dim()
.underlined()
);
println!();
CONS.write_line(version.as_str())?;
CONS.flush()?;
}
}
} else {
println!("Finished scanning all found dependencies.");
exit(0)
}
Ok(())
}