surjection_core/scan/external/
mod.rs1pub mod download;
17mod process;
18pub mod skillspector;
19pub mod snyk_leftover;
20mod text;
21
22use serde::{Deserialize, Serialize};
23
24pub use process::RUNS_DIR;
25
26use super::{Finding, Severity, Verdict};
27use crate::error::{CoreError, ErrorCode};
28
29pub const TOOLS_DIR: &str = "tools";
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
36pub enum Scanner {
37 #[serde(rename = "skillspector")]
38 SkillSpector,
39}
40
41impl Scanner {
42 pub const fn as_str(self) -> &'static str {
44 match self {
45 Scanner::SkillSpector => "skillspector",
46 }
47 }
48
49 pub const fn name(self) -> &'static str {
51 match self {
52 Scanner::SkillSpector => "SkillSpector",
53 }
54 }
55}
56
57#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
59#[serde(rename_all = "snake_case")]
60pub enum ExternalStatus {
61 Scanned,
64 NotScanned,
66 NotUnderstood,
69}
70
71impl ExternalStatus {
72 pub const fn as_str(self) -> &'static str {
73 match self {
74 ExternalStatus::Scanned => "scanned",
75 ExternalStatus::NotScanned => "not_scanned",
76 ExternalStatus::NotUnderstood => "not_understood",
77 }
78 }
79}
80
81#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
83#[serde(rename_all = "snake_case")]
84pub enum NotScanned {
85 NotAvailable,
87 NotPrepared,
90 Timeout,
92 Canceled,
94 Failed,
97}
98
99impl NotScanned {
100 pub const fn as_str(self) -> &'static str {
101 match self {
102 NotScanned::NotAvailable => "not_available",
103 NotScanned::NotPrepared => "not_prepared",
104 NotScanned::Timeout => "timeout",
105 NotScanned::Canceled => "canceled",
106 NotScanned::Failed => "failed",
107 }
108 }
109
110 pub const fn text(self) -> &'static str {
112 match self {
113 NotScanned::NotAvailable => "SkillSpector was not found; see `surject scanner status`",
114 NotScanned::NotPrepared => {
115 "SkillSpector is not downloaded yet; run `surject scanner prepare`"
116 }
117 NotScanned::Timeout => "timed out",
118 NotScanned::Canceled => "canceled",
119 NotScanned::Failed => "the scanner failed",
120 }
121 }
122}
123
124#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
126pub struct ExternalReport {
127 pub scanner: Scanner,
128 pub version: String,
130 pub status: ExternalStatus,
131 pub reason: Option<NotScanned>,
133 pub verdict: Verdict,
137 pub findings: Vec<Finding>,
139 #[serde(default)]
142 pub incomplete: bool,
143 pub content_hash: Option<String>,
145 pub detail: Option<String>,
148}
149
150impl ExternalReport {
151 pub fn not_scanned(
152 scanner: Scanner,
153 version: &str,
154 reason: NotScanned,
155 detail: Option<String>,
156 ) -> Self {
157 Self {
158 scanner,
159 version: version.to_owned(),
160 status: ExternalStatus::NotScanned,
161 reason: Some(reason),
162 verdict: Verdict::Clean,
163 findings: Vec::new(),
164 incomplete: false,
165 content_hash: None,
166 detail,
167 }
168 }
169
170 pub fn not_understood(scanner: Scanner, version: &str, why: &str) -> Self {
171 Self {
172 scanner,
173 version: version.to_owned(),
174 status: ExternalStatus::NotUnderstood,
175 reason: None,
176 verdict: Verdict::Clean,
177 findings: Vec::new(),
178 incomplete: false,
179 content_hash: None,
180 detail: Some(format!(
181 "scanner output not understood (version {version}): {why}"
182 )),
183 }
184 }
185
186 pub fn summary(&self) -> String {
191 match (self.status, self.reason) {
192 (ExternalStatus::NotScanned, Some(reason)) => {
193 format!("not scanned ({})", reason.text())
194 }
195 (ExternalStatus::NotUnderstood, _) => {
196 format!("scanner output not understood (version {})", self.version)
197 }
198 _ => {
199 let n = self.findings.len();
200 let plural = if n == 1 { "" } else { "s" };
201 match (self.incomplete, self.verdict) {
202 (true, Verdict::Clean) => format!("not fully scanned ({n} finding{plural})"),
203 (true, verdict) => {
204 format!(
205 "{} ({n} finding{plural}), not fully scanned",
206 verdict.as_str()
207 )
208 }
209 (false, verdict) => format!("{} ({n} finding{plural})", verdict.as_str()),
210 }
211 }
212 }
213 }
214
215 pub fn dangerous(&self) -> impl Iterator<Item = &Finding> {
217 self.findings
218 .iter()
219 .filter(|finding| finding.severity == Severity::Dangerous)
220 }
221}
222
223pub fn gate(report: &ExternalReport, allow_dangerous: bool) -> Result<(), CoreError> {
227 if allow_dangerous || report.verdict != Verdict::Dangerous {
228 return Ok(());
229 }
230 let labels: Vec<String> = report.dangerous().map(Finding::label).collect();
231 let mut shown = labels
232 .iter()
233 .take(3)
234 .cloned()
235 .collect::<Vec<_>>()
236 .join(", ");
237 if labels.len() > 3 {
238 shown.push_str(&format!(" and {} more", labels.len() - 3));
239 }
240 let plural = if labels.len() == 1 { "" } else { "s" };
241 Err(CoreError::new(
242 ErrorCode::ScanBlocked,
243 format!(
244 "{} found {} dangerous finding{plural}: {shown}; review them, and install anyway only if you trust this content",
245 report.scanner.name(),
246 labels.len()
247 ),
248 ))
249}
250
251#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
253pub enum Choice {
254 #[default]
256 Default,
257 On,
258 Off,
259}
260
261impl Choice {
262 pub const fn wants(self, enabled: bool) -> bool {
265 match self {
266 Choice::On => true,
267 Choice::Off => false,
268 Choice::Default => enabled,
269 }
270 }
271}