kingfisher-bin 2.8.0

MongoDB's blazingly fast and accurate secret scanning and validation tool
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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
use anyhow::{Context, bail};
use clap::{Args, Subcommand, ValueEnum, ValueHint};
use path_dedot::ParseDot;
use serde::{Deserialize, Serialize};
use std::{
    fs,
    net::IpAddr,
    path::{Path, PathBuf},
    str::FromStr,
};
use strum::Display;
use tracing::debug;
use url::Url;

use crate::{
    cli::{
        commands::{
            azure::AzureRepoSpecifiers,
            bitbucket::BitbucketRepoSpecifiers,
            gitea::GiteaRepoSpecifiers,
            github::GitHubRepoSpecifiers,
            gitlab::GitLabRepoSpecifiers,
            huggingface::HuggingFaceRepoSpecifiers,
            inputs::{ContentFilteringArgs, InputSpecifierArgs},
            output::{OutputArgs, ReportOutputFormat},
            rules::{RuleCacheArgs, RuleSpecifierArgs},
            view,
        },
        global::RAM_GB,
    },
    git_url::GitUrl,
    rules::rule::Confidence,
    util::expand_tilde,
};

/// Sentinel `max_results` meaning "keep paging while the provider returns
/// results", set by the `--all` flag. Provider fetch loops stop on the last
/// page or an empty page rather than on this bound.
pub const UNLIMITED_RESULTS: usize = usize::MAX;

/// Determine the default number of parallel scan jobs.
///
/// * Target = `available_parallelism`.
/// * Cap by RAM at ≈ 1 GiB per job (so 16 GiB ⇒ max 16 jobs).
/// * Always ≥ 1.
/// * When `-v/--verbose` is passed, the computed value is logged at DEBUG.
fn default_scan_jobs() -> usize {
    // How many logical CPUs do we see? (Falls back to 1 on error.)
    let cpu_count = std::thread::available_parallelism().map(usize::from).unwrap_or(1);

    // One scan worker per logical CPU keeps the CPU-bound matcher saturated without
    // oversubscribing the Rayon and Tokio pools.
    let desired = cpu_count;

    match *RAM_GB {
        // If we know how much RAM we have, cap by a 1 GiB-per-job heuristic.
        Some(ram_gb) => {
            let max_by_ram = ram_gb.ceil() as usize; // 1 GiB per job
            let jobs = desired.min(max_by_ram).max(1);

            debug!(
                "Using {jobs} parallel scan jobs \
                 (cpus = {cpu_count}, desired = {desired}, \
                 ram = {ram_gb:.1} GiB, cap_by_ram = {max_by_ram})"
            );
            jobs
        }
        // If RAM is unknown, just use the desired value.
        None => {
            debug!("Using {desired} parallel scan jobs (cpus = {cpu_count}, ram unknown)");
            desired
        }
    }
}

/// `kingfisher scan` command and flags
#[derive(Args, Debug, Clone)]
pub struct ScanArgs {
    /// Number of parallel scanning threads
    #[arg(global = true, long = "jobs", short = 'j', default_value_t = default_scan_jobs())]
    pub num_jobs: usize,

    #[command(flatten)]
    pub rules: RuleSpecifierArgs,

    #[command(flatten)]
    pub rule_cache: RuleCacheArgs,

    #[command(flatten)]
    pub input_specifier_args: InputSpecifierArgs,

    #[command(flatten)]
    pub content_filtering_args: ContentFilteringArgs,

    /// Minimum confidence level for reporting findings
    #[arg(global = true, long, short = 'c', default_value = "medium")]
    pub confidence: ConfidenceLevel,

    /// Temporarily offload completed findings to disk with automatic cleanup.
    /// Uses a private temporary file that is automatically removed when closed or the process exits.
    /// Offloading starts only after a repository or input root finishes, so a single-repository
    /// scan may use the same peak memory with or without this flag.
    /// Final deduplication, validation, and reporting still use an in-memory working set.
    #[arg(global = true, long, default_value_t = false)]
    pub disk_offload: bool,

    /// Disable secret validation
    #[arg(global = true, long, short = 'n', default_value_t = false)]
    pub no_validate: bool,

