scour-secrets 0.19.0

Deterministic one-way data sanitization engine
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
use crate::cli_args::{InitArgs, InstallHookArgs};
use crate::hooks::{
    global_default_secrets_path, global_settings_path, run_install_hook, sanitize_config_dir,
};
use std::fs;
use std::path::{Path, PathBuf};

/// Unified scan configuration loaded from any of the three config files:
///
/// - `~/.config/scour-secrets/settings.yaml`      — global per-user defaults
/// - `<project>/.scour-secrets.yaml`              — per-project defaults (cwd-walk)
/// - `<namespace-dir>/settings.yaml`         — per-namespace defaults (MCP only)
///
/// All fields are optional. An explicit CLI flag always wins over any layer.
/// Lists (`app`, `allow`, `exclude_path`, `include_path`, `context_keywords`)
/// are merged additively across all layers rather than replaced.
#[derive(Debug, Default, serde::Deserialize)]
pub(crate) struct SanitizeConfig {
    // --- Additive lists -------------------------------------------------
    /// App bundles to load. Merged across all layers; CLI `--app` appends further.
    #[serde(default)]
    pub(crate) app: Vec<String>,
    /// Allow-list patterns (exact, glob, or `regex:<pat>`). Merged across layers.
    #[serde(default)]
    pub(crate) allow: Vec<String>,
    /// Path glob patterns to exclude. Merged across layers.
    #[serde(default)]
    pub(crate) exclude_path: Vec<String>,
    /// Path glob patterns to include (directory walks only). Merged across layers.
    #[serde(default)]
    pub(crate) include_path: Vec<String>,
    /// Extra context-extraction keywords. Merged with built-in defaults.
    #[serde(default)]
    pub(crate) context_keywords: Vec<String>,

    // --- Source files (project / namespace only) -------------------------
    /// Path to a secrets file, relative to the config file location.
    pub(crate) secrets_file: Option<PathBuf>,
    /// Treat the secrets file as AES-GCM encrypted.
    pub(crate) encrypted_secrets: Option<bool>,
    /// Path to a field-level profile YAML, relative to the config file location.
    pub(crate) profile: Option<PathBuf>,

    // --- Bool scan-behavior flags ----------------------------------------
    /// Exit with code 2 when any match is found (`--fail-on-match`).
    pub(crate) fail_on_match: Option<bool>,
    /// Abort on the first processing error (`--strict`).
    pub(crate) strict: Option<bool>,
    /// Suppress structured-to-scanner value handoff (`--no-structured-handoff`).
    pub(crate) no_structured_handoff: Option<bool>,
    /// Disable the field-name signal heuristic (`--no-field-signal`).
    pub(crate) no_field_signal: Option<bool>,
    /// Bypass all structured processors, streaming scanner only (`--force-text`).
    pub(crate) force_text: Option<bool>,
    /// Process binary entries inside archives (`--include-binary`).
    pub(crate) include_binary: Option<bool>,
    /// Walk hidden files and directories (`--hidden`).
    pub(crate) hidden: Option<bool>,
    /// Replace context_keywords entirely instead of merging (`--context-keywords-replace`).
    pub(crate) context_keywords_replace: Option<bool>,
    /// Case-sensitive keyword matching for context extraction (`--context-case-sensitive`).
    pub(crate) context_case_sensitive: Option<bool>,
    /// Extract keyword-matched log context after sanitization (`--extract-context`).
    pub(crate) extract_context: Option<bool>,

    // --- Option<T> parameters (None = CLI default applies) ---------------
    /// Worker thread count; `null` = auto-detect (`--threads`).
    pub(crate) threads: Option<usize>,
    /// Shannon entropy threshold for high-entropy token detection (`--entropy-threshold`).
    pub(crate) entropy_threshold: Option<f64>,
    /// Log output format: `"human"` or `"json"` (`--log-format`).
    pub(crate) log_format: Option<String>,
    /// Log level: off, error, warn, info, debug, trace (`--log-level`).
    pub(crate) log_level: Option<String>,

