foxguard 0.12.0

A security scanner as fast as a linter, written in Rust. 200+ built-in rules across 12 source languages.
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
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
use foxguard::app::{execute_diff, execute_scan, execute_secrets, DiffExecution, ScanExecution};
use foxguard::cli::{
    ChangeModeArgs, DiffArgs, OutputFormat, ScanArgs, SecretsArgs, SeverityFilter,
};
use foxguard::engine::ScanStats;
use foxguard::report::{cbom::build_cbom, sarif::build_sarif};
use foxguard::rules::RuleRegistry;
use serde_json::{json, Value};
use std::io::{self, BufRead, Write};
use std::path::Path;

const DEFAULT_MAX_FILE_SIZE: u64 = 1_048_576;

fn main() {
    let registry = RuleRegistry::new();
    let stdin = io::stdin();
    let stdout = io::stdout();

    for line in stdin.lock().lines() {
        let line = match line {
            Ok(l) => l,
            Err(_) => break,
        };

        if line.trim().is_empty() {
            continue;
        }

        let request: Value = match serde_json::from_str(&line) {
            Ok(v) => v,
            Err(e) => {
                let error_response = json!({
                    "jsonrpc": "2.0",
                    "id": null,
                    "error": {
                        "code": -32700,
                        "message": format!("Parse error: {}", e)
                    }
                });
                let mut out = stdout.lock();
                let _ = writeln!(out, "{}", error_response);
                let _ = out.flush();
                continue;
            }
        };

        let response = handle_request(&request, &registry);

        if let Some(resp) = response {
            let mut out = stdout.lock();
            let _ = writeln!(out, "{}", resp);
            let _ = out.flush();
        }
    }
}

fn handle_request(request: &Value, registry: &RuleRegistry) -> Option<Value> {
    let method = request.get("method")?.as_str()?;
    let id = request.get("id");

    // Notifications (no id) are handled silently
    let id = id?.clone();

    let result = match method {
        "initialize" => handle_initialize(),
        "tools/list" => handle_tools_list(),
        "tools/call" => handle_tools_call(request, registry),
        _ => {
            return Some(json!({
                "jsonrpc": "2.0",
                "id": id,
                "error": {
                    "code": -32601,
                    "message": format!("Method not found: {}", method)
                }
            }));
        }
    };

    match result {
        Ok(value) => Some(json!({
            "jsonrpc": "2.0",
            "id": id,
            "result": value
        })),
        Err(err) => Some(json!({
            "jsonrpc": "2.0",
            "id": id,
            "error": {
                "code": -32602,
                "message": err
            }
        })),
    }
}

fn handle_initialize() -> Result<Value, String> {
    Ok(json!({
        "protocolVersion": "2024-11-05",
        "capabilities": { "tools": {} },
        "serverInfo": {
            "name": "foxguard",
            "version": env!("CARGO_PKG_VERSION")
        }
    }))
}

fn handle_tools_list() -> Result<Value, String> {
    Ok(json!({
        "tools": [
            {
                "name": "scan_file",
                "description": "Run foxguard security scan on a single file",
                "inputSchema": scan_input_schema(["path"], "Absolute path to the file to scan")
            },
            {
                "name": "scan_directory",
                "description": "Run foxguard security scan on a directory",
                "inputSchema": scan_input_schema(["path"], "Absolute path to the directory to scan")
            },
            {
                "name": "scan_pqc",
                "description": "Run foxguard post-quantum crypto audit rules on a file or directory",
                "inputSchema": scan_input_schema(["path"], "Absolute path to the file or directory to scan")
            },
            {
                "name": "scan_secrets",
                "description": "Run foxguard secrets scanning on a file or directory",
                "inputSchema": secrets_input_schema()
            },
            {
                "name": "scan_diff",
                "description": "Run foxguard diff mode and return findings new against a target branch",
                "inputSchema": diff_input_schema()
            },
            {
                "name": "emit_sarif",
                "description": "Run a scan and return SARIF JSON",
                "inputSchema": scan_input_schema(["path"], "Absolute path to the file or directory to scan")
            },
            {
                "name": "emit_cbom",
                "description": "Run a PQC scan and return CycloneDX CBOM JSON",
                "inputSchema": scan_input_schema(["path"], "Absolute path to the file or directory to scan")
            },
            {
                "name": "list_rules",
                "description": "List built-in foxguard rules and rule metadata",
                "inputSchema": {
                    "type": "object",
                    "properties": {},
                    "required": []
                }
            },
            {
                "name": "explain_finding",
                "description": "Summarize a foxguard finding, including source-to-sink trace metadata when present",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "finding": {
                            "type": "object",
                            "description": "A finding object returned by another foxguard MCP tool"
                        }
                    },
                    "required": ["finding"]
                }
            },
            {
                "name": "suggest_suppression",
                "description": "Return inline and config suppression snippets for an accepted finding",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "rule_id": {
                            "type": "string",
                            "description": "Rule ID to suppress"
                        },
                        "path": {
                            "type": "string",
                            "description": "Optional repository-relative path for config suppression"
                        }
                    },
                    "required": ["rule_id"]
                }
            }
        ]
    }))
}

