mod cache;
mod engine;
pub mod external;
pub mod rules;
#[cfg(test)]
mod tests;
use std::borrow::Cow;
use std::path::Path;
use serde::{Deserialize, Serialize};
use crate::error::{CoreError, ErrorCode};
use crate::fs::Fs;
use crate::paths::SurjectionHome;
use crate::skills::{SkillFiles, content_hash};
pub use cache::CACHE_DIR;
use cache::Cache;
pub use rules::{Rule, Rules, Target};
pub const SCANNER: &str = "builtin";
pub const RULES_OVERRIDE_FILE: &str = "scan-rules.toml";
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Severity {
Info,
Warning,
Dangerous,
}
impl Severity {
pub const fn as_str(self) -> &'static str {
match self {
Severity::Info => "info",
Severity::Warning => "warning",
Severity::Dangerous => "dangerous",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Verdict {
Clean,
Warning,
Dangerous,
}
impl Verdict {
pub const fn as_str(self) -> &'static str {
match self {
Verdict::Clean => "clean",
Verdict::Warning => "warning",
Verdict::Dangerous => "dangerous",
}
}
pub fn of(findings: &[Finding]) -> Self {
match findings.iter().map(|finding| finding.severity).max() {
Some(Severity::Dangerous) => Verdict::Dangerous,
Some(Severity::Warning) => Verdict::Warning,
Some(Severity::Info) | None => Verdict::Clean,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Finding {
pub scanner: String,
pub rule: String,
pub severity: Severity,
pub file: Option<String>,
pub line: Option<u32>,
pub message: String,
pub excerpt: Option<String>,
}
impl Finding {
pub fn label(&self) -> String {
match (&self.file, self.line) {
(Some(file), Some(line)) => format!("{} ({file}:{line})", self.rule),
(Some(file), None) => format!("{} ({file})", self.rule),
_ => self.rule.clone(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ScanReport {
pub verdict: Verdict,
pub findings: Vec<Finding>,
pub rules_version: u32,
pub content_hash: Option<String>,
#[serde(default, skip_serializing)]
pub cached: bool,
#[serde(default, skip_serializing)]
pub custom_rules: bool,
#[serde(default, skip_serializing)]
pub warnings: Vec<String>,
}
impl ScanReport {
fn new(rules_version: u32, content_hash: Option<String>, mut findings: Vec<Finding>) -> Self {
findings.sort_by(|a, b| (&a.file, a.line, &a.rule).cmp(&(&b.file, b.line, &b.rule)));
Self {
verdict: Verdict::of(&findings),
findings,
rules_version,
content_hash,
cached: false,
custom_rules: false,
warnings: Vec::new(),
}
}
pub fn merge(&mut self, other: ScanReport) {
let content_hash = match (self.content_hash.take(), other.content_hash) {
(Some(a), Some(b)) if a == b => Some(a),
(Some(hash), None) | (None, Some(hash)) => Some(hash),
_ => None,
};
let mut findings = std::mem::take(&mut self.findings);
findings.extend(other.findings);
let cached = self.cached && other.cached;
let custom_rules = self.custom_rules || other.custom_rules;
let mut warnings = std::mem::take(&mut self.warnings);
for warning in other.warnings {
if !warnings.contains(&warning) {
warnings.push(warning);
}
}
*self = ScanReport::new(
self.rules_version.max(other.rules_version),
content_hash,
findings,
);
self.cached = cached;
self.custom_rules = custom_rules;
self.warnings = warnings;
}
pub fn dangerous(&self) -> impl Iterator<Item = &Finding> {
self.findings
.iter()
.filter(|finding| finding.severity == Severity::Dangerous)
}
}
pub fn gate(report: &ScanReport, allow_dangerous: bool) -> Result<(), CoreError> {
if allow_dangerous || report.verdict != Verdict::Dangerous {
return Ok(());
}
let labels: Vec<String> = report.dangerous().map(Finding::label).collect();
let mut shown = labels
.iter()
.take(3)
.cloned()
.collect::<Vec<_>>()
.join(", ");
if labels.len() > 3 {
shown.push_str(&format!(" and {} more", labels.len() - 3));
}
let plural = if labels.len() == 1 { "" } else { "s" };
Err(CoreError::new(
ErrorCode::ScanBlocked,
format!(
"the built-in scan found {} dangerous finding{plural}: {shown}; review them, and install anyway only if you trust this content",
labels.len()
),
))
}
pub struct Scanner<'a> {
rules: Cow<'static, Rules>,
cache: Option<Cache<'a>>,
warnings: Vec<String>,
}
impl<'a> Scanner<'a> {
pub fn new(fs: &'a dyn Fs, home: &SurjectionHome) -> Self {
let (rules, warnings) = load_rules(fs, &home.state_dir().join(RULES_OVERRIDE_FILE));
Self {
rules,
cache: Some(Cache::new(fs, home.cache_dir().join(CACHE_DIR))),
warnings,
}
}
pub fn rules(&self) -> &Rules {
&self.rules
}
pub fn warnings(&self) -> &[String] {
&self.warnings
}
fn finish(&self, mut report: ScanReport) -> ScanReport {
report.custom_rules = self.rules.is_custom();
report.warnings.clone_from(&self.warnings);
report
}
pub fn scan_files(&self, files: &SkillFiles) -> ScanReport {
let hash = content_hash(files);
let key = self.cache.as_ref().map(|_| Cache::key(&self.rules, &hash));
if let (Some(cache), Some(key)) = (&self.cache, &key)
&& let Some(report) = cache.get(key)
{
return self.finish(report);
}
let report = ScanReport::new(
self.rules.version(),
Some(hash),
engine::scan_files(&self.rules, files),
);
if let (Some(cache), Some(key)) = (&self.cache, &key) {
cache.put(key, &report);
}
self.finish(report)
}
pub fn scan_command(&self, command: &str, args: &[String]) -> ScanReport {
let mut line = command.to_owned();
for arg in args {
line.push(' ');
line.push_str(arg);
}
self.finish(ScanReport::new(
self.rules.version(),
None,
engine::scan_command(&self.rules, &line),
))
}
}
impl Scanner<'static> {
pub fn builtin() -> Self {
Self::with_rules(Cow::Borrowed(Rules::builtin()))
}
pub fn with_rules(rules: Cow<'static, Rules>) -> Self {
Self {
rules,
cache: None,
warnings: Vec::new(),
}
}
}
fn load_rules(fs: &dyn Fs, path: &Path) -> (Cow<'static, Rules>, Vec<String>) {
let builtin = Rules::builtin();
if !fs.exists(path) {
return (Cow::Borrowed(builtin), Vec::new());
}
let merged = fs
.read_to_string(path)
.map_err(|err| CoreError::new(ErrorCode::IoError, err.to_string()))
.and_then(|text| builtin.with_custom(&text));
match merged {
Ok((rules, warnings)) => (Cow::Owned(rules), warnings),
Err(err) => (
Cow::Borrowed(builtin),
vec![format!(
"state/{RULES_OVERRIDE_FILE} is not used, only the built-in scan rules ran: {}",
err.message()
)],
),
}
}