    // --- Numeric fields with concrete CLI defaults -----------------------
    /// Streaming chunk size in bytes; default 1 MiB (`--chunk-size`).
    pub(crate) chunk_size: Option<usize>,
    /// Max unique mapping cache entries; default 10 M (`--max-mappings`).
    pub(crate) max_mappings: Option<usize>,
    /// Max structured file size in bytes; default 256 MiB (`--max-structured-size`).
    pub(crate) max_structured_size: Option<u64>,
    /// Max recursive archive nesting depth; default 5 (`--max-archive-depth`).
    pub(crate) max_archive_depth: Option<u32>,
    /// Context lines captured before/after each keyword hit; default 10 (`--context-lines`).
    pub(crate) context_lines: Option<usize>,
    /// Max keyword matches returned per file; default 50 (`--max-context-matches`).
    pub(crate) max_context_matches: Option<usize>,
    /// Max match locations stored per run; default 500 (`--max-match-locations`).
    pub(crate) max_match_locations: Option<usize>,
    /// Progress reporting interval in ms; default 200 (`--progress-interval-ms`).
    pub(crate) progress_interval_ms: Option<u64>,

    // --- Display flags ---------------------------------------------------
    /// Suppress all progress output (`--no-progress`).
    pub(crate) no_progress: Option<bool>,
    /// Suppress non-error output (`--quiet`).
    pub(crate) quiet: Option<bool>,
}

// ---------------------------------------------------------------------------
// Global settings (~/.config/scour-secrets/settings.yaml)
// ---------------------------------------------------------------------------

/// Load `~/.config/scour-secrets/settings.yaml`. Silently returns defaults when
/// absent or unreadable. Set `SCOUR_SECRETS_NO_SETTINGS=1` to skip entirely.
pub(crate) fn load_settings() -> SanitizeConfig {
    if std::env::var("SCOUR_SECRETS_NO_SETTINGS").as_deref() == Ok("1") {
        return SanitizeConfig::default();
    }
    let path = global_settings_path();
    load_yaml_config(&path, "settings")
}

// ---------------------------------------------------------------------------
// Project config (.scour-secrets.yaml)
// ---------------------------------------------------------------------------

/// Search for `.scour-secrets.yaml` starting from `dir` and walking upward.
/// Returns the path of the first file found, or `None`.
pub(crate) fn find_project_config_from(dir: &Path) -> Option<PathBuf> {
    let mut current = dir.to_path_buf();
    loop {
        let candidate = current.join(".scour-secrets.yaml");
        if candidate.is_file() {
            return Some(candidate);
        }
        current = current.parent()?.to_path_buf();
    }
}

/// Locate the project config to load.
///
/// Resolution order:
/// 1. `SCOUR_SECRETS_NO_CONFIG=1` → skip (returns `None`).
/// 2. `SCOUR_SECRETS_CONFIG=<path>` → use that file if it exists.
/// 3. Walk up from CWD looking for `.scour-secrets.yaml`.
pub(crate) fn find_project_config() -> Option<PathBuf> {
    if std::env::var("SCOUR_SECRETS_NO_CONFIG").as_deref() == Ok("1") {
        return None;
    }
    if let Ok(explicit) = std::env::var("SCOUR_SECRETS_CONFIG") {
        let p = PathBuf::from(&explicit);
        if p.is_file() {
            return Some(p);
        }
        eprintln!("warning: SCOUR_SECRETS_CONFIG={explicit} does not exist — ignoring");
        return None;
    }
    let cwd = std::env::current_dir().ok()?;
    find_project_config_from(&cwd)
}

/// Parse a `.scour-secrets.yaml` file. Returns `(config, config_dir)` so relative
/// paths inside the file can be resolved against the file's location.
/// Silently returns defaults on read or parse error.
pub(crate) fn load_project_config(path: &Path) -> (SanitizeConfig, PathBuf) {
    let config_dir = path.parent().unwrap_or(Path::new(".")).to_path_buf();
    (load_yaml_config(path, "project config"), config_dir)
}

// ---------------------------------------------------------------------------
// Shared YAML loader
// ---------------------------------------------------------------------------

