agent-shield 1.0.2

Security scanner for AI agent extensions - offline-first, multi-framework, SARIF output
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
pub mod builtin;
pub mod custom;
pub mod finding;
pub mod policy;

use std::collections::HashSet;
use std::path::{Path, PathBuf};

use crate::analysis::DetectionInput;
use crate::ir::ScanTarget;
use crate::ir::SourceLocation;

pub use custom::{CustomRuleDef, CustomRuleDetector, load_custom_rules_from_dir};
pub use finding::{
    AttackCategory, Confidence, Evidence, Finding, OwaspMcp, RuleMetadata, Severity,
};

/// A detector checks a `ScanTarget` and produces findings.
pub trait Detector: Send + Sync {
    /// Metadata about this rule (id, name, severity, CWE).
    fn metadata(&self) -> RuleMetadata;

    /// Run the detector against a scan target.
    fn run(&self, target: &ScanTarget) -> Vec<Finding>;
}

pub(crate) trait ContextDetector: Send + Sync {
    fn metadata(&self) -> RuleMetadata;

    fn run(&self, input: &DetectionInput<'_>) -> Vec<Finding>;
}

/// The rule engine runs all registered detectors against a target.
pub struct RuleEngine {
    detectors: Vec<Box<dyn Detector>>,
    context_detectors: Vec<Box<dyn ContextDetector>>,
    custom_detectors: Vec<CustomRuleDetector>,
}

impl RuleEngine {
    /// Create a new engine with all built-in detectors registered.
    pub fn new() -> Self {
        Self {
            detectors: builtin::all_detectors(),
            context_detectors: builtin::all_context_detectors(),
            custom_detectors: Vec::new(),
        }
    }

    /// Add custom rule detectors to the engine.
    pub fn with_custom_rules(mut self, custom: Vec<CustomRuleDetector>) -> Self {
        self.custom_detectors.extend(custom);
        self
    }

    /// Load custom rules from a directory into this engine.
    pub fn load_custom_rules_from(&mut self, dir: &Path) -> crate::error::Result<()> {
        let loaded = custom::load_custom_rules_from_dir(dir)?;
        self.custom_detectors.extend(loaded);
        Ok(())
    }

    /// Run all detectors against a scan target.
    pub fn run(&self, target: &ScanTarget) -> Vec<Finding> {
        let composite_flows =
            crate::adapter::mcp::loader::compute_composite_flows_for_target(target);
        let input = DetectionInput {
            target,
            composite_flows: &composite_flows,
        };
        self.run_with_context(&input)
    }

    /// Run all built-in detectors, including contextual and custom detectors.
    pub(crate) fn run_with_context(&self, input: &DetectionInput<'_>) -> Vec<Finding> {
        let mut findings = self
            .detectors
            .iter()
            .flat_map(|detector| detector.run(input.target))
            .collect::<Vec<_>>();
        findings.extend(
            self.context_detectors
                .iter()
                .flat_map(|detector| detector.run(input)),
        );
        findings.extend(
            self.custom_detectors
                .iter()
                .flat_map(|detector| detector.run(input.target)),
        );
        apply_overlapping_rule_suppression(findings)
    }

    /// List metadata for target-only and custom rules.
    /// Use `list_scanner_rules()` to include contextual rules such as `SHIELD-020`.
    pub fn list_rules(&self) -> Vec<RuleMetadata> {
        let mut rules: Vec<RuleMetadata> = self.detectors.iter().map(|d| d.metadata()).collect();
        rules.extend(self.custom_detectors.iter().map(|d| d.metadata()));
        rules
    }

