sql-splitter 1.18.2

High-performance CLI tool for splitting large SQL dump files into individual table files
Documentation
//! The credential guard: columns whose *name* marks them as secret material
//! (password, token, api key, secret, private key) are always generated by a
//! synthetic-only credential generator, no matter what values the dump held.
//!
//! This is deliberately high precedence — above every semantic-name and
//! observed-distribution heuristic — so a dump full of real password hashes or
//! live API keys can never round-trip into the emitted model as observed
//! samples. These candidates never carry source literals.

use super::{generator, Candidate, ColumnContext, Confidence, Precedence};
use crate::synthetic::model::GeneratorConfig;
use crate::synthetic::schema::SqlTypeFamily;

/// Every family that can hold secret string-ish material and must therefore be
/// covered by the guard. Numeric families are deliberately excluded — a
/// `token_count` integer is not a token — but every non-numeric string-shaped
/// family is included, so a `refresh_token uuid` or an `api_key json` column
/// cannot slip past the guard into an observed-value replay.
fn guardable_family(family: &SqlTypeFamily) -> bool {
    matches!(
        family,
        SqlTypeFamily::Text
            | SqlTypeFamily::Other
            | SqlTypeFamily::Bytes
            | SqlTypeFamily::Uuid
            | SqlTypeFamily::Json
    )
}

/// Whether `name` names secret material, regardless of the column's SQL type.
///
/// Shared by the guard (which also gates on family) and, as a hard backstop, by
/// the literal-emitting distribution heuristics (which refuse to persist source
/// values for such a column even if they are somehow reached). "High
/// confidence" here means a definite credential-name match — any classified
/// credential term, not a fuzzy guess.
pub(super) fn is_high_confidence_credential(name: &str) -> bool {
    classify(name).is_some()
}

/// Whether a column would be credential-guarded: a credential-looking name on a
/// string-shaped family. Callers use this to suppress source-derived rules (a
/// declared DEFAULT, observed samples) that must never win over the synthetic
/// credential generator for such a column.
pub(super) fn is_guarded(ctx: &ColumnContext<'_>) -> bool {
    let column = ctx.column();
    guardable_family(&column.family) && classify(&column.name).is_some()
}

/// Propose the credential-guard candidate for a column, if its name looks like
/// secret material.
///
/// The generator is always synthetic (never source-derived), and is chosen so
/// the emitted rule *type-checks against the column's family*: a `Text`/`Other`
/// credential column gets the specific `credential.*` kind, but a `uuid`/`json`/
/// `bytea` credential column gets the family-native synthetic generator
/// (`uuid`/`json_value`/`bytes`) — because a `credential.token` value is a
/// random string, not a valid UUID/JSON/blob, and would fail the compiler's
/// type-check (or render invalid data) for those columns.
pub(super) fn candidates(ctx: &ColumnContext<'_>) -> Vec<Candidate> {
    let column = ctx.column();
    // Only string-shaped columns carry credential material we know how to
    // synthesize; a numeric "token_count" is not a token.
    if !guardable_family(&column.family) {
        return Vec::new();
    }

    let Some((kind, confidence)) = classify(&column.name) else {
        return Vec::new();
    };

    let generator = match column.family {
        // Text/Other: the specific credential generator (accepts Text).
        SqlTypeFamily::Text | SqlTypeFamily::Other => credential_generator(kind),
        // Uuid: a fresh random synthetic UUID — format-valid, not source-derived.
        SqlTypeFamily::Uuid => generator("uuid"),
        // Json: a synthetic JSON document (accepts Json).
        SqlTypeFamily::Json => generator("json_value"),
        // Bytes: a random synthetic byte string (accepts Bytes).
        SqlTypeFamily::Bytes => generator("bytes"),
        // `guardable_family` admits nothing else.
        _ => return Vec::new(),
    };

    vec![Candidate::new(
        Precedence::CredentialGuard,
        confidence,
        "credential_name_guard",
        generator,
    )]
}

/// Which credential generator a column name maps to, and how sure we are.
fn classify(name: &str) -> Option<(&'static str, Confidence)> {
    let lower = name.to_ascii_lowercase();

    // Password columns -> an unmistakably synthetic password hash.
    if contains_any(&lower, &["password", "passwd", "pwd", "password_hash"]) {
        return Some(("credential.password_hash", Confidence::High));
    }
    // Private keys / PEM material -> a fixed, invalid placeholder.
    if contains_any(&lower, &["private_key", "privatekey", "secret_key", "pem"]) {
        return Some(("credential.placeholder", Confidence::High));
    }
    // API keys.
    if contains_any(
        &lower,
        &["api_key", "apikey", "api_secret", "client_secret"],
    ) {
        return Some(("credential.api_key", Confidence::High));
    }
    // Bearer / session / refresh tokens.
    if contains_any(
        &lower,
        &[
            "access_token",
            "refresh_token",
            "session_token",
            "auth_token",
            "bearer",
            "token",
        ],
    ) {
        return Some(("credential.token", Confidence::High));
    }
    // Generic secrets and salts.
    if contains_any(&lower, &["secret", "salt"]) {
        return Some(("credential.secret", Confidence::Medium));
    }
    None
}

fn credential_generator(kind: &'static str) -> GeneratorConfig {
    generator(kind)
}

fn contains_any(haystack: &str, needles: &[&str]) -> bool {
    needles.iter().any(|needle| haystack.contains(needle))
}