fn load_yaml_config(path: &Path, label: &str) -> SanitizeConfig {
    if !path.exists() {
        return SanitizeConfig::default();
    }
    match fs::read_to_string(path) {
        Ok(text) => serde_yaml_ng::from_str(&text).unwrap_or_else(|e| {
            eprintln!(
                "warning: could not parse {} {}: {e} — ignoring",
                label,
                path.display()
            );
            SanitizeConfig::default()
        }),
        Err(e) => {
            eprintln!(
                "warning: could not read {} {}: {e} — ignoring",
                label,
                path.display()
            );
            SanitizeConfig::default()
        }
    }
}

// ---------------------------------------------------------------------------
// Templates
// ---------------------------------------------------------------------------

/// Template written to `settings.yaml` by `scour-secrets init-hook`.
pub(crate) const SETTINGS_TEMPLATE: &str = "\
# scour-secrets settings  (~/.config/scour-secrets/settings.yaml)
# Values here become defaults for every run. Explicit CLI flags always win.
# All fields are optional — uncomment and edit to activate.

# ── Pattern loading ──────────────────────────────────────────────────────────

# App bundles to load on every run (--app). Additive with --app on the CLI.
# app:
#   - gitlab
#   - kubernetes

# Values that pass through unchanged; supports * glob and regex:<pat> (--allow).
# allow:
#   - localhost
#   - \"*.internal\"

# ── Path filtering ───────────────────────────────────────────────────────────

# Glob patterns to skip when walking directories (--exclude-path).
# exclude_path:
#   - \"*.test.yaml\"
#   - \"fixtures/**\"

# Glob patterns to restrict directory walks to (--include-path).
# include_path:
#   - \"**/*.log\"
#   - \"**/*.conf\"

# ── Scan behavior ────────────────────────────────────────────────────────────

# Exit with code 2 when any match is found (--fail-on-match).
# fail_on_match: false

# Abort on the first processing error (--strict).
# strict: false

# Suppress the structured-to-scanner value handoff (--no-structured-handoff).
# no_structured_handoff: false

# Disable the field-name signal heuristic (--no-field-signal).
# no_field_signal: false

# Bypass all structured processors; streaming scanner only (--force-text).
# force_text: false

# Process binary entries inside archives (--include-binary).
# include_binary: false

# Walk hidden files and directories (--hidden).
# hidden: false

# Shannon entropy threshold for high-entropy token detection (--entropy-threshold).
# entropy_threshold: 4.5

# ── Performance ──────────────────────────────────────────────────────────────

# Worker thread count; omit for auto-detect (--threads).
# threads: 4

# Streaming chunk size in bytes (--chunk-size). Default: 1048576 (1 MiB).
# chunk_size: 1048576

# Max unique mapping cache entries (--max-mappings). Default: 10000000.
# max_mappings: 10000000

# Max structured file size in bytes (--max-structured-size). Default: 268435456.
# max_structured_size: 268435456

# Max recursive archive nesting depth (--max-archive-depth). Default: 5.
# max_archive_depth: 5

# ── Context extraction ───────────────────────────────────────────────────────

# Enable keyword-matched log context extraction (--extract-context).
# extract_context: false

# Context lines captured before/after each hit (--context-lines). Default: 10.
# context_lines: 10

# Extra keywords to flag in addition to built-in defaults (--context-keywords).
# context_keywords:
#   - timeout
#   - oomkilled

# Replace built-in keyword list entirely (--context-keywords-replace).
# context_keywords_replace: false

# Case-sensitive keyword matching (--context-case-sensitive).
# context_case_sensitive: false

# Max keyword matches returned per file (--max-context-matches). Default: 50.
# max_context_matches: 50

# ── Display ──────────────────────────────────────────────────────────────────

# Log output format: \"human\" (default) or \"json\" for SIEM ingestion (--log-format).
# log_format: human

# Log level: off, error, warn (default), info, debug, trace (--log-level).
# log_level: warn

# Suppress progress output (--no-progress).
# no_progress: false

# Suppress non-error output entirely (--quiet).
# quiet: false
";

// ---------------------------------------------------------------------------
// show-config
// ---------------------------------------------------------------------------