    /// List metadata for all scanner rules, including contextual and custom detectors.
    pub fn list_scanner_rules(&self) -> Vec<RuleMetadata> {
        let mut rules = self
            .detectors
            .iter()
            .map(|d| d.metadata())
            .collect::<Vec<_>>();
        rules.extend(self.context_detectors.iter().map(|d| d.metadata()));
        rules.extend(self.custom_detectors.iter().map(|d| d.metadata()));
        rules
    }
}

impl Default for RuleEngine {
    fn default() -> Self {
        Self::new()
    }
}

const OVERLAPPING_RULE_PAIRS: &[(&str, &str)] = &[
    // Keep precise/specific signal and suppress broader overlap.
    ("SHIELD-013", "SHIELD-003"), // Metadata/private SSRF suppresses generic SSRF
    ("SHIELD-002", "SHIELD-018"), // Credential exfil suppresses generic secret leakage
    ("SHIELD-004", "SHIELD-015"), // Arbitrary file access suppresses overbroad filesystem scope
    ("SHIELD-004", "SHIELD-006"), // Arbitrary file access suppresses self-modification overlap on parameter-driven writes
    ("SHIELD-011", "SHIELD-016"), // Dynamic eval/import suppression suppresses unsafe deserialization overlap
];

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct SourceLocationKey {
    file: PathBuf,
    line: usize,
    column: usize,
    end_line: Option<usize>,
    end_column: Option<usize>,
}

impl From<&SourceLocation> for SourceLocationKey {
    fn from(location: &SourceLocation) -> Self {
        Self {
            file: location.file.clone(),
            line: location.line,
            column: location.column,
            end_line: location.end_line,
            end_column: location.end_column,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct SuppressionKey {
    dominant_rule: &'static str,
    location: SourceLocationKey,
}

fn apply_overlapping_rule_suppression(findings: Vec<Finding>) -> Vec<Finding> {
    // Only dominant rules can suppress a candidate. Indexing those keys avoids
    // comparing every finding with every other finding on the scan path.
    let suppressors: HashSet<SuppressionKey> = findings
        .iter()
        .filter_map(|dominant| {
            let dominant_rule = dominant_rule_for_dominant(dominant.rule_id.as_str())?;
            let location = dominant.location.as_ref()?;
            dominant_finding_can_suppress(dominant).then_some(SuppressionKey {
                dominant_rule,
                location: location.into(),
            })
        })
        .collect();

    findings
        .into_iter()
        .filter(|candidate| {
            let Some(dominant_rule) = dominant_rule_for_candidate(candidate.rule_id.as_str())
            else {
                return true;
            };
            let Some(location) = candidate.location.as_ref() else {
                return true;
            };

            !suppressors.contains(&SuppressionKey {
                dominant_rule,
                location: location.into(),
            })
        })
        .collect()
}

fn dominant_rule_for_candidate(candidate_rule: &str) -> Option<&'static str> {
    OVERLAPPING_RULE_PAIRS
        .iter()
        .find_map(|(dominant, dominated)| (*dominated == candidate_rule).then_some(*dominant))
}

fn dominant_rule_for_dominant(dominant_rule: &str) -> Option<&'static str> {
    OVERLAPPING_RULE_PAIRS
        .iter()
        .find_map(|(dominant, _)| (*dominant == dominant_rule).then_some(*dominant))
}

fn dominant_finding_can_suppress(dominant: &Finding) -> bool {
    // A tainted URL is enough to retain the critical SHIELD-013 signal, but it
    // is not proof that the destination is metadata/private. Keep the generic
    // SHIELD-003 finding in that uncertain case; suppress it only when the
    // dominant finding carries a concrete metadata/private indication (or is a
    // synthetic dominant finding with no taint path).
    if dominant.rule_id == "SHIELD-013"
        && dominant.taint_path.is_some()
        && !dominant
            .evidence
            .iter()
            .any(|evidence| evidence.description.starts_with("Sink: HTTP request to "))
    {
        return false;
    }

    true
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ir::ArgumentSource;
    use crate::ir::data_surface::*;
    use crate::ir::execution_surface::*;
    use std::path::PathBuf;

    fn loc() -> SourceLocation {
        SourceLocation {
            file: PathBuf::from("server.py"),
            line: 10,
            column: 0,
            end_line: None,
            end_column: None,
        }
    }

    fn empty_target() -> ScanTarget {
        ScanTarget {
            name: "test".into(),
            framework: crate::ir::Framework::Mcp,
            root_path: PathBuf::from("."),
            tools: vec![],
            execution: ExecutionSurface::default(),
            data: DataSurface::default(),
            dependencies: Default::default(),
            provenance: Default::default(),
            source_files: vec![],
        }
    }

    fn simple_finding(rule_id: &str, location: Option<SourceLocation>) -> Finding {
        Finding {
            rule_id: rule_id.to_string(),
            rule_name: rule_id.to_string(),
            severity: Severity::Critical,
            confidence: Confidence::High,
            attack_category: AttackCategory::ArbitraryFileAccess,
            message: "test".into(),
            location,
            evidence: vec![],
            taint_path: None,
            remediation: None,
            cwe_id: None,
        }
    }

    #[test]
    fn all_builtin_rules_have_owasp_mcp_mapping() {
        let engine = RuleEngine::new();
        let rules = engine.list_rules();
        assert!(!rules.is_empty());
        for rule in &rules {
            assert!(
                rule.owasp_mcp.is_some(),
                "rule {} is missing an OWASP MCP Top 10 mapping",
                rule.id
            );
        }
    }

    #[test]
    fn suppresses_overlapping_014_pairs_in_engine_output() {
        let target = {
            let mut target = empty_target();
            let overlap_loc = loc();

            target.data.taint_paths.push(TaintPath {
                source: TaintSource {
                    source_type: TaintSourceType::ToolArgument,
                    description: "url".into(),
                    location: overlap_loc.clone(),
                },
                sink: TaintSink {
                    sink_type: TaintSinkType::HttpRequest,
                    description: "requests.get".into(),
                    location: overlap_loc.clone(),
                },
                through: vec![],
                confidence: 0.9,
            });
            target.execution.network_operations.push(NetworkOperation {
                function: "requests.get".into(),
                url_arg: ArgumentSource::Literal("http://169.254.169.254/latest/meta-data/".into()),
                method: Some("GET".into()),
                sends_data: false,
                location: overlap_loc.clone(),
            });

            target.execution.file_operations.push(FileOperation {
                operation: FileOpType::Read,
                path_arg: ArgumentSource::Parameter {
                    name: "path".into(),
                },
                location: overlap_loc.clone(),
            });

            target
        };

        let findings = RuleEngine::new().run(&target);
        let has_metadata_ssrf = findings.iter().any(|f| f.rule_id == "SHIELD-013");
        let has_ssrf = findings.iter().any(|f| f.rule_id == "SHIELD-003");
        assert!(has_metadata_ssrf, "should keep SHIELD-013");
        assert!(
            !has_ssrf,
            "SHIELD-003 should be suppressed when SHIELD-013 is present at same location"
        );
        assert!(has_metadata_ssrf || has_ssrf);
    }

    #[test]
    fn suppresses_overlapping_findings_for_arbitrary_file_and_overbroad_fs() {
        let target = {
            let mut target = empty_target();
            target.execution.file_operations.push(FileOperation {
                operation: FileOpType::Write,
                path_arg: ArgumentSource::Parameter {
                    name: "file_path".into(),
                },
                location: loc(),
            });
            target
        };

        let findings = RuleEngine::new().run(&target);
        let has_arf = findings.iter().any(|f| f.rule_id == "SHIELD-004");
        let has_overbroad = findings.iter().any(|f| f.rule_id == "SHIELD-015");
        assert!(has_arf);
        assert!(!has_overbroad);
    }

    #[test]
    fn suppresses_overlapping_pairs_by_rule_with_same_location() {
        let overlap_loc = loc();
        let findings = vec![
            simple_finding("SHIELD-016", Some(overlap_loc.clone())),
            simple_finding("SHIELD-011", Some(overlap_loc.clone())),
            simple_finding("SHIELD-018", Some(overlap_loc.clone())),
            simple_finding("SHIELD-002", Some(overlap_loc.clone())),
            simple_finding("SHIELD-003", Some(overlap_loc.clone())),
            simple_finding("SHIELD-013", Some(overlap_loc)),
        ];
        let filtered = apply_overlapping_rule_suppression(findings);
        let ids: Vec<_> = filtered.iter().map(|f| f.rule_id.as_str()).collect();
        assert!(ids.contains(&"SHIELD-013"));
        assert!(!ids.contains(&"SHIELD-003"));
        assert!(ids.contains(&"SHIELD-002"));
        assert!(!ids.contains(&"SHIELD-018"));
        assert!(ids.contains(&"SHIELD-011"));
        assert!(!ids.contains(&"SHIELD-016"));
    }

    #[test]
    fn suppresses_self_modification_overlap_with_arbitrary_file_access() {
        // Parameter-driven writes fire both SHIELD-004 and SHIELD-006 at the
        // same location; only the dominant SHIELD-004 should survive.
        let overlap_loc = loc();
        let findings = vec![
            simple_finding("SHIELD-004", Some(overlap_loc.clone())),
            simple_finding("SHIELD-006", Some(overlap_loc)),
        ];
        let filtered = apply_overlapping_rule_suppression(findings);
        let ids: Vec<_> = filtered.iter().map(|f| f.rule_id.as_str()).collect();
        assert!(ids.contains(&"SHIELD-004"));
        assert!(!ids.contains(&"SHIELD-006"));
    }

    #[test]
    fn engine_suppresses_self_modification_for_parameter_driven_writes() {
        let mut target = empty_target();
        target.execution.file_operations.push(FileOperation {
            operation: FileOpType::Write,
            path_arg: ArgumentSource::Parameter {
                name: "file_path".into(),
            },
            location: loc(),
        });

        let findings = RuleEngine::new().run(&target);
        assert!(findings.iter().any(|f| f.rule_id == "SHIELD-004"));
        assert!(
            !findings.iter().any(|f| f.rule_id == "SHIELD-006"),
            "SHIELD-006 should be suppressed when SHIELD-004 fires at the same location"
        );
    }

    #[test]
    fn keeps_overlapping_rules_for_distinct_spans_on_the_same_line() {
        let mut first_span = loc();
        first_span.column = 4;
        first_span.end_line = Some(first_span.line);
        first_span.end_column = Some(14);

        let mut second_span = first_span.clone();
        second_span.column = 16;
        second_span.end_column = Some(33);

        let findings = vec![
            simple_finding("SHIELD-004", Some(first_span)),
            simple_finding("SHIELD-015", Some(second_span)),
        ];

        let filtered = apply_overlapping_rule_suppression(findings);
        let ids: Vec<_> = filtered
            .iter()
            .map(|finding| finding.rule_id.as_str())
            .collect();

        assert_eq!(filtered.len(), 2);
        assert!(ids.contains(&"SHIELD-004"));
        assert!(ids.contains(&"SHIELD-015"));
    }
}