oxidized-agentic-audit 0.6.0

Security scanning for AI agent skills — scans skill directories for dangerous bash patterns, prompt injection, supply chain risks, secret leakage, and frontmatter quality issues
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
mod cli;

use clap::Parser;
use cli::{Cli, Commands, RuleMode, ScanType};
use colored::Colorize;
use oxidized_agentic_audit::{
    config,
    finding::ScanReport,
    output,
    scan::{self, ScanMode},
    scanners,
};

fn main() {
    let cli = Cli::parse();

    match cli.command {
        Commands::Scan {
            path,
            scan_type,
            format,
            output: output_path,
            strict,
            config: config_path,
            min_score,
        } => {
            let (mode, sentinel, entity, article) = scan_type_meta(scan_type);
            if !path.exists() {
                eprintln!("Error: path does not exist: {}", path.display());
                std::process::exit(2);
            }

            // Detect collection directories early to give a helpful error rather
            // than a confusing sentinel-file-not-found failure on every scanner.
            let children = find_artifact_dirs(&path, sentinel);
            if !path.join(sentinel).exists() && !children.is_empty() {
                eprintln!(
                    "Error: '{}' looks like {} {}s collection directory, not a single {}.",
                    path.display(),
                    article,
                    entity,
                    entity
                );
                eprintln!();
                eprintln!("To scan all {}s at once:", entity);
                eprintln!(
                    "  oxidized-agentic-audit scan-all --type {} {}",
                    entity,
                    path.display()
                );
                eprintln!();
                eprintln!("To scan a specific {}:", entity);
                for child in &children {
                    eprintln!(
                        "  oxidized-agentic-audit scan --type {} {}",
                        entity,
                        child.display()
                    );
                }
                std::process::exit(2);
            }

            let mut config = config::Config::load(config_path.as_deref()).unwrap_or_else(|e| {
                eprintln!("Error: {e}");
                std::process::exit(2);
            });

            if strict {
                config.strict.enabled = true;
            }

            let report = scan::run_scan(&path, &config, mode);
            let formatted = output::format_report(&report, &format);

            if let Some(out_path) = output_path {
                std::fs::write(&out_path, &formatted).unwrap_or_else(|e| {
                    eprintln!("Error writing output: {e}");
                    std::process::exit(2);
                });
                eprintln!("Output written to {}", out_path.display());
            } else {
                print!("{formatted}");
            }

            if let Some(min) = min_score {
                if report.security_score < min {
                    eprintln!(
                        "Error: security score {}/100 is below the required minimum of {}",
                        report.security_score, min
                    );
                    std::process::exit(1);
                }
            }

            std::process::exit(if report.passed { 0 } else { 1 });
        }

        Commands::ScanAll {
            path,
            scan_type,
            format,
            strict,
            config: config_path,
            min_score,
        } => {
            if !path.exists() {
                eprintln!("Error: path does not exist: {}", path.display());
                std::process::exit(2);
            }

            let (mode, sentinel, entity, _article) = scan_type_meta(scan_type);

            let dirs = find_artifact_dirs(&path, sentinel);
            if dirs.is_empty() {
                eprintln!(
                    "Error: no {} directories found in '{}' (no subdirectory contains a {})",
                    entity,
                    path.display(),
                    sentinel,
                );
                std::process::exit(2);
            }

            let mut config = config::Config::load(config_path.as_deref()).unwrap_or_else(|e| {
                eprintln!("Error: {e}");
                std::process::exit(2);
            });

            if strict {
                config.strict.enabled = true;
            }

            let mut reports: Vec<ScanReport> = Vec::new();
            for dir in &dirs {
                let report = scan::run_scan(dir, &config, mode);
                let formatted = output::format_report(&report, &format);
                print!("{formatted}");
                reports.push(report);
            }

            if matches!(format, output::OutputFormat::Pretty) {
                print!(
                    "{}",
                    format_collection_summary(&path, &reports, min_score, entity)
                );
            }

            let all_passed = reports.iter().all(|r| r.passed);
            let all_above_min = min_score
                .map(|min| reports.iter().all(|r| r.security_score >= min))
                .unwrap_or(true);
            std::process::exit(if all_passed && all_above_min { 0 } else { 1 });
        }

        Commands::CheckTools => {
            println!("{}", "Scanner Availability".bold().underline());
            println!();

            // Deduplicate across skill and agent scanner sets so shared scanners
            // appear once and both frontmatter variants are shown.
            let mut seen = std::collections::HashSet::new();
            let combined: Vec<Box<dyn scanners::Scanner>> = scanners::skill_scanners()
                .into_iter()
                .chain(scanners::agent_scanners())
                .filter(|s| seen.insert(s.name().to_string()))
                .collect();

            for scanner in &combined {
                let status = if scanner.is_available() {
                    "READY".green().bold().to_string()
                } else {
                    "NOT AVAILABLE".red().to_string()
                };

                println!(
                    "  [{status}] {name:<20} {desc}",
                    name = scanner.name(),
                    desc = scanner.description(),
                );
            }

            println!();
            println!(
                "Note: Core scanners (bash_patterns, prompt, package_install) require no external tools."
            );
        }

        Commands::ListRules { mode } => {
            let rules = match mode {
                RuleMode::Skill => scanners::all_rules(),
                RuleMode::Agent => scanners::all_agent_rules(),
                RuleMode::All => scanners::all_unique_rules(),
            };

            let mode_label = match mode {
                RuleMode::Skill => "Skill",
                RuleMode::Agent => "Agent",
                RuleMode::All => "All",
            };

            println!(
                "{}",
                format!("Built-in Rules ({mode_label})").bold().underline()
            );
            println!();

            let mut current_scanner = "";
            for rule in &rules {
                if rule.scanner != current_scanner {
                    if !current_scanner.is_empty() {
                        println!();
                    }
                    println!("  {}", rule.scanner.bold());
                    current_scanner = rule.scanner;
                }

                let severity = match rule.severity {
                    "error" => "ERROR".red().bold().to_string(),
                    "warning" => " WARN".yellow().bold().to_string(),
                    "info" => " INFO".blue().to_string(),
                    _ => rule.severity.to_string(),
                };

                println!(
                    "    [{severity}] {id:<40} {message}",
                    id = rule.id,
                    message = rule.message,
                );
            }

            println!();
            println!("  Total: {} rules", rules.len());
        }

        Commands::Explain { rule_id, mode } => {
            let rules = match mode {
                RuleMode::Skill => scanners::all_rules(),
                RuleMode::Agent => scanners::all_agent_rules(),
                RuleMode::All => scanners::all_unique_rules(),
            };
            match rules.iter().find(|r| r.id == rule_id) {
                Some(rule) => {
                    println!("{}", rule.id.bold());
                    println!();
                    println!("  Scanner:      {}", rule.scanner);
                    println!("  Severity:     {}", rule.severity);
                    println!("  Description:  {}", rule.message);
                    println!("  Remediation:  {}", rule.remediation);
                }
                None => {
                    eprintln!("Unknown rule: {rule_id}");
                    eprintln!(
                        "Use 'oxidized-agentic-audit list-rules --mode all' to see all available rules."
                    );
                    std::process::exit(2);
                }
            }
        }
    }
}

