agent-shield 0.8.0

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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
use crate::ir::data_surface::{TaintSinkType, TaintSourceType};
use crate::ir::ScanTarget;
use crate::rules::{
    AttackCategory, Confidence, Detector, Evidence, Finding, RuleMetadata, Severity,
};

/// SHIELD-007: Prompt Injection Surface
///
/// Flags tools that fetch external content (HTTP, file read) and could
/// return it unsanitized to the LLM. External content may contain
/// adversarial instructions that hijack the agent's behavior.
///
/// When `DataSurface.taint_paths` are available, taint-path based findings
/// take priority. Falls back to `ArgumentSource`-based network operation
/// checking when no taint paths exist.
pub struct PromptInjectionDetector;

impl Detector for PromptInjectionDetector {
    fn metadata(&self) -> RuleMetadata {
        RuleMetadata {
            id: "SHIELD-007".into(),
            name: "Prompt Injection Surface".into(),
            description:
                "Tool fetches external content that may be returned unsanitized to the LLM".into(),
            default_severity: Severity::Medium,
            attack_category: AttackCategory::PromptInjectionSurface,
            cwe_id: None,
        }
    }

    fn run(&self, target: &ScanTarget) -> Vec<Finding> {
        let mut findings = Vec::new();

        // Phase 1: Taint-path based detection (higher confidence)
        for path in &target.data.taint_paths {
            let source_matches = matches!(
                path.source.source_type,
                TaintSourceType::ToolArgument | TaintSourceType::PromptContent
            );
            let sink_matches = matches!(
                path.sink.sink_type,
                TaintSinkType::DynamicEval | TaintSinkType::ResponseToLlm
            );

            if !source_matches || !sink_matches {
                continue;
            }

            // Check for sanitization in the path — if there are intermediate
            // nodes, we still flag but with slightly lower confidence
            let has_sanitization = false; // TaintPath.through contains locations,
                                          // not sanitizer info — reserved for future use

            if has_sanitization {
                continue;
            }

            let confidence = if path.confidence >= 0.8 {
                Confidence::High
            } else {
                Confidence::Medium
            };

            findings.push(Finding {
                rule_id: "SHIELD-007".into(),
                rule_name: "Prompt Injection Surface".into(),
                severity: Severity::Medium,
                confidence,
                attack_category: AttackCategory::PromptInjectionSurface,
                message: format!(
                    "Taint path: {} flows to {} without sanitization",
                    path.source.description, path.sink.description,
                ),
                location: Some(path.sink.location.clone()),
                evidence: vec![
                    Evidence {
                        description: format!(
                            "Taint source ({:?}): {}",
                            path.source.source_type, path.source.description
                        ),
                        location: Some(path.source.location.clone()),
                        snippet: None,
                    },
                    Evidence {
                        description: format!(
                            "Taint sink ({:?}): {}",
                            path.sink.sink_type, path.sink.description
                        ),
                        location: Some(path.sink.location.clone()),
                        snippet: None,
                    },
                ],
                taint_path: Some(path.clone()),
                remediation: Some(
                    "Sanitize or escape external content before returning it to the LLM. \
                     Consider stripping HTML tags, limiting response length, and adding \
                     content boundaries."
                        .into(),
                ),
                cwe_id: None,
            });
        }

        // Phase 2: Fallback to existing network operation checking
        let fallback_findings = self.run_fallback(target);

        // Deduplicate: keep taint-path findings over fallback at same location.
        // Collect owned locations to avoid borrowing `findings` during mutation.
        let taint_path_locations: Vec<_> =
            findings.iter().filter_map(|f| f.location.clone()).collect();

        let new_findings: Vec<_> = fallback_findings
            .into_iter()
            .filter(|finding| {
                let dominated = finding
                    .location
                    .as_ref()
                    .is_some_and(|loc| taint_path_locations.iter().any(|tp_loc| tp_loc == loc));
                !dominated
            })
            .collect();

        findings.extend(new_findings);

        findings
    }
}

