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
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
461
462
463
//! Builds a populated `DataSurface` from parsed tool definitions and execution surfaces.
//!
//! Called by each adapter after merging `ParsedFile` results into `ExecutionSurface`
//! and `ToolSurface`. Constructs taint sources, sinks, and 1-hop taint paths.

pub(crate) mod paths;
pub(crate) mod sinks;
pub(crate) mod sources;

pub(crate) use paths::build_taint_paths;
pub(crate) use sinks::collect_sinks;
pub(crate) use sources::collect_sources;

use crate::ir::data_surface::DataSurface;
use crate::ir::execution_surface::ExecutionSurface;
use crate::ir::tool_surface::ToolSurface;

/// Build a `DataSurface` from tool definitions and execution surface.
///
/// Extracts taint sources (tool parameters, env vars), sinks (process exec,
/// HTTP requests, file writes, dynamic eval), and connects them with 1-hop
/// taint paths when an operation uses a tainted argument.
pub fn build_data_surface(tools: &[ToolSurface], execution: &ExecutionSurface) -> DataSurface {
    let sources = collect_sources(tools, execution);
    let sinks = collect_sinks(execution);
    let taint_paths = build_taint_paths(&sources, execution);

    DataSurface {
        sources,
        sinks,
        taint_paths,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use std::path::PathBuf;

    use crate::ir::data_surface::{TaintSinkType, TaintSourceType};
    use crate::ir::execution_surface::*;
    use crate::ir::tool_surface::ToolSurface;
    use crate::ir::{ArgumentSource, SourceLocation};

    fn make_location(line: usize) -> SourceLocation {
        SourceLocation {
            file: PathBuf::from("test.py"),
            line,
            column: 0,
            end_line: None,
            end_column: None,
        }
    }

    fn make_tool(name: &str, params: &[&str]) -> ToolSurface {
        let mut properties = serde_json::Map::new();
        for p in params {
            properties.insert(p.to_string(), json!({"type": "string"}));
        }
        ToolSurface {
            name: name.to_string(),
            description: Some("test tool".to_string()),
            input_schema: Some(json!({"properties": properties})),
            output_schema: None,
            declared_permissions: vec![],
            defined_at: Some(make_location(1)),
            declared_capabilities: Default::default(),
            capability_declarations: Vec::new(),
            observed_capabilities: Default::default(),
            capability_observation_complete: false,
            capability_evidence: Vec::new(),
        }
    }

    #[test]
    fn test_sources_from_tool_parameters() {
        let tools = vec![make_tool("run_cmd", &["command", "cwd"])];
        let execution = ExecutionSurface::default();

        let surface = build_data_surface(&tools, &execution);

        assert_eq!(surface.sources.len(), 2);
        assert!(
            surface
                .sources
                .iter()
                .all(|s| s.source_type == TaintSourceType::ToolArgument)
        );
        assert!(
            surface
                .sources
                .iter()
                .any(|s| s.description.contains("command"))
        );
        assert!(
            surface
                .sources
                .iter()
                .any(|s| s.description.contains("cwd"))
        );
    }

    #[test]
    fn test_sources_from_env_accesses() {
        let tools = vec![];
        let execution = ExecutionSurface {
            env_accesses: vec![EnvAccess {
                var_name: ArgumentSource::Literal("API_KEY".to_string()),
                is_sensitive: true,
                location: make_location(10),
            }],
            ..Default::default()
        };

        let surface = build_data_surface(&tools, &execution);

        assert_eq!(surface.sources.len(), 1);
        assert_eq!(surface.sources[0].source_type, TaintSourceType::EnvVariable);
        assert!(surface.sources[0].description.contains("API_KEY"));
    }

    #[test]
    fn test_sinks_from_commands() {
        let execution = ExecutionSurface {
            commands: vec![CommandInvocation {
                function: "subprocess.run".to_string(),
                command_arg: ArgumentSource::Parameter {
                    name: "cmd".to_string(),
                },
                location: make_location(5),
            }],
            ..Default::default()
        };

        let surface = build_data_surface(&[], &execution);

        assert_eq!(surface.sinks.len(), 1);
        assert_eq!(surface.sinks[0].sink_type, TaintSinkType::ProcessExec);
        assert!(surface.sinks[0].description.contains("subprocess.run"));
    }

    #[test]
    fn test_sinks_from_network_operations() {
        let execution = ExecutionSurface {
            network_operations: vec![NetworkOperation {
                function: "requests.get".to_string(),
                url_arg: ArgumentSource::Interpolated,
                method: Some("GET".to_string()),
                sends_data: false,
                location: make_location(8),
            }],
            ..Default::default()
        };

        let surface = build_data_surface(&[], &execution);

        assert_eq!(surface.sinks.len(), 1);
        assert_eq!(surface.sinks[0].sink_type, TaintSinkType::HttpRequest);
    }

    #[test]
    fn test_sinks_from_file_write_only() {
        let execution = ExecutionSurface {
            file_operations: vec![
                FileOperation {
                    operation: FileOpType::Read,
                    path_arg: ArgumentSource::Parameter {
                        name: "path".to_string(),
                    },
                    location: make_location(3),
                },
                FileOperation {
                    operation: FileOpType::Write,
                    path_arg: ArgumentSource::Parameter {
                        name: "out".to_string(),
                    },
                    location: make_location(7),
                },
            ],
            ..Default::default()
        };

        let surface = build_data_surface(&[], &execution);

        // Only the Write should produce a sink
        assert_eq!(surface.sinks.len(), 1);
        assert_eq!(surface.sinks[0].sink_type, TaintSinkType::FileWrite);
        assert_eq!(surface.sinks[0].location.line, 7);
    }

    #[test]
    fn test_sinks_from_dynamic_exec() {
        let execution = ExecutionSurface {
            dynamic_exec: vec![DynamicExec {
                function: "eval".to_string(),
                code_arg: ArgumentSource::Unknown,
                location: make_location(12),
            }],
            ..Default::default()
        };

        let surface = build_data_surface(&[], &execution);

        assert_eq!(surface.sinks.len(), 1);
        assert_eq!(surface.sinks[0].sink_type, TaintSinkType::DynamicEval);
    }

    #[test]
    fn test_taint_path_from_parameter_to_command() {
        let tools = vec![make_tool("exec_tool", &["command"])];
        let execution = ExecutionSurface {
            commands: vec![CommandInvocation {
                function: "subprocess.run".to_string(),
                command_arg: ArgumentSource::Parameter {
                    name: "command".to_string(),
                },
                location: make_location(10),
            }],
            ..Default::default()
        };

        let surface = build_data_surface(&tools, &execution);

        assert_eq!(surface.taint_paths.len(), 1);
        let path = &surface.taint_paths[0];
        assert_eq!(path.source.source_type, TaintSourceType::ToolArgument);
        assert!(path.source.description.contains("command"));
        assert_eq!(path.sink.sink_type, TaintSinkType::ProcessExec);
        assert!((path.confidence - 0.9).abs() < f32::EPSILON);
        assert!(path.through.is_empty());
    }

    #[test]
    fn test_no_taint_path_for_literal() {
        let execution = ExecutionSurface {
            commands: vec![CommandInvocation {
                function: "subprocess.run".to_string(),
                command_arg: ArgumentSource::Literal("ls -la".to_string()),
                location: make_location(5),
            }],
            ..Default::default()
        };

        let surface = build_data_surface(&[], &execution);

        // Sink should exist, but no taint path (literal is safe)
        assert_eq!(surface.sinks.len(), 1);
        assert!(
            surface.taint_paths.is_empty(),
            "literal args should not produce taint paths"
        );
    }

    #[test]
    fn test_sanitized_mismatched_sink_still_tainted() {
        // A `Sanitized` arg only suppresses a taint path when its sanitizer
        // matches the sink category. `validateCommand` is NOT a recognized
        // command sanitizer (the design only honors Path→FilePath and
        // Network→NetworkUrl), so the command sink stays tainted and a
        // taint path is still produced (issue #36).
        let execution = ExecutionSurface {
            commands: vec![CommandInvocation {
                function: "subprocess.run".to_string(),
                command_arg: ArgumentSource::Sanitized {
                    sanitizer: "validateCommand".to_string(),
                },
                location: make_location(5),
            }],
            ..Default::default()
        };

        let surface = build_data_surface(&[], &execution);

        assert_eq!(surface.sinks.len(), 1);
        assert!(
            !surface.taint_paths.is_empty(),
            "sanitizer that does not match the sink must not suppress the taint path"
        );
    }

    #[test]
    fn test_sanitized_matching_sink_suppresses_path() {
        // A path sanitizer on a FilePath (write) sink IS recognized, so the
        // taint path is suppressed.
        let execution = ExecutionSurface {
            file_operations: vec![FileOperation {
                operation: FileOpType::Write,
                path_arg: ArgumentSource::Sanitized {
                    sanitizer: "validatePath".to_string(),
                },
                location: make_location(5),
            }],
            ..Default::default()
        };

        let surface = build_data_surface(&[], &execution);

        assert!(
            surface.taint_paths.is_empty(),
            "matching sanitizer should suppress the taint path"
        );
    }

    #[test]
    fn test_interpolated_confidence() {
        let execution = ExecutionSurface {
            network_operations: vec![NetworkOperation {
                function: "requests.get".to_string(),
                url_arg: ArgumentSource::Interpolated,
                method: Some("GET".to_string()),
                sends_data: false,
                location: make_location(15),
            }],
            ..Default::default()
        };

        let surface = build_data_surface(&[], &execution);

        assert_eq!(surface.taint_paths.len(), 1);
        assert!((surface.taint_paths[0].confidence - 0.8).abs() < f32::EPSILON);
    }

    #[test]
    fn test_envvar_confidence() {
        let execution = ExecutionSurface {
            commands: vec![CommandInvocation {
                function: "os.system".to_string(),
                command_arg: ArgumentSource::EnvVar {
                    name: "CMD".to_string(),
                },
                location: make_location(3),
            }],
            ..Default::default()
        };

        let surface = build_data_surface(&[], &execution);

        assert_eq!(surface.taint_paths.len(), 1);
        assert!((surface.taint_paths[0].confidence - 0.7).abs() < f32::EPSILON);
    }

    #[test]
    fn test_unknown_confidence() {
        let execution = ExecutionSurface {
            dynamic_exec: vec![DynamicExec {
                function: "eval".to_string(),
                code_arg: ArgumentSource::Unknown,
                location: make_location(20),
            }],
            ..Default::default()
        };

        let surface = build_data_surface(&[], &execution);

        assert_eq!(surface.taint_paths.len(), 1);
        assert!((surface.taint_paths[0].confidence - 0.5).abs() < f32::EPSILON);
    }

    #[test]
    fn test_tool_without_schema_produces_no_sources() {
        let tools = vec![ToolSurface {
            name: "no_schema".to_string(),
            description: None,
            input_schema: None,
            output_schema: None,
            declared_permissions: vec![],
            defined_at: None,
            declared_capabilities: Default::default(),
            capability_declarations: Vec::new(),
            observed_capabilities: Default::default(),
            capability_observation_complete: false,
            capability_evidence: Vec::new(),
        }];

        let surface = build_data_surface(&tools, &ExecutionSurface::default());

        assert!(surface.sources.is_empty());
        assert!(surface.sinks.is_empty());
        assert!(surface.taint_paths.is_empty());
    }

    #[test]
    fn test_combined_sources_sinks_paths() {
        let tools = vec![make_tool("fetch", &["url"])];
        let execution = ExecutionSurface {
            commands: vec![CommandInvocation {
                function: "subprocess.run".to_string(),
                command_arg: ArgumentSource::Literal("echo hi".to_string()),
                location: make_location(5),
            }],
            network_operations: vec![NetworkOperation {
                function: "requests.get".to_string(),
                url_arg: ArgumentSource::Parameter {
                    name: "url".to_string(),
                },
                method: Some("GET".to_string()),
                sends_data: false,
                location: make_location(10),
            }],
            env_accesses: vec![EnvAccess {
                var_name: ArgumentSource::Literal("TOKEN".to_string()),
                is_sensitive: true,
                location: make_location(2),
            }],
            ..Default::default()
        };

        let surface = build_data_surface(&tools, &execution);

        // 1 tool param source + 1 env source = 2 sources
        assert_eq!(surface.sources.len(), 2);
        // 1 command sink + 1 network sink = 2 sinks
        assert_eq!(surface.sinks.len(), 2);
        // Only network op is tainted (command is literal) = 1 path
        assert_eq!(surface.taint_paths.len(), 1);
        assert_eq!(
            surface.taint_paths[0].sink.sink_type,
            TaintSinkType::HttpRequest
        );
    }

    #[test]
    fn test_data_surface_from_vuln_fixture() {
        use crate::adapter::Adapter;

        let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("tests/fixtures/mcp_servers/vuln_cmd_inject");
        let adapter = crate::adapter::mcp::McpAdapter;
        let targets = adapter.load(&dir, false).unwrap();
        assert_eq!(targets.len(), 1);

        let target = &targets[0];

        // The vuln_cmd_inject fixture has tainted commands, so DataSurface should be populated
        assert!(
            !target.data.sinks.is_empty(),
            "vuln_cmd_inject should produce taint sinks"
        );

        // Should have ProcessExec sinks from subprocess calls
        assert!(
            target
                .data
                .sinks
                .iter()
                .any(|s| s.sink_type == TaintSinkType::ProcessExec),
            "expected ProcessExec sink from subprocess usage"
        );

        // Should have taint paths connecting tainted args to sinks
        assert!(
            !target.data.taint_paths.is_empty(),
            "vuln_cmd_inject should produce taint paths from parameter to subprocess"
        );

        // At least one path should have high confidence (parameter source)
        assert!(
            target.data.taint_paths.iter().any(|p| p.confidence >= 0.8),
            "expected high-confidence taint path"
        );
    }
}