use std::collections::{BTreeMap, BTreeSet};
use serde::{Deserialize, Serialize};
pub const DRY_RUN_ENV: &str = "DRY_RUN";
pub const REDACTED: &str = "[REDACTED]";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RedactionKind {
KnownValue,
TokenPattern,
EnvLine,
}
impl RedactionKind {
fn order(self) -> u8 {
match self {
Self::KnownValue => 0,
Self::TokenPattern => 1,
Self::EnvLine => 2,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Redaction {
pub kind: RedactionKind,
pub name: String,
pub count: u64,
}
type ReportCounts = std::collections::BTreeMap<(u8, String), (RedactionKind, u64)>;
#[must_use]
pub fn merge_redaction_reports<I: IntoIterator<Item = Redaction>>(reports: I) -> Vec<Redaction> {
let mut counts: ReportCounts = std::collections::BTreeMap::new();
for r in reports {
let e = counts
.entry((r.kind.order(), r.name.clone()))
.or_insert((r.kind, 0));
e.1 += r.count;
}
counts
.into_iter()
.map(|((_, name), (kind, count))| Redaction { kind, name, count })
.collect()
}
#[derive(Debug, Clone, Default)]
pub struct SecretRedactor {
known: Vec<(String, String)>,
patterns: bool,
}
impl SecretRedactor {
#[must_use]
pub fn patterns_only() -> Self {
Self {
known: Vec::new(),
patterns: true,
}
}
#[must_use]
pub fn from_env_names(names: &[String]) -> Self {
let mut known: Vec<(String, String)> = names
.iter()
.filter_map(|name| {
let value = std::env::var(name).ok()?;
if value.len() >= 8 {
Some((name.clone(), value))
} else {
None
}
})
.collect();
known.sort_by_key(|(_, v)| std::cmp::Reverse(v.len()));
Self {
known,
patterns: true,
}
}
#[must_use]
pub fn with_env_names(mut self, names: &[String]) -> Self {
let extra = Self::from_env_names(names);
self.known.extend(extra.known);
self.known.sort_by_key(|(_, v)| std::cmp::Reverse(v.len()));
self
}
#[must_use]
pub fn without_patterns(mut self) -> Self {
self.patterns = false;
self
}
#[must_use]
pub fn is_noop(&self) -> bool {
self.known.is_empty() && !self.patterns
}
#[must_use]
pub fn redact(&self, text: &str) -> String {
self.redact_with_report(text).0
}
#[must_use]
pub fn redact_with_report(&self, text: &str) -> (String, Vec<Redaction>) {
if self.is_noop() {
return (text.to_string(), Vec::new());
}
let mut out = text.to_string();
let mut counts: ReportCounts = std::collections::BTreeMap::new();
let mut ordered: Vec<&(String, String)> = self.known.iter().collect();
ordered.sort_by_key(|(_, v)| std::cmp::Reverse(v.len()));
for (name, value) in ordered {
let count: u64 = out.matches(value.as_str()).count() as u64;
if count > 0 {
out = out.replace(value.as_str(), &format!("[REDACTED:{name}]"));
let e = counts
.entry((RedactionKind::KnownValue.order(), name.clone()))
.or_insert((RedactionKind::KnownValue, 0));
e.1 += count;
}
}
if self.patterns {
let (pass2, token_counts) = redact_token_patterns_with_report(&out);
let (pass3, env_counts) = redact_env_lines_with_report(&pass2);
out = pass3;
counts.extend(token_counts);
counts.extend(env_counts);
}
let report = counts
.into_iter()
.map(|((_, name), (kind, count))| Redaction { kind, name, count })
.collect();
(out, report)
}
}
const TOKEN_PREFIXES: &[(&str, usize)] = &[
("github_pat_", 20),
("ghp_", 20),
("gho_", 20),
("ghu_", 20),
("ghs_", 20),
("ghr_", 20),
("sk-", 20),
("xoxb-", 12),
("xoxa-", 12),
("xoxp-", 12),
("xoxr-", 12),
("xoxs-", 12),
];
fn is_token_char(c: char) -> bool {
c.is_ascii_alphanumeric() || c == '_' || c == '-'
}
fn redact_token_patterns_with_report(text: &str) -> (String, ReportCounts) {
let mut counts: ReportCounts = std::collections::BTreeMap::new();
let mut bump = |prefix: &str| {
let e = counts
.entry((RedactionKind::TokenPattern.order(), prefix.to_string()))
.or_insert((RedactionKind::TokenPattern, 0));
e.1 += 1;
};
let chars: Vec<char> = text.chars().collect();
let mut out = String::with_capacity(text.len());
let mut i = 0;
while i < chars.len() {
let rest: String = chars[i..].iter().collect();
if let Some((prefix, min_tail)) = TOKEN_PREFIXES
.iter()
.find(|(prefix, _)| rest.starts_with(prefix))
{
let tail_len = chars[i + prefix.len()..]
.iter()
.take_while(|c| is_token_char(**c))
.count();
if tail_len >= *min_tail {
out.push_str("[REDACTED:token]");
bump(prefix);
i += prefix.len() + tail_len;
continue;
}
}
if rest.starts_with("AKIA") {
let tail: String = chars[i + 4..]
.iter()
.take(16)
.take_while(|c| c.is_ascii_uppercase() || c.is_ascii_digit())
.collect();
if tail.len() == 16 {
out.push_str("[REDACTED:token]");
bump("AKIA");
i += 4 + 16;
continue;
}
}
out.push(chars[i]);
i += 1;
}
(out, counts)
}
const SECRET_SEGMENTS: &[&str] = &["TOKEN", "SECRET", "PASSWORD", "PASSWD", "CREDENTIAL"];
fn is_secret_shaped_name(name: &str) -> bool {
if name.is_empty()
|| !name
.chars()
.all(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || c == '_')
{
return false;
}
let segments: Vec<&str> = name.split('_').collect();
segments.iter().any(|s| SECRET_SEGMENTS.contains(s))
|| segments.last() == Some(&"KEY")
|| segments.contains(&"APIKEY")
}
fn redact_env_lines_with_report(text: &str) -> (String, ReportCounts) {
let mut counts: ReportCounts = std::collections::BTreeMap::new();
let mut out = String::with_capacity(text.len());
let mut first = true;
for line in text.split('\n') {
if !first {
out.push('\n');
}
first = false;
if let Some((name, value)) = line.split_once('=')
&& !value.is_empty()
&& !value.starts_with("[REDACTED")
&& is_secret_shaped_name(name)
{
out.push_str(name);
out.push('=');
out.push_str(REDACTED);
let e = counts
.entry((RedactionKind::EnvLine.order(), name.to_string()))
.or_insert((RedactionKind::EnvLine, 0));
e.1 += 1;
} else {
out.push_str(line);
}
}
(out, counts)
}
#[derive(Debug, Clone, Default, PartialEq)]
pub enum ExecEnvPolicy {
#[default]
Inherit,
Allowlist {
allow: BTreeSet<String>,
},
}
impl ExecEnvPolicy {
#[must_use]
pub fn allowlist(names: &[String]) -> Self {
Self::Allowlist {
allow: names.iter().cloned().collect(),
}
}
#[must_use]
pub fn resolve_from<I>(&self, parent: I) -> Option<BTreeMap<String, String>>
where
I: IntoIterator<Item = (String, String)>,
{
match self {
Self::Inherit => None,
Self::Allowlist { allow } => {
let env: BTreeMap<String, String> = parent
.into_iter()
.filter(|(name, _)| allow.contains(name) || name == DRY_RUN_ENV)
.collect();
Some(env)
}
}
}
#[must_use]
pub fn resolve(&self) -> Option<BTreeMap<String, String>> {
self.resolve_from(std::env::vars())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn redactor_with(name: &str, value: &str) -> SecretRedactor {
SecretRedactor {
known: vec![(name.to_string(), value.to_string())],
patterns: false,
}
}
#[test]
fn known_value_is_redacted_anywhere_in_the_text() {
let r = redactor_with("KIAN_TEST_SECRET", "gh-value-aaaaaaaaaaaaaaaaaaaa");
let env_dump =
"PATH=/usr/bin\nKIAN_TEST_SECRET=gh-value-aaaaaaaaaaaaaaaaaaaa\nHOME=/root\n";
let out = r.redact(env_dump);
assert!(!out.contains("gh-value-aaaaaaaaaaaaaaaaaaaa"));
assert!(out.contains("KIAN_TEST_SECRET=[REDACTED:KIAN_TEST_SECRET]"));
assert!(out.contains("PATH=/usr/bin"));
assert!(out.contains("HOME=/root"));
}
#[test]
fn known_value_is_redacted_in_loose_echo_output_not_just_env_lines() {
let r = redactor_with("DEEPSEEK_API_KEY", "sk-loose-value-1234567890abcd");
let out = r.redact("the key is sk-loose-value-1234567890abcd ok?");
assert_eq!(out, "the key is [REDACTED:DEEPSEEK_API_KEY] ok?");
}
#[test]
fn longer_known_values_win_over_shorter_ones_sharing_a_prefix() {
let mut r = redactor_with("SHORTY", "abcdefgh");
r.known.push(("LONGONE".into(), "abcdefghij1234".into()));
let out = r.redact("abcdefghij1234 and abcdefgh");
assert!(out.contains("[REDACTED:LONGONE]"));
assert!(out.contains("[REDACTED:SHORTY]"));
assert!(!out.contains("abcdefgh"));
}
#[test]
fn github_classic_token_shape_is_redacted_unconfigured() {
let r = SecretRedactor::patterns_only();
let out = r.redact("token=ghp_abcdefghijklmnopqrstuvwx");
assert_eq!(out, "token=[REDACTED:token]");
}
#[test]
fn openai_style_sk_token_is_redacted() {
let r = SecretRedactor::patterns_only();
let out = r.redact("key sk-abcdefghij1234567890XYZ over");
assert_eq!(out, "key [REDACTED:token] over");
}
#[test]
fn short_or_malformed_token_lookalikes_are_left_alone() {
let r = SecretRedactor::patterns_only();
let out = r.redact("sk-short and ghp_tiny stay");
assert_eq!(out, "sk-short and ghp_tiny stay");
}
#[test]
fn aws_access_key_id_shape_is_redacted() {
let r = SecretRedactor::patterns_only();
let out = r.redact("aws AKIAIOSFODNN7EXAMPLE here");
assert_eq!(out, "aws [REDACTED:token] here");
}
#[test]
fn secret_shaped_env_lines_are_redacted_unconfigured() {
let r = SecretRedactor::patterns_only();
let dump = "PATH=/bin\nMY_SERVICE_TOKEN=zzz-not-a-known-pattern\nDEEPSEEK_API_KEY=whatever123\nKEYBOARD=logitech\nMONKEY_POOL=yes\nHOME=/root\n";
let out = r.redact(dump);
assert!(out.contains("MY_SERVICE_TOKEN=[REDACTED]"));
assert!(out.contains("DEEPSEEK_API_KEY=[REDACTED]"));
assert!(out.contains("KEYBOARD=logitech"));
assert!(out.contains("MONKEY_POOL=yes"));
assert!(out.contains("PATH=/bin"));
assert!(out.contains("HOME=/root"));
}
#[test]
fn lowercase_non_env_names_are_not_touched() {
let r = SecretRedactor::patterns_only();
let out = r.redact("some password=lame in prose");
assert_eq!(out, "some password=lame in prose");
}
#[test]
fn noop_redactor_returns_text_unchanged() {
let r = SecretRedactor::default();
assert!(r.is_noop());
let text = "KIAN_TEST_SECRET=supersecretvalue123";
assert_eq!(r.redact(text), text);
}
#[test]
fn without_patterns_still_redacts_known_values() {
let mut r = redactor_with("T", "knownvalue123456");
r.patterns = false;
let out = r.redact("x=knownvalue123456 ghp_abcdefghijklmnopqrstuvwx");
assert!(out.contains("x=[REDACTED:T]"));
assert!(out.contains("ghp_abcdefghijklmnopqrstuvwx"));
}
#[test]
fn report_counts_all_three_kinds_and_text_matches_plain_redact() {
let r = redactor_with("KIAN_TEST_SECRET", "gh-value-aaaaaaaaaaaaaaaaaaaa");
let mut r = r;
r.patterns = true;
let dump = "PATH=/bin\n\
KIAN_TEST_SECRET=gh-value-aaaaaaaaaaaaaaaaaaaa\n\
MY_SERVICE_TOKEN=zzz-not-a-known-pattern\n\
loose ghp_aaaaaaaaaaaaaaaaaaaaaaaaaaaa token\n\
twice: gh-value-aaaaaaaaaaaaaaaaaaaa and gh-value-aaaaaaaaaaaaaaaaaaaa\n";
let (text, report) = r.redact_with_report(dump);
assert_eq!(text, r.redact(dump));
assert!(text.contains("KIAN_TEST_SECRET=[REDACTED:KIAN_TEST_SECRET]"));
assert!(text.contains("MY_SERVICE_TOKEN=[REDACTED]"));
assert!(text.contains("loose [REDACTED:token] token"));
assert_eq!(
report,
vec![
Redaction {
kind: RedactionKind::KnownValue,
name: "KIAN_TEST_SECRET".into(),
count: 3,
},
Redaction {
kind: RedactionKind::TokenPattern,
name: "ghp_".into(),
count: 1,
},
Redaction {
kind: RedactionKind::EnvLine,
name: "MY_SERVICE_TOKEN".into(),
count: 1,
},
]
);
}
#[test]
fn noop_redactor_reports_nothing() {
let r = SecretRedactor::default();
let (text, report) = r.redact_with_report("DEEPSEEK_API_KEY=whatever123");
assert_eq!(text, "DEEPSEEK_API_KEY=whatever123");
assert!(report.is_empty());
}
#[test]
fn redaction_serializes_as_the_additive_provenance_shape() {
let r = serde_json::to_value(Redaction {
kind: RedactionKind::KnownValue,
name: "DEEPSEEK_API_KEY".into(),
count: 2,
})
.unwrap();
assert_eq!(
r,
serde_json::json!({"kind": "known_value", "name": "DEEPSEEK_API_KEY", "count": 2})
);
}
#[test]
fn merge_redaction_reports_sums_and_sorts() {
let merged = merge_redaction_reports([
Redaction {
kind: RedactionKind::EnvLine,
name: "A_TOKEN".into(),
count: 1,
},
Redaction {
kind: RedactionKind::EnvLine,
name: "A_TOKEN".into(),
count: 2,
},
Redaction {
kind: RedactionKind::KnownValue,
name: "Z_SECRET".into(),
count: 1,
},
]);
assert_eq!(
merged,
vec![
Redaction {
kind: RedactionKind::KnownValue,
name: "Z_SECRET".into(),
count: 1,
},
Redaction {
kind: RedactionKind::EnvLine,
name: "A_TOKEN".into(),
count: 3,
},
]
);
}
#[test]
fn inherit_policy_resolves_to_none() {
assert_eq!(ExecEnvPolicy::Inherit.resolve_from(Vec::new()), None);
}
#[test]
fn allowlist_policy_keeps_only_allowed_vars_and_dry_run() {
let policy = ExecEnvPolicy::allowlist(&[
"KIAN_ENV_TEST_PATH".to_string(),
"KIAN_ENV_TEST_HOME".to_string(),
]);
let parent = vec![
("KIAN_ENV_TEST_PATH".to_string(), "/bin".to_string()),
("KIAN_ENV_TEST_HOME".to_string(), "/root".to_string()),
("KIAN_GITHUB_TOKEN".to_string(), "gh-secret".to_string()),
("DEEPSEEK_API_KEY".to_string(), "sk-secret".to_string()),
("DRY_RUN".to_string(), "1".to_string()),
];
let resolved = policy.resolve_from(parent).expect("allowlist resolves");
assert_eq!(
resolved,
BTreeMap::from([
("KIAN_ENV_TEST_PATH".to_string(), "/bin".to_string()),
("KIAN_ENV_TEST_HOME".to_string(), "/root".to_string()),
("DRY_RUN".to_string(), "1".to_string()),
])
);
}
#[test]
fn allowlist_policy_without_dry_run_set_leaves_it_out() {
let policy = ExecEnvPolicy::allowlist(&["KIAN_ENV_TEST_PATH".to_string()]);
let parent = vec![("KIAN_ENV_TEST_PATH".to_string(), "/bin".to_string())];
let resolved = policy.resolve_from(parent).expect("allowlist resolves");
assert_eq!(
resolved,
BTreeMap::from([("KIAN_ENV_TEST_PATH".to_string(), "/bin".to_string())])
);
}
#[test]
fn empty_allowlist_is_a_closed_minimal_environment() {
let policy = ExecEnvPolicy::allowlist(&[]);
let parent = vec![("ANYTHING".to_string(), "else".to_string())];
let resolved = policy.resolve_from(parent).expect("allowlist resolves");
assert!(!resolved.contains_key("ANYTHING"));
}
}