Skip to main content

rust_sanitize/
secrets.rs

1//! Encrypted secrets management.
2//!
3//! This module provides **in-memory-only** decryption of user-supplied
4//! secrets files. Secrets are never written to disk in plaintext form;
5//! they are loaded from an AES-256-GCM encrypted `.enc` file, decrypted
6//! into memory, parsed, and converted directly into [`ScanPattern`]s
7//! for the streaming scanner.
8//!
9//! # Encryption Format
10//!
11//! ```text
12//! ┌────────────────────────────────┬──────────────┬─────────────────────────────┐
13//! │  Salt (32 B)                   │  Nonce (12 B)│  AES-256-GCM Ciphertext     │
14//! └────────────────────────────────┴──────────────┴─────────────────────────────┘
15//! ```
16//!
17//! - **Salt** (32 bytes): random, used for PBKDF2-derived key.
18//! - **Nonce** (12 bytes): random, for AES-256-GCM.
19//! - **Ciphertext**: authenticated encryption of the plaintext secrets
20//!   file (JSON / YAML / TOML).
21//!
22//! The 256-bit AES key is derived from the user password using
23//! PBKDF2-HMAC-SHA256 with 600 000 iterations, which meets current
24//! OWASP recommendations.
25//!
26//! # Key Derivation
27//!
28//! ```text
29//! key = PBKDF2-HMAC-SHA256(password, salt, iterations=600_000, dkLen=32)
30//! ```
31//!
32//! # Secrets File Schema
33//!
34//! The plaintext secrets file (before encryption) must deserialize to
35//! `Vec<SecretEntry>`:
36//!
37//! ```json
38//! [
39//!   {
40//!     "pattern": "alice@corp\\.com",
41//!     "kind": "regex",
42//!     "category": "email",
43//!     "label": "alice_email"
44//!   },
45//!   {
46//!     "pattern": "sk-proj-abc123secret",
47//!     "kind": "literal",
48//!     "category": "custom:api_key",
49//!     "label": "openai_key"
50//!   }
51//! ]
52//! ```
53//!
54//! # Thread Safety
55//!
56//! All public types are `Send + Sync`. Decrypted secrets use
57//! [`zeroize::Zeroizing`] to scrub plaintext from memory on drop.
58//!
59//! # Security Considerations
60//!
61//! - AES-256-GCM provides both confidentiality and integrity (AEAD).
62//! - PBKDF2 with 600 000 iterations resists offline brute-force attacks.
63//! - Decrypted plaintext is held in [`Zeroizing<Vec<u8>>`] and zeroed
64//!   on drop.
65//! - The plaintext secrets file is never written to disk by this crate.
66//! - Nonce and salt are generated with OS CSPRNG (`rand`).
67
68use crate::category::Category;
69use crate::error::{Result, SanitizeError};
70use crate::scanner::ScanPattern;
71
72/// Result of compiling secret entries into patterns.
73/// Contains successfully compiled patterns and a list of (index, error) for failures.
74pub type PatternCompileResult = (Vec<ScanPattern>, Vec<(usize, SanitizeError)>);
75
76use aes_gcm::aead::{Aead, KeyInit};
77use aes_gcm::{Aes256Gcm, Nonce};
78use hmac::Hmac;
79use rand::RngCore;
80use serde::{Deserialize, Serialize};
81use sha2::Sha256;
82use zeroize::{Zeroize, Zeroizing};
83
84// ---------------------------------------------------------------------------
85// Constants
86// ---------------------------------------------------------------------------
87
88/// Salt length for PBKDF2 key derivation (bytes).
89const SALT_LEN: usize = 32;
90
91/// AES-GCM nonce length (bytes). Must be 12 for AES-256-GCM.
92const NONCE_LEN: usize = 12;
93
94/// PBKDF2 iteration count — OWASP 2023+ recommendation.
95const PBKDF2_ITERATIONS: u32 = 600_000;
96
97/// Minimum ciphertext size: salt + nonce + at least 16-byte AES-GCM tag.
98const MIN_ENCRYPTED_LEN: usize = SALT_LEN + NONCE_LEN + 16;
99
100/// Maximum size of a plaintext secrets file accepted by [`parse_secrets`].
101/// Prevents OOM from accidentally passing a large binary or log file as secrets.
102const MAX_SECRETS_PLAINTEXT_BYTES: usize = 10 * 1024 * 1024; // 10 MiB
103
104// ---------------------------------------------------------------------------
105// Secrets file schema
106// ---------------------------------------------------------------------------
107
108/// A single secret entry as stored in the (plaintext) secrets file.
109///
110/// After decryption the entries are parsed from JSON, YAML, or TOML and
111/// converted into [`ScanPattern`]s.
112///
113/// Implements [`Drop`] via [`Zeroize`] to scrub sensitive pattern data
114/// from memory when no longer needed (S-1 fix).
115#[derive(Debug, Clone, Serialize, Deserialize)]
116pub struct SecretEntry {
117    /// The pattern string (regex or literal text).
118    ///
119    /// For `kind: allow` entries this is the single allowlist pattern.
120    /// Omit when using [`values`](Self::values) instead.
121    #[serde(default)]
122    pub pattern: String,
123
124    /// `"regex"`, `"literal"`, `"allow"`, `"entropy"`, or `"field-name"`.
125    ///
126    /// `"field-name"` entries are not compiled into scanner patterns — they
127    /// are extracted separately and injected into structured-processor profiles
128    /// as field-name signals.  The `pattern` field is a case-insensitive
129    /// regex matched against bare field/key names; `threshold` controls the
130    /// entropy gate (defaults to `3.5` bits/char when omitted).
131    #[serde(default = "default_kind")]
132    pub kind: String,
133
134    /// Category string. Supported values:
135    /// `email`, `name`, `phone`, `ipv4`, `ipv6`, `credit_card`, `ssn`,
136    /// `hostname`, `mac_address`, `container_id`, `uuid`, `jwt`,
137    /// `auth_token`, `file_path`, `windows_sid`, `url`, `aws_arn`,
138    /// `azure_resource_id`, or `custom:<tag>`.
139    #[serde(default = "default_category")]
140    pub category: String,
141
142    /// Human-readable label for stats reporting (appears in the redaction
143    /// summary, findings, and reports). When omitted: a `regex` pattern defaults
144    /// to a truncated form of its (non-secret) pattern text; a `literal` pattern
145    /// — whose text *is* the secret value — defaults to `literal:<category>` so
146    /// the value is never exposed in reporting output.
147    #[serde(default)]
148    pub label: Option<String>,
149
150    /// Multiple allowlist patterns for `kind: allow` entries.
151    ///
152    /// When non-empty, used instead of `pattern`. Allows a single entry to
153    /// allowlist many values compactly:
154    ///
155    /// ```toml
156    /// [[secrets]]
157    /// kind = "allow"
158    /// values = ["localhost", "true", "false", "null", "0.0.0.0"]
159    /// ```
160    #[serde(default, skip_serializing_if = "Vec::is_empty")]
161    pub values: Vec<String>,
162
163    // ── Entropy-detection fields (only used when kind = "entropy") ──────────
164    /// Minimum token length to consider (default: 20).
165    #[serde(default, skip_serializing_if = "Option::is_none")]
166    pub min_length: Option<usize>,
167
168    /// Maximum token length to consider (default: 200).
169    #[serde(default, skip_serializing_if = "Option::is_none")]
170    pub max_length: Option<usize>,
171
172    /// Shannon entropy threshold in bits per character (default: 4.5).
173    /// Tokens whose entropy is at or above this value are flagged.
174    #[serde(default, skip_serializing_if = "Option::is_none")]
175    pub threshold: Option<f64>,
176
177    /// Character set the token must consist of exclusively.
178    /// `"alphanumeric"` (default), `"base64"`, `"hex"`, or `"any"`.
179    #[serde(default, skip_serializing_if = "Option::is_none")]
180    pub charset: Option<String>,
181}
182
183impl Drop for SecretEntry {
184    fn drop(&mut self) {
185        self.pattern.zeroize();
186        self.kind.zeroize();
187        self.category.zeroize();
188        if let Some(ref mut l) = self.label {
189            l.zeroize();
190        }
191        for v in &mut self.values {
192            v.zeroize();
193        }
194        if let Some(ref mut s) = self.charset {
195            s.zeroize();
196        }
197    }
198}
199
200fn default_kind() -> String {
201    "literal".into()
202}
203
204fn default_category() -> String {
205    "custom:secret".into()
206}
207
208/// Supported plaintext file formats for secrets.
209#[derive(Debug, Clone, Copy, PartialEq, Eq)]
210pub enum SecretsFormat {
211    Json,
212    Yaml,
213    Toml,
214}
215
216impl SecretsFormat {
217    /// Detect format from file extension.
218    pub fn from_extension(path: &str) -> Option<Self> {
219        // Strip .enc suffix first if present.
220        let base = path.strip_suffix(".enc").unwrap_or(path);
221        let ext = std::path::Path::new(base).extension();
222        if ext.is_some_and(|e| e.eq_ignore_ascii_case("json")) {
223            Some(Self::Json)
224        } else if ext
225            .is_some_and(|e| e.eq_ignore_ascii_case("yaml") || e.eq_ignore_ascii_case("yml"))
226        {
227            Some(Self::Yaml)
228        } else if ext.is_some_and(|e| e.eq_ignore_ascii_case("toml")) {
229            Some(Self::Toml)
230        } else {
231            None
232        }
233    }
234
235    /// Try to auto-detect format from content.
236    pub fn detect(content: &[u8]) -> Self {
237        let s = String::from_utf8_lossy(content);
238        // Skip leading comment lines — both YAML and TOML use `#`, so a file
239        // that opens with comments must be scanned further to find the first
240        // meaningful token.
241        let first_meaningful = s
242            .lines()
243            .map(str::trim)
244            .find(|l| !l.is_empty() && !l.starts_with('#'))
245            .unwrap_or("");
246        if first_meaningful.starts_with('[') || first_meaningful.starts_with('{') {
247            // `[` is ambiguous: JSON arrays and TOML table headers both start
248            // with it. We pick JSON here because our secrets files are never
249            // bare TOML tables, and a wrong guess produces a clear parse error.
250            Self::Json
251        } else if first_meaningful.starts_with('-') || first_meaningful.starts_with("---") {
252            Self::Yaml
253        } else {
254            // Fallback: assume TOML
255            Self::Toml
256        }
257    }
258}
259
260// ---------------------------------------------------------------------------
261// TOML wrapper — serde_toml expects a top-level table
262// ---------------------------------------------------------------------------
263
264/// Wrapper for TOML deserialization: `secrets = [...]`
265#[derive(Deserialize)]
266struct TomlSecrets {
267    secrets: Vec<SecretEntry>,
268}
269
270/// Wrapper for TOML serialization.
271#[derive(Serialize)]
272struct TomlSecretsRef<'a> {
273    secrets: &'a [SecretEntry],
274}
275
276// ---------------------------------------------------------------------------
277// Key derivation
278// ---------------------------------------------------------------------------
279
280/// Derive a 256-bit AES key from a password and salt using PBKDF2.
281fn derive_key(password: &[u8], salt: &[u8]) -> Zeroizing<[u8; 32]> {
282    let mut key = Zeroizing::new([0u8; 32]);
283    pbkdf2::pbkdf2::<Hmac<Sha256>>(password, salt, PBKDF2_ITERATIONS, key.as_mut())
284        .expect("PBKDF2 output length is valid");
285    key
286}
287
288// ---------------------------------------------------------------------------
289// Encryption
290// ---------------------------------------------------------------------------
291
292/// Encrypt a plaintext secrets file.
293///
294/// Returns the encrypted blob: `salt (32) || nonce (12) || ciphertext`.
295///
296/// # Arguments
297///
298/// - `plaintext` — raw bytes of the secrets file (JSON / YAML / TOML).
299/// - `password` — user-supplied password.
300///
301/// # Errors
302///
303/// Returns [`SanitizeError::SecretsEmptyPassword`] if the password is empty, or
304/// [`SanitizeError::SecretsCipherError`] if encryption fails.
305///
306/// # Security
307///
308/// - Salt and nonce are generated with CSPRNG.
309/// - Key is derived with PBKDF2 (600 000 iterations).
310/// - AES-256-GCM provides authenticated encryption.
311pub fn encrypt_secrets(plaintext: &[u8], password: &str) -> Result<Vec<u8>> {
312    if password.is_empty() {
313        return Err(SanitizeError::SecretsEmptyPassword);
314    }
315
316    let mut rng = rand::rng();
317
318    // Generate random salt and nonce.
319    let mut salt = [0u8; SALT_LEN];
320    rng.fill_bytes(&mut salt);
321
322    let mut nonce_bytes = [0u8; NONCE_LEN];
323    rng.fill_bytes(&mut nonce_bytes);
324    let nonce = Nonce::from_slice(&nonce_bytes);
325
326    // Derive key.
327    let key = derive_key(password.as_bytes(), &salt);
328    let cipher = Aes256Gcm::new_from_slice(key.as_ref())
329        .map_err(|e| SanitizeError::SecretsCipherError(format!("cipher init: {}", e)))?;
330
331    // Encrypt.
332    let ciphertext = cipher
333        .encrypt(nonce, plaintext)
334        .map_err(|e| SanitizeError::SecretsCipherError(format!("encryption: {}", e)))?;
335
336    // Assemble: salt || nonce || ciphertext
337    let mut output = Vec::with_capacity(SALT_LEN + NONCE_LEN + ciphertext.len());
338    output.extend_from_slice(&salt);
339    output.extend_from_slice(&nonce_bytes);
340    output.extend_from_slice(&ciphertext);
341
342    Ok(output)
343}
344
345// ---------------------------------------------------------------------------
346// Decryption
347// ---------------------------------------------------------------------------
348
349/// Decrypt an encrypted secrets blob in memory.
350///
351/// Returns the plaintext wrapped in [`Zeroizing`] so it is scrubbed on drop.
352///
353/// # Arguments
354///
355/// - `encrypted` — `salt (32) || nonce (12) || ciphertext`.
356/// - `password` — user-supplied password.
357///
358/// # Errors
359///
360/// - [`SanitizeError::SecretsTooShort`] if the blob is too short,
361///   [`SanitizeError::SecretsDecryptFailed`] if the password is wrong or the ciphertext has been tampered with.
362pub fn decrypt_secrets(encrypted: &[u8], password: &str) -> Result<Zeroizing<Vec<u8>>> {
363    if encrypted.len() < MIN_ENCRYPTED_LEN {
364        return Err(SanitizeError::SecretsTooShort);
365    }
366
367    let salt = &encrypted[..SALT_LEN];
368    let nonce_bytes = &encrypted[SALT_LEN..SALT_LEN + NONCE_LEN];
369    let ciphertext = &encrypted[SALT_LEN + NONCE_LEN..];
370
371    let nonce = Nonce::from_slice(nonce_bytes);
372
373    let key = derive_key(password.as_bytes(), salt);
374    let cipher = Aes256Gcm::new_from_slice(key.as_ref())
375        .map_err(|e| SanitizeError::SecretsCipherError(format!("cipher init: {}", e)))?;
376
377    let plaintext = cipher
378        .decrypt(nonce, ciphertext)
379        .map_err(|_| SanitizeError::SecretsDecryptFailed)?;
380
381    Ok(Zeroizing::new(plaintext))
382}
383
384// ---------------------------------------------------------------------------
385// Parsing
386// ---------------------------------------------------------------------------
387
388/// Parse a decrypted plaintext into secret entries.
389///
390/// Supports JSON, YAML, and TOML. Format is auto-detected if `format`
391/// is `None`.
392///
393/// # Errors
394///
395/// Returns [`SanitizeError::SecretsInvalidUtf8`] if the plaintext is not
396/// valid UTF-8, [`SanitizeError::SecretsFormatError`] if it cannot be parsed
397/// in the specified format or if the file exceeds the size limit.
398pub fn parse_secrets(plaintext: &[u8], format: Option<SecretsFormat>) -> Result<Vec<SecretEntry>> {
399    if plaintext.len() > MAX_SECRETS_PLAINTEXT_BYTES {
400        return Err(SanitizeError::SecretsFormatError {
401            format: "secrets file".into(),
402            message: format!(
403                "file is {} bytes, exceeding the {} byte limit — \
404                 secrets files should be small YAML/JSON/TOML pattern lists",
405                plaintext.len(),
406                MAX_SECRETS_PLAINTEXT_BYTES,
407            ),
408        });
409    }
410    let fmt = format.unwrap_or_else(|| SecretsFormat::detect(plaintext));
411    let text = std::str::from_utf8(plaintext)
412        .map_err(|e| SanitizeError::SecretsInvalidUtf8(e.to_string()))?;
413
414    match fmt {
415        SecretsFormat::Json => {
416            serde_json::from_str(text).map_err(|e| SanitizeError::SecretsFormatError {
417                format: "JSON".into(),
418                message: e.to_string(),
419            })
420        }
421        SecretsFormat::Yaml => {
422            serde_yaml_ng::from_str(text).map_err(|e| SanitizeError::SecretsFormatError {
423                format: "YAML".into(),
424                message: e.to_string(),
425            })
426        }
427        SecretsFormat::Toml => {
428            let wrapper: TomlSecrets =
429                toml::from_str(text).map_err(|e| SanitizeError::SecretsFormatError {
430                    format: "TOML".into(),
431                    message: e.to_string(),
432                })?;
433            Ok(wrapper.secrets)
434        }
435    }
436}
437
438/// Serialize secret entries back into a plaintext format.
439///
440/// Used by the encryption helper CLI.
441///
442/// # Errors
443///
444/// Returns [`SanitizeError::SecretsFormatError`] if serialization fails.
445pub fn serialize_secrets(entries: &[SecretEntry], format: SecretsFormat) -> Result<Vec<u8>> {
446    match format {
447        SecretsFormat::Json => {
448            serde_json::to_vec_pretty(entries).map_err(|e| SanitizeError::SecretsFormatError {
449                format: "JSON-serialize".into(),
450                message: e.to_string(),
451            })
452        }
453        SecretsFormat::Yaml => serde_yaml_ng::to_string(entries)
454            .map(|s| s.into_bytes())
455            .map_err(|e| SanitizeError::SecretsFormatError {
456                format: "YAML-serialize".into(),
457                message: e.to_string(),
458            }),
459        SecretsFormat::Toml => {
460            let wrapper = TomlSecretsRef { secrets: entries };
461            toml::to_string_pretty(&wrapper)
462                .map(|s| s.into_bytes())
463                .map_err(|e| SanitizeError::SecretsFormatError {
464                    format: "TOML-serialize".into(),
465                    message: e.to_string(),
466                })
467        }
468    }
469}
470
471// ---------------------------------------------------------------------------
472// Category parsing
473// ---------------------------------------------------------------------------
474
475/// Parse a category string into a [`Category`].
476///
477/// Accepted values: `email`, `name`, `phone`, `ipv4`, `ipv6`,
478/// `credit_card`, `ssn`, `hostname`, `mac_address`, `container_id`,
479/// `uuid`, `jwt`, `auth_token`, `file_path`, `windows_sid`, `url`,
480/// `aws_arn`, `azure_resource_id`, or `custom:<tag>`.
481pub fn parse_category(s: &str) -> Category {
482    match s {
483        "email" => Category::Email,
484        "name" => Category::Name,
485        "phone" => Category::Phone,
486        "ipv4" => Category::IpV4,
487        "ipv6" => Category::IpV6,
488        "credit_card" => Category::CreditCard,
489        "ssn" => Category::Ssn,
490        "hostname" => Category::Hostname,
491        "mac_address" => Category::MacAddress,
492        "container_id" => Category::ContainerId,
493        "uuid" => Category::Uuid,
494        "jwt" => Category::Jwt,
495        "auth_token" => Category::AuthToken,
496        "file_path" => Category::FilePath,
497        "windows_sid" => Category::WindowsSid,
498        "url" => Category::Url,
499        "aws_arn" => Category::AwsArn,
500        "azure_resource_id" => Category::AzureResourceId,
501        other => {
502            let tag = other.strip_prefix("custom:").unwrap_or(other);
503            Category::Custom(tag.into())
504        }
505    }
506}
507
508// ---------------------------------------------------------------------------
509// Conversion to ScanPatterns
510// ---------------------------------------------------------------------------
511
512/// Extract allowlist patterns from a set of entries.
513///
514/// Entries with `kind: allow` are returned as raw pattern strings to be
515/// compiled into an [`AllowlistMatcher`](crate::allowlist::AllowlistMatcher). They are skipped by
516/// [`entries_to_patterns`].
517///
518/// Each entry contributes either its `values` list (when non-empty) or its
519/// `pattern` field (when `values` is absent), so both forms are supported:
520///
521/// ```toml
522/// # single pattern
523/// [[secrets]]
524/// kind = "allow"
525/// pattern = "localhost"
526///
527/// # compact multi-value form
528/// [[secrets]]
529/// kind = "allow"
530/// values = ["true", "false", "null", "0.0.0.0"]
531/// ```
532pub fn extract_allow_patterns(entries: &[SecretEntry]) -> Vec<String> {
533    let mut patterns = Vec::new();
534    for entry in entries.iter().filter(|e| e.kind == "allow") {
535        if !entry.values.is_empty() {
536            patterns.extend(entry.values.iter().cloned());
537        } else if !entry.pattern.is_empty() {
538            patterns.push(entry.pattern.clone());
539        }
540    }
541    patterns
542}
543
544/// Convert parsed [`SecretEntry`]s into compiled [`ScanPattern`]s.
545///
546/// Entries with `kind: allow` are silently skipped — they are handled by
547/// [`extract_allow_patterns`] instead.
548///
549/// Invalid entries (e.g. bad regex) are collected as errors and
550/// returned alongside the successfully compiled patterns.
551pub fn entries_to_patterns(entries: &[SecretEntry]) -> PatternCompileResult {
552    let mut patterns = Vec::with_capacity(entries.len());
553    let mut errors = Vec::new();
554
555    for (i, entry) in entries.iter().enumerate() {
556        if entry.kind == "allow"
557            || entry.kind == "entropy"
558            || entry.kind == "field-name"
559            || entry.pattern.is_empty()
560        {
561            continue;
562        }
563        let category = parse_category(&entry.category);
564        // A `literal` pattern's text IS the secret value, and labels surface in
565        // the redaction summary, findings, reports, and logs — so a literal must
566        // never default its label to its own pattern (that leaks the secret).
567        // Fall back to the category instead. A `regex` pattern is not itself a
568        // secret, so its (truncated) text remains a safe, informative default.
569        let label = entry.label.clone().unwrap_or_else(|| {
570            if entry.kind == "literal" {
571                format!("literal:{}", entry.category)
572            } else {
573                truncate_label(&entry.pattern)
574            }
575        });
576
577        let result = match entry.kind.as_str() {
578            "regex" => ScanPattern::from_regex(&entry.pattern, category, label),
579            "literal" => ScanPattern::from_literal(&entry.pattern, category, label),
580            other => {
581                errors.push((
582                    i,
583                    SanitizeError::InvalidConfig(format!(
584                        "unknown kind {:?} — expected \"literal\", \"regex\", \"allow\", \"entropy\", or \"field-name\"",
585                        other
586                    )),
587                ));
588                continue;
589            }
590        };
591
592        match result {
593            Ok(pat) => patterns.push(pat),
594            Err(e) => errors.push((i, e)),
595        }
596    }
597
598    (patterns, errors)
599}
600
601const MAX_LABEL_CHARS: usize = 32;
602
603/// Truncate to a maximum label length.
604fn truncate_label(s: &str) -> String {
605    if s.len() <= MAX_LABEL_CHARS {
606        s.to_string()
607    } else {
608        // Find a char boundary just before the limit to avoid panicking on
609        // multi-byte UTF-8 characters (e.g. Unicode in user-supplied patterns).
610        let cut = s
611            .char_indices()
612            .nth(MAX_LABEL_CHARS - 1)
613            .map_or(s.len(), |(i, _)| i);
614        format!("{}…", &s[..cut])
615    }
616}
617
618// ---------------------------------------------------------------------------
619// High-level: load encrypted secrets → ScanPatterns
620// ---------------------------------------------------------------------------
621
622/// Load, decrypt, parse, and compile an encrypted secrets file into
623/// [`ScanPattern`]s ready for the streaming scanner.
624///
625/// This is the primary entry point for CLI integration.
626///
627/// # Arguments
628///
629/// - `encrypted_bytes` — raw bytes of the `.enc` file.
630/// - `password` — user-supplied password.
631/// - `format` — optional explicit format override.
632///
633/// # Returns
634///
635/// `(patterns, warnings)` where `warnings` contains indices and errors
636/// for entries that failed to compile.
637///
638/// # Security
639///
640/// The decrypted plaintext is held in zeroizing memory and dropped
641/// immediately after parsing.
642///
643/// # Errors
644///
645/// Returns a secrets-related [`SanitizeError`] if decryption or parsing fails.
646pub fn load_encrypted_secrets(
647    encrypted_bytes: &[u8],
648    password: &str,
649    format: Option<SecretsFormat>,
650) -> Result<(PatternCompileResult, Vec<String>)> {
651    let plaintext = decrypt_secrets(encrypted_bytes, password)?;
652    let entries = parse_secrets(&plaintext, format)?;
653    let allow = extract_allow_patterns(&entries);
654    let result = entries_to_patterns(&entries);
655    // SecretEntry implements Drop with explicit zeroize() calls, so dropping
656    // the Vec is sufficient to scrub sensitive pattern data from heap memory.
657    drop(entries);
658    Ok((result, allow))
659}
660
661/// Load and parse a plaintext secrets file into [`ScanPattern`]s.
662///
663/// This function mirrors [`load_encrypted_secrets`] but skips
664/// AES decryption and password prompts entirely. It preserves
665/// memory hygiene by zeroizing parsed entries after compilation.
666///
667/// # Arguments
668///
669/// - `plaintext` — raw bytes of the secrets file (JSON / YAML / TOML).
670/// - `format` — optional explicit format override.
671///
672/// # Security
673///
674/// Even for unencrypted secrets, entries are zeroized after pattern
675/// compilation to minimise the window during which sensitive values
676/// reside in memory.
677///
678/// # Errors
679///
680/// Returns a secrets-related [`SanitizeError`] if parsing or pattern
681/// compilation fails.
682pub fn load_plaintext_secrets(
683    plaintext: &[u8],
684    format: Option<SecretsFormat>,
685) -> Result<(PatternCompileResult, Vec<String>)> {
686    let entries = parse_secrets(plaintext, format)?;
687    let allow = extract_allow_patterns(&entries);
688    let result = entries_to_patterns(&entries);
689    // SecretEntry implements Drop with explicit zeroize() calls, so dropping
690    // the Vec is sufficient to scrub sensitive pattern data from heap memory.
691    drop(entries);
692    Ok((result, allow))
693}
694
695/// Detect whether raw file bytes look like an AES-256-GCM encrypted
696/// secrets blob (binary with salt+nonce header) or a plaintext secrets
697/// file (UTF-8 JSON / YAML / TOML).
698///
699/// Returns `true` if the content appears to be encrypted.
700///
701/// Heuristic:
702/// 1. Files shorter than the minimum encrypted length cannot be valid
703///    ciphertext — return `false`.
704/// 2. The **entire** content is checked for UTF-8 validity (not just the
705///    first few bytes). Only if the whole file is valid UTF-8 and begins
706///    with a recognisable plaintext marker (`[`, `{`, `-`, `#`) is it
707///    treated as plaintext — return `false`.
708/// 3. Binary content (not valid UTF-8) or UTF-8 without a plaintext
709///    marker is assumed to be encrypted — return `true`.
710///
711/// Note: a pathological plaintext file that is valid UTF-8 but lacks a
712/// leading plaintext marker (e.g. a TOML file whose first non-whitespace
713/// character is a letter) will be misclassified as encrypted and produce
714/// a `SecretsDecryptFailed` error. Use `force_plaintext: true` in
715/// [`load_secrets_auto`] to bypass the heuristic in that case.
716pub fn looks_encrypted(data: &[u8]) -> bool {
717    if data.len() < MIN_ENCRYPTED_LEN {
718        // Too short for a valid encrypted blob — might be a tiny
719        // plaintext file, but definitely not encrypted.
720        return false;
721    }
722    // If the file is valid UTF-8 and starts with a recognisable
723    // plaintext marker, treat it as plaintext.
724    if let Ok(text) = std::str::from_utf8(data) {
725        let trimmed = text.trim_start();
726        // Recognisable plaintext markers:
727        //   JSON  → '[' (array) or '{' (object)
728        //   YAML  → '-' (sequence item) or '---' (document start)
729        //   TOML  → '#' (comment) or '[' (table header)
730        //   TOML bare-key → any ASCII letter (e.g. `pattern = "foo"`)
731        //
732        // starts_with('[') already covers "[["; starts_with('-') covers "---".
733        // The ASCII-letter check handles TOML files whose first meaningful token
734        // is a bare key — without it those files were misclassified as encrypted
735        // and produced a confusing "decryption failed" error.
736        //
737        // A valid AES-256-GCM ciphertext starts with 32 bytes of random salt and
738        // 12 bytes of random nonce; the probability that those 44 bytes are entirely
739        // valid UTF-8 AND begin with an ASCII letter is negligible (< 2^-6).
740        let first_char = trimmed.chars().next();
741        let has_marker = trimmed.starts_with('[')
742            || trimmed.starts_with('{')
743            || trimmed.starts_with('-')
744            || trimmed.starts_with('#')
745            || first_char.is_some_and(|c| c.is_ascii_alphabetic());
746        if has_marker {
747            return false;
748        }
749    }
750    // Binary / non-UTF-8 → assume encrypted.
751    true
752}
753
754/// Unified loader: auto-detect encrypted vs plaintext and load
755/// secret patterns accordingly.
756///
757/// When `force_plaintext` is `true`, decryption is skipped regardless
758/// of file content. When `false`, the function uses [`looks_encrypted`]
759/// to choose the path automatically.
760///
761/// # Arguments
762///
763/// - `data` — raw bytes read from the secrets file.
764/// - `password` — password for decryption (ignored when plaintext).
765/// - `format` — optional format override.
766/// - `force_plaintext` — if `true`, always treat as plaintext.
767///
768/// # Returns
769///
770/// `(patterns, warnings, was_encrypted)` — the compiled patterns,
771/// any compile warnings, and a flag indicating which path was taken.
772///
773/// # Errors
774///
775/// Returns a secrets-related [`SanitizeError`] if decryption or parsing
776/// fails, or if a password is required but not provided.
777/// Returns `((patterns, warnings, allow_patterns), was_encrypted)`.
778///
779/// `allow_patterns` are the raw strings from `kind: allow` entries in the
780/// secrets file — the caller should combine these with any `--allow` CLI
781/// values and pass the merged list to [`AllowlistMatcher::new`](crate::allowlist::AllowlistMatcher::new).
782pub fn load_secrets_auto(
783    data: &[u8],
784    password: Option<&str>,
785    format: Option<SecretsFormat>,
786    force_plaintext: bool,
787) -> Result<((PatternCompileResult, Vec<String>), bool)> {
788    if force_plaintext || !looks_encrypted(data) {
789        let (result, allow) = load_plaintext_secrets(data, format)?;
790        Ok(((result, allow), false))
791    } else {
792        let pw = password.ok_or(SanitizeError::SecretsPasswordRequired)?;
793        let (result, allow) = load_encrypted_secrets(data, pw, format)?;
794        Ok(((result, allow), true))
795    }
796}
797
798// ---------------------------------------------------------------------------
799// Unit tests
800// ---------------------------------------------------------------------------
801
802#[cfg(test)]
803mod tests {
804    use super::*;
805
806    fn sample_json() -> &'static str {
807        r#"[
808            {
809                "pattern": "alice@corp\\.com",
810                "kind": "regex",
811                "category": "email",
812                "label": "alice_email"
813            },
814            {
815                "pattern": "sk-proj-abc123secret",
816                "kind": "literal",
817                "category": "custom:api_key",
818                "label": "openai_key"
819            }
820        ]"#
821    }
822
823    fn sample_yaml() -> &'static str {
824        r#"- pattern: "alice@corp\\.com"
825  kind: regex
826  category: email
827  label: alice_email
828- pattern: sk-proj-abc123secret
829  kind: literal
830  category: "custom:api_key"
831  label: openai_key
832"#
833    }
834
835    fn sample_toml() -> &'static str {
836        r#"[[secrets]]
837pattern = "alice@corp\\.com"
838kind = "regex"
839category = "email"
840label = "alice_email"
841
842[[secrets]]
843pattern = "sk-proj-abc123secret"
844kind = "literal"
845category = "custom:api_key"
846label = "openai_key"
847"#
848    }
849
850    // ---- Parsing ----
851
852    #[test]
853    fn parse_json_entries() {
854        let entries = parse_secrets(sample_json().as_bytes(), Some(SecretsFormat::Json)).unwrap();
855        assert_eq!(entries.len(), 2);
856        assert_eq!(entries[0].kind, "regex");
857        assert_eq!(entries[0].category, "email");
858        assert_eq!(entries[1].kind, "literal");
859    }
860
861    #[test]
862    fn parse_yaml_entries() {
863        let entries = parse_secrets(sample_yaml().as_bytes(), Some(SecretsFormat::Yaml)).unwrap();
864        assert_eq!(entries.len(), 2);
865        assert_eq!(entries[0].label, Some("alice_email".into()));
866    }
867
868    #[test]
869    fn parse_toml_entries() {
870        let entries = parse_secrets(sample_toml().as_bytes(), Some(SecretsFormat::Toml)).unwrap();
871        assert_eq!(entries.len(), 2);
872        assert_eq!(entries[1].pattern, "sk-proj-abc123secret");
873    }
874
875    #[test]
876    fn parse_auto_detect_json() {
877        let entries = parse_secrets(sample_json().as_bytes(), None).unwrap();
878        assert_eq!(entries.len(), 2);
879    }
880
881    #[test]
882    fn parse_auto_detect_yaml() {
883        let entries = parse_secrets(sample_yaml().as_bytes(), None).unwrap();
884        assert_eq!(entries.len(), 2);
885    }
886
887    // ---- Category parsing ----
888
889    #[test]
890    fn parse_builtin_categories() {
891        assert_eq!(parse_category("email"), Category::Email);
892        assert_eq!(parse_category("ipv4"), Category::IpV4);
893        assert_eq!(parse_category("ssn"), Category::Ssn);
894    }
895
896    #[test]
897    fn parse_custom_category() {
898        match parse_category("custom:api_key") {
899            Category::Custom(tag) => assert_eq!(tag.as_str(), "api_key"),
900            other => panic!("expected Custom, got {:?}", other),
901        }
902    }
903
904    #[test]
905    fn parse_unknown_category_becomes_custom() {
906        match parse_category("foobar") {
907            Category::Custom(tag) => assert_eq!(tag.as_str(), "foobar"),
908            other => panic!("expected Custom, got {:?}", other),
909        }
910    }
911
912    // ---- Entries to patterns ----
913
914    #[test]
915    fn entries_to_patterns_success() {
916        let entries = parse_secrets(sample_json().as_bytes(), Some(SecretsFormat::Json)).unwrap();
917        let (patterns, errors) = entries_to_patterns(&entries);
918        assert_eq!(patterns.len(), 2);
919        assert!(errors.is_empty());
920    }
921
922    #[test]
923    fn entries_to_patterns_bad_regex() {
924        let json = r#"[{"pattern": "[invalid(", "kind": "regex", "category": "email"}]"#;
925        let entries = parse_secrets(json.as_bytes(), Some(SecretsFormat::Json)).unwrap();
926        let (patterns, errors) = entries_to_patterns(&entries);
927        assert!(patterns.is_empty());
928        assert_eq!(errors.len(), 1);
929        assert_eq!(errors[0].0, 0);
930    }
931
932    // ---- Encrypt / Decrypt round-trip ----
933
934    #[test]
935    fn encrypt_decrypt_roundtrip() {
936        let plaintext = sample_json().as_bytes();
937        let password = "test-password-42";
938
939        let encrypted = encrypt_secrets(plaintext, password).unwrap();
940
941        // Encrypted blob must be larger than plaintext (salt + nonce + tag).
942        assert!(encrypted.len() > plaintext.len());
943
944        let decrypted = decrypt_secrets(&encrypted, password).unwrap();
945        assert_eq!(decrypted.as_slice(), plaintext);
946    }
947
948    #[test]
949    fn decrypt_wrong_password_fails() {
950        let plaintext = b"hello";
951        let encrypted = encrypt_secrets(plaintext, "correct").unwrap();
952        let result = decrypt_secrets(&encrypted, "wrong");
953        assert!(result.is_err());
954    }
955
956    #[test]
957    fn decrypt_truncated_blob_fails() {
958        let result = decrypt_secrets(&[0u8; 10], "any");
959        assert!(result.is_err());
960    }
961
962    #[test]
963    fn decrypt_tampered_blob_fails() {
964        let plaintext = b"hello world";
965        let mut encrypted = encrypt_secrets(plaintext, "pw").unwrap();
966        // Flip a byte in the ciphertext portion.
967        let last = encrypted.len() - 1;
968        encrypted[last] ^= 0xFF;
969        let result = decrypt_secrets(&encrypted, "pw");
970        assert!(result.is_err());
971    }
972
973    #[test]
974    fn encrypt_empty_password_rejected() {
975        let result = encrypt_secrets(b"hello", "");
976        assert!(result.is_err());
977    }
978
979    // ---- Full pipeline: encrypt → decrypt → parse → patterns ----
980
981    #[test]
982    fn full_pipeline_json() {
983        let plaintext = sample_json().as_bytes();
984        let password = "pipeline-test";
985
986        let encrypted = encrypt_secrets(plaintext, password).unwrap();
987        let ((patterns, errors), _allow) =
988            load_encrypted_secrets(&encrypted, password, Some(SecretsFormat::Json)).unwrap();
989
990        assert_eq!(patterns.len(), 2);
991        assert!(errors.is_empty());
992        assert_eq!(patterns[0].label(), "alice_email");
993        assert_eq!(patterns[1].label(), "openai_key");
994    }
995
996    #[test]
997    fn full_pipeline_yaml() {
998        let plaintext = sample_yaml().as_bytes();
999        let password = "yaml-test";
1000
1001        let encrypted = encrypt_secrets(plaintext, password).unwrap();
1002        let ((patterns, errors), _allow) =
1003            load_encrypted_secrets(&encrypted, password, Some(SecretsFormat::Yaml)).unwrap();
1004
1005        assert_eq!(patterns.len(), 2);
1006        assert!(errors.is_empty());
1007    }
1008
1009    #[test]
1010    fn full_pipeline_toml() {
1011        let plaintext = sample_toml().as_bytes();
1012        let password = "toml-test";
1013
1014        let encrypted = encrypt_secrets(plaintext, password).unwrap();
1015        let ((patterns, errors), _allow) =
1016            load_encrypted_secrets(&encrypted, password, Some(SecretsFormat::Toml)).unwrap();
1017
1018        assert_eq!(patterns.len(), 2);
1019        assert!(errors.is_empty());
1020    }
1021
1022    // ---- Plaintext loader ----
1023
1024    #[test]
1025    fn load_plaintext_secrets_works() {
1026        let ((patterns, errors), _allow) =
1027            load_plaintext_secrets(sample_json().as_bytes(), Some(SecretsFormat::Json)).unwrap();
1028        assert_eq!(patterns.len(), 2);
1029        assert!(errors.is_empty());
1030    }
1031
1032    // ---- Serialization round-trip ----
1033
1034    #[test]
1035    fn serialize_roundtrip_json() {
1036        let entries = parse_secrets(sample_json().as_bytes(), Some(SecretsFormat::Json)).unwrap();
1037        let serialized = serialize_secrets(&entries, SecretsFormat::Json).unwrap();
1038        let reparsed = parse_secrets(&serialized, Some(SecretsFormat::Json)).unwrap();
1039        assert_eq!(entries.len(), reparsed.len());
1040        assert_eq!(entries[0].pattern, reparsed[0].pattern);
1041    }
1042
1043    // ---- Format detection ----
1044
1045    #[test]
1046    fn format_from_extension() {
1047        assert_eq!(
1048            SecretsFormat::from_extension("secrets.json"),
1049            Some(SecretsFormat::Json)
1050        );
1051        assert_eq!(
1052            SecretsFormat::from_extension("secrets.json.enc"),
1053            Some(SecretsFormat::Json)
1054        );
1055        assert_eq!(
1056            SecretsFormat::from_extension("secrets.yaml"),
1057            Some(SecretsFormat::Yaml)
1058        );
1059        assert_eq!(
1060            SecretsFormat::from_extension("secrets.yml.enc"),
1061            Some(SecretsFormat::Yaml)
1062        );
1063        assert_eq!(
1064            SecretsFormat::from_extension("secrets.toml"),
1065            Some(SecretsFormat::Toml)
1066        );
1067        assert_eq!(SecretsFormat::from_extension("secrets.txt"), None);
1068    }
1069
1070    #[test]
1071    fn detect_yaml_with_leading_comment_header() {
1072        // Regression: the auto-provisioned global secrets file opens with '#'
1073        // comment lines. Before the fix, detect() saw '#' first, fell through
1074        // to the TOML fallback, and failed to parse valid YAML.
1075        let content = "# Global sanitize allowlist — add patterns here.\n# Auto-loaded on every plain run.\n\n- pattern: foo\n  kind: allow\n";
1076        assert_eq!(
1077            SecretsFormat::detect(content.as_bytes()),
1078            SecretsFormat::Yaml
1079        );
1080    }
1081
1082    #[test]
1083    fn detect_yaml_comment_header_parses_correctly() {
1084        // Round-trip: same shape as the auto-provisioned file must load without error.
1085        let content = "# Global sanitize allowlist — add patterns or kind:regex entries here.\n# Auto-loaded on every plain run. Edit freely; deleted values take effect immediately.\n\n- pattern: ''\n  kind: allow\n  category: ''\n  values:\n  - localhost\n  - 127.0.0.1\n";
1086        let entries = parse_secrets(content.as_bytes(), None)
1087            .expect("auto-provisioned secrets file with comment header must parse");
1088        assert_eq!(entries.len(), 1);
1089        assert_eq!(entries[0].kind, "allow");
1090        assert!(entries[0].values.contains(&"localhost".to_string()));
1091    }
1092
1093    #[test]
1094    fn detect_json_array() {
1095        assert_eq!(
1096            SecretsFormat::detect(b"[{\"pattern\": \"foo\"}]"),
1097            SecretsFormat::Json
1098        );
1099    }
1100
1101    #[test]
1102    fn detect_toml_fallback() {
1103        // TOML that doesn't open with '[' or '{' — must not be mistaken for YAML.
1104        assert_eq!(
1105            SecretsFormat::detect(b"# toml comment\nkey = \"value\""),
1106            SecretsFormat::Toml
1107        );
1108    }
1109
1110    // ---- Defaults ----
1111
1112    #[test]
1113    fn default_kind_is_literal() {
1114        let json = r#"[{"pattern": "foo"}]"#;
1115        let entries = parse_secrets(json.as_bytes(), Some(SecretsFormat::Json)).unwrap();
1116        assert_eq!(entries[0].kind, "literal");
1117    }
1118
1119    #[test]
1120    fn default_category_is_custom_secret() {
1121        let json = r#"[{"pattern": "foo"}]"#;
1122        let entries = parse_secrets(json.as_bytes(), Some(SecretsFormat::Json)).unwrap();
1123        assert_eq!(entries[0].category, "custom:secret");
1124    }
1125
1126    #[test]
1127    fn literal_default_label_is_category_not_value() {
1128        // A `literal` pattern's text IS the secret value, and labels surface in
1129        // summaries / reports / logs — so the default label must be the
1130        // category, never the value.
1131        let json = r#"[{"pattern": "short"}]"#;
1132        let entries = parse_secrets(json.as_bytes(), Some(SecretsFormat::Json)).unwrap();
1133        let (patterns, _) = entries_to_patterns(&entries);
1134        assert_eq!(patterns[0].label(), "literal:custom:secret");
1135        assert!(!patterns[0].label().contains("short"));
1136    }
1137
1138    #[test]
1139    fn regex_default_label_is_pattern() {
1140        // A regex pattern is not itself a secret, so its text remains the
1141        // informative default label.
1142        let json = r#"[{"pattern": "ab[0-9]+", "kind": "regex"}]"#;
1143        let entries = parse_secrets(json.as_bytes(), Some(SecretsFormat::Json)).unwrap();
1144        let (patterns, _) = entries_to_patterns(&entries);
1145        assert_eq!(patterns[0].label(), "ab[0-9]+");
1146    }
1147
1148    // ---- looks_encrypted ----
1149
1150    #[test]
1151    fn looks_encrypted_json_plaintext() {
1152        assert!(!looks_encrypted(sample_json().as_bytes()));
1153    }
1154
1155    #[test]
1156    fn looks_encrypted_yaml_plaintext() {
1157        assert!(!looks_encrypted(sample_yaml().as_bytes()));
1158    }
1159
1160    #[test]
1161    fn looks_encrypted_toml_plaintext() {
1162        assert!(!looks_encrypted(sample_toml().as_bytes()));
1163    }
1164
1165    #[test]
1166    fn looks_encrypted_actual_encrypted() {
1167        let encrypted = encrypt_secrets(sample_json().as_bytes(), "pw").unwrap();
1168        assert!(looks_encrypted(&encrypted));
1169    }
1170
1171    #[test]
1172    fn looks_encrypted_too_short() {
1173        assert!(!looks_encrypted(&[0u8; 10]));
1174    }
1175
1176    // ---- load_secrets_auto ----
1177
1178    #[test]
1179    fn auto_load_plaintext_json() {
1180        let data = sample_json().as_bytes();
1181        let (((pats, errs), _allow), was_enc) =
1182            load_secrets_auto(data, None, Some(SecretsFormat::Json), false).unwrap();
1183        assert!(!was_enc);
1184        assert_eq!(pats.len(), 2);
1185        assert!(errs.is_empty());
1186    }
1187
1188    #[test]
1189    fn auto_load_encrypted_json() {
1190        let encrypted = encrypt_secrets(sample_json().as_bytes(), "pw").unwrap();
1191        let (((pats, errs), _allow), was_enc) =
1192            load_secrets_auto(&encrypted, Some("pw"), Some(SecretsFormat::Json), false).unwrap();
1193        assert!(was_enc);
1194        assert_eq!(pats.len(), 2);
1195        assert!(errs.is_empty());
1196    }
1197
1198    #[test]
1199    fn auto_load_force_plaintext() {
1200        let data = sample_json().as_bytes();
1201        let (((pats, _), _allow), was_enc) =
1202            load_secrets_auto(data, None, Some(SecretsFormat::Json), true).unwrap();
1203        assert!(!was_enc);
1204        assert_eq!(pats.len(), 2);
1205    }
1206
1207    #[test]
1208    fn auto_load_encrypted_no_password_fails() {
1209        let encrypted = encrypt_secrets(sample_json().as_bytes(), "pw").unwrap();
1210        let result = load_secrets_auto(&encrypted, None, None, false);
1211        assert!(result.is_err());
1212    }
1213
1214    #[test]
1215    fn parse_secrets_rejects_oversized_input() {
1216        // Construct input just over the 10 MiB cap.
1217        let oversized = vec![b' '; MAX_SECRETS_PLAINTEXT_BYTES + 1];
1218        let result = parse_secrets(&oversized, None);
1219        assert!(result.is_err());
1220        let msg = result.unwrap_err().to_string();
1221        assert!(
1222            msg.contains("exceeding") || msg.contains("limit"),
1223            "unexpected error message: {msg}"
1224        );
1225    }
1226
1227    #[test]
1228    fn parse_secrets_accepts_input_at_limit() {
1229        // Valid JSON just at the cap boundary — should succeed or fail on
1230        // parse, not on the size check. We use a tiny valid payload here
1231        // to confirm the size gate does not block small files.
1232        let tiny = b"[]";
1233        let result = parse_secrets(tiny, Some(SecretsFormat::Json));
1234        assert!(
1235            result.is_ok(),
1236            "unexpected error: {:?}",
1237            result.unwrap_err()
1238        );
1239    }
1240
1241    #[test]
1242    fn truncate_label_at_boundary() {
1243        let short = "a".repeat(32);
1244        assert_eq!(truncate_label(&short), short);
1245
1246        let long = "a".repeat(33);
1247        let truncated = truncate_label(&long);
1248        assert!(truncated.ends_with('…'), "expected ellipsis: {truncated}");
1249        // Character count (not byte count) must be within the limit.
1250        // The trailing '…' is 1 char; the rest must be < MAX_LABEL_CHARS.
1251        assert!(
1252            truncated.chars().count() <= MAX_LABEL_CHARS,
1253            "char count {} exceeds limit: {truncated}",
1254            truncated.chars().count()
1255        );
1256    }
1257
1258    // ---- Multi-value allow entries ----
1259
1260    #[test]
1261    fn allow_single_pattern_field() {
1262        let json = r#"[{"kind":"allow","pattern":"localhost"}]"#;
1263        let entries = parse_secrets(json.as_bytes(), Some(SecretsFormat::Json)).unwrap();
1264        let patterns = extract_allow_patterns(&entries);
1265        assert_eq!(patterns, vec!["localhost"]);
1266    }
1267
1268    #[test]
1269    fn allow_values_list_used_instead_of_pattern() {
1270        let json = r#"[{"kind":"allow","values":["localhost","true","false","null"]}]"#;
1271        let entries = parse_secrets(json.as_bytes(), Some(SecretsFormat::Json)).unwrap();
1272        let patterns = extract_allow_patterns(&entries);
1273        assert_eq!(patterns, vec!["localhost", "true", "false", "null"]);
1274    }
1275
1276    #[test]
1277    fn allow_values_list_yaml() {
1278        let yaml =
1279            "- kind: allow\n  values:\n    - localhost\n    - \"127.0.0.1\"\n    - \"0.0.0.0\"\n";
1280        let entries = parse_secrets(yaml.as_bytes(), Some(SecretsFormat::Yaml)).unwrap();
1281        let patterns = extract_allow_patterns(&entries);
1282        assert_eq!(patterns, vec!["localhost", "127.0.0.1", "0.0.0.0"]);
1283    }
1284
1285    #[test]
1286    fn allow_values_list_toml() {
1287        let toml = "[[secrets]]\nkind = \"allow\"\nvalues = [\"localhost\", \"true\", \"false\"]\n";
1288        let entries = parse_secrets(toml.as_bytes(), Some(SecretsFormat::Toml)).unwrap();
1289        let patterns = extract_allow_patterns(&entries);
1290        assert_eq!(patterns, vec!["localhost", "true", "false"]);
1291    }
1292
1293    #[test]
1294    fn allow_mixed_single_and_multi_value_entries() {
1295        let json = r#"[
1296            {"kind":"allow","pattern":"localhost"},
1297            {"kind":"allow","values":["true","false","null"]},
1298            {"kind":"allow","pattern":"*.internal"}
1299        ]"#;
1300        let entries = parse_secrets(json.as_bytes(), Some(SecretsFormat::Json)).unwrap();
1301        let patterns = extract_allow_patterns(&entries);
1302        assert_eq!(
1303            patterns,
1304            vec!["localhost", "true", "false", "null", "*.internal"]
1305        );
1306    }
1307
1308    #[test]
1309    fn allow_entries_skipped_by_entries_to_patterns() {
1310        let json = r#"[
1311            {"pattern":"secret","kind":"literal"},
1312            {"kind":"allow","values":["localhost","true"]}
1313        ]"#;
1314        let entries = parse_secrets(json.as_bytes(), Some(SecretsFormat::Json)).unwrap();
1315        let (patterns, errors) = entries_to_patterns(&entries);
1316        assert_eq!(patterns.len(), 1);
1317        assert!(errors.is_empty());
1318        assert_eq!(patterns[0].label(), "literal:custom:secret");
1319    }
1320
1321    #[test]
1322    fn allow_empty_values_falls_back_to_pattern() {
1323        // An entry with an empty `values` list should still use `pattern`.
1324        let json = r#"[{"kind":"allow","pattern":"localhost","values":[]}]"#;
1325        let entries = parse_secrets(json.as_bytes(), Some(SecretsFormat::Json)).unwrap();
1326        let patterns = extract_allow_patterns(&entries);
1327        assert_eq!(patterns, vec!["localhost"]);
1328    }
1329
1330    // ── kind: field-name ─────────────────────────────────────────────────────
1331
1332    #[test]
1333    fn field_name_entries_skipped_by_entries_to_patterns() {
1334        // kind:field-name entries must not produce ScanPatterns — they are
1335        // handled separately as FieldNameSignals injected into profiles.
1336        let json = r#"[
1337            {"pattern":"secret","kind":"literal"},
1338            {"pattern":"^password$","kind":"field-name","threshold":3.0}
1339        ]"#;
1340        let entries = parse_secrets(json.as_bytes(), Some(SecretsFormat::Json)).unwrap();
1341        let (patterns, errors) = entries_to_patterns(&entries);
1342        assert_eq!(
1343            patterns.len(),
1344            1,
1345            "only the literal entry should produce a pattern"
1346        );
1347        assert!(errors.is_empty());
1348        assert_eq!(patterns[0].label(), "literal:custom:secret");
1349    }
1350
1351    #[test]
1352    fn field_name_entry_parses_correctly() {
1353        let yaml = "- kind: field-name\n  pattern: \"^(password|secret)$\"\n  threshold: 3.0\n  label: my-signal\n";
1354        let entries = parse_secrets(yaml.as_bytes(), Some(SecretsFormat::Yaml)).unwrap();
1355        assert_eq!(entries.len(), 1);
1356        assert_eq!(entries[0].kind, "field-name");
1357        assert_eq!(entries[0].pattern, "^(password|secret)$");
1358        assert_eq!(entries[0].threshold, Some(3.0));
1359        assert_eq!(entries[0].label, Some("my-signal".into()));
1360    }
1361
1362    #[test]
1363    fn field_name_entry_not_extracted_as_allow_pattern() {
1364        // kind:field-name entries must not bleed into the allowlist.
1365        let json = r#"[{"pattern":"^password$","kind":"field-name"}]"#;
1366        let entries = parse_secrets(json.as_bytes(), Some(SecretsFormat::Json)).unwrap();
1367        let allow = extract_allow_patterns(&entries);
1368        assert!(allow.is_empty());
1369    }
1370}