    /// Timeout for validation requests in seconds (1-60)
    #[arg(
        global = true,
        long = "validation-timeout",
        default_value_t = 10,
        value_name = "SECONDS",
        value_parser = clap::value_parser!(u64).range(1..=60)
    )]
    pub validation_timeout: u64,

    /// Number of retries for validation requests (0-5)
    #[arg(
        global = true,
        long = "validation-retries",
        default_value_t = 1,
        value_name = "N",
        value_parser = clap::value_parser!(u32).range(0..=5)
    )]
    pub validation_retries: u32,

    /// Global validation request rate limit in requests per second
    #[arg(global = true, long = "validation-rps", value_name = "RPS")]
    pub validation_rps: Option<f64>,

    /// Rule-scoped validation request rate limit (RULE_SELECTOR=RPS), repeatable
    #[arg(global = true, long = "validation-rps-rule", value_name = "RULE_SELECTOR=RPS")]
    pub validation_rps_rule: Vec<String>,

    /// Include full validation response bodies without truncation
    #[arg(global = true, long, default_value_t = false)]
    pub full_validation_response: bool,

    /// Maximum bytes to store from validation response bodies (0 = unlimited).
    /// Overridden by --full-validation-response which forces unlimited storage.
    #[arg(
        global = true,
        long = "max-validation-response-length",
        default_value_t = 2048,
        value_name = "BYTES"
    )]
    pub max_validation_response_length: usize,

    /// Map validated cloud credentials to their effective identities and blast radius; use only when
    /// authorized for the target account because this triggers additional network
    /// requests to determine granted access
    #[arg(global = true, long = "blast-radius", alias = "access-map", default_value_t = false)]
    pub access_map: bool,

    // /// Optional path to write a consolidated access-map HTML report
    // #[arg(long, value_name = "PATH")]
    // pub access_map_html: Option<PathBuf>,
    /// Display only validated findings
    #[arg(global = true, long, default_value_t = false, conflicts_with = "validation_filter")]
    pub only_valid: bool,

    /// Filter findings by validation outcome. `active` is equivalent to
    /// `--only-valid`; `actionable` includes active credentials and high-confidence
    /// assumed-valid secrets. This conflicts with the `--only-valid` compatibility alias.
    #[arg(global = true, long, value_enum, conflicts_with = "only_valid")]
    pub validation_filter: Option<ValidationFilter>,

    /// Include hidden helper-rule findings in reports and scan summaries
    #[arg(global = true, long, default_value_t = false)]
    pub include_hidden_findings: bool,

    /// Override the default minimum entropy threshold
    #[arg(global = true, long, short = 'e')]
    pub min_entropy: Option<f32>,

    /// Show performance statistics for each rule
    #[arg(global = true, long, default_value_t = false)]
    pub rule_stats: bool,

    /// Display every occurrence of a finding
    #[arg(global = true, long, default_value_t = false)]
    pub no_dedup: bool,

    /// Serve a JSON report locally and open the browser (http://127.0.0.1:7890)
    #[arg(skip)]
    pub view_report: bool,

    /// Redact findings values using a secure hash
    #[arg(global = true, long, short = 'r', default_value_t = false)]
    pub redact: bool,

    /// Skip decoding Base64 blobs before scanning
    #[arg(global = true, long, default_value_t = false)]
    pub no_base64: bool,

    /// Turbo mode: equivalent to --commit-metadata=false --no-base64 and disables MIME sniffing, language detection, and parser-based context verification
    #[arg(global = true, long = "turbo", default_value_t = false)]
    pub turbo: bool,

    /// Timeout for Git repository scanning in seconds
    #[arg(global = true, long, default_value_t = 1800, value_name = "SECONDS")]
    pub git_repo_timeout: u64,

    /// Write incremental repository discovery, fetch, and scan audit events as JSON Lines
    #[arg(global = true, long = "audit-log", value_name = "FILE", value_hint = ValueHint::FilePath)]
    pub audit_log: Option<PathBuf>,

    #[command(flatten)]
    pub output_args: OutputArgs<ReportOutputFormat>,

    /// Baseline file to filter known secrets
    #[arg(global = true, long, value_name = "FILE")]
    pub baseline_file: Option<std::path::PathBuf>,

    /// Create or update the baseline file with current findings
    #[arg(global = true, long, default_value_t = false)]
    pub manage_baseline: bool,

    /// Regex patterns to allow-list secret matches (repeatable)
    #[arg(global = true, long = "skip-regex", value_name = "PATTERN")]
    pub skip_regex: Vec<String>,

    /// Skipwords to allow-list secret matches (case-insensitive, repeatable)
    #[arg(global = true, long = "skip-word", value_name = "WORD")]
    pub skip_word: Vec<String>,

    /// AWS account IDs whose findings should skip live credential validation (repeatable)
    #[arg(
        global = true,
        long = "skip-aws-account",
        value_name = "ACCOUNT_ID",
        value_delimiter = ','
    )]
    pub skip_aws_account: Vec<String>,

    /// File containing AWS account IDs to skip (one per line, `#` comments ignored)
    #[arg(global = true, long = "skip-aws-account-file", value_name = "FILE")]
    pub skip_aws_account_file: Option<PathBuf>,

    /// Additional inline ignore directives to recognise (repeatable)
    #[arg(global = true, long = "ignore-comment", value_name = "DIRECTIVE")]
    pub extra_ignore_comments: Vec<String>,

    /// Disable inline ignore directives entirely
    #[arg(global = true, long = "no-ignore", default_value_t = false)]
    pub no_inline_ignore: bool,

    /// Disable rule-level `ignore_if_contains` filtering for pattern requirements
    #[arg(global = true, long = "no-ignore-if-contains", default_value_t = false)]
    pub no_ignore_if_contains: bool,

    #[arg(skip)]
    pub view_report_port: u16,
    #[arg(skip)]
    pub view_report_address: String,

    /// POST scan results to a webhook URL when scanning completes (repeatable).
    /// Use multiple `--alert-webhook` flags to fan out to several destinations.
    #[arg(global = true, long = "alert-webhook", value_name = "URL")]
    pub alert_webhook: Vec<String>,

    /// Format for `--alert-webhook` payloads. Default is inferred from the URL
    /// host (slack.com → slack, *.office.com → teams, discord.com → discord,
    /// chat.googleapis.com → googlechat, otherwise generic). Mattermost is
    /// self-hosted and never inferred — pass `--alert-format mattermost`
    /// explicitly.
    #[arg(global = true, long = "alert-format", value_name = "FORMAT")]
    pub alert_format: Option<crate::alerts::AlertFormat>,

    /// When to post alerts: only when there are findings, or always.
    #[arg(global = true, long = "alert-on", value_name = "MODE", default_value = "findings")]
    pub alert_on: crate::alerts::AlertOn,

    /// Minimum confidence required for a finding to be included in alert payloads.
    #[arg(
        global = true,
        long = "alert-min-confidence",
        value_name = "LEVEL",
        default_value = "medium"
    )]
    pub alert_min_confidence: ConfidenceLevel,

    /// Include the (possibly truncated) secret value in alert payloads.
    /// Off by default; on, the snippet is truncated to ~32 chars.
    #[arg(global = true, long = "alert-include-secret", default_value_t = false)]
    pub alert_include_secret: bool,

    /// Pivot link rendered in the payload — typically the URL of the full
    /// scan report (CI run, S3 object, SARIF in Code Scanning, etc.). When
    /// present, every alert payload includes a "Full report" link, which is
    /// the right place to send operators who hit the truncated finding cap.
    /// Falls back to env var `KINGFISHER_ALERT_REPORT_URL` if unset.
    #[arg(
        global = true,
        long = "alert-report-url",
        value_name = "URL",
        env = "KINGFISHER_ALERT_REPORT_URL"
    )]
    pub alert_report_url: Option<String>,

    /// How much per-finding detail to include in alert payloads. `auto`
    /// (default) shows up to 10 findings inline, but switches to a
    /// summary-only payload once the per-sink filtered finding count exceeds
    /// 25 — at that volume, chat detail blocks add noise and the operator
    /// should be pivoting to the full report instead.
    #[arg(global = true, long = "alert-detail", value_name = "MODE", default_value = "auto")]
    pub alert_detail: crate::alerts::AlertDetail,

    /// Restrict which findings are eligible for alert payloads, on top of
    /// `--alert-min-confidence`. `exclude-inactive` drops "Inactive
    /// Credential" findings; `only-active` keeps only "Active Credential"
    /// findings; `access-map-only` keeps only findings with a successful,
    /// matching `--blast-radius` result (requires `--blast-radius` to be set, otherwise
    /// this filter matches nothing).
    #[arg(
        global = true,
        long = "alert-finding-filter",
        value_name = "FILTER",
        default_value = "all"
    )]
    pub alert_finding_filter: crate::alerts::AlertFindingFilter,

    /// Skip a webhook entirely when `--alert-min-confidence` /
    /// `--alert-finding-filter` leave nothing to report, instead of posting
    /// an alert with an empty findings list. `--alert-on always` sinks are
    /// heartbeats and always post, so that silence keeps meaning "the scan
    /// never ran". Off by default to preserve existing behavior.
    #[arg(global = true, long = "alert-prevent-empty", default_value_t = false)]
    pub alert_prevent_empty: bool,

    /// Build and log each alert sink's resolved payload instead of POSTing
    /// it — use to validate `--alert-*` filter configuration against a real
    /// scan before wiring a production webhook URL. Dry-run payloads always
    /// redact secret values, even when `--alert-include-secret` is set.
    #[arg(global = true, long = "alert-dry-run", default_value_t = false)]
    pub alert_dry_run: bool,

    /// Per-webhook overrides loaded from `kingfisher.yaml`. Indexed in lockstep
    /// with `alert_webhook` for the trailing config-sourced URLs. Not parsed
    /// from the CLI; populated by `apply_config` in main.rs.
    #[arg(skip)]
    pub config_webhook_overrides: Vec<ConfigWebhookOverride>,
}

