grippo 0.1.1

Parse, match and scrub data at scale.
Documentation
// Project:   grippo
// File:      src/scrub/config.rs
// Purpose:   The gitleaks rule config, read as plain serde types
// Language:  Rust
//
// License:   Apache-2.0
// Copyright: (c) 2026 HYPERI PTY LIMITED

//! The parts of a gitleaks config the scrubber runs. Path and commit
//! conditions are read so a rule gated on them can be left out, since log text
//! has no path.

use serde::Deserialize;

/// A whole config.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default)]
#[non_exhaustive]
pub struct RuleConfig {
    /// The global allowlist.
    pub allowlist: Option<AllowlistConfig>,
    /// The rules, in file order.
    pub rules: Vec<RuleSpec>,
}

/// One rule.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default)]
#[non_exhaustive]
pub struct RuleSpec {
    /// The rule id, such as `aws-access-token`.
    pub id: String,
    /// The regex, in RE2 syntax.
    pub regex: Option<String>,
    /// Literals one of which must appear, compared without case.
    pub keywords: Vec<String>,
    /// Minimum Shannon entropy of the secret. A finding at or below it is dropped.
    pub entropy: Option<f64>,
    /// A file path condition. A rule with one is left out of log scrubbing.
    pub path: Option<String>,
    /// The capture group that holds the secret.
    #[serde(rename = "secretGroup")]
    pub secret_group: Option<usize>,
    /// The rule's own allowlists.
    pub allowlists: Vec<AllowlistConfig>,
}

/// An allowlist: a finding it matches is not reported.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default)]
#[non_exhaustive]
pub struct AllowlistConfig {
    /// Regexes tested against the target.
    pub regexes: Vec<String>,
    /// Substrings of the lower-cased secret.
    pub stopwords: Vec<String>,
    /// `secret` (default), `match` or `line`.
    #[serde(rename = "regexTarget")]
    pub regex_target: Option<String>,
    /// `OR` (default) or `AND`.
    pub condition: Option<String>,
    /// File path conditions. An allowlist with one never matches log text.
    pub paths: Vec<String>,
    /// Commit conditions. An allowlist with one never matches log text.
    pub commits: Vec<String>,
}

/// The gitleaks v8.30.1 default rule config, as shipped.
pub const GITLEAKS: &str = include_str!("gitleaks.toml");

impl RuleConfig {
    /// Read a config.
    ///
    /// # Errors
    ///
    /// The TOML error when the text isn't a gitleaks config.
    pub fn from_toml(text: &str) -> Result<Self, toml::de::Error> {
        toml::from_str(text)
    }
}