/// Maps a [`ScanType`] to its scan mode, sentinel filename, entity label, and article.
///
/// Returns `(ScanMode, sentinel, entity_label, article)` where `article` is
/// the grammatically correct indefinite article ("a" or "an") for the entity.
fn scan_type_meta(scan_type: ScanType) -> (ScanMode, &'static str, &'static str, &'static str) {
    match scan_type {
        ScanType::Skill => (ScanMode::Skill, "SKILL.md", "skill", "a"),
        ScanType::Agent => (ScanMode::Agent, "AGENT.md", "agent", "an"),
    }
}

/// Returns immediate child directories of `path` that contain a file named `sentinel`,
/// sorted alphabetically by directory name.
fn find_artifact_dirs(path: &std::path::Path, sentinel: &str) -> Vec<std::path::PathBuf> {
    let Ok(entries) = std::fs::read_dir(path) else {
        return vec![];
    };

    let mut dirs: Vec<std::path::PathBuf> = entries
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().map(|t| t.is_dir()).unwrap_or(false))
        .map(|e| e.path())
        .filter(|p| p.join(sentinel).exists())
        .collect();

    dirs.sort();
    dirs
}

/// Renders a compact summary table after all individual reports have been printed.
///
/// `entity_label` should be `"skill"` or `"agent"` to customise the summary header.
/// When `min_score` is `Some(n)`, rows whose score is below `n` are annotated
/// with a red `[< N]` marker so failures are immediately visible.
fn format_collection_summary(
    collection_path: &std::path::Path,
    reports: &[ScanReport],
    min_score: Option<u8>,
    entity_label: &str,
) -> String {
    use oxidized_agentic_audit::finding::ScanStatus;

    let mut out = String::new();
    let separator = "".repeat(66);

    out.push('\n');
    out.push_str(&format!(
        "{}\n",
        format!(
            "  Collection Summary: {}  ({} {}s)",
            collection_path.display(),
            reports.len(),
            entity_label,
        )
        .bold()
        .underline()
    ));
    out.push_str(&format!("{}\n", separator.dimmed()));

    let mut n_failed = 0usize;
    let mut n_warned = 0usize;
    let mut n_passed = 0usize;
    let mut n_below_min = 0usize;

    for report in reports {
        let (icon, status_str) = match report.status {
            ScanStatus::Passed => {
                n_passed += 1;
                (
                    "".green().to_string(),
                    "PASSED ".green().bold().to_string(),
                )
            }
            ScanStatus::Warning => {
                n_warned += 1;
                (
                    "".yellow().to_string(),
                    "WARNING".yellow().bold().to_string(),
                )
            }
            ScanStatus::Failed => {
                n_failed += 1;
                ("".red().to_string(), "FAILED ".red().bold().to_string())
            }
        };

        let score_col = {
            let s = format!(
                "{:>3}/100 ({})",
                report.security_score, report.security_grade
            );
            match report.security_score {
                90..=100 => s.green().bold().to_string(),
                60..=89 => s.yellow().bold().to_string(),
                _ => s.red().bold().to_string(),
            }
        };

        let min_score_marker = match min_score {
            Some(min) if report.security_score < min => {
                n_below_min += 1;
                format!(" {}", format!("[< {min}]").red().bold())
            }
            _ => String::new(),
        };

        let (errors, warnings, info) = report.count_by_severity();
        out.push_str(&format!(
            "  {icon}  {name:<22} {status}  {score}  {e} err, {w} warn, {i} info{marker}\n",
            name = report.skill,
            status = status_str,
            score = score_col,
            e = errors,
            w = warnings,
            i = info,
            marker = min_score_marker,
        ));
    }

    out.push_str(&format!("{}\n", separator.dimmed()));

    let mut footer = format!(
        "  Total: {}  {}  {}",
        format!("{} failed", n_failed).red().bold(),
        format!("{} warnings", n_warned).yellow().bold(),
        format!("{} passed", n_passed).green().bold(),
    );
    if let Some(min) = min_score {
        if n_below_min > 0 {
            footer.push_str(&format!(
                "  {}",
                format!("{} below min-score ({})", n_below_min, min)
                    .red()
                    .bold()
            ));
        }
    }
    footer.push('\n');
    out.push_str(&footer);

    out
}