/// Override values for a webhook entry that came from the config file.
/// Each field that is `None` falls back to the corresponding `--alert-*` CLI
/// flag's value.
#[derive(Debug, Clone, Default)]
pub struct ConfigWebhookOverride {
    pub format: Option<crate::alerts::AlertFormat>,
    pub on: Option<crate::alerts::AlertOn>,
    pub min_confidence: Option<ConfidenceLevel>,
    pub include_secret: Option<bool>,
    pub report_url: Option<String>,
    pub detail: Option<crate::alerts::AlertDetail>,
    pub finding_filter: Option<crate::alerts::AlertFindingFilter>,
    pub prevent_empty: Option<bool>,
}

/// Confidence levels for findings
#[derive(Copy, Clone, Debug, Display, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
#[strum(serialize_all = "kebab-case")]
pub enum ConfidenceLevel {
    Low,
    Medium,
    High,
}

/// Controls which validation outcomes are included in scan reports.
#[derive(
    Copy, Clone, Debug, Display, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Serialize, Deserialize,
)]
#[strum(serialize_all = "kebab-case")]
#[serde(rename_all = "kebab-case")]
pub enum ValidationFilter {
    /// Include every finding, regardless of validation outcome.
    All,
    /// Include only credentials proven active by a validator.
    Active,
    /// Include active credentials and high-confidence assumed-valid secrets.
    Actionable,
}

impl ScanArgs {
    /// Rejects `--audit-log` aliasing a path the scan reads or rewrites.
    ///
    /// The audit log is opened with truncation at scan startup, before the
    /// baseline is loaded and before custom rules are read, so an aliased
    /// baseline (including the `baseline-file.yaml` default used by
    /// `--manage-baseline`), rule file, or report output would be destroyed.
    /// This runs once during CLI validation and again after kingfisher.yaml
    /// merging in the caller: `apply_config` can populate `output_args.output`
    /// from `output.path` after the CLI-only check has already passed, so the
    /// effective paths must be re-compared against the merged values.
    pub fn validate_audit_log_collisions(
        &self,
        endpoint_config: Option<&Path>,
        project_config: Option<&Path>,
    ) -> anyhow::Result<()> {
        let Some(audit_log) = &self.audit_log else { return Ok(()) };
        // Shells do not expand `~` in `--audit-log=~/...`, so normalize it
        // before comparing against the other paths.
        let audit_log = &expand_tilde(audit_log);

        if let Some(output) = &self.output_args.output
            && paths_refer_to_same_file(audit_log, output)
        {
            bail!("--audit-log and --output must use different paths");
        }

        if self.baseline_file.is_some() || self.manage_baseline {
            let baseline = self
                .baseline_file
                .clone()
                .unwrap_or_else(|| std::path::PathBuf::from("baseline-file.yaml"));
            if paths_refer_to_same_file(audit_log, &baseline) {
                bail!("--audit-log and the baseline file must use different paths");
            }
        }

        for rules_path in &self.rules.rules_path {
            let rules_path = expand_tilde(rules_path);
            if paths_refer_to_same_file(audit_log, &rules_path)
                || path_is_beneath(audit_log, &rules_path)
            {
                bail!("--audit-log must not overwrite a custom rules file");
            }
        }

        for input in &self.input_specifier_args.path_inputs {
            if input == Path::new("-") {
                continue;
            }
            if paths_refer_to_same_file(audit_log, input) || path_is_beneath(audit_log, input) {
                bail!(
                    "--audit-log must not alias or reside inside the scanned input {}",
                    input.display()
                );
            }
        }

        let file_inputs = self
            .input_specifier_args
            .gcs_service_account
            .iter()
            .chain(self.input_specifier_args.docker_archive.iter())
            .chain(self.skip_aws_account_file.iter());
        for input in file_inputs {
            if paths_refer_to_same_file(audit_log, input) {
                bail!("--audit-log must not overwrite input file {}", input.display());
            }
        }

        if let Some(endpoint_config) = endpoint_config
            && paths_refer_to_same_file(audit_log, endpoint_config)
        {
            bail!("--audit-log must not overwrite the endpoint configuration file");
        }
        if let Some(project_config) = project_config
            && paths_refer_to_same_file(audit_log, project_config)
        {
            bail!("--audit-log must not overwrite the project configuration file");
        }

        Ok(())
    }

    /// Resolve the compatibility `--only-valid` flag and the richer filter.
    pub fn effective_validation_filter(&self) -> ValidationFilter {
        if self.only_valid {
            ValidationFilter::Active
        } else {
            self.validation_filter.unwrap_or(ValidationFilter::All)
        }
    }
}

impl From<ConfidenceLevel> for Confidence {
    fn from(level: ConfidenceLevel) -> Self {
        match level {
            ConfidenceLevel::Low => Confidence::Low,
            ConfidenceLevel::Medium => Confidence::Medium,
            ConfidenceLevel::High => Confidence::High,
        }
    }
}

#[derive(Args, Debug, Clone)]
pub struct ScanCommandArgs {
    #[command(flatten)]
    pub scan_args: ScanArgs,

    /// Serve a JSON report locally and open the browser (http://127.0.0.1:7890)
    #[arg(global = true, long = "view-report", default_value_t = false)]
    pub view_report: bool,

    /// Port for the report viewer when using --view-report (default 7890)
    #[arg(
        global = true,
        long = "view-report-port",
        default_value_t = view::DEFAULT_PORT,
        value_name = "PORT"
    )]
    pub view_report_port: u16,

    /// Bind address for the report viewer when using --view-report (default 127.0.0.1). Use 0.0.0.0 to allow access from Docker or other hosts.
    #[arg(
        global = true,
        long = "view-report-address",
        default_value = view::DEFAULT_ADDRESS,
        value_name = "ADDRESS"
    )]
    pub view_report_address: String,

    #[command(subcommand)]
    pub provider: Option<ScanInputCommand>,
}

#[allow(clippy::large_enum_variant)]
#[derive(Debug)]
pub enum ScanOperation {
    Scan(ScanArgs),
    ListRepositories(ListRepositoriesCommand),
}

#[derive(Debug)]
pub enum ListRepositoriesCommand {
    Github { api_url: Url, specifiers: GitHubRepoSpecifiers },
    Gitlab { api_url: Url, specifiers: GitLabRepoSpecifiers },
    Gitea { api_url: Url, specifiers: GiteaRepoSpecifiers },
    Bitbucket { api_url: Url, specifiers: BitbucketRepoSpecifiers },
    Azure { base_url: Url, specifiers: AzureRepoSpecifiers },
    Huggingface { specifiers: HuggingFaceRepoSpecifiers },
}

