agent-shield 1.0.1

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
use once_cell::sync::Lazy;
use regex::Regex;

use crate::ir::{Language, ScanTarget, SourceLocation};
use crate::rules::{
    AttackCategory, Confidence, Detector, Evidence, Finding, OwaspMcp, RuleMetadata, Severity,
};

/// SHIELD-036: Tool Response Prompt Injection / Unsanitized Parameter Reflection
///
/// Detects MCP tool handler functions that directly interpolate untrusted parameters
/// into their return value without sanitization or escaping, enabling an attacker who
/// controls tool inputs to inject arbitrary instructions into the downstream LLM prompt
/// (CWE-1336, OWASP MCP PromptInjection).
///
/// Covered patterns:
///   Python  — `@mcp.tool` / `@tool` decorated functions whose `return` statement
///             uses an f-string, `.format()`, `%`-formatting, or string concatenation
///             of a parameter variable.
///   TS/JS   — Functions registered via `.tool(` / `.addTool(` / `server.tool(` whose
///             body contains a template-literal or string-concatenation `return`.
pub struct ToolResponseInjectionDetector;

// ── Python statics (applied per-line; no (?m) flag) ───────────────────────────

// Matches @mcp.tool, @tool, @server.tool, @app.tool decorator lines
static PY_TOOL_DECORATOR_RE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r#"^\s*@(?:\w+\.)*tool\b"#).expect("valid regex")
});

// Matches `def <name>(` where <name> contains a tool-related keyword
static PY_TOOL_DEF_RE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(
        r#"^\s*(?:async\s+)?def\s+\w*(?:tool|handler|execute|process|handle|invoke)\w*\s*\("#,
    )
    .expect("valid regex")
});

// Matches any `def <name>(` — used to detect the function start after a decorator
static PY_DEF_RE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r#"^\s*(?:async\s+)?def\s+\w+\s*\("#).expect("valid regex")
});

// Matches `return f"...{var}..."` or `return f'...'` (f-string with at least one interpolation)
static PY_RETURN_FSTRING_RE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r#"^\s*return\s+f(?:"[^"]*\{[^}]+\}|'[^']*\{[^}]+\})"#).expect("valid regex")
});

// Matches `return "..." + var` or `return var + "..."` string concatenation
static PY_RETURN_CONCAT_RE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r#"^\s*return\s+(?:["'][^"']*["']\s*\+|\w+\s*\+\s*["'])"#).expect("valid regex")
});

// Matches `return "...".format(` or `return (...) % `
static PY_RETURN_FORMAT_RE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r#"^\s*return\s+(?:["'][^"']*["']|\w+)\s*(?:\.format\s*\(|%\s*[\w(])"#)
        .expect("valid regex")
});

// ── TypeScript / JavaScript statics (applied per-line; no (?m) flag) ──────────

// Matches tool registration: `.tool(`, `.addTool(`, `server.tool(`, `mcp.tool(`
static TS_TOOL_REGISTRATION_RE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r#"\b(?:addTool|server\.tool|mcp\.tool|\.tool)\s*\("#).expect("valid regex")
});

// Matches template literal return with interpolation: return `...${...}...`
static TS_RETURN_TEMPLATE_RE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r#"^\s*return\s+`[^`]*\$\{[^}]+\}"#).expect("valid regex")
});

// Matches string concatenation in return: return "..." + var or return var + "..."
static TS_RETURN_CONCAT_RE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r#"^\s*return\s+(?:["'][^"']*["']\s*\+|\w+\s*\+\s*["'])"#).expect("valid regex")
});

impl Detector for ToolResponseInjectionDetector {
    fn metadata(&self) -> RuleMetadata {
        RuleMetadata {
            id: "SHIELD-036".into(),
            name: "Tool Response Prompt Injection / Unsanitized Parameter Reflection".into(),
            description: "MCP tool handler returns untrusted parameter values via f-string, \
                          template literal, or string concatenation without sanitization, enabling \
                          prompt injection into the downstream LLM conversation"
                .into(),
            default_severity: Severity::High,
            attack_category: AttackCategory::PromptInjectionSurface,
            cwe_id: Some("CWE-1336".into()),
            owasp_mcp: Some(OwaspMcp::PromptInjection),
        }
    }

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

        for file in &target.source_files {
            let lines: Vec<&str> = file.content.lines().collect();

            match file.language {
                Language::Python => {
                    scan_python(&lines, file, &mut findings);
                }
                Language::TypeScript | Language::JavaScript => {
                    scan_ts_js(&lines, file, &mut findings);
                }
                _ => {}
            }
        }