fn scan_input_schema<const N: usize>(required: [&str; N], path_description: &str) -> Value {
    let required = required.to_vec();

    json!({
        "type": "object",
        "properties": {
            "path": {
                "type": "string",
                "description": path_description
            },
            "config": {
                "type": "string",
                "description": "Optional foxguard config path"
            },
            "severity": {
                "type": "string",
                "enum": ["low", "medium", "high", "critical"],
                "description": "Minimum severity to report"
            },
            "min_confidence": {
                "type": "number",
                "minimum": 0.0,
                "maximum": 1.0,
                "description": "Minimum confidence threshold"
            },
            "max_file_size": {
                "type": "integer",
                "minimum": 1,
                "description": "Maximum file size in bytes"
            },
            "exclude": {
                "type": "array",
                "items": { "type": "string" },
                "description": "Scan-relative path prefixes or globs to exclude"
            },
            "rules": {
                "type": "string",
                "description": "Optional external rule file or directory"
            },
            "baseline": {
                "type": "string",
                "description": "Optional baseline file to apply"
            },
            "changed": {
                "type": "boolean",
                "description": "Scan changed files only"
            },
            "staged": {
                "type": "boolean",
                "description": "Scan staged files only"
            },
            "unstaged": {
                "type": "boolean",
                "description": "Scan unstaged and untracked files only"
            },
            "all_changes": {
                "type": "boolean",
                "description": "Scan staged, unstaged, and untracked changes"
            },
            "explain": {
                "type": "boolean",
                "description": "Include dataflow trace fields where available"
            }
        },
        "required": required
    })
}

fn secrets_input_schema() -> Value {
    json!({
        "type": "object",
        "properties": {
            "path": {
                "type": "string",
                "description": "Absolute path to the file or directory to scan"
            },
            "config": {
                "type": "string",
                "description": "Optional foxguard config path"
            },
            "baseline": {
                "type": "string",
                "description": "Optional secrets baseline file to apply"
            },
            "exclude_paths": {
                "type": "array",
                "items": { "type": "string" },
                "description": "File or directory prefixes to exclude"
            },
            "ignored_rules": {
                "type": "array",
                "items": { "type": "string" },
                "description": "Secret rule IDs to ignore"
            },
            "max_file_size": {
                "type": "integer",
                "minimum": 1,
                "description": "Maximum file size in bytes"
            },
            "changed": { "type": "boolean" },
            "staged": { "type": "boolean" },
            "unstaged": { "type": "boolean" },
            "all_changes": { "type": "boolean" }
        },
        "required": ["path"]
    })
}

fn diff_input_schema() -> Value {
    let mut schema = scan_input_schema(
        ["path", "target"],
        "Absolute path to the repository to scan",
    );
    if let Some(properties) = schema.get_mut("properties").and_then(Value::as_object_mut) {
        properties.insert(
            "target".to_string(),
            json!({
                "type": "string",
                "description": "Target branch or revision to compare against"
            }),
        );
    }
    schema
}