fn load_github_event_users(
    cli_users: Vec<String>,
    user_file: Option<&Path>,
) -> anyhow::Result<Vec<String>> {
    fn is_valid_github_username(user: &str) -> bool {
        user.len() <= 39
            && !user.starts_with('-')
            && !user.ends_with('-')
            && user.chars().all(|c| c.is_ascii_alphanumeric() || c == '-')
    }

    fn push_user(users: &mut Vec<String>, raw: &str, source: &str) -> anyhow::Result<()> {
        let user = raw.trim().trim_start_matches('@');
        if user.is_empty() {
            return Ok(());
        }
        if !is_valid_github_username(user) {
            bail!("Invalid GitHub username in {source}: {user:?}");
        }
        if !users.iter().any(|existing| existing.eq_ignore_ascii_case(user)) {
            users.push(user.to_string());
        }
        Ok(())
    }

    let mut users = Vec::new();
    for user in &cli_users {
        push_user(&mut users, user, "--user")?;
    }

    if let Some(path) = user_file {
        let contents = fs::read_to_string(path).with_context(|| {
            format!("Failed to read GitHub public event user file {}", path.display())
        })?;
        for (line_number, line) in contents.lines().enumerate() {
            let trimmed = line.trim();
            if trimmed.is_empty() || trimmed.starts_with('#') {
                continue;
            }
            push_user(&mut users, trimmed, &format!("{}:{}", path.display(), line_number + 1))?;
        }
    }

    Ok(users)
}

impl ScanCommandArgs {
    fn infer_positional_git_urls(&mut self) {
        let mut inferred_git_urls = Vec::new();
        let mut retained_paths = Vec::new();

        for path in self.scan_args.input_specifier_args.path_inputs.drain(..) {
            if path.as_path() == Path::new("-") || path.exists() {
                retained_paths.push(path);
                continue;
            }

            if let Some(git_url) = parse_git_url_target(&path) {
                inferred_git_urls.push(git_url);
            } else {
                retained_paths.push(path);
            }
        }

        self.scan_args.input_specifier_args.path_inputs = retained_paths;
        self.scan_args.input_specifier_args.git_url.extend(inferred_git_urls);
    }