        findings
    }
}

fn scan_python(
    lines: &[&str],
    file: &crate::ir::SourceFile,
    findings: &mut Vec<Finding>,
) {
    let mut in_tool_fn = false;
    let mut expecting_tool_def = false;
    let mut tool_fn_indent: usize = 0;

    for (line_idx, &line) in lines.iter().enumerate() {
        let trimmed = line.trim();
        if trimmed.starts_with('#') || trimmed.is_empty() {
            continue;
        }

        let current_indent = line.len() - line.trim_start().len();

        if expecting_tool_def {
            if PY_DEF_RE.is_match(line) {
                in_tool_fn = true;
                expecting_tool_def = false;
                tool_fn_indent = current_indent;
                continue;
            }
        }

        if in_tool_fn && current_indent <= tool_fn_indent {
            in_tool_fn = false;
        }

        if !in_tool_fn {
            if PY_TOOL_DECORATOR_RE.is_match(line) {
                expecting_tool_def = true;
                continue;
            } else if PY_TOOL_DEF_RE.is_match(line) {
                in_tool_fn = true;
                tool_fn_indent = current_indent;
                continue;
            }
        }

        if in_tool_fn {
            let is_unsafe_return = PY_RETURN_FSTRING_RE.is_match(line)
                || PY_RETURN_CONCAT_RE.is_match(line)
                || PY_RETURN_FORMAT_RE.is_match(line);

            if is_unsafe_return {
                let col = line.find("return").unwrap_or(0);
                let loc = SourceLocation {
                    file: file.path.clone(),
                    line: line_idx + 1,
                    column: col,
                    end_line: None,
                    end_column: None,
                };
                findings.push(Finding {
                    rule_id: "SHIELD-036".into(),
                    rule_name: "Tool Response Prompt Injection / Unsanitized Parameter Reflection"
                        .into(),
                    severity: Severity::High,
                    confidence: Confidence::Medium,
                    attack_category: AttackCategory::PromptInjectionSurface,
                    message: "Tool handler returns an unsanitized parameter via string \
                              interpolation — an attacker controlling tool inputs can inject \
                              arbitrary instructions into the downstream LLM prompt"
                        .into(),
                    location: Some(loc.clone()),
                    evidence: vec![Evidence {
                        description: "Unsanitized return in tool handler".into(),
                        location: Some(loc),
                        snippet: Some(trimmed.to_string()),
                    }],
                    taint_path: None,
                    remediation: Some(
                        "Sanitize or escape tool output before returning it. Consider wrapping \
                         the value in a structured JSON response, stripping control characters, \
                         or validating that output conforms to an expected schema."
                            .into(),
                    ),
                    cwe_id: Some("CWE-1336".into()),
                });
            }
        }
    }
}

