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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
use std::path::PathBuf;
use std::process;
#[path = "cli/discover.rs"]
mod discover;
#[path = "../discovery/mod.rs"]
mod discovery;
#[path = "cli/fix.rs"]
mod fix;
#[path = "cli/reporting.rs"]
mod reporting;
#[path = "cli/rules.rs"]
mod rules;
#[path = "cli/runtime.rs"]
mod runtime;
#[path = "cli/scan.rs"]
mod scan;
#[path = "cli/setup.rs"]
mod setup;
use clap::{Parser, Subcommand};
use discover::cmd_discover;
use reporting::{cmd_certify, cmd_list_suppressions, cmd_suppress};
use rules::cmd_list_rules;
#[cfg(all(feature = "runtime-guard", feature = "runtime"))]
use runtime::cmd_guard_http_sse;
#[cfg(feature = "runtime")]
use runtime::cmd_wrap;
#[cfg(feature = "runtime-guard")]
use runtime::{cmd_guard, cmd_mcp_proxy, cmd_mcp_proxy_transport};
use scan::{ScanArgs, cmd_scan};
use setup::{CiInstallRequest, cmd_ci_install, cmd_doctor, cmd_init, cmd_quickstart};
#[derive(Parser)]
#[command(
name = "agentshield",
about = "Security scanner for AI agent extensions (MCP, OpenClaw, Hermes Agent, CrewAI, LangChain, GPT Actions, Cursor Rules)",
long_about = "AgentShield scans AI agent extensions for security vulnerabilities.\n\n\
It detects command injection, credential exfiltration, SSRF, arbitrary \
file access, supply chain issues, and more. Results can be output as \
console text, JSON, SARIF (GitHub Code Scanning), or HTML reports.",
version,
author
)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Discover allowlisted local client configurations without scanning them
Discover {
/// Do not inspect known configuration paths under the effective profile
#[arg(long)]
no_default_paths: bool,
/// Inspect allowlisted configuration layouts under this explicit root
#[arg(long, value_name = "PATH")]
root: Vec<PathBuf>,
/// Output format (console, json)
#[arg(long, short = 'f', default_value = "console")]
format: String,
/// Explain paths, limits, and symlink policy before reading
#[arg(long)]
explain: bool,
},
/// Scan an agent extension for security issues
Scan {
/// Path to the extension directory
#[arg(default_value = ".")]
path: PathBuf,
/// Config file path
#[arg(long, short = 'c')]
config: Option<PathBuf>,
/// Output format (console, json, sarif, html)
#[arg(long, short = 'f', default_value = "console")]
format: String,
/// Minimum severity to fail (info, low, medium, high, critical)
#[arg(long)]
fail_on: Option<String>,
/// Write output to file instead of stdout
#[arg(long, short = 'o')]
output: Option<PathBuf>,
/// Skip test files (test/, tests/, __tests__/, *.test.ts, *.spec.ts, etc.)
#[arg(long)]
ignore_tests: bool,
/// Filter out findings that match a previously written baseline file
#[arg(long, value_name = "PATH")]
baseline: Option<PathBuf>,
/// Extra include patterns for scan path filtering (repeatable).
#[arg(long, value_name = "PATTERN")]
include: Vec<String>,
/// Extra exclude patterns for scan path filtering (repeatable).
#[arg(long, value_name = "PATTERN")]
exclude: Vec<String>,
/// Write all current findings as a baseline file
#[arg(long, value_name = "PATH")]
write_baseline: Option<PathBuf>,
/// Analyze scan results and emit a starter egress policy to the given path
#[arg(long, value_name = "PATH")]
emit_egress_policy: Option<PathBuf>,
/// Explain the gate, coverage, confidence, grouped findings, and next actions.
///
/// This is console-only so JSON, SARIF, and HTML output contracts stay stable.
#[arg(long)]
explain: bool,
/// Add an experimental informational risk index (console or JSON only)
#[arg(long)]
experimental_risk: bool,
/// Directory containing custom declarative rules (*.yaml, *.yml, *.json)
#[arg(long, value_name = "PATH")]
rules_dir: Option<PathBuf>,
},
/// First-run setup: create config, inspect coverage, and run an explained scan
Quickstart {
/// Project directory to initialize and scan
#[arg(default_value = ".")]
path: PathBuf,
/// Overwrite an existing .agentshield.toml
#[arg(long)]
force: bool,
/// Minimum severity to fail (info, low, medium, high, critical)
#[arg(long, default_value = "high")]
fail_on: String,
/// Include test files in the first scan and generated config
#[arg(long)]
include_tests: bool,
},
/// Install CI integration helpers
Ci {
#[command(subcommand)]
command: CiCommand,
},
/// List all available detection rules
ListRules {
/// Output format (table, json)
#[arg(long, short = 'f', default_value = "table")]
format: String,
/// Optional directory containing custom declarative rules (*.yaml, *.yml, *.json)
#[arg(long, value_name = "PATH")]
rules_dir: Option<PathBuf>,
},
/// Generate a starter .agentshield.toml config file
Init {
/// Overwrite existing config file
#[arg(long)]
force: bool,
},
/// Add a suppression entry to .agentshield.toml for a specific finding
Suppress {
/// SHA-256 fingerprint of the finding to suppress (from --format json output)
fingerprint: String,
/// Mandatory reason explaining why this finding is suppressed
#[arg(long, short = 'r')]
reason: String,
/// Optional expiry date in YYYY-MM-DD format
#[arg(long, short = 'e')]
expires: Option<String>,
/// Config file path (defaults to .agentshield.toml in the current directory)
#[arg(long, short = 'c')]
config: Option<PathBuf>,
},
/// List all suppressions in .agentshield.toml
ListSuppressions {
/// Config file path (defaults to .agentshield.toml in the current directory)
#[arg(long, short = 'c')]
config: Option<PathBuf>,
},
/// Print environment and configuration diagnostics
Doctor {
/// Path to inspect
#[arg(default_value = ".")]
path: PathBuf,
/// Config file path
#[arg(long, short = 'c')]
config: Option<PathBuf>,
/// Emit diagnostics as JSON
#[arg(long)]
json: bool,
/// Skip test files in effective scan settings
#[arg(long)]
ignore_tests: bool,
},
/// Evaluate one runtime event from stdin as JSON or start an MCP proxy guard
#[cfg(feature = "runtime-guard")]
Guard {
/// Read a RuntimeEvent JSON document from stdin
#[arg(long)]
stdin: bool,
/// EXPERIMENTAL: run an MCP JSON-RPC proxy guard. Reads JSON-RPC
/// messages (one per line) on stdin, evaluates `tools/call` against
/// runtime policy, and emits a forward marker or a block error.
#[arg(long)]
mcp_proxy: bool,
/// Listen address for HTTP/SSE MCP proxy guard (e.g. 127.0.0.1:8080)
#[arg(long)]
listen: Option<String>,
/// Target URL of the upstream HTTP/SSE MCP server (e.g. http://127.0.0.1:3000)
#[arg(long)]
target: Option<String>,
/// Optional audit log path for HTTP/SSE proxy requests
#[arg(long)]
audit_log: Option<std::path::PathBuf>,
/// Path to .agentshield.toml for the `[runtime.proxy]` policy
/// (defaults to ./.agentshield.toml).
#[arg(long)]
config: Option<std::path::PathBuf>,
/// EXPERIMENTAL: with --mcp-proxy, the command after `--` is spawned as
/// the downstream MCP server and stdio is bridged both ways. Forwarded
/// `tools/call` requests reach the server and its real response is
/// returned; blocked ones are not forwarded. With no command, the proxy
/// runs in line mode (emits `{"forward": ...}` envelopes).
#[arg(last = true)]
server: Vec<String>,
},
/// Automatically fix deterministic security issues (unsafe deserializers, unpinned dependencies)
Fix {
/// Path to the extension directory or file
#[arg(default_value = ".")]
path: PathBuf,
/// Preview proposed fixes as a unified diff without writing to disk
#[arg(long)]
dry_run: bool,
/// Limit fixes to specific rule IDs (e.g., SHIELD-016, SHIELD-009)
#[arg(long, value_name = "RULE")]
rules: Vec<String>,
},
/// Generate a DSSE attestation envelope for scan results
Certify {
/// Path to the extension directory
#[arg(default_value = ".")]
path: PathBuf,
/// Path to Ed25519 private key file (32 bytes, raw binary)
#[arg(long)]
sign_key: Option<PathBuf>,
/// Write output to file instead of stdout
#[arg(short, long)]
output: Option<PathBuf>,
/// Config file path
#[arg(long, short = 'c')]
config: Option<PathBuf>,
/// Skip test files
#[arg(long)]
ignore_tests: bool,
},
/// Enforce egress policy on a command via a local HTTP proxy
#[cfg(feature = "runtime")]
Wrap {
/// Path to egress policy file (agentshield.egress.toml)
#[arg(long, value_name = "PATH")]
policy: PathBuf,
/// Path to an operator override policy (same format). Can only restrict, never expand.
#[arg(long = "override-policy", value_name = "PATH")]
override_policy: Option<PathBuf>,
/// Audit log output path (overrides policy config)
#[arg(long, value_name = "PATH")]
audit_log: Option<PathBuf>,
/// The command to wrap (use -- before the command)
#[arg(last = true, required = true)]
command: Vec<String>,
},
}
#[derive(Subcommand)]
enum CiCommand {
/// Generate a GitHub Actions workflow for AgentShield
Install {
/// Workflow output path
#[arg(long, short = 'o', default_value = ".github/workflows/agentshield.yml")]
output: PathBuf,
/// Overwrite an existing workflow file
#[arg(long)]
force: bool,
/// Repository path to scan in CI
#[arg(long, default_value = ".")]
scan_path: String,
/// Minimum severity to fail (info, low, medium, high, critical)
#[arg(long, default_value = "high")]
fail_on: String,
/// Include test files in CI scans
#[arg(long)]
include_tests: bool,
/// Baseline file to filter existing findings in CI
#[arg(long, value_name = "PATH")]
baseline: Option<String>,
/// Disable AgentShield SARIF upload in the generated workflow
#[arg(long)]
no_sarif: bool,
/// Generate a broader security suite with CodeQL, Gitleaks, Semgrep CE, and AgentShield
#[arg(long)]
suite: bool,
},
}
fn main() {
let cli = Cli::parse();
let result = match cli.command {
Commands::Discover {
no_default_paths,
root,
format,
explain,
} => cmd_discover(!no_default_paths, root, format, explain),
Commands::Scan {
path,
config,
format,
fail_on,
output,
ignore_tests,
baseline,
include: include_patterns,
exclude: exclude_patterns,
write_baseline,
emit_egress_policy,
explain,
experimental_risk,
rules_dir,
} => cmd_scan(ScanArgs {
path,
config,
format_str: format,
fail_on_str: fail_on,
output_path: output,
ignore_tests,
baseline_path: baseline,
include_patterns,
exclude_patterns,
write_baseline_path: write_baseline,
emit_egress_policy_path: emit_egress_policy,
explain,
experimental_risk,
rules_dir,
}),
Commands::Quickstart {
path,
force,
fail_on,
include_tests,
} => cmd_quickstart(path, force, fail_on, include_tests),
Commands::Ci { command } => match command {
CiCommand::Install {
output,
force,
scan_path,
fail_on,
include_tests,
baseline,
no_sarif,
suite,
} => cmd_ci_install(CiInstallRequest {
output,
force,
scan_path,
fail_on,
include_tests,
baseline,
upload_sarif: !no_sarif,
suite,
}),
},
Commands::ListRules { format, rules_dir } => cmd_list_rules(format, rules_dir),
Commands::Init { force } => cmd_init(force),
Commands::Suppress {
fingerprint,
reason,
expires,
config,
} => cmd_suppress(fingerprint, reason, expires, config),
Commands::ListSuppressions { config } => cmd_list_suppressions(config),
Commands::Doctor {
path,
config,
json,
ignore_tests,
} => cmd_doctor(path, config, json, ignore_tests),
Commands::Fix {
path,
dry_run,
rules,
} => fix::cmd_fix(fix::FixArgs {
path,
dry_run,
rules,
}),
#[cfg(feature = "runtime-guard")]
Commands::Guard {
stdin,
mcp_proxy,
listen,
target,
audit_log,
config,
server,
} => {
if let (Some(listen_addr), Some(target_url)) = (listen, target) {
#[cfg(feature = "runtime")]
{
cmd_guard_http_sse(listen_addr, target_url, config, audit_log)
}
#[cfg(not(feature = "runtime"))]
{
let _ = (listen_addr, target_url, config, audit_log);
Err(agentshield::error::ShieldError::Config(
"HTTP/SSE proxy requires compiling with feature 'runtime' or 'full'".into(),
))
}
} else if mcp_proxy {
if server.is_empty() {
cmd_mcp_proxy(config)
} else {
cmd_mcp_proxy_transport(config, server)
}
} else {
cmd_guard(stdin)
}
}
Commands::Certify {
path,
sign_key,
output,
config,
ignore_tests,
} => cmd_certify(path, sign_key, output, config, ignore_tests),
#[cfg(feature = "runtime")]
Commands::Wrap {
policy,
override_policy,
audit_log,
command,
} => cmd_wrap(policy, override_policy, audit_log, command),
};
match result {
Ok(exit_code) => process::exit(exit_code),
Err(e) => {
eprintln!("Error: {}", e);
process::exit(e.exit_code());
}
}
}