    /// Convert CLI arguments into a scan or repository-listing operation.
    pub fn into_operation(mut self) -> anyhow::Result<ScanOperation> {
        let mut used_provider_subcommand = false;

        self.scan_args.view_report = self.view_report;
        self.scan_args.view_report_port = self.view_report_port;
        self.scan_args.view_report_address = self.view_report_address.clone();

        if let Some(provider) = self.provider.take() {
            used_provider_subcommand = true;
            let scan_args = &mut self.scan_args;
            let maybe_list = match provider {
                ScanInputCommand::Filesystem(args) => {
                    if args.paths.is_empty() {
                        bail!("Provide at least one path when using the filesystem subcommand");
                    }
                    scan_args.input_specifier_args.path_inputs = args.paths;
                    scan_args.input_specifier_args.git_url = args.git_url;
                    None
                }
                ScanInputCommand::Github(args) => {
                    let mut specifiers = args.specifiers;
                    if args.event_user_file.is_some() && !args.public_events {
                        bail!("--user-file can only be used with --public-events");
                    }
                    if args.public_events {
                        if let (Some(audit_log), Some(user_file)) =
                            (scan_args.audit_log.as_deref(), args.event_user_file.as_deref())
                            && paths_refer_to_same_file(
                                &expand_tilde(audit_log),
                                &expand_tilde(user_file),
                            )
                        {
                            bail!(
                                "--audit-log must not overwrite the GitHub public-event user file"
                            );
                        }
                        let event_users = load_github_event_users(
                            std::mem::take(&mut specifiers.user),
                            args.event_user_file.as_deref(),
                        )?;
                        if event_users.is_empty() {
                            bail!(
                                "You must specify at least one --user or --user-file when scanning GitHub public events"
                            );
                        }
                        if !specifiers.organization.is_empty() || specifiers.all_organizations {
                            bail!(
                                "GitHub public event scanning supports --user and --user-file only"
                            );
                        }
                        if args.include_contributors {
                            bail!("--include-contributors cannot be used with --public-events");
                        }
                        if args.list_only {
                            bail!("--list-only cannot be used with --public-events");
                        }
                        if specifiers.repo_type
                            != crate::cli::commands::github::GitHubRepoType::Source
                        {
                            bail!("--repo-type cannot be used with --public-events");
                        }

                        scan_args.input_specifier_args.github_event_user = event_users;
                        scan_args.input_specifier_args.github_event_lookback_hours =
                            args.event_lookback_hours;
                        scan_args.input_specifier_args.github_exclude = specifiers.exclude_repos;
                        scan_args.input_specifier_args.github_api_url = args.api_url;
                        scan_args.input_specifier_args.repo_clone_limit = args.repo_clone_limit;
                        None
                    } else if specifiers.is_empty() {
                        bail!(
                            "You must specify at least one --user, --org, or use --all-orgs when scanning GitHub"
                        );
                    } else if args.list_only {
                        Some(ListRepositoriesCommand::Github { api_url: args.api_url, specifiers })
                    } else {
                        scan_args.input_specifier_args.github_include_gists =
                            specifiers.include_gists;
                        scan_args.input_specifier_args.github_user = specifiers.user;
                        scan_args.input_specifier_args.github_organization =
                            specifiers.organization;
                        scan_args.input_specifier_args.github_exclude = specifiers.exclude_repos;
                        scan_args.input_specifier_args.all_github_organizations =
                            specifiers.all_organizations;
                        scan_args.input_specifier_args.github_repo_type = specifiers.repo_type;
                        scan_args.input_specifier_args.github_api_url = args.api_url;
                        scan_args.input_specifier_args.repo_clone_limit = args.repo_clone_limit;
                        scan_args.input_specifier_args.include_contributors =
                            args.include_contributors;
                        None
                    }
                }
                ScanInputCommand::Gitlab(args) => {
                    if args.specifiers.is_empty() {
                        bail!(
                            "You must specify at least one --user, --group, or use --all-groups when scanning GitLab"
                        );
                    }
                    if args.list_only {
                        Some(ListRepositoriesCommand::Gitlab {
                            api_url: args.api_url,
                            specifiers: args.specifiers,
                        })
                    } else {
                        scan_args.input_specifier_args.gitlab_include_snippets =
                            args.specifiers.include_snippets;
                        scan_args.input_specifier_args.gitlab_user = args.specifiers.user;
                        scan_args.input_specifier_args.gitlab_group = args.specifiers.group;
                        scan_args.input_specifier_args.gitlab_exclude =
                            args.specifiers.exclude_repos;
                        scan_args.input_specifier_args.all_gitlab_groups =
                            args.specifiers.all_groups;
                        scan_args.input_specifier_args.gitlab_include_subgroups =
                            args.specifiers.include_subgroups;
                        scan_args.input_specifier_args.gitlab_repo_type = args.specifiers.repo_type;
                        scan_args.input_specifier_args.gitlab_api_url = args.api_url;
                        scan_args.input_specifier_args.repo_clone_limit = args.repo_clone_limit;
                        scan_args.input_specifier_args.include_contributors =
                            args.include_contributors;
                        None
                    }
                }
                ScanInputCommand::Gitea(args) => {
                    if args.specifiers.is_empty() {
                        bail!(
                            "Specify at least one --user, --org, or use --all-orgs when scanning Gitea"
                        );
                    }
                    if args.list_only {
                        Some(ListRepositoriesCommand::Gitea {
                            api_url: args.api_url,
                            specifiers: args.specifiers,
                        })
                    } else {
                        scan_args.input_specifier_args.gitea_user = args.specifiers.user;
                        scan_args.input_specifier_args.gitea_organization =
                            args.specifiers.organization;
                        scan_args.input_specifier_args.gitea_exclude =
                            args.specifiers.exclude_repos;
                        scan_args.input_specifier_args.all_gitea_organizations =
                            args.specifiers.all_organizations;
                        scan_args.input_specifier_args.gitea_repo_type = args.specifiers.repo_type;
                        scan_args.input_specifier_args.gitea_api_url = args.api_url;
                        None
                    }
                }
                ScanInputCommand::Bitbucket(args) => {
                    if args.specifiers.is_empty() {
                        bail!(
                            "You must specify at least one --user, --workspace, --project, or use --all-workspaces when scanning Bitbucket"
                        );
                    }
                    if args.list_only {
                        Some(ListRepositoriesCommand::Bitbucket {
                            api_url: args.api_url,
                            specifiers: args.specifiers,
                        })
                    } else {
                        scan_args.input_specifier_args.bitbucket_include_snippets =
                            args.specifiers.include_snippets;
                        scan_args.input_specifier_args.bitbucket_user = args.specifiers.user;
                        scan_args.input_specifier_args.bitbucket_workspace =
                            args.specifiers.workspace;
                        scan_args.input_specifier_args.bitbucket_project = args.specifiers.project;
                        scan_args.input_specifier_args.bitbucket_exclude =
                            args.specifiers.exclude_repos;
                        scan_args.input_specifier_args.all_bitbucket_workspaces =
                            args.specifiers.all_workspaces;
                        scan_args.input_specifier_args.bitbucket_repo_type =
                            args.specifiers.repo_type;
                        scan_args.input_specifier_args.bitbucket_api_url = args.api_url;
                        None
                    }
                }
                ScanInputCommand::Azure(args) => {
                    if args.specifiers.is_empty() {
                        bail!(
                            "You must specify at least one --organization, --project, or use --all-projects when scanning Azure DevOps"
                        );
                    }
                    if args.list_only {
                        Some(ListRepositoriesCommand::Azure {
                            base_url: args.base_url,
                            specifiers: args.specifiers,
                        })
                    } else {
                        scan_args.input_specifier_args.azure_organization =
                            args.specifiers.organization;
                        scan_args.input_specifier_args.azure_project = args.specifiers.project;
                        scan_args.input_specifier_args.azure_exclude =
                            args.specifiers.exclude_repos;
                        scan_args.input_specifier_args.all_azure_projects =
                            args.specifiers.all_projects;
                        scan_args.input_specifier_args.azure_repo_type = args.specifiers.repo_type;
                        scan_args.input_specifier_args.azure_base_url = args.base_url;
                        None
                    }
                }
                ScanInputCommand::Huggingface(args) => {
                    if args.specifiers.is_empty() {
                        bail!(
                            "You must specify at least one --user, --org, --model, --dataset, --space, or --bucket when scanning Hugging Face"
                        );
                    }
                    if args.list_only {
                        Some(ListRepositoriesCommand::Huggingface { specifiers: args.specifiers })
                    } else {
                        scan_args.input_specifier_args.huggingface_user = args.specifiers.user;
                        scan_args.input_specifier_args.huggingface_organization =
                            args.specifiers.organization;
                        scan_args.input_specifier_args.huggingface_model = args.specifiers.model;
                        scan_args.input_specifier_args.huggingface_dataset =
                            args.specifiers.dataset;
                        scan_args.input_specifier_args.huggingface_space = args.specifiers.space;
                        scan_args.input_specifier_args.huggingface_bucket = args.specifiers.bucket;
                        scan_args.input_specifier_args.huggingface_exclude =
                            args.specifiers.exclude;
                        None
                    }
                }
                ScanInputCommand::Slack(args) => {
                    scan_args.input_specifier_args.slack_query = Some(args.query);
                    scan_args.input_specifier_args.slack_api_url = args.api_url;
                    scan_args.input_specifier_args.max_results = args.max_results;
                    None
                }
                ScanInputCommand::Teams(args) => {
                    scan_args.input_specifier_args.teams_query = Some(args.query);
                    scan_args.input_specifier_args.teams_api_url = args.api_url;
                    scan_args.input_specifier_args.max_results = args.max_results;
                    None
                }
                ScanInputCommand::Jira(args) => {
                    scan_args.input_specifier_args.jira_url = Some(args.url);
                    scan_args.input_specifier_args.jql = Some(args.jql);
                    scan_args.input_specifier_args.max_results =
                        if args.all { UNLIMITED_RESULTS } else { args.max_results };
                    scan_args.input_specifier_args.jira_include_comments = args.include_comments;
                    scan_args.input_specifier_args.jira_include_changelog = args.include_changelog;
                    None
                }
                ScanInputCommand::Confluence(args) => {
                    scan_args.input_specifier_args.confluence_url = Some(args.url);
                    scan_args.input_specifier_args.cql = Some(args.cql);
                    scan_args.input_specifier_args.max_results =
                        if args.all { UNLIMITED_RESULTS } else { args.max_results };
                    None
                }
                ScanInputCommand::Postman(args) => {
                    if !args.all
                        && args.workspaces.is_empty()
                        && args.collections.is_empty()
                        && args.environments.is_empty()
                    {
                        bail!(
                            "Specify --workspace, --collection, --environment, or --all when using the postman subcommand"
                        );
                    }
                    scan_args.input_specifier_args.postman_workspaces = args.workspaces;
                    scan_args.input_specifier_args.postman_collections = args.collections;
                    scan_args.input_specifier_args.postman_environments = args.environments;
                    scan_args.input_specifier_args.postman_all = args.all;
                    scan_args.input_specifier_args.postman_include_mocks_monitors =
                        args.include_mocks_monitors;
                    scan_args.input_specifier_args.postman_api_url = args.api_url;
                    scan_args.input_specifier_args.max_results = args.max_results;
                    None
                }
                ScanInputCommand::S3(args) => {
                    scan_args.input_specifier_args.s3_bucket = Some(args.bucket);
                    scan_args.input_specifier_args.s3_prefix = args.prefix;
                    scan_args.input_specifier_args.role_arn = args.role_arn;
                    scan_args.input_specifier_args.aws_local_profile = args.profile;
                    None
                }
                ScanInputCommand::Gcs(args) => {
                    scan_args.input_specifier_args.gcs_bucket = Some(args.bucket);
                    scan_args.input_specifier_args.gcs_prefix = args.prefix;
                    scan_args.input_specifier_args.gcs_service_account = args.service_account;
                    None
                }
                ScanInputCommand::Docker(args) => {
                    if args.images.is_empty() && args.archives.is_empty() {
                        bail!(
                            "Provide at least one image or --archive path when using the docker subcommand"
                        );
                    }
                    scan_args.input_specifier_args.docker_image = args.images;
                    scan_args.input_specifier_args.docker_archive = args.archives;
                    None
                }
            };

            if let Some(list_command) = maybe_list {
                return Ok(ScanOperation::ListRepositories(list_command));
            }
        }

        let used_legacy_git_url_flag = !self.scan_args.input_specifier_args.git_url.is_empty();
        self.infer_positional_git_urls();

        if !self.scan_args.input_specifier_args.has_any_input() {
            bail!(
                "Specify a path or Git URL (for example: 'kingfisher scan github.com/org/repo'), or use a provider subcommand such as 'kingfisher scan github'"
            );
        }

        for path in &self.scan_args.input_specifier_args.path_inputs {
            if path.as_path() == Path::new("-") {
                continue;
            }

            if !path.exists() {
                bail!("Error: unrecognized scan target or path does not exist: {}", path.display());
            }
        }

        if !used_provider_subcommand {
            self.scan_args.input_specifier_args.emit_deprecated_warnings(used_legacy_git_url_flag);
        }

        if self.scan_args.manage_baseline {
            self.scan_args.no_dedup = true;
        }

        if self.scan_args.turbo {
            self.scan_args.no_base64 = true;
            self.scan_args.input_specifier_args.commit_metadata = false;
        }

        if self.scan_args.access_map && self.scan_args.no_validate {
            bail!("--blast-radius cannot be used with --no-validate");
        }

        self.scan_args.validate_audit_log_collisions(None, None)?;

        Ok(ScanOperation::Scan(self.scan_args))
    }
}