fn handle_tools_call(request: &Value, registry: &RuleRegistry) -> Result<Value, String> {
    let params = request
        .get("params")
        .ok_or_else(|| "Missing params".to_string())?;

    let tool_name = params
        .get("name")
        .and_then(|v| v.as_str())
        .ok_or_else(|| "Missing tool name".to_string())?;

    let empty_arguments = json!({});
    let arguments = params.get("arguments").unwrap_or(&empty_arguments);

    match tool_name {
        "scan_file" => {
            validate_path(arguments, PathExpectation::File)?;
            let execution = execute_scan(&scan_args(arguments, false)?)?;
            Ok(tool_result(scan_execution_value("scan_file", execution)))
        }
        "scan_directory" => {
            validate_path(arguments, PathExpectation::Directory)?;
            let execution = execute_scan(&scan_args(arguments, false)?)?;
            Ok(tool_result(scan_execution_value(
                "scan_directory",
                execution,
            )))
        }
        "scan_pqc" => {
            validate_path(arguments, PathExpectation::Any)?;
            let execution = execute_scan(&scan_args(arguments, true)?)?;
            Ok(tool_result(scan_execution_value("scan_pqc", execution)))
        }
        "scan_secrets" => {
            validate_path(arguments, PathExpectation::Any)?;
            let execution = execute_secrets(&secrets_args(arguments)?)?;
            Ok(tool_result(secrets_execution_value(execution)))
        }
        "scan_diff" => {
            validate_path(arguments, PathExpectation::Directory)?;
            let execution = execute_diff(&diff_args(arguments)?)?;
            Ok(tool_result(diff_execution_value(execution)))
        }
        "emit_sarif" => {
            validate_path(arguments, PathExpectation::Any)?;
            let pq_mode = optional_bool(arguments, "pqc")?;
            let execution = execute_scan(&scan_args(arguments, pq_mode)?)?;
            let sarif = build_sarif(&execution.findings);
            Ok(tool_result(json!({
                "command": "emit_sarif",
                "sarif": sarif,
                "findings_count": execution.findings.len(),
                "files_scanned": execution.files_scanned,
                "duration_ms": execution.duration.as_millis() as u64,
                "notices": execution.notices
            })))
        }
        "emit_cbom" => {
            validate_path(arguments, PathExpectation::Any)?;
            let execution = execute_scan(&scan_args(arguments, true)?)?;
            let (cbom, empty_but_findings_present) = build_cbom(&execution.findings);
            Ok(tool_result(json!({
                "command": "emit_cbom",
                "cbom": cbom,
                "empty_but_findings_present": empty_but_findings_present,
                "findings_count": execution.findings.len(),
                "files_scanned": execution.files_scanned,
                "duration_ms": execution.duration.as_millis() as u64,
                "notices": execution.notices
            })))
        }
        "list_rules" => Ok(tool_result(list_rules(registry))),
        "explain_finding" => Ok(tool_result(explain_finding(arguments)?)),
        "suggest_suppression" => Ok(tool_result(suggest_suppression(arguments)?)),
        _ => Err(format!("Unknown tool: {}", tool_name)),
    }
}

enum PathExpectation {
    File,
    Directory,
    Any,
}

fn validate_path(arguments: &Value, expectation: PathExpectation) -> Result<(), String> {
    let path = required_string(arguments, "path")?;
    // MCP clients pass local filesystem paths by design; validate existence
    // and expected type before dispatching the scanner.
    // foxguard: ignore[rs/no-path-traversal]
    let p = Path::new(&path);
    if !p.exists() {
        return Err(format!("Path does not exist: {}", path));
    }

    match expectation {
        PathExpectation::File if !p.is_file() => Err(format!("Path is not a file: {}", path)),
        PathExpectation::Directory if !p.is_dir() => {
            Err(format!("Path is not a directory: {}", path))
        }
        _ => Ok(()),
    }
}

fn tool_result(output: Value) -> Value {
    let text = match serde_json::to_string(&output) {
        Ok(text) => text,
        Err(error) => json!({
            "serialization_error": error.to_string()
        })
        .to_string(),
    };
    json!({
        "content": [
            {
                "type": "text",
                "text": text
            }
        ],
        "structuredContent": output
    })
}

