use serde_json::{Map, Value};
const SENSITIVE_PATTERNS: &[&str] = &[
"token",
"password",
"passwd",
"secret",
"key",
"credential",
"session_id",
"session",
"cookie",
"auth",
"api_key",
"apikey",
"access_token",
"refresh_token",
"user_token",
"bearer",
"authorization",
"private",
"login",
"email",
"phone",
"sms",
"serial",
"mac",
"mac_address",
"ssid",
];
const ZERO_VISIBILITY_PATTERNS: &[&str] = &[
"token",
"password",
"passwd",
"secret",
"key",
"credential",
"session_id",
"session",
"cookie",
"auth",
"api_key",
"apikey",
"access_token",
"refresh_token",
"user_token",
"bearer",
"authorization",
"private",
];
const VISIBLE_CHARS: usize = 4;
const MAX_REDACTION_DEPTH: usize = 16;
const UNTRAVERSABLE_PLACEHOLDER: &str = "[REDACTED:cyclic-or-too-deep]";
fn is_sensitive_key(key: &str) -> bool {
let key_lower = key.to_lowercase();
SENSITIVE_PATTERNS
.iter()
.any(|pattern| key_lower.contains(pattern))
}
fn is_zero_visibility_key(key: &str) -> bool {
let key_lower = key.to_lowercase();
ZERO_VISIBILITY_PATTERNS
.iter()
.any(|pattern| key_lower.contains(pattern))
}
fn redact_value(value: &Value, visible_chars: usize) -> Value {
let text = match value {
Value::Null => return Value::String("[NONE]".to_string()),
Value::Bool(_) => return Value::String("[REDACTED:bool]".to_string()),
Value::Number(_) => return Value::String("[REDACTED:number]".to_string()),
Value::String(s) => s.clone(),
Value::Array(_) | Value::Object(_) => value.to_string(),
};
if text.is_empty() {
return Value::String("[EMPTY]".to_string());
}
let length = text.chars().count();
if visible_chars == 0 || length <= visible_chars {
return Value::String(format!("[REDACTED:{length}chars]"));
}
let visible: String = text.chars().take(visible_chars).collect();
Value::String(format!("{visible}...[REDACTED:{length}chars]"))
}
fn redact_object(map: &Map<String, Value>, depth: usize) -> Value {
let mut result = Map::with_capacity(map.len());
for (key, value) in map {
let redacted = if is_sensitive_key(key) {
let visible_chars = if is_zero_visibility_key(key) {
0
} else {
VISIBLE_CHARS
};
redact_value(value, visible_chars)
} else {
redact_nested(value, depth + 1)
};
result.insert(key.clone(), redacted);
}
Value::Object(result)
}
fn redact_nested(value: &Value, depth: usize) -> Value {
match value {
Value::Object(_) | Value::Array(_) if depth >= MAX_REDACTION_DEPTH => {
Value::String(UNTRAVERSABLE_PLACEHOLDER.to_string())
}
Value::Object(map) => redact_object(map, depth),
Value::Array(items) => Value::Array(
items
.iter()
.map(|item| redact_nested(item, depth + 1))
.collect(),
),
other => other.clone(),
}
}
pub fn redact_sensitive(value: &Value) -> Value {
redact_nested(value, 0)
}
#[cfg(test)]
mod tests {
use super::{is_sensitive_key, is_zero_visibility_key, redact_sensitive, redact_value};
use serde_json::{Value, json};
#[test]
fn detects_token_variations() {
assert!(is_sensitive_key("token"));
assert!(is_sensitive_key("user_token"));
assert!(is_sensitive_key("access_token"));
assert!(is_sensitive_key("refresh_token"));
assert!(is_sensitive_key("TOKEN"));
assert!(is_sensitive_key("User_Token"));
}
#[test]
fn detects_password_variations() {
assert!(is_sensitive_key("password"));
assert!(is_sensitive_key("passwd"));
assert!(is_sensitive_key("user_password"));
assert!(is_sensitive_key("PASSWORD"));
}
#[test]
fn detects_secret_variations() {
assert!(is_sensitive_key("secret"));
assert!(is_sensitive_key("client_secret"));
assert!(is_sensitive_key("api_secret"));
}
#[test]
fn detects_key_variations() {
assert!(is_sensitive_key("key"));
assert!(is_sensitive_key("api_key"));
assert!(is_sensitive_key("apikey"));
assert!(is_sensitive_key("private_key"));
}
#[test]
fn detects_session_variations() {
assert!(is_sensitive_key("session"));
assert!(is_sensitive_key("session_id"));
assert!(is_sensitive_key("cookie"));
}
#[test]
fn detects_auth_variations() {
assert!(is_sensitive_key("auth"));
assert!(is_sensitive_key("authorization"));
assert!(is_sensitive_key("bearer"));
}
#[test]
fn non_sensitive_keys_are_not_flagged() {
assert!(!is_sensitive_key("name"));
assert!(!is_sensitive_key("status"));
assert!(!is_sensitive_key("id"));
assert!(!is_sensitive_key("count"));
assert!(!is_sensitive_key("network_id"));
}
#[test]
fn substring_quirk_matches_incidental_words() {
assert!(is_sensitive_key("session_expiry")); assert!(is_sensitive_key("author")); assert!(is_sensitive_key("monkey")); }
#[test]
fn detects_identifier_shaped_variations_new_at_v8_0_4() {
assert!(is_sensitive_key("login"));
assert!(is_sensitive_key("email"));
assert!(is_sensitive_key("phone"));
assert!(is_sensitive_key("sms"));
assert!(is_sensitive_key("serial"));
assert!(is_sensitive_key("mac"));
assert!(is_sensitive_key("mac_address"));
assert!(is_sensitive_key("ssid"));
}
#[test]
fn credential_shaped_keys_are_zero_visibility() {
assert!(is_zero_visibility_key("token"));
assert!(is_zero_visibility_key("password"));
assert!(is_zero_visibility_key("session_id"));
assert!(is_zero_visibility_key("cookie"));
}
#[test]
fn identifier_shaped_keys_are_not_zero_visibility() {
assert!(!is_zero_visibility_key("login"));
assert!(!is_zero_visibility_key("email"));
assert!(!is_zero_visibility_key("phone"));
assert!(!is_zero_visibility_key("ssid"));
}
#[test]
fn redacts_null() {
assert_eq!(
redact_value(&json!(null), super::VISIBLE_CHARS),
json!("[NONE]")
);
}
#[test]
fn redacts_empty_string() {
assert_eq!(
redact_value(&json!(""), super::VISIBLE_CHARS),
json!("[EMPTY]")
);
}
#[test]
fn redacts_short_string() {
assert_eq!(
redact_value(&json!("abc"), super::VISIBLE_CHARS),
json!("[REDACTED:3chars]")
);
assert_eq!(
redact_value(&json!("abcd"), super::VISIBLE_CHARS),
json!("[REDACTED:4chars]")
);
}
#[test]
fn redacts_long_string() {
let result = redact_value(&json!("secret123456"), super::VISIBLE_CHARS);
let text = result.as_str().unwrap();
assert!(text.starts_with("secr"));
assert!(text.contains("[REDACTED:12chars]"));
}
#[test]
fn redacts_boolean() {
assert_eq!(
redact_value(&json!(true), super::VISIBLE_CHARS),
json!("[REDACTED:bool]")
);
assert_eq!(
redact_value(&json!(false), super::VISIBLE_CHARS),
json!("[REDACTED:bool]")
);
}
#[test]
fn redacts_numbers() {
assert_eq!(
redact_value(&json!(12345), super::VISIBLE_CHARS),
json!("[REDACTED:number]")
);
assert_eq!(
redact_value(&json!(3.5), super::VISIBLE_CHARS),
json!("[REDACTED:number]")
);
}
#[test]
fn redacts_sensitive_keys() {
let result = redact_sensitive(&json!({"user_token": "abc123", "name": "John"}));
assert!(!result["user_token"].as_str().unwrap().contains("abc123"));
assert!(result["user_token"].as_str().unwrap().contains("[REDACTED"));
assert_eq!(result["name"], "John");
}
#[test]
fn preserves_non_sensitive_keys() {
let result = redact_sensitive(&json!({"status": "ok", "count": 5, "items": ["a", "b"]}));
assert_eq!(result["status"], "ok");
assert_eq!(result["count"], 5);
assert_eq!(result["items"], json!(["a", "b"]));
}
#[test]
fn zero_visibility_keys_never_show_a_leading_character() {
let result = redact_sensitive(&json!({
"password": "super-secret-value",
"session_id": "abcdef123456",
"token": "eyJhbGciOiJIUzI1NiJ9",
}));
for key in ["password", "session_id", "token"] {
let redacted = result[key].as_str().unwrap();
assert!(
redacted.starts_with("[REDACTED:"),
"{key} must show no leading characters of its value, got {redacted:?}"
);
}
}
#[test]
fn identifier_shaped_keys_still_show_a_short_prefix() {
let result = redact_sensitive(&json!({"email": "user@example.com"}));
let redacted = result["email"].as_str().unwrap();
assert!(
redacted.starts_with("user"),
"identifier-shaped keys keep the normal partial-prefix redaction, got {redacted:?}"
);
assert!(redacted.contains("[REDACTED"));
}
#[test]
fn handles_nested_dicts() {
let result = redact_sensitive(&json!({
"user": {"name": "John", "password": "secret123"},
"session_id": "xyz789",
}));
assert_eq!(result["user"]["name"], "John");
assert!(
result["user"]["password"]
.as_str()
.unwrap()
.contains("[REDACTED")
);
assert!(result["session_id"].as_str().unwrap().contains("[REDACTED"));
}
#[test]
fn handles_nested_list_of_lists_of_dicts() {
let result = redact_sensitive(&json!({
"outer": [[{"token": "abc123"}]]
}));
let redacted = result["outer"][0][0]["token"].as_str().unwrap();
assert!(
redacted.contains("[REDACTED"),
"a token nested two array levels deep must still be redacted, got {redacted:?}"
);
}
#[test]
fn handles_list_of_dicts() {
let result = redact_sensitive(&json!({
"users": [
{"name": "John", "token": "abc"},
{"name": "Jane", "token": "xyz"},
]
}));
assert_eq!(result["users"][0]["name"], "John");
assert!(
result["users"][0]["token"]
.as_str()
.unwrap()
.contains("[REDACTED")
);
assert_eq!(result["users"][1]["name"], "Jane");
assert!(
result["users"][1]["token"]
.as_str()
.unwrap()
.contains("[REDACTED")
);
}
#[test]
fn handles_empty_object() {
assert_eq!(redact_sensitive(&json!({})), json!({}));
}
#[test]
fn handles_dict() {
let result = redact_sensitive(&json!({"token": "secret"}));
assert!(result["token"].as_str().unwrap().contains("[REDACTED"));
}
#[test]
fn passes_through_non_dict() {
assert_eq!(redact_sensitive(&json!("hello")), json!("hello"));
assert_eq!(redact_sensitive(&json!(123)), json!(123));
assert_eq!(redact_sensitive(&json!(null)), json!(null));
}
#[test]
fn passes_through_scalars_and_strings_unchanged() {
assert_eq!(redact_sensitive(&json!("s=abc")), json!("s=abc"));
assert_eq!(redact_sensitive(&json!(true)), json!(true));
}
const CANARY: &str = "CANARY-tok-5b2e";
fn nest(levels: usize, leaf: Value) -> Value {
(0..levels).fold(leaf, |inner, _| Value::Array(vec![inner]))
}
fn descend(value: &Value, levels: usize) -> &Value {
(0..levels).fold(value, |current, _| ¤t[0])
}
#[test]
fn top_level_array_of_objects_is_redacted() {
let result = redact_sensitive(&json!([{"token": CANARY, "name": "n"}]));
assert!(result.is_array());
assert_eq!(result[0]["name"], "n");
assert_eq!(result[0]["token"], "[REDACTED:15chars]");
assert!(!result.to_string().contains(CANARY));
}
#[test]
fn top_level_array_of_arrays_is_redacted() {
let result = redact_sensitive(&json!([[{"password": CANARY}]]));
assert!(result.is_array());
assert!(result[0].is_array());
assert!(result[0][0].is_object());
assert!(!result.to_string().contains(CANARY));
}
#[test]
fn array_in_array_in_object_keeps_every_container_kind() {
let result = redact_sensitive(&json!({"outer": [[{"token": CANARY}]]}));
assert!(result["outer"].is_array());
assert!(result["outer"][0].is_array());
assert!(result["outer"][0][0].is_object());
assert!(!result.to_string().contains(CANARY));
}
#[test]
fn argument_is_unchanged_after_redaction() {
let input = json!({"token": CANARY, "items": [{"password": CANARY}]});
let snapshot = input.clone();
let _ = redact_sensitive(&input);
assert_eq!(input, snapshot);
assert_eq!(input["token"], CANARY);
}
#[test]
fn sensitive_key_with_container_value_is_fully_redacted() {
let result = redact_sensitive(&json!({"token": {"a": 1}}));
let redacted = result["token"].as_str().unwrap();
assert!(redacted.starts_with("[REDACTED:"));
assert!(!redacted.contains('{'));
assert_eq!(redacted, "[REDACTED:7chars]");
}
#[test]
fn account_email_object_is_replaced_with_its_json_length() {
let result = redact_sensitive(&json!({
"email": {"address": "user@example.com", "verified": true}
}));
assert_eq!(result["email"], "{\"ad...[REDACTED:46chars]");
}
#[test]
fn a_container_shows_a_prefix_only_under_an_identifier_shaped_key() {
let result = redact_sensitive(&json!({
"emails": ["ab@x.io"],
"token": ["abcdefgh"],
}));
assert_eq!(result["emails"], "[\"ab...[REDACTED:11chars]");
assert_eq!(result["token"], "[REDACTED:12chars]");
}
#[test]
fn nesting_at_the_cap_is_still_walked() {
let result = redact_sensitive(&nest(15, json!({"token": CANARY, "name": "n"})));
let text = result.to_string();
assert!(text.contains("\"name\":\"n\""));
assert!(!text.contains("cyclic-or-too-deep"));
assert!(!text.contains(CANARY));
assert_eq!(descend(&result, 15)["token"], "[REDACTED:15chars]");
}
#[test]
fn nesting_beyond_the_cap_is_replaced_by_placeholder() {
let result = redact_sensitive(&nest(16, json!({"token": CANARY, "note": CANARY})));
assert_eq!(descend(&result, 16), "[REDACTED:cyclic-or-too-deep]");
assert!(!result.to_string().contains(CANARY));
}
#[test]
fn empty_container_beyond_the_cap_is_replaced() {
for leaf in [json!({}), json!([])] {
let result = redact_sensitive(&nest(16, leaf));
assert_eq!(descend(&result, 16), "[REDACTED:cyclic-or-too-deep]");
}
}
#[test]
fn scalar_beyond_the_cap_passes_through() {
let result = redact_sensitive(&nest(16, json!("plain")));
assert_eq!(descend(&result, 16), "plain");
}
#[test]
fn sensitive_key_value_at_the_cap_is_length_redacted_not_replaced() {
let result = redact_sensitive(&nest(15, json!({"token": [[1]]})));
assert_eq!(descend(&result, 15)["token"], "[REDACTED:5chars]");
}
#[test]
fn mixed_object_array_nesting_beyond_the_cap_hides_the_canary() {
let mut value = json!({"token": CANARY, "note": CANARY});
for _ in 0..64 {
value = json!({ "n": [value] });
}
let text = redact_sensitive(&value).to_string();
assert!(text.contains("[REDACTED:cyclic-or-too-deep]"));
assert!(!text.contains(CANARY));
}
#[test]
fn very_deep_nesting_terminates() {
let value = nest(2000, json!({"token": CANARY}));
let text = redact_sensitive(&value).to_string();
assert!(text.contains("[REDACTED:cyclic-or-too-deep]"));
assert!(!text.contains(CANARY));
}
}