john-slop/src/config.rs

223 lines
7.2 KiB
Rust

//! Typed configuration consumed by the shared library.
use figment::{
Figment,
providers::{Format, Toml},
};
use serde::{Deserialize, Serialize};
use crate::Category;
const EMBEDDED_CONFIG: &str = include_str!("../assets/config.toml");
const EMBEDDED_HEURISTICS: &str = include_str!("../assets/heuristics.toml");
/// One configured Unicode-to-ASCII replacement.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ReplacementRule {
/// Stable identifier used by diagnostics and overrides.
pub id: String,
/// Unicode character to replace.
pub source: char,
/// ASCII replacement text.
pub replacement: String,
/// Why this replacement is safe or desirable.
pub rationale: String,
/// Whether matching and edit generation are active for this rule.
pub enabled: bool,
}
/// Resolved John Slop configuration.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
/// Configuration schema understood by this build.
pub schema_version: u32,
/// Enabled replacement rules in deterministic matching order.
pub replacements: Vec<ReplacementRule>,
/// Configured detection rules in deterministic declaration order.
pub rules: Vec<HeuristicRule>,
/// Named confidence-band boundaries.
pub thresholds: ConfidenceThresholds,
/// Resource and traversal limits applied while reading inputs.
pub input: InputLimits,
}
/// One configured data-driven detection rule.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct HeuristicRule {
/// Stable lowercase kebab-case identifier.
pub id: String,
/// Broad evidence family.
pub category: Category,
/// Matching implementation selected for this rule.
pub matcher: MatcherType,
/// Exact phrase, regular expression, or named replacement source.
pub pattern: String,
/// Evidence contribution before aggregation and damping.
pub weight: f64,
/// Concise explanation shown with a finding.
pub message: String,
/// Remediation or review guidance.
pub help: String,
/// Maximum contributing occurrences per document.
pub occurrence_cap: usize,
/// Whether this rule participates in detection.
pub enabled: bool,
/// Content and extension filters for this rule.
pub scope: RuleScope,
}
/// Matcher implementation named by a heuristic rule.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum MatcherType {
/// Literal phrase matching.
ExactPhrase,
/// Rust regular-expression matching.
Regex,
/// Findings produced by the configured replacement catalog.
Replacement,
}
/// Applicability filters attached to a heuristic rule.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RuleScope {
/// Broad kinds of content this rule accepts.
pub content: Vec<ContentKind>,
/// Lowercase extensions without a leading dot; empty means unrestricted.
pub extensions: Vec<String>,
}
/// Broad input kind used for heuristic applicability.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ContentKind {
/// Articles, documentation, and other prose.
Prose,
/// Programming-language source text.
SourceCode,
}
/// Confidence boundaries used to name aggregate score bands.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ConfidenceThresholds {
/// First score considered low confidence.
pub low: u8,
/// First score considered medium confidence.
pub medium: u8,
/// First score considered high confidence.
pub high: u8,
}
/// Bounded resource policy for filesystem and archive inputs.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct InputLimits {
/// Maximum bytes accepted for a regular file or one archive member.
pub max_document_bytes: u64,
/// Maximum bytes accepted for an outer archive.
pub max_archive_bytes: u64,
/// Maximum total declared expanded bytes across archive members.
pub max_expanded_bytes: u64,
/// Maximum number of members in one archive.
pub max_archive_entries: usize,
/// Maximum allowed expanded-to-compressed ratio.
pub max_compression_ratio: u64,
/// Maximum directory traversal depth, including the root at depth zero.
pub max_walk_depth: usize,
}
impl Default for InputLimits {
fn default() -> Self {
Self {
max_document_bytes: 8 * 1024 * 1024,
max_archive_bytes: 64 * 1024 * 1024,
max_expanded_bytes: 256 * 1024 * 1024,
max_archive_entries: 10_000,
max_compression_ratio: 100,
max_walk_depth: 64,
}
}
}
impl Config {
/// Load the replacement defaults embedded in this build.
pub fn embedded() -> Self {
Figment::from(Toml::string(EMBEDDED_CONFIG))
.merge(Toml::string(EMBEDDED_HEURISTICS))
.extract()
.expect("embedded configuration must be valid")
}
}
impl Default for Config {
fn default() -> Self {
Self::embedded()
}
}
#[cfg(test)]
mod tests {
use std::collections::BTreeSet;
use super::*;
#[test]
fn embedded_replacements_are_unique_and_ascii() {
let config = Config::embedded();
let ids = config
.replacements
.iter()
.map(|rule| rule.id.as_str())
.collect::<BTreeSet<_>>();
let sources = config
.replacements
.iter()
.map(|rule| rule.source)
.collect::<BTreeSet<_>>();
assert_eq!(config.replacements.len(), 10);
assert_eq!(config.schema_version, 1);
assert_eq!(ids.len(), config.replacements.len());
assert_eq!(sources.len(), config.replacements.len());
assert!(config.replacements.iter().all(|rule| rule.enabled
&& !rule.source.is_ascii()
&& rule.replacement.is_ascii()
&& !rule.rationale.is_empty()));
}
#[test]
fn default_input_limits_match_the_accepted_policy() {
let limits = InputLimits::default();
assert_eq!(limits.max_document_bytes, 8 * 1024 * 1024);
assert_eq!(limits.max_archive_bytes, 64 * 1024 * 1024);
assert_eq!(limits.max_expanded_bytes, 256 * 1024 * 1024);
assert_eq!(limits.max_archive_entries, 10_000);
assert_eq!(limits.max_compression_ratio, 100);
assert_eq!(limits.max_walk_depth, 64);
}
#[test]
fn embedded_heuristics_have_complete_metadata() {
let config = Config::embedded();
assert_eq!(config.rules.len(), 6);
assert_eq!(config.thresholds.low, 20);
assert_eq!(config.thresholds.medium, 50);
assert_eq!(config.thresholds.high, 75);
assert!(config.rules.iter().all(|rule| {
!rule.id.is_empty()
&& !rule.pattern.is_empty()
&& !rule.message.is_empty()
&& !rule.help.is_empty()
&& rule.occurrence_cap > 0
&& !rule.scope.content.is_empty()
}));
}
}