pub(crate) fn run_show_config() -> Result<(), (String, i32)> {
    let secrets_path = global_default_secrets_path();
    let settings_path = global_settings_path();
    let no_settings = std::env::var("SCOUR_SECRETS_NO_SETTINGS").as_deref() == Ok("1");
    let no_config = std::env::var("SCOUR_SECRETS_NO_CONFIG").as_deref() == Ok("1");

    println!("Config directory: {}", sanitize_config_dir().display());
    println!();

    // ── secrets file ──────────────────────────────────────────────────────────
    print!("Secrets:  {}", secrets_path.display());
    if secrets_path.exists() {
        println!(" (found — auto-loaded when --secrets-file is not given)");
    } else {
        println!(" (not found — will be created automatically on the next plain run)");
    }

    // ── settings file ─────────────────────────────────────────────────────────
    println!();
    print!("Settings: {}", settings_path.display());
    if no_settings {
        println!(" (skipped — SCOUR_SECRETS_NO_SETTINGS=1)");
    } else if !settings_path.exists() {
        println!(" (not found — run 'scour-secrets init-hook' to create it)");
    } else {
        println!();
        let s = load_settings();
        show_config_fields(&s, None);
    }

    // ── app bundle copies ─────────────────────────────────────────────────────
    println!();
    if let Some(apps_dir) = crate::apps::user_apps_dir() {
        print!("App copies: {}", apps_dir.display());
        let provisioned: Vec<String> = std::fs::read_dir(&apps_dir)
            .map(|entries| {
                entries
                    .flatten()
                    .filter(|e| e.file_type().map(|t| t.is_dir()).unwrap_or(false))
                    .map(|e| e.file_name().to_string_lossy().to_string())
                    .collect()
            })
            .unwrap_or_default();
        if provisioned.is_empty() {
            println!(" (empty — populated on first --app use)");
        } else {
            let mut names = provisioned;
            names.sort();
            println!();
            println!("  provisioned: {}", names.join(", "));
        }
        println!(
            "  --app runs persist discovered values into <app>/secrets.yaml so later\n\
             \x20 runs re-redact them; pass --no-structured-handoff to skip the write-back."
        );
    }

    // ── project config (.scour-secrets.yaml) ──────────────────────────────────────
    println!();
    if no_config {
        println!("Project config: (skipped — SCOUR_SECRETS_NO_CONFIG=1)");
        return Ok(());
    }
    match find_project_config() {
        None => {
            println!(
                "Project config: (none — no .scour-secrets.yaml found in this directory or its parents)"
            );
        }
        Some(ref path) => {
            println!("Project config: {}", path.display());
            let (pc, config_dir) = load_project_config(path);
            show_config_fields(&pc, Some(&config_dir));
        }
    }

    Ok(())
}

fn show_config_fields(cfg: &SanitizeConfig, config_dir: Option<&Path>) {
    fn list(label: &str, v: &[String]) {
        if v.is_empty() {
            println!("  {label:<26} (not set)");
        } else {
            println!("  {label:<26} {}", v.join(", "));
        }
    }
    fn opt<T: std::fmt::Display>(label: &str, v: Option<T>) {
        match v {
            Some(ref val) => println!("  {label:<26} {val}"),
            None => println!("  {label:<26} (not set)"),
        }
    }
    fn path_field(label: &str, v: Option<&Path>, base: Option<&Path>) {
        match v {
            Some(p) => {
                let resolved = if p.is_absolute() {
                    p.to_path_buf()
                } else if let Some(b) = base {
                    b.join(p)
                } else {
                    p.to_path_buf()
                };
                println!("  {label:<26} {}", resolved.display());
            }
            None => println!("  {label:<26} (not set)"),
        }
    }

    list("app:", &cfg.app);
    list("allow:", &cfg.allow);
    list("exclude_path:", &cfg.exclude_path);
    list("include_path:", &cfg.include_path);
    if config_dir.is_some() {
        path_field("secrets_file:", cfg.secrets_file.as_deref(), config_dir);
        opt("encrypted_secrets:", cfg.encrypted_secrets);
        path_field("profile:", cfg.profile.as_deref(), config_dir);
    }
    opt("fail_on_match:", cfg.fail_on_match);
    opt("strict:", cfg.strict);
    opt("no_structured_handoff:", cfg.no_structured_handoff);
    opt("no_field_signal:", cfg.no_field_signal);
    opt("force_text:", cfg.force_text);
    opt("include_binary:", cfg.include_binary);
    opt("hidden:", cfg.hidden);
    opt("entropy_threshold:", cfg.entropy_threshold);
    opt("threads:", cfg.threads);
    opt("chunk_size:", cfg.chunk_size);
    opt("max_archive_depth:", cfg.max_archive_depth);
    opt("extract_context:", cfg.extract_context);
    opt("context_lines:", cfg.context_lines);
    list("context_keywords:", &cfg.context_keywords);
    opt("context_keywords_replace:", cfg.context_keywords_replace);
    opt("max_context_matches:", cfg.max_context_matches);
    opt(
        "log_format:",
        cfg.log_format.as_deref().map(|s| s.to_string()),
    );
    opt(
        "log_level:",
        cfg.log_level.as_deref().map(|s| s.to_string()),
    );
    opt("no_progress:", cfg.no_progress);
    opt("quiet:", cfg.quiet);
}

