use std::collections::BTreeSet;
use std::sync::LazyLock;
use regex::Regex;
use serde::Deserialize;
use sha2::{Digest, Sha256};
use super::Severity;
use crate::error::{CoreError, ErrorCode};
pub const SCHEMA: u32 = 1;
const BUILTIN_TEXT: &str = include_str!("rules.toml");
static BUILTIN: LazyLock<Rules> = LazyLock::new(|| match Rules::parse(BUILTIN_TEXT) {
Ok(rules) => rules,
Err(err) => panic!("the built-in scan rules do not load: {err:?}"),
});
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Target {
Markdown,
Code,
Command,
}
#[derive(Debug, Clone)]
pub struct Rule {
pub id: String,
pub severity: Severity,
pub message: String,
pub(super) prose: Option<Severity>,
pub(super) targets: Vec<Target>,
pub(super) path: Option<Regex>,
pub(super) pattern: Option<Regex>,
pub(super) unless: Option<Regex>,
pub(super) requires: Vec<Regex>,
}
impl Rule {
pub fn applies_to(&self, target: Target) -> bool {
self.targets.contains(&target)
}
}
#[derive(Debug, Clone)]
pub struct Rules {
version: u32,
fingerprint: String,
rules: Vec<Rule>,
custom: bool,
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RawRules {
#[allow(dead_code)]
schema: u32,
version: u32,
#[serde(default, rename = "rule")]
rules: Vec<RawRule>,
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RawRule {
id: String,
severity: Severity,
message: String,
targets: Option<Vec<Target>>,
path: Option<String>,
pattern: Option<String>,
unless: Option<String>,
#[serde(default)]
requires: Vec<String>,
prose: Option<Severity>,
}
fn invalid(code: ErrorCode, message: String) -> CoreError {
CoreError::new(code, format!("scan rules: {message}"))
}
fn is_rule_id(id: &str) -> bool {
!id.is_empty()
&& id.split('-').all(|part| {
!part.is_empty()
&& part
.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit())
})
}
fn ascii_word_boundaries(pattern: &str) -> String {
let mut out = String::with_capacity(pattern.len() + 16);
let mut chars = pattern.chars();
let mut class = 0usize;
while let Some(c) = chars.next() {
match c {
'\\' => match chars.next() {
Some('b') if class == 0 => out.push_str("(?-u:\\b)"),
Some(next) => {
out.push('\\');
out.push(next);
}
None => out.push('\\'),
},
'[' => {
class += 1;
out.push(c);
}
']' if class > 0 => {
class -= 1;
out.push(c);
}
_ => out.push(c),
}
}
out
}
fn compile(id: &str, field: &str, pattern: &str) -> Result<Regex, CoreError> {
Regex::new(&ascii_word_boundaries(pattern)).map_err(|err| {
invalid(
ErrorCode::ParseError,
format!("rule {id:?}: `{field}`: {err}"),
)
})
}
fn fingerprint_of(text: &str) -> String {
Sha256::digest(text.as_bytes())
.iter()
.map(|byte| format!("{byte:02x}"))
.collect()
}
impl Rules {
pub fn builtin() -> &'static Rules {
&BUILTIN
}
pub fn parse(text: &str) -> Result<Rules, CoreError> {
let table: toml::Table = toml::from_str(text)
.map_err(|err| invalid(ErrorCode::ParseError, err.message().to_owned()))?;
match table.get("schema").and_then(toml::Value::as_integer) {
Some(schema) if schema == i64::from(SCHEMA) => {}
Some(schema) => {
return Err(invalid(
ErrorCode::UnsupportedSchema,
format!("schema {schema} is not supported (this build reads {SCHEMA})"),
));
}
None => return Err(invalid(ErrorCode::MissingField, "no `schema`".to_owned())),
}
let raw: RawRules =
toml::Value::Table(table)
.try_into()
.map_err(|err: toml::de::Error| {
invalid(ErrorCode::ParseError, err.message().to_owned())
})?;
if raw.version == 0 {
return Err(invalid(
ErrorCode::InvalidValue,
"`version` must be 1 or more".to_owned(),
));
}
let mut seen = BTreeSet::new();
let mut rules = Vec::with_capacity(raw.rules.len());
for rule in raw.rules {
rules.push(check(rule, &mut seen)?);
}
Ok(Rules {
version: raw.version,
fingerprint: fingerprint_of(text),
rules,
custom: false,
})
}
pub fn with_custom(&self, text: &str) -> Result<(Rules, Vec<String>), CoreError> {
let custom = Rules::parse(text)?;
let mut rules = self.rules.clone();
let mut warnings = Vec::new();
for rule in custom.rules {
match rules.iter_mut().find(|known| known.id == rule.id) {
Some(known) if rule.severity >= known.severity => {
known.severity = rule.severity;
}
Some(known) => warnings.push(format!(
"custom scan rule {:?} would lower a built-in rule from {} to {}; the built-in severity is kept",
rule.id,
known.severity.as_str(),
rule.severity.as_str()
)),
None => rules.push(rule),
}
}
Ok((
Rules {
version: self.version,
fingerprint: fingerprint_of(&format!(
"{}\0{}",
self.fingerprint, custom.fingerprint
)),
rules,
custom: true,
},
warnings,
))
}
pub fn version(&self) -> u32 {
self.version
}
pub fn fingerprint(&self) -> &str {
&self.fingerprint
}
pub fn is_custom(&self) -> bool {
self.custom
}
pub fn rules(&self) -> &[Rule] {
&self.rules
}
}
fn check(raw: RawRule, seen: &mut BTreeSet<String>) -> Result<Rule, CoreError> {
let id = raw.id;
if !is_rule_id(&id) {
return Err(invalid(
ErrorCode::InvalidId,
format!("rule id {id:?} is not kebab-case (a-z, 0-9, -)"),
));
}
if !seen.insert(id.clone()) {
return Err(invalid(
ErrorCode::AlreadyExists,
format!("rule {id:?} is defined twice"),
));
}
let message = raw.message.trim().to_owned();
if message.is_empty() {
return Err(invalid(
ErrorCode::MissingField,
format!("rule {id:?} has no message"),
));
}
let not_allowed =
|what: &str| invalid(ErrorCode::FieldNotAllowed, format!("rule {id:?}: {what}"));
let targets = match (&raw.pattern, raw.targets) {
(None, None) if raw.path.is_none() => {
return Err(invalid(
ErrorCode::MissingField,
format!("rule {id:?} has neither `pattern` nor `path`"),
));
}
(None, None) => Vec::new(),
(None, Some(_)) => {
return Err(not_allowed(
"`targets` needs a `pattern` (a path rule matches files)",
));
}
(Some(_), None) => vec![Target::Markdown, Target::Code],
(Some(_), Some(targets)) if targets.is_empty() => {
return Err(invalid(
ErrorCode::InvalidValue,
format!("rule {id:?} has empty `targets`"),
));
}
(Some(_), Some(targets)) => targets,
};
if raw.pattern.is_none() && (raw.unless.is_some() || !raw.requires.is_empty()) {
return Err(not_allowed("`unless` and `requires` need a `pattern`"));
}
if raw.path.is_some() && targets.contains(&Target::Command) {
return Err(not_allowed(
"a command line has no path; `path` and the `command` target exclude each other",
));
}
if let Some(prose) = raw.prose
&& (!targets.contains(&Target::Markdown) || prose >= raw.severity)
{
return Err(not_allowed(&format!(
"`prose` ({}) needs the markdown target and a severity below `severity`",
prose.as_str()
)));
}
Ok(Rule {
path: raw
.path
.as_deref()
.map(|p| compile(&id, "path", p))
.transpose()?,
pattern: raw
.pattern
.as_deref()
.map(|p| compile(&id, "pattern", p))
.transpose()?,
unless: raw
.unless
.as_deref()
.map(|p| compile(&id, "unless", p))
.transpose()?,
requires: raw
.requires
.iter()
.map(|p| compile(&id, "requires", p))
.collect::<Result<_, _>>()?,
id,
severity: raw.severity,
message,
prose: raw.prose,
targets,
})
}