impl PromptInjectionDetector {
    /// Original network-operation based detection logic.
    fn run_fallback(&self, target: &ScanTarget) -> Vec<Finding> {
        let mut findings = Vec::new();

        // Any network GET that reads external content is a prompt injection surface
        for net_op in &target.execution.network_operations {
            // Only flag reads (GET), not sends (POST with sends_data)
            if net_op.sends_data {
                continue;
            }

            findings.push(Finding {
                rule_id: "SHIELD-007".into(),
                rule_name: "Prompt Injection Surface".into(),
                severity: Severity::Medium,
                confidence: Confidence::Medium,
                attack_category: AttackCategory::PromptInjectionSurface,
                message: format!(
                    "'{}' fetches external content that may be returned to the LLM unsanitized",
                    net_op.function
                ),
                location: Some(net_op.location.clone()),
                evidence: vec![Evidence {
                    description: format!("External content fetch via '{}'", net_op.function),
                    location: Some(net_op.location.clone()),
                    snippet: None,
                }],
                taint_path: None,
                remediation: Some(
                    "Sanitize or escape external content before returning it to the LLM. \
                     Consider stripping HTML tags, limiting response length, and adding \
                     content boundaries."
                        .into(),
                ),
                cwe_id: None,
            });
        }

        findings
    }
}

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

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

    fn loc_at(file: &str, line: usize) -> SourceLocation {
        SourceLocation {
            file: PathBuf::from(file),
            line,
            column: 0,
            end_line: None,
            end_column: None,
        }
    }

    #[test]
    fn flags_get_request() {
        let target = ScanTarget {
            name: "test".into(),
            framework: Framework::Mcp,
            root_path: PathBuf::from("."),
            tools: vec![],
            execution: ExecutionSurface {
                network_operations: vec![NetworkOperation {
                    function: "requests.get".into(),
                    url_arg: ArgumentSource::Parameter { name: "url".into() },
                    method: Some("GET".into()),
                    sends_data: false,
                    location: loc(),
                }],
                ..Default::default()
            },
            data: Default::default(),
            dependencies: Default::default(),
            provenance: Default::default(),
            source_files: vec![],
        };
        let findings = PromptInjectionDetector.run(&target);
        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].rule_id, "SHIELD-007");
    }

    #[test]
    fn ignores_post_with_data() {
        let target = ScanTarget {
            name: "test".into(),
            framework: Framework::Mcp,
            root_path: PathBuf::from("."),
            tools: vec![],
            execution: ExecutionSurface {
                network_operations: vec![NetworkOperation {
                    function: "requests.post".into(),
                    url_arg: ArgumentSource::Literal("https://api.example.com".into()),
                    method: Some("POST".into()),
                    sends_data: true,
                    location: loc(),
                }],
                ..Default::default()
            },
            data: Default::default(),
            dependencies: Default::default(),
            provenance: Default::default(),
            source_files: vec![],
        };
        let findings = PromptInjectionDetector.run(&target);
        assert!(findings.is_empty());
    }

    #[test]
    fn prompt_injection_with_taint_path() {
        let target = ScanTarget {
            name: "test".into(),
            framework: Framework::Mcp,
            root_path: PathBuf::from("."),
            tools: vec![],
            execution: Default::default(),
            data: DataSurface {
                sources: vec![],
                sinks: vec![],
                taint_paths: vec![TaintPath {
                    source: TaintSource {
                        source_type: TaintSourceType::ToolArgument,
                        description: "user_query parameter".into(),
                        location: loc_at("handler.py", 3),
                    },
                    sink: TaintSink {
                        sink_type: TaintSinkType::ResponseToLlm,
                        description: "return response to LLM".into(),
                        location: loc_at("handler.py", 15),
                    },
                    through: vec![],
                    confidence: 0.85,
                }],
            },
            dependencies: Default::default(),
            provenance: Default::default(),
            source_files: vec![],
        };

        let findings = PromptInjectionDetector.run(&target);
        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].rule_id, "SHIELD-007");
        assert_eq!(findings[0].confidence, Confidence::High);
        assert!(
            findings[0].taint_path.is_some(),
            "finding should have taint_path populated"
        );

        let tp = findings[0].taint_path.as_ref().unwrap();
        assert_eq!(tp.source.source_type, TaintSourceType::ToolArgument);
        assert_eq!(tp.sink.sink_type, TaintSinkType::ResponseToLlm);
    }

    #[test]
    fn prompt_injection_with_dynamic_eval_sink() {
        let target = ScanTarget {
            name: "test".into(),
            framework: Framework::Mcp,
            root_path: PathBuf::from("."),
            tools: vec![],
            execution: Default::default(),
            data: DataSurface {
                sources: vec![],
                sinks: vec![],
                taint_paths: vec![TaintPath {
                    source: TaintSource {
                        source_type: TaintSourceType::ToolArgument,
                        description: "code parameter".into(),
                        location: loc_at("eval_tool.py", 1),
                    },
                    sink: TaintSink {
                        sink_type: TaintSinkType::DynamicEval,
                        description: "eval(code)".into(),
                        location: loc_at("eval_tool.py", 5),
                    },
                    through: vec![],
                    confidence: 0.95,
                }],
            },
            dependencies: Default::default(),
            provenance: Default::default(),
            source_files: vec![],
        };

        let findings = PromptInjectionDetector.run(&target);
        assert_eq!(findings.len(), 1);
        assert!(findings[0].taint_path.is_some());
    }

    #[test]
    fn prompt_injection_fallback_without_taint_paths() {
        // Empty DataSurface but has network operations — old behavior works
        let target = ScanTarget {
            name: "test".into(),
            framework: Framework::Mcp,
            root_path: PathBuf::from("."),
            tools: vec![],
            execution: ExecutionSurface {
                network_operations: vec![NetworkOperation {
                    function: "fetch".into(),
                    url_arg: ArgumentSource::Parameter { name: "url".into() },
                    method: Some("GET".into()),
                    sends_data: false,
                    location: loc_at("tool.ts", 20),
                }],
                ..Default::default()
            },
            data: Default::default(),
            dependencies: Default::default(),
            provenance: Default::default(),
            source_files: vec![],
        };

        let findings = PromptInjectionDetector.run(&target);
        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].rule_id, "SHIELD-007");
        assert!(
            findings[0].taint_path.is_none(),
            "fallback findings should not have taint_path"
        );
    }

    #[test]
    fn prompt_injection_deduplicates_by_location() {
        let shared_loc = loc_at("handler.py", 15);

        let target = ScanTarget {
            name: "test".into(),
            framework: Framework::Mcp,
            root_path: PathBuf::from("."),
            tools: vec![],
            execution: ExecutionSurface {
                network_operations: vec![NetworkOperation {
                    function: "requests.get".into(),
                    url_arg: ArgumentSource::Parameter { name: "url".into() },
                    method: Some("GET".into()),
                    sends_data: false,
                    location: shared_loc.clone(),
                }],
                ..Default::default()
            },
            data: DataSurface {
                sources: vec![],
                sinks: vec![],
                taint_paths: vec![TaintPath {
                    source: TaintSource {
                        source_type: TaintSourceType::ToolArgument,
                        description: "url parameter".into(),
                        location: loc_at("handler.py", 3),
                    },
                    sink: TaintSink {
                        sink_type: TaintSinkType::ResponseToLlm,
                        description: "return fetched content".into(),
                        location: shared_loc,
                    },
                    through: vec![],
                    confidence: 0.9,
                }],
            },
            dependencies: Default::default(),
            provenance: Default::default(),
            source_files: vec![],
        };

        let findings = PromptInjectionDetector.run(&target);

        // Both point to handler.py:15, taint-path finding should win
        let taint_path_count = findings.iter().filter(|f| f.taint_path.is_some()).count();
        let fallback_count = findings.iter().filter(|f| f.taint_path.is_none()).count();

        assert_eq!(taint_path_count, 1, "should have one taint-path finding");
        assert_eq!(
            fallback_count, 0,
            "fallback at same location should be deduplicated"
        );
    }

    #[test]
    fn prompt_injection_ignores_irrelevant_taint_paths() {
        // EnvVariable -> FileWrite should NOT trigger SHIELD-007
        let target = ScanTarget {
            name: "test".into(),
            framework: Framework::Mcp,
            root_path: PathBuf::from("."),
            tools: vec![],
            execution: Default::default(),
            data: DataSurface {
                sources: vec![],
                sinks: vec![],
                taint_paths: vec![TaintPath {
                    source: TaintSource {
                        source_type: TaintSourceType::EnvVariable,
                        description: "HOME".into(),
                        location: loc_at("config.py", 1),
                    },
                    sink: TaintSink {
                        sink_type: TaintSinkType::FileWrite,
                        description: "write config".into(),
                        location: loc_at("config.py", 5),
                    },
                    through: vec![],
                    confidence: 0.7,
                }],
            },
            dependencies: Default::default(),
            provenance: Default::default(),
            source_files: vec![],
        };

        let findings = PromptInjectionDetector.run(&target);
        assert!(
            findings.is_empty(),
            "EnvVariable->FileWrite should not trigger prompt injection"
        );
    }
}