/// Determines whether two output paths refer to the same file.
///
/// Plain `PathBuf` equality misses aliases such as `--output report.json
/// --audit-log ./report.json`, pre-existing symlink and hard-link aliases,
/// paths whose parent directories are symlinks, and case-variant names on
/// case-insensitive filesystems. Paths are compared after absolutizing and
/// dot-normalization. Existing paths are compared by on-disk file identity
/// (`same-file`: device/inode on Unix, volume serial/file index on Windows),
/// which detects both symlink and hard-link aliases. Not-yet-existing paths
/// are resolved through their nearest existing ancestor so symlinked parent
/// directories are followed the same way the kernel resolves them at open
/// time; because such a final component cannot be identity-checked, its
/// comparison is case-folded conservatively (on a case-insensitive filesystem
/// — the default on macOS and Windows — case variants are the same file, and
/// on a case-sensitive one distinct names differing only by case are rejected
/// as a precaution).
fn paths_refer_to_same_file(a: &Path, b: &Path) -> bool {
    fn normalize(path: &Path) -> Option<PathBuf> {
        std::path::absolute(path)
            .ok()
            .map(|abs| abs.parse_dot().map(|dedotted| dedotted.into_owned()).unwrap_or(abs))
    }

    let (Some(a), Some(b)) = (normalize(a), normalize(b)) else { return a == b };
    if a == b {
        return true;
    }
    // When both paths exist, compare file identity so symlink and hard-link
    // aliases are detected on every platform: `same-file` compares device and
    // inode on Unix and volume serial number and file index on Windows. An
    // error means a path does not exist (or cannot be opened), so fall through
    // to resolution below.
    if let Ok(same) = same_file::is_same_file(&a, &b) {
        return same;
    }
    // Not both paths exist: resolve each through its nearest existing ancestor
    // so that a symlinked parent directory still aliases correctly.
    match (canonicalize_best_effort(&a), canonicalize_best_effort(&b)) {
        (Some(resolved_a), Some(resolved_b)) => {
            // Canonicalization resolves existing components to on-disk casing,
            // but not-yet-existing components keep their typed case. On
            // case-insensitive filesystems (the default on macOS and Windows)
            // case-variant names would still open the same file, so compare
            // the resolved paths case-insensitively rather than letting
            // --audit-log and --output clobber each other. On case-sensitive
            // filesystems this can conservatively flag distinct files; the
            // error tells the user exactly what to rename.
            resolved_a.to_string_lossy().to_lowercase()
                == resolved_b.to_string_lossy().to_lowercase()
        }
        _ => false,
    }
}

/// Canonicalizes a path, resolving the nearest existing ancestor when the
/// final components do not exist yet.
///
/// `fs::canonicalize` requires the whole path to exist, but output files are
/// typically created during the scan. Walking up to the closest existing
/// directory (which may be reached through a symlink) and re-appending the
/// missing components mirrors how a later open resolves the same path.
fn canonicalize_best_effort(path: &Path) -> Option<PathBuf> {
    if let Ok(canonical) = fs::canonicalize(path) {
        return Some(canonical);
    }
    let mut tail = vec![path.file_name()?.to_os_string()];
    for ancestor in path.ancestors().skip(1) {
        if let Ok(canonical) = fs::canonicalize(ancestor) {
            let mut resolved = canonical;
            for component in tail.iter().rev() {
                resolved.push(component);
            }
            return Some(resolved);
        }
        tail.push(ancestor.file_name()?.to_os_string());
    }
    None
}

fn path_is_beneath(candidate: &Path, root: &Path) -> bool {
    let candidate_path = root_for_comparison(candidate);
    let root_path = root_for_comparison(root);
    if candidate_path.starts_with(&root_path) && candidate_path != root_path {
        return true;
    }
    let Some(candidate) = canonicalize_best_effort(&candidate_path) else {
        return false;
    };
    let Some(root) = canonicalize_best_effort(&root_path) else {
        return false;
    };
    candidate.starts_with(&root) && candidate != root
}

fn root_for_comparison(path: &Path) -> PathBuf {
    std::path::absolute(path)
        .ok()
        .and_then(|absolute| absolute.parse_dot().ok().map(|path| path.into_owned()))
        .unwrap_or_else(|| path.to_path_buf())
}

fn parse_git_url_target(path: &Path) -> Option<GitUrl> {
    let raw = path.to_str()?.trim();
    if raw.is_empty() || raw == "-" || raw.contains('\\') {
        return None;
    }

    if let Ok(url) = GitUrl::from_str(raw) {
        return Some(url);
    }

    if raw.contains("://")
        || raw.starts_with('/')
        || raw.starts_with("./")
        || raw.starts_with("../")
        || raw.starts_with('~')
    {
        return None;
    }

    let (host, suffix) = raw.split_once('/')?;
    if host.is_empty() || suffix.is_empty() {
        return None;
    }

    let path_segments = suffix.split('/').filter(|segment| !segment.is_empty()).count();
    if path_segments < 2 {
        return None;
    }

    let host_looks_valid =
        host.contains('.') || host == "localhost" || host.parse::<IpAddr>().is_ok();
    if !host_looks_valid {
        return None;
    }

    GitUrl::from_str(&format!("https://{raw}")).ok()
}

#[derive(Subcommand, Debug, Clone)]
pub enum ScanInputCommand {
    /// Scan local files, directories, or Git repositories
    #[command(hide = true)]
    Filesystem(FilesystemScanArgs),

    /// Enumerate and scan GitHub repositories
    Github(GithubScanArgs),

    /// Enumerate and scan GitLab repositories
    Gitlab(GitLabScanArgs),

    /// Enumerate and scan Gitea repositories
    Gitea(GiteaScanArgs),

    /// Enumerate and scan Bitbucket repositories
    Bitbucket(BitbucketScanArgs),