fn scan_args(arguments: &Value, pq_mode: bool) -> Result<ScanArgs, String> {
    Ok(ScanArgs {
        path: required_string(arguments, "path")?,
        config: optional_string(arguments, "config")?,
        format: OutputFormat::Json,
        severity: optional_severity(arguments, "severity")?,
        rules: optional_string(arguments, "rules")?,
        codeql_db: optional_string(arguments, "codeql_db")?,
        no_builtins: optional_bool(arguments, "no_builtins")?,
        changes: change_mode_args(arguments)?,
        changed_files_from: None,
        exclude: optional_string_array(arguments, "exclude")?,
        baseline: optional_string(arguments, "baseline")?,
        write_baseline: None,
        explain: optional_bool(arguments, "explain")?,
        fix: false,
        github_pr: None,
        quiet: true,
        output: None,
        max_file_size: optional_u64(arguments, "max_file_size")?.unwrap_or(DEFAULT_MAX_FILE_SIZE),
        show_confidence: false,
        min_confidence: optional_f32(arguments, "min_confidence")?,
        pq_mode,
        cnsa2: pq_mode || optional_bool(arguments, "cnsa2")?,
        sca: false,
        sca_offline: false,
        sca_db: None,
        sca_cache: None,
    })
}

fn secrets_args(arguments: &Value) -> Result<SecretsArgs, String> {
    Ok(SecretsArgs {
        path: required_string(arguments, "path")?,
        config: optional_string(arguments, "config")?,
        format: OutputFormat::Json,
        changes: change_mode_args(arguments)?,
        baseline: optional_string(arguments, "baseline")?,
        write_baseline: None,
        output: None,
        exclude_paths: optional_string_array(arguments, "exclude_paths")?,
        exclude_path_file: optional_string(arguments, "exclude_path_file")?,
        ignored_rules: optional_string_array(arguments, "ignored_rules")?,
        max_file_size: optional_u64(arguments, "max_file_size")?.unwrap_or(DEFAULT_MAX_FILE_SIZE),
    })
}

fn diff_args(arguments: &Value) -> Result<DiffArgs, String> {
    Ok(DiffArgs {
        target: required_string(arguments, "target")?,
        path: required_string(arguments, "path")?,
        config: optional_string(arguments, "config")?,
        format: OutputFormat::Json,
        severity: optional_severity(arguments, "severity")?,
        rules: optional_string(arguments, "rules")?,
        no_builtins: optional_bool(arguments, "no_builtins")?,
        output: None,
        github_pr: None,
        max_file_size: optional_u64(arguments, "max_file_size")?.unwrap_or(DEFAULT_MAX_FILE_SIZE),
    })
}

fn change_mode_args(arguments: &Value) -> Result<ChangeModeArgs, String> {
    let changes = ChangeModeArgs {
        changed: optional_bool(arguments, "changed")?,
        staged: optional_bool(arguments, "staged")?,
        unstaged: optional_bool(arguments, "unstaged")?,
        all_changes: optional_bool(arguments, "all_changes")?,
    };

    let selected = [
        changes.changed,
        changes.staged,
        changes.unstaged,
        changes.all_changes,
    ]
    .into_iter()
    .filter(|selected| *selected)
    .count();
    if selected > 1 {
        return Err(
            "Only one change mode may be set: changed, staged, unstaged, all_changes".to_string(),
        );
    }

    Ok(changes)
}

fn scan_execution_value(command: &str, execution: ScanExecution) -> Value {
    let findings_count = execution.findings.len();
    json!({
        "command": command,
        "findings": execution.findings,
        "findings_count": findings_count,
        "files_scanned": execution.files_scanned,
        "duration_ms": execution.duration.as_millis() as u64,
        "stats": scan_stats_value(&execution.stats),
        "notices": execution.notices
    })
}