fn scan_ts_js(
    lines: &[&str],
    file: &crate::ir::SourceFile,
    findings: &mut Vec<Finding>,
) {
    for (line_idx, &line) in lines.iter().enumerate() {
        let trimmed = line.trim();
        if trimmed.starts_with("//") || trimmed.starts_with('*') {
            continue;
        }

        if TS_TOOL_REGISTRATION_RE.is_match(line) {
            // Scan the next 50 lines for template-literal or concat returns
            let end_idx = (line_idx + 50).min(lines.len());
            for (body_offset, &body_line) in lines[line_idx..end_idx].iter().enumerate() {
                let body_trimmed = body_line.trim();
                if body_trimmed.starts_with("//") {
                    continue;
                }

                let is_unsafe_return = TS_RETURN_TEMPLATE_RE.is_match(body_line)
                    || TS_RETURN_CONCAT_RE.is_match(body_line);

                if is_unsafe_return {
                    let col = body_line.find("return").unwrap_or(0);
                    let finding_line = line_idx + body_offset + 1;
                    let loc = SourceLocation {
                        file: file.path.clone(),
                        line: finding_line,
                        column: col,
                        end_line: None,
                        end_column: None,
                    };
                    findings.push(Finding {
                        rule_id: "SHIELD-036".into(),
                        rule_name:
                            "Tool Response Prompt Injection / Unsanitized Parameter Reflection"
                                .into(),
                        severity: Severity::High,
                        confidence: Confidence::Medium,
                        attack_category: AttackCategory::PromptInjectionSurface,
                        message: "Tool handler returns an unsanitized template literal or string \
                                  concatenation — an attacker controlling tool inputs can inject \
                                  arbitrary instructions into the downstream LLM prompt"
                            .into(),
                        location: Some(loc.clone()),
                        evidence: vec![Evidence {
                            description: "Unsanitized template-literal or concat return in tool"
                                .into(),
                            location: Some(loc),
                            snippet: Some(body_trimmed.to_string()),
                        }],
                        taint_path: None,
                        remediation: Some(
                            "Sanitize tool output before returning it. Prefer structured JSON \
                             responses over raw string interpolation, or escape special characters \
                             before the value enters the LLM context."
                                .into(),
                        ),
                        cwe_id: Some("CWE-1336".into()),
                    });
                    // One finding per tool registration block
                    break;
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ir::{Framework, Language, ScanTarget, SourceFile};
    use std::path::PathBuf;

    fn target_with_source(code: &str, language: Language) -> ScanTarget {
        ScanTarget {
            name: "test-tool-agent".into(),
            framework: Framework::Mcp,
            root_path: PathBuf::from("/test"),
            tools: Vec::new(),
            execution: Default::default(),
            data: Default::default(),
            dependencies: Default::default(),
            provenance: Default::default(),
            source_files: vec![SourceFile {
                path: PathBuf::from(match language {
                    Language::Python => "tool.py",
                    Language::TypeScript => "tool.ts",
                    _ => "tool.js",
                }),
                language,
                size_bytes: code.len() as u64,
                content_hash: "hash".into(),
                content: code.to_string(),
            }],
        }
    }

    #[test]
    fn detects_python_fstring_return_in_mcp_tool() {
        let code = r#"
@mcp.tool
def search_database(query: str) -> str:
    results = db.search(query)
    return f"Search results for '{query}': {results}"
"#;
        let target = target_with_source(code, Language::Python);
        let detector = ToolResponseInjectionDetector;
        let findings = detector.run(&target);
        assert_eq!(
            findings.len(),
            1,
            "expected 1 finding for f-string return in @mcp.tool; got {}: {findings:#?}",
            findings.len()
        );
        assert_eq!(findings[0].rule_id, "SHIELD-036");
        assert_eq!(findings[0].severity, Severity::High);
        assert_eq!(findings[0].confidence, Confidence::Medium);
    }

    #[test]
    fn ignores_python_hardcoded_return_in_tool() {
        let code = r#"
@mcp.tool
def get_status() -> str:
    return "OK"
"#;
        let target = target_with_source(code, Language::Python);
        let detector = ToolResponseInjectionDetector;
        let findings = detector.run(&target);
        assert!(
            findings.is_empty(),
            "hardcoded return must not trigger SHIELD-036; got: {findings:#?}"
        );
    }

    #[test]
    fn detects_python_concat_return_in_tool_by_name() {
        let code = r#"
async def execute_query(query: str) -> str:
    result = db.run(query)
    return "Result: " + result
"#;
        let target = target_with_source(code, Language::Python);
        let detector = ToolResponseInjectionDetector;
        let findings = detector.run(&target);
        assert_eq!(
            findings.len(),
            1,
            "expected 1 finding for concat return in tool-named function; got {}: {findings:#?}",
            findings.len()
        );
    }

    #[test]
    fn detects_ts_template_literal_return_in_tool() {
        let code = r#"
server.tool("search", async ({ query }) => {
    const results = await db.search(query);
    return `Found: ${results}`;
});
"#;
        let target = target_with_source(code, Language::TypeScript);
        let detector = ToolResponseInjectionDetector;
        let findings = detector.run(&target);
        assert_eq!(
            findings.len(),
            1,
            "expected 1 finding for template literal return; got {}: {findings:#?}",
            findings.len()
        );
        assert_eq!(findings[0].rule_id, "SHIELD-036");
    }

    #[test]
    fn ignores_ts_hardcoded_return_in_tool() {
        let code = r#"
server.tool("ping", async () => {
    return "pong";
});
"#;
        let target = target_with_source(code, Language::TypeScript);
        let detector = ToolResponseInjectionDetector;
        let findings = detector.run(&target);
        assert!(
            findings.is_empty(),
            "hardcoded string return must not trigger SHIELD-036; got: {findings:#?}"
        );
    }

    #[test]
    fn ignores_non_tool_function_with_fstring() {
        // A regular Python helper (no tool decorator, no tool-related name) must not fire
        let code = r#"
def format_greeting(name: str) -> str:
    return f"Hello, {name}!"
"#;
        let target = target_with_source(code, Language::Python);
        let detector = ToolResponseInjectionDetector;
        let findings = detector.run(&target);
        assert!(
            findings.is_empty(),
            "non-tool function must not trigger SHIELD-036; got: {findings:#?}"
        );
    }
}