    /// Enumerate and scan Azure DevOps repositories
    Azure(AzureScanArgs),

    /// Enumerate and scan Hugging Face repositories and buckets
    Huggingface(HuggingfaceScanArgs),

    /// Scan Slack messages and files matching a search query
    Slack(SlackScanArgs),

    /// Scan Microsoft Teams messages via Microsoft Graph
    Teams(TeamsScanArgs),

    /// Scan Jira issues using JQL
    Jira(JiraScanArgs),

    /// Scan Confluence content using CQL
    Confluence(ConfluenceScanArgs),

    /// Scan Postman workspaces, collections, and environments
    Postman(PostmanScanArgs),

    /// Scan an S3 bucket
    S3(S3ScanArgs),

    /// Scan a Google Cloud Storage bucket
    Gcs(GcsScanArgs),

    /// Scan Docker or OCI images
    Docker(DockerScanArgs),
}

#[derive(Args, Debug, Clone, Default)]
pub struct FilesystemScanArgs {
    /// Files, directories, or '-' for stdin
    #[arg(value_name = "PATH", value_hint = ValueHint::AnyPath)]
    pub paths: Vec<PathBuf>,

    /// Deprecated: git repository URLs to clone and scan. Prefer positional targets.
    #[arg(long = "git-url", value_hint = ValueHint::Url)]
    pub git_url: Vec<GitUrl>,
}

#[derive(Args, Debug, Clone)]
pub struct GithubScanArgs {
    #[command(flatten)]
    pub specifiers: GitHubRepoSpecifiers,

    /// Scan recent public events for the specified --user actors
    #[arg(
        long = "public-events",
        alias = "events",
        default_value_t = false,
        conflicts_with = "include_gists"
    )]
    pub public_events: bool,

    /// Look back this many hours when scanning --public-events
    #[arg(long = "event-lookback-hours", value_name = "HOURS", default_value_t = 24)]
    pub event_lookback_hours: u64,

    /// Read GitHub public-event users from a file, one username per line
    #[arg(long = "user-file", value_name = "FILE", value_hint = ValueHint::FilePath)]
    pub event_user_file: Option<PathBuf>,

    /// Include contributor repositories when scanning git URLs
    #[arg(long = "include-contributors", default_value_t = false)]
    pub include_contributors: bool,

    /// Limit the number of repositories cloned (including contributor repos)
    #[arg(long = "repo-clone-limit", value_name = "COUNT")]
    pub repo_clone_limit: Option<usize>,

    /// List matching repositories without scanning them
    #[arg(long = "list-only")]
    pub list_only: bool,

    /// Override the GitHub API URL (e.g. Enterprise)
    #[arg(
        long = "api-url",
        alias = "github-api-url",
        default_value = "https://api.github.com/",
        value_hint = ValueHint::Url
    )]
    pub api_url: Url,
}

#[derive(Args, Debug, Clone)]
pub struct GitLabScanArgs {
    #[command(flatten)]
    pub specifiers: GitLabRepoSpecifiers,

    /// Include contributor repositories when scanning git URLs
    #[arg(long = "include-contributors", default_value_t = false)]
    pub include_contributors: bool,

    /// Limit the number of repositories cloned (including contributor repos)
    #[arg(long = "repo-clone-limit", value_name = "COUNT")]
    pub repo_clone_limit: Option<usize>,

    /// List matching repositories without scanning them
    #[arg(long = "list-only")]
    pub list_only: bool,

    /// Override the GitLab API URL (e.g. self-hosted)
    #[arg(
        long = "api-url",
        alias = "gitlab-api-url",
        default_value = "https://gitlab.com/",
        value_hint = ValueHint::Url
    )]
    pub api_url: Url,
}

#[derive(Args, Debug, Clone)]
pub struct GiteaScanArgs {
    #[command(flatten)]
    pub specifiers: GiteaRepoSpecifiers,

    /// List matching repositories without scanning them
    #[arg(long = "list-only")]
    pub list_only: bool,

    /// Override the Gitea API URL (e.g. self-hosted)
    #[arg(
        long = "api-url",
        alias = "gitea-api-url",
        default_value = "https://gitea.com/api/v1/",
        value_hint = ValueHint::Url
    )]
    pub api_url: Url,
}

#[derive(Args, Debug, Clone)]
pub struct BitbucketScanArgs {
    #[command(flatten)]
    pub specifiers: BitbucketRepoSpecifiers,

    /// List matching repositories without scanning them
    #[arg(long = "list-only")]
    pub list_only: bool,

    /// Override the Bitbucket API URL (Cloud or self-hosted)
    #[arg(
        long = "api-url",
        alias = "bitbucket-api-url",
        default_value = "https://api.bitbucket.org/2.0/",
        value_hint = ValueHint::Url
    )]
    pub api_url: Url,
}

#[derive(Args, Debug, Clone)]
pub struct AzureScanArgs {
    #[command(flatten)]
    pub specifiers: AzureRepoSpecifiers,

    /// List matching repositories without scanning them
    #[arg(long = "list-only")]
    pub list_only: bool,

    /// Override the Azure DevOps base URL
    #[arg(
        long = "base-url",
        alias = "azure-base-url",
        default_value = "https://dev.azure.com/",
        value_hint = ValueHint::Url
    )]
    pub base_url: Url,
}

#[derive(Args, Debug, Clone, Default)]
pub struct HuggingfaceScanArgs {
    #[command(flatten)]
    pub specifiers: HuggingFaceRepoSpecifiers,

    /// List matching repositories without scanning them
    #[arg(long = "list-only")]
    pub list_only: bool,
}

#[derive(Args, Debug, Clone)]
pub struct SlackScanArgs {
    /// Slack search query to apply to messages and files
    #[arg(value_name = "QUERY")]
    pub query: String,

    /// Override the Slack API URL
    #[arg(
        long = "api-url",
        alias = "slack-api-url",
        default_value = "https://slack.com/api/",
        value_hint = ValueHint::Url
    )]
    pub api_url: Url,

    /// Maximum number of message results and file results to fetch
    #[arg(long = "max-results", default_value_t = 100)]
    pub max_results: usize,
}

#[derive(Args, Debug, Clone)]
pub struct TeamsScanArgs {
    /// Microsoft Teams search query
    #[arg(value_name = "QUERY")]
    pub query: String,

    /// Override the Microsoft Graph API URL
    #[arg(
        long = "api-url",
        alias = "teams-api-url",
        default_value = "https://graph.microsoft.com/",
        value_hint = ValueHint::Url
    )]
    pub api_url: Url,

    /// Maximum number of results to fetch
    #[arg(long = "max-results", default_value_t = 100)]
    pub max_results: usize,
}

#[derive(Args, Debug, Clone)]
pub struct JiraScanArgs {
    /// Jira base URL
    #[arg(long = "url", alias = "jira-url", value_hint = ValueHint::Url)]
    pub url: Url,

    /// JQL query to select Jira issues
    #[arg(long, alias = "jql")]
    pub jql: String,

    /// Maximum number of Jira issues to fetch
    #[arg(long = "max-results", default_value_t = 100)]
    pub max_results: usize,