fn secrets_execution_value(execution: foxguard::app::SecretsExecution) -> Value {
    let findings_count = execution.findings.len();
    json!({
        "command": "scan_secrets",
        "findings": execution.findings,
        "findings_count": findings_count,
        "files_scanned": execution.files_scanned,
        "duration_ms": execution.duration.as_millis() as u64,
        "notices": execution.notices
    })
}

fn diff_execution_value(execution: DiffExecution) -> Value {
    let findings_count = execution.findings.len();
    json!({
        "command": "scan_diff",
        "findings": execution.findings,
        "findings_count": findings_count,
        "files_scanned": execution.files_scanned,
        "duration_ms": execution.duration.as_millis() as u64,
        "total_current": execution.total_current,
        "existing_count": execution.existing_count,
        "notices": execution.notices
    })
}

fn scan_stats_value(stats: &ScanStats) -> Value {
    json!({
        "files_discovered": stats.files_discovered,
        "files_scanned": stats.files_scanned,
        "files_skipped": stats.files_skipped,
        "files_ignored": stats.files_ignored,
        "unsupported_files": stats.unsupported_files,
        "noise_files": stats.noise_files,
        "too_large_files": stats.too_large_files,
        "metadata_error_files": stats.metadata_error_files,
        "binary_files": stats.binary_files,
        "read_error_files": stats.read_error_files,
        "minified_files": stats.minified_files,
        "parse_error_files": stats.parse_error_files
    })
}

fn list_rules(registry: &RuleRegistry) -> Value {
    let mut rules = registry
        .all_rules()
        .iter()
        .map(|rule| {
            json!({
                "id": rule.id(),
                "severity": rule.severity(),
                "cwe": rule.cwe(),
                "description": rule.description(),
                "language": rule.language().to_string(),
                "cnsa2_deadline": rule.cnsa2_deadline(),
                "taint": rule.id().contains("/taint-")
            })
        })
        .collect::<Vec<_>>();
    rules.sort_by(|a, b| {
        a["id"]
            .as_str()
            .unwrap_or_default()
            .cmp(b["id"].as_str().unwrap_or_default())
    });

    json!({
        "command": "list_rules",
        "rules_count": rules.len(),
        "rules": rules
    })
}

fn explain_finding(arguments: &Value) -> Result<Value, String> {
    let finding = arguments.get("finding").unwrap_or(arguments);
    let rule_id = required_string(finding, "rule_id")?;
    let source_line = optional_usize(finding, "source_line")?;
    let sink_line = optional_usize(finding, "sink_line")?;
    let source_description = optional_string(finding, "source_description")?;
    let sink_description = optional_string(finding, "sink_description")?;
    let has_dataflow = source_line.is_some()
        || sink_line.is_some()
        || source_description.is_some()
        || sink_description.is_some();

    Ok(json!({
        "command": "explain_finding",
        "rule_id": rule_id,
        "severity": optional_string(finding, "severity")?,
        "cwe": optional_string(finding, "cwe")?,
        "description": optional_string(finding, "description")?,
        "location": {
            "file": optional_string(finding, "file")?,
            "line": optional_usize(finding, "line")?,
            "column": optional_usize(finding, "column")?,
            "end_line": optional_usize(finding, "end_line")?,
            "end_column": optional_usize(finding, "end_column")?
        },
        "snippet": optional_string(finding, "snippet")?,
        "dataflow": {
            "available": has_dataflow,
            "source": {
                "line": source_line,
                "description": source_description
            },
            "sink": {
                "line": sink_line,
                "description": sink_description
            },
            "taint_hops": optional_usize(finding, "taint_hops")?
        },
        "fix_suggestion": optional_string(finding, "fix_suggestion")?
    }))
}

