-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan.rs
More file actions
345 lines (302 loc) · 10.1 KB
/
scan.rs
File metadata and controls
345 lines (302 loc) · 10.1 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use clap::Args;
use socket_patch_core::api::client::get_api_client_from_env;
use socket_patch_core::api::types::BatchPackagePatches;
use socket_patch_core::crawlers::{CrawlerOptions, Ecosystem};
use std::collections::HashSet;
use std::path::PathBuf;
use crate::ecosystem_dispatch::crawl_all_ecosystems;
const DEFAULT_BATCH_SIZE: usize = 100;
#[derive(Args)]
pub struct ScanArgs {
/// Working directory
#[arg(long, default_value = ".")]
pub cwd: PathBuf,
/// Organization slug
#[arg(long)]
pub org: Option<String>,
/// Output results as JSON
#[arg(long, default_value_t = false)]
pub json: bool,
/// Scan globally installed npm packages
#[arg(short = 'g', long, default_value_t = false)]
pub global: bool,
/// Custom path to global node_modules
#[arg(long = "global-prefix")]
pub global_prefix: Option<PathBuf>,
/// Number of packages to query per API request
#[arg(long = "batch-size", default_value_t = DEFAULT_BATCH_SIZE)]
pub batch_size: usize,
/// Socket API URL (overrides SOCKET_API_URL env var)
#[arg(long = "api-url")]
pub api_url: Option<String>,
/// Socket API token (overrides SOCKET_API_TOKEN env var)
#[arg(long = "api-token")]
pub api_token: Option<String>,
}
pub async fn run(args: ScanArgs) -> i32 {
// Override env vars if CLI options provided
if let Some(ref url) = args.api_url {
std::env::set_var("SOCKET_API_URL", url);
}
if let Some(ref token) = args.api_token {
std::env::set_var("SOCKET_API_TOKEN", token);
}
let (api_client, _use_public_proxy) = get_api_client_from_env(args.org.as_deref()).await;
// org slug is already stored in the client
let effective_org_slug: Option<&str> = None;
let crawler_options = CrawlerOptions {
cwd: args.cwd.clone(),
global: args.global,
global_prefix: args.global_prefix.clone(),
batch_size: args.batch_size,
};
let scan_target = if args.global || args.global_prefix.is_some() {
"global packages"
} else {
"packages"
};
if !args.json {
eprint!("Scanning {scan_target}...");
}
// Crawl packages
let (all_crawled, eco_counts) = crawl_all_ecosystems(&crawler_options).await;
let all_purls: Vec<String> = all_crawled.iter().map(|p| p.purl.clone()).collect();
let package_count = all_purls.len();
if package_count == 0 {
if !args.json {
eprintln!();
}
if args.json {
println!(
"{}",
serde_json::to_string_pretty(&serde_json::json!({
"scannedPackages": 0,
"packagesWithPatches": 0,
"totalPatches": 0,
"freePatches": 0,
"paidPatches": 0,
"canAccessPaidPatches": false,
"packages": [],
}))
.unwrap()
);
} else if args.global || args.global_prefix.is_some() {
println!("No global packages found.");
} else {
#[cfg(feature = "cargo")]
let install_cmds = "npm/yarn/pnpm/pip/cargo";
#[cfg(not(feature = "cargo"))]
let install_cmds = "npm/yarn/pnpm/pip";
println!("No packages found. Run {install_cmds} install first.");
}
return 0;
}
// Build ecosystem summary
let mut eco_parts = Vec::new();
for eco in Ecosystem::all() {
let count = eco_counts.get(eco).copied().unwrap_or(0);
if count > 0 {
eco_parts.push(format!("{count} {}", eco.display_name()));
}
}
let eco_summary = if eco_parts.is_empty() {
String::new()
} else {
format!(" ({})", eco_parts.join(", "))
};
if !args.json {
eprintln!("\rFound {package_count} packages{eco_summary}");
}
// Query API in batches
let mut all_packages_with_patches: Vec<BatchPackagePatches> = Vec::new();
let mut can_access_paid_patches = false;
let total_batches = all_purls.len().div_ceil(args.batch_size);
if !args.json {
eprint!("Querying API for patches... (batch 1/{total_batches})");
}
for (batch_idx, chunk) in all_purls.chunks(args.batch_size).enumerate() {
if !args.json {
eprint!(
"\rQuerying API for patches... (batch {}/{})",
batch_idx + 1,
total_batches
);
}
let purls: Vec<String> = chunk.to_vec();
match api_client
.search_patches_batch(effective_org_slug, &purls)
.await
{
Ok(response) => {
if response.can_access_paid_patches {
can_access_paid_patches = true;
}
for pkg in response.packages {
if !pkg.patches.is_empty() {
all_packages_with_patches.push(pkg);
}
}
}
Err(e) => {
if !args.json {
eprintln!("\nError querying batch {}: {e}", batch_idx + 1);
}
}
}
}
let total_patches_found: usize = all_packages_with_patches
.iter()
.map(|p| p.patches.len())
.sum();
if !args.json {
if total_patches_found > 0 {
eprintln!(
"\rFound {total_patches_found} patches for {} packages",
all_packages_with_patches.len()
);
} else {
eprintln!("\rAPI query complete");
}
}
// Calculate patch counts
let mut free_patches = 0usize;
let mut paid_patches = 0usize;
for pkg in &all_packages_with_patches {
for patch in &pkg.patches {
if patch.tier == "free" {
free_patches += 1;
} else {
paid_patches += 1;
}
}
}
let total_patches = free_patches + paid_patches;
if args.json {
let result = serde_json::json!({
"scannedPackages": package_count,
"packagesWithPatches": all_packages_with_patches.len(),
"totalPatches": total_patches,
"freePatches": free_patches,
"paidPatches": paid_patches,
"canAccessPaidPatches": can_access_paid_patches,
"packages": all_packages_with_patches,
});
println!("{}", serde_json::to_string_pretty(&result).unwrap());
return 0;
}
if all_packages_with_patches.is_empty() {
println!("\nNo patches available for installed packages.");
return 0;
}
// Print table
println!("\n{}", "=".repeat(100));
println!(
"{} {} {} VULNERABILITIES",
"PACKAGE".to_string() + &" ".repeat(33),
"PATCHES".to_string() + " ",
"SEVERITY".to_string() + &" ".repeat(8),
);
println!("{}", "=".repeat(100));
for pkg in &all_packages_with_patches {
let max_purl_len = 40;
let display_purl = if pkg.purl.len() > max_purl_len {
format!("{}...", &pkg.purl[..max_purl_len - 3])
} else {
pkg.purl.clone()
};
let pkg_free = pkg.patches.iter().filter(|p| p.tier == "free").count();
let pkg_paid = pkg.patches.iter().filter(|p| p.tier == "paid").count();
let count_str = if pkg_paid > 0 {
if can_access_paid_patches {
format!("{}+{}", pkg_free, pkg_paid)
} else {
format!("{}\x1b[33m+{}\x1b[0m", pkg_free, pkg_paid)
}
} else {
format!("{}", pkg_free)
};
// Get highest severity
let severity = pkg
.patches
.iter()
.filter_map(|p| p.severity.as_deref())
.min_by_key(|s| severity_order(s))
.unwrap_or("unknown");
// Collect vuln IDs
let mut all_cves = HashSet::new();
let mut all_ghsas = HashSet::new();
for patch in &pkg.patches {
for cve in &patch.cve_ids {
all_cves.insert(cve.clone());
}
for ghsa in &patch.ghsa_ids {
all_ghsas.insert(ghsa.clone());
}
}
let vuln_ids: Vec<_> = all_cves.into_iter().chain(all_ghsas).collect();
let vuln_str = if vuln_ids.len() > 2 {
format!(
"{} (+{})",
vuln_ids[..2].join(", "),
vuln_ids.len() - 2
)
} else if vuln_ids.is_empty() {
"-".to_string()
} else {
vuln_ids.join(", ")
};
println!(
"{:<40} {:>8} {:<16} {}",
display_purl,
count_str,
format_severity(severity),
vuln_str,
);
}
println!("{}", "=".repeat(100));
// Summary
if can_access_paid_patches {
println!(
"\nSummary: {} package(s) with {} available patch(es)",
all_packages_with_patches.len(),
total_patches,
);
} else {
println!(
"\nSummary: {} package(s) with {} free patch(es)",
all_packages_with_patches.len(),
free_patches,
);
if paid_patches > 0 {
println!(
"\x1b[33m + {} additional patch(es) available with paid subscription\x1b[0m",
paid_patches,
);
println!(
"\nUpgrade to Socket's paid plan to access all patches: https://socket.dev/pricing"
);
}
}
println!("\nTo apply a patch, run:");
println!(" socket-patch get <package-name-or-purl>");
println!(" socket-patch get <CVE-ID>");
0
}
fn severity_order(s: &str) -> u8 {
match s.to_lowercase().as_str() {
"critical" => 0,
"high" => 1,
"medium" => 2,
"low" => 3,
_ => 4,
}
}
fn format_severity(s: &str) -> String {
match s.to_lowercase().as_str() {
"critical" => "\x1b[31mcritical\x1b[0m".to_string(),
"high" => "\x1b[91mhigh\x1b[0m".to_string(),
"medium" => "\x1b[33mmedium\x1b[0m".to_string(),
"low" => "\x1b[36mlow\x1b[0m".to_string(),
other => other.to_string(),
}
}