    /// Fetch every issue matching the JQL query, ignoring `--max-results`
    #[arg(long = "all", conflicts_with = "max_results")]
    pub all: bool,

    /// Include Jira issue comments in the scan
    #[arg(long = "include-comments", default_value_t = false)]
    pub include_comments: bool,

    /// Include Jira issue changelog entries in the scan
    #[arg(long = "include-changelog", default_value_t = false)]
    pub include_changelog: bool,
}

#[derive(Args, Debug, Clone)]
pub struct ConfluenceScanArgs {
    /// Confluence base URL
    #[arg(long = "url", alias = "confluence-url", value_hint = ValueHint::Url)]
    pub url: Url,

    /// CQL query to select Confluence content
    #[arg(long, alias = "cql")]
    pub cql: String,

    /// Maximum number of results to fetch
    #[arg(long = "max-results", default_value_t = 100)]
    pub max_results: usize,

    /// Fetch every page matching the CQL query, ignoring `--max-results`
    #[arg(long = "all", conflicts_with = "max_results")]
    pub all: bool,
}

#[derive(Args, Debug, Clone)]
pub struct PostmanScanArgs {
    /// Scan a Postman workspace by ID or web URL (repeatable)
    #[arg(long = "workspace", alias = "postman-workspace", value_name = "ID_OR_URL")]
    pub workspaces: Vec<String>,

    /// Scan a single Postman collection by UID or web URL (repeatable)
    #[arg(long = "collection", alias = "postman-collection", value_name = "UID_OR_URL")]
    pub collections: Vec<String>,

    /// Scan a single Postman environment by UID (repeatable)
    #[arg(long = "environment", alias = "postman-environment", value_name = "UID")]
    pub environments: Vec<String>,

    /// Scan every workspace, collection, and environment visible to the API key
    #[arg(
        long = "all",
        alias = "postman-all",
        conflicts_with_all = ["workspaces", "collections", "environments"],
    )]
    pub all: bool,

    /// Include Postman mocks and monitors when scanning a workspace (off by default)
    #[arg(long = "include-mocks-monitors", alias = "postman-include-mocks-monitors")]
    pub include_mocks_monitors: bool,

    /// Override the Postman API base URL
    #[arg(
        long = "api-url",
        alias = "postman-api-url",
        default_value = "https://api.getpostman.com/",
        value_hint = ValueHint::Url,
    )]
    pub api_url: Url,

    /// Maximum number of resources to fetch
    #[arg(long = "max-results", default_value_t = 100)]
    pub max_results: usize,
}

#[derive(Args, Debug, Clone)]
pub struct S3ScanArgs {
    /// S3 bucket to scan
    #[arg(value_name = "BUCKET")]
    pub bucket: String,

    /// Optional prefix within the bucket
    #[arg(long = "prefix", alias = "s3-prefix")]
    pub prefix: Option<String>,

    /// AWS IAM role ARN to assume
    #[arg(long = "role-arn")]
    pub role_arn: Option<String>,

    /// AWS profile name to use for credentials
    #[arg(long = "profile", alias = "aws-local-profile")]
    pub profile: Option<String>,
}

#[derive(Args, Debug, Clone)]
pub struct GcsScanArgs {
    /// Google Cloud Storage bucket to scan
    #[arg(value_name = "BUCKET")]
    pub bucket: String,

    /// Optional prefix within the bucket
    #[arg(long = "prefix", alias = "gcs-prefix")]
    pub prefix: Option<String>,

    /// Service account JSON file for authentication
    #[arg(long = "service-account", alias = "gcs-service-account", value_hint = ValueHint::FilePath)]
    pub service_account: Option<PathBuf>,
}

#[derive(Args, Debug, Clone)]
pub struct DockerScanArgs {
    /// Docker or OCI images to scan
    #[arg(value_name = "IMAGE")]
    pub images: Vec<String>,

    /// Docker image archive files to scan, such as files produced by docker save
    #[arg(long = "archive", value_name = "PATH", value_hint = ValueHint::FilePath)]
    pub archives: Vec<PathBuf>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn equivalent_output_paths_are_detected() {
        assert!(paths_refer_to_same_file(Path::new("report.json"), Path::new("report.json")));
        assert!(paths_refer_to_same_file(Path::new("report.json"), Path::new("./report.json")));
        assert!(paths_refer_to_same_file(Path::new("a/../report.json"), Path::new("report.json")));
        // Textual equality is not required when the paths are not normalized
        // the same way, but existing files must still resolve consistently.
        assert!(!paths_refer_to_same_file(Path::new("report.json"), Path::new("other.json")));
    }

    #[test]
    fn case_variant_names_are_treated_as_the_same_file() {
        // On case-insensitive filesystems (the default on macOS and Windows)
        // case-variant names are the same file once created, so not-yet-
        // existing paths are compared case-folded.
        assert!(paths_refer_to_same_file(Path::new("report.json"), Path::new("Report.json")));
        assert!(paths_refer_to_same_file(
            Path::new("sub/report.json"),
            Path::new("sub/REPORT.JSON")
        ));
        // Different names are still distinct.
        assert!(!paths_refer_to_same_file(Path::new("Report.json"), Path::new("Repo.json")));
    }

    #[cfg(unix)]
    #[test]
    fn symlink_aliases_are_detected_for_existing_files() {
        let dir = tempfile::tempdir().unwrap();
        let target = dir.path().join("report.json");
        std::fs::write(&target, b"x").unwrap();
        let link = dir.path().join("alias.json");
        std::os::unix::fs::symlink(&target, &link).unwrap();

        assert!(paths_refer_to_same_file(&target, &link));
        assert!(paths_refer_to_same_file(Path::new("."), Path::new("./")));
    }

    #[cfg(unix)]
    #[test]
    fn symlinked_parent_directories_are_detected_for_new_files() {
        let dir = tempfile::tempdir().unwrap();
        let real = dir.path().join("real");
        std::fs::create_dir(&real).unwrap();
        let link = dir.path().join("link");
        std::os::unix::fs::symlink(&real, &link).unwrap();

        // Neither output file exists yet, but both opens would resolve through
        // the symlinked parent to the same file.
        assert!(paths_refer_to_same_file(&real.join("report.json"), &link.join("report.json")));
        // Same symlinked parent, different final components: distinct files.
        assert!(!paths_refer_to_same_file(&real.join("report.json"), &link.join("other.json")));
    }

    #[cfg(unix)]
    #[test]
    fn hard_link_aliases_are_detected() {
        let dir = tempfile::tempdir().unwrap();
        let a = dir.path().join("a.json");
        std::fs::write(&a, b"x").unwrap();
        let b = dir.path().join("b.json");
        std::fs::hard_link(&a, &b).unwrap();

        assert!(paths_refer_to_same_file(&a, &b));
    }

    #[test]
    fn audit_log_inside_scanned_directory_is_rejected() {
        let dir = tempfile::tempdir().unwrap();
        let input = dir.path().join("workspace");
        std::fs::create_dir(&input).unwrap();
        let audit_log = input.join("audit.jsonl");

        assert!(path_is_beneath(&audit_log, &input));
    }
}