fn suggest_suppression(arguments: &Value) -> Result<Value, String> {
    let rule_id = required_string(arguments, "rule_id")?;
    let path = optional_string(arguments, "path")?;
    let comment_prefix = path.as_deref().map(comment_prefix_for_path).unwrap_or("//");
    let path_for_yaml = path.as_deref().unwrap_or("path/to/file");
    let escaped_pattern = regex::escape(path_for_yaml);

    Ok(json!({
        "command": "suggest_suppression",
        "rule_id": rule_id,
        "path": path,
        "inline": {
            "comment": format!("{comment_prefix} foxguard: ignore[{rule_id}]"),
            "note": "Place this on the finding line or the nearest preceding line supported by the file type."
        },
        "config_ignore_rule": {
            "yaml": format!(
                "scan:\n  ignore_rules:\n    - path: {path_for_yaml}\n      rules:\n        - {rule_id}\n"
            )
        },
        "config_pattern_suppression": {
            "yaml": format!(
                "scan:\n  suppressions:\n    - rule_id: {rule_id}\n      path_pattern: \"^{escaped_pattern}$\"\n"
            )
        }
    }))
}

fn comment_prefix_for_path(path: &str) -> &'static str {
    let file_name = match path.rsplit(['/', '\\']).next() {
        Some(file_name) => file_name,
        None => path,
    };
    let extension = match file_name.rsplit_once('.') {
        Some((_, extension)) => extension,
        None => "",
    };

    match extension {
        "py" | "pyw" | "rb" | "rake" | "gemspec" | "yml" | "yaml" | "toml" | "sh" | "bash"
        | "zsh" => "#",
        _ => "//",
    }
}

fn required_string(value: &Value, key: &str) -> Result<String, String> {
    value
        .get(key)
        .and_then(Value::as_str)
        .map(ToOwned::to_owned)
        .ok_or_else(|| format!("Missing {key} argument"))
}

fn optional_string(value: &Value, key: &str) -> Result<Option<String>, String> {
    match value.get(key) {
        None | Some(Value::Null) => Ok(None),
        Some(value) => value
            .as_str()
            .map(|value| Some(value.to_string()))
            .ok_or_else(|| format!("{key} must be a string")),
    }
}

fn optional_string_array(value: &Value, key: &str) -> Result<Vec<String>, String> {
    match value.get(key) {
        None | Some(Value::Null) => Ok(Vec::new()),
        Some(Value::Array(values)) => values
            .iter()
            .map(|value| {
                value
                    .as_str()
                    .map(ToOwned::to_owned)
                    .ok_or_else(|| format!("{key} entries must be strings"))
            })
            .collect(),
        Some(_) => Err(format!("{key} must be an array of strings")),
    }
}

fn optional_bool(value: &Value, key: &str) -> Result<bool, String> {
    match value.get(key) {
        None | Some(Value::Null) => Ok(false),
        Some(value) => value
            .as_bool()
            .ok_or_else(|| format!("{key} must be a boolean")),
    }
}

fn optional_u64(value: &Value, key: &str) -> Result<Option<u64>, String> {
    match value.get(key) {
        None | Some(Value::Null) => Ok(None),
        Some(value) => value
            .as_u64()
            .map(Some)
            .ok_or_else(|| format!("{key} must be a positive integer")),
    }
}

fn optional_usize(value: &Value, key: &str) -> Result<Option<usize>, String> {
    optional_u64(value, key).map(|value| value.map(|value| value as usize))
}

fn optional_f32(value: &Value, key: &str) -> Result<Option<f32>, String> {
    match value.get(key) {
        None | Some(Value::Null) => Ok(None),
        Some(value) => {
            let raw = value
                .as_f64()
                .ok_or_else(|| format!("{key} must be a number in [0.0, 1.0]"))?;
            if !(0.0..=1.0).contains(&raw) {
                return Err(format!("{key} must be in [0.0, 1.0]"));
            }
            Ok(Some(raw as f32))
        }
    }
}

fn optional_severity(value: &Value, key: &str) -> Result<Option<SeverityFilter>, String> {
    let Some(raw) = optional_string(value, key)? else {
        return Ok(None);
    };

    match raw.as_str() {
        "low" => Ok(Some(SeverityFilter::Low)),
        "medium" => Ok(Some(SeverityFilter::Medium)),
        "high" => Ok(Some(SeverityFilter::High)),
        "critical" => Ok(Some(SeverityFilter::Critical)),
        _ => Err(format!("{key} must be one of: low, medium, high, critical")),
    }
}