// ---------------------------------------------------------------------------
// init-hook
// ---------------------------------------------------------------------------

pub(crate) fn run_init(args: &InitArgs) -> Result<(), (String, i32)> {
    let settings_path = global_settings_path();

    let hook_args = InstallHookArgs {
        hook: args.hook,
        mode: args.mode,
        global: args.global,
        force: args.force,
        remove: false,
        app: None,
        secrets_file: None,
        dry_run: args.dry_run,
    };

    if args.dry_run {
        println!("Would create (dry-run):");
        if settings_path.exists() && !args.force {
            println!(
                "  {} (already exists — use --force to overwrite)",
                settings_path.display()
            );
        } else {
            println!("  {} — persistent flag defaults", settings_path.display());
        }
        println!();
        run_install_hook(&hook_args)?;
        return Ok(());
    }

    if settings_path.exists() && !args.force {
        println!("Settings file already exists: {}", settings_path.display());
        println!("  Use --force to overwrite, or edit it directly.");
    } else {
        if let Some(parent) = settings_path.parent() {
            fs::create_dir_all(parent)
                .map_err(|e| (format!("failed to create {}: {e}", parent.display()), 1))?;
        }
        fs::write(&settings_path, SETTINGS_TEMPLATE).map_err(|e| {
            (
                format!("failed to write {}: {e}", settings_path.display()),
                1,
            )
        })?;
        println!("Created: {}", settings_path.display());
        println!("  Uncomment fields to set persistent flag defaults.");
    }

    println!();
    run_install_hook(&hook_args)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn env_lock() -> std::sync::MutexGuard<'static, ()> {
        crate::test_env_lock()
    }

    // ── find_project_config_from ─────────────────────────────────────────────

    #[test]
    fn find_project_config_from_finds_in_same_dir() {
        let dir = tempfile::tempdir().unwrap();
        let config = dir.path().join(".scour-secrets.yaml");
        fs::write(&config, "").unwrap();
        assert_eq!(find_project_config_from(dir.path()), Some(config));
    }

    #[test]
    fn find_project_config_from_finds_in_parent() {
        let dir = tempfile::tempdir().unwrap();
        let config = dir.path().join(".scour-secrets.yaml");
        fs::write(&config, "").unwrap();
        let child = dir.path().join("subdir/nested");
        fs::create_dir_all(&child).unwrap();
        assert_eq!(find_project_config_from(&child), Some(config));
    }

    #[test]
    fn find_project_config_from_returns_none_when_absent() {
        let dir = tempfile::tempdir().unwrap();
        let child = dir.path().join("a/b/c");
        fs::create_dir_all(&child).unwrap();
        let result = find_project_config_from(&child);
        if let Some(ref found) = result {
            assert!(
                !found.starts_with(dir.path()),
                "should not find config inside temp dir"
            );
        }
    }

    // ── load_project_config ──────────────────────────────────────────────────

    #[test]
    fn load_project_config_parses_all_fields() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join(".scour-secrets.yaml");
        fs::write(
            &path,
            r#"
app: [gitlab, kubernetes]
allow: [localhost, "*.internal"]
exclude_path: ["*.test.yaml"]
include_path: ["**/*.log"]
secrets_file: secrets.yaml
encrypted_secrets: true
profile: profile.yaml
fail_on_match: true
strict: true
no_structured_handoff: true
no_field_signal: true
force_text: true
include_binary: true
hidden: true
entropy_threshold: 4.5
threads: 8
chunk_size: 2097152
max_archive_depth: 3
context_lines: 5
context_keywords: [timeout, oomkilled]
log_format: json
log_level: debug
no_progress: true
quiet: true
"#,
        )
        .unwrap();
        let (cfg, cfg_dir) = load_project_config(&path);
        assert_eq!(cfg.app, vec!["gitlab", "kubernetes"]);
        assert_eq!(cfg.allow, vec!["localhost", "*.internal"]);
        assert_eq!(cfg.exclude_path, vec!["*.test.yaml"]);
        assert_eq!(cfg.include_path, vec!["**/*.log"]);
        assert_eq!(cfg.secrets_file, Some(PathBuf::from("secrets.yaml")));
        assert_eq!(cfg.encrypted_secrets, Some(true));
        assert_eq!(cfg.profile, Some(PathBuf::from("profile.yaml")));
        assert_eq!(cfg.fail_on_match, Some(true));
        assert_eq!(cfg.strict, Some(true));
        assert_eq!(cfg.no_structured_handoff, Some(true));
        assert_eq!(cfg.no_field_signal, Some(true));
        assert_eq!(cfg.force_text, Some(true));
        assert_eq!(cfg.include_binary, Some(true));
        assert_eq!(cfg.hidden, Some(true));
        assert_eq!(cfg.entropy_threshold, Some(4.5));
        assert_eq!(cfg.threads, Some(8));
        assert_eq!(cfg.chunk_size, Some(2_097_152));
        assert_eq!(cfg.max_archive_depth, Some(3));
        assert_eq!(cfg.context_lines, Some(5));
        assert_eq!(cfg.context_keywords, vec!["timeout", "oomkilled"]);
        assert_eq!(cfg.log_format.as_deref(), Some("json"));
        assert_eq!(cfg.log_level.as_deref(), Some("debug"));
        assert_eq!(cfg.no_progress, Some(true));
        assert_eq!(cfg.quiet, Some(true));
        assert_eq!(cfg_dir, dir.path());
    }

    #[test]
    fn load_project_config_partial_file_fills_rest_with_defaults() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join(".scour-secrets.yaml");
        fs::write(&path, "app:\n  - gitlab\nfail_on_match: true\n").unwrap();
        let (cfg, _) = load_project_config(&path);
        assert_eq!(cfg.app, vec!["gitlab"]);
        assert_eq!(cfg.fail_on_match, Some(true));
        assert!(cfg.strict.is_none());
        assert!(cfg.secrets_file.is_none());
    }

    #[test]
    fn load_project_config_returns_default_on_invalid_yaml() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join(".scour-secrets.yaml");
        fs::write(&path, "this: is: not: valid: ][[[").unwrap();
        let (cfg, _) = load_project_config(&path);
        assert!(cfg.app.is_empty());
        assert!(cfg.fail_on_match.is_none());
    }

    #[test]
    fn load_project_config_resolves_config_dir() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join(".scour-secrets.yaml");
        fs::write(&path, "secrets_file: secrets.yaml\n").unwrap();
        let (cfg, cfg_dir) = load_project_config(&path);
        assert_eq!(cfg.secrets_file, Some(PathBuf::from("secrets.yaml")));
        assert_eq!(cfg_dir, dir.path());
    }

    // ── load_settings ────────────────────────────────────────────────────────

    #[test]
    fn load_settings_skips_when_env_var_set() {
        let _guard = env_lock();
        std::env::set_var("SCOUR_SECRETS_NO_SETTINGS", "1");
        let s = load_settings();
        std::env::remove_var("SCOUR_SECRETS_NO_SETTINGS");
        assert!(s.app.is_empty());
        assert!(s.allow.is_empty());
    }

    #[test]
    fn load_settings_returns_default_when_file_missing() {
        let _guard = env_lock();
        let dir = tempfile::tempdir().unwrap();
        std::env::remove_var("SCOUR_SECRETS_NO_SETTINGS");
        #[cfg(windows)]
        std::env::set_var("APPDATA", dir.path());
        #[cfg(not(windows))]
        std::env::set_var("XDG_CONFIG_HOME", dir.path());
        let s = load_settings();
        #[cfg(windows)]
        std::env::remove_var("APPDATA");
        #[cfg(not(windows))]
        std::env::remove_var("XDG_CONFIG_HOME");
        assert!(s.app.is_empty());
    }

    #[test]
    fn load_settings_parses_all_fields() {
        let _guard = env_lock();
        let dir = tempfile::tempdir().unwrap();
        let config_dir = dir.path().join("scour-secrets");
        fs::create_dir_all(&config_dir).unwrap();
        fs::write(
            config_dir.join("settings.yaml"),
            "app:\n  - gitlab\nallow:\n  - localhost\nfail_on_match: true\nthreads: 4\nno_progress: true\nforce_text: true\nentropy_threshold: 3.5\n",
        )
        .unwrap();
        std::env::remove_var("SCOUR_SECRETS_NO_SETTINGS");
        #[cfg(windows)]
        std::env::set_var("APPDATA", dir.path());
        #[cfg(not(windows))]
        std::env::set_var("XDG_CONFIG_HOME", dir.path());
        let s = load_settings();
        #[cfg(windows)]
        std::env::remove_var("APPDATA");
        #[cfg(not(windows))]
        std::env::remove_var("XDG_CONFIG_HOME");
        assert_eq!(s.app, vec!["gitlab"]);
        assert_eq!(s.allow, vec!["localhost"]);
        assert_eq!(s.fail_on_match, Some(true));
        assert_eq!(s.threads, Some(4));
        assert_eq!(s.no_progress, Some(true));
        assert_eq!(s.force_text, Some(true));
        assert_eq!(s.entropy_threshold, Some(3.5));
    }

    #[test]
    fn load_settings_returns_default_on_malformed_yaml() {
        let _guard = env_lock();
        let dir = tempfile::tempdir().unwrap();
        let config_dir = dir.path().join("scour-secrets");
        fs::create_dir_all(&config_dir).unwrap();
        fs::write(
            config_dir.join("settings.yaml"),
            "this: is: not: valid: ][[[",
        )
        .unwrap();
        std::env::remove_var("SCOUR_SECRETS_NO_SETTINGS");
        #[cfg(windows)]
        std::env::set_var("APPDATA", dir.path());
        #[cfg(not(windows))]
        std::env::set_var("XDG_CONFIG_HOME", dir.path());
        let s = load_settings();
        #[cfg(windows)]
        std::env::remove_var("APPDATA");
        #[cfg(not(windows))]
        std::env::remove_var("XDG_CONFIG_HOME");
        assert!(s.app.is_empty());
    }

    // ── find_project_config ──────────────────────────────────────────────────

    #[test]
    fn find_project_config_returns_none_when_disabled() {
        let _guard = env_lock();
        std::env::set_var("SCOUR_SECRETS_NO_CONFIG", "1");
        let result = find_project_config();
        std::env::remove_var("SCOUR_SECRETS_NO_CONFIG");
        assert!(result.is_none());
    }

    #[test]
    fn find_project_config_uses_explicit_env_var() {
        let _guard = env_lock();
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("custom.yaml");
        fs::write(&path, "").unwrap();
        std::env::remove_var("SCOUR_SECRETS_NO_CONFIG");
        std::env::set_var("SCOUR_SECRETS_CONFIG", path.to_str().unwrap());
        let result = find_project_config();
        std::env::remove_var("SCOUR_SECRETS_CONFIG");
        assert_eq!(result, Some(path));
    }

    #[test]
    fn find_project_config_returns_none_for_nonexistent_explicit_path() {
        let _guard = env_lock();
        std::env::remove_var("SCOUR_SECRETS_NO_CONFIG");
        std::env::set_var("SCOUR_SECRETS_CONFIG", "/nonexistent/path/custom.yaml");
        let result = find_project_config();
        std::env::remove_var("SCOUR_SECRETS_CONFIG");
        assert!(result.is_none());
    }
}