Skip to main content

agentshield/rules/
mod.rs

1pub mod builtin;
2pub mod finding;
3pub mod policy;
4
5use crate::ir::ScanTarget;
6
7pub use finding::{AttackCategory, Confidence, Evidence, Finding, RuleMetadata, Severity};
8
9/// A detector checks a `ScanTarget` and produces findings.
10pub trait Detector: Send + Sync {
11    /// Metadata about this rule (id, name, severity, CWE).
12    fn metadata(&self) -> RuleMetadata;
13
14    /// Run the detector against a scan target.
15    fn run(&self, target: &ScanTarget) -> Vec<Finding>;
16}
17
18/// The rule engine runs all registered detectors against a target.
19pub struct RuleEngine {
20    detectors: Vec<Box<dyn Detector>>,
21}
22
23impl RuleEngine {
24    /// Create a new engine with all built-in detectors registered.
25    pub fn new() -> Self {
26        Self {
27            detectors: builtin::all_detectors(),
28        }
29    }
30
31    /// Run all detectors against a scan target.
32    pub fn run(&self, target: &ScanTarget) -> Vec<Finding> {
33        self.detectors.iter().flat_map(|d| d.run(target)).collect()
34    }
35
36    /// List metadata for all registered rules.
37    pub fn list_rules(&self) -> Vec<RuleMetadata> {
38        self.detectors.iter().map(|d| d.metadata()).collect()
39    }
40}
41
42impl Default for RuleEngine {
43    fn default() -> Self {
44        Self::new()
45    }
46}