Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(logging): redact any snake_case *_token key and escaped JSON keys

M3gA-Mindc4e566420fe9
diff --git a/crates/openhuman-core/src/core/log_redaction.rs b/crates/openhuman-core/src/core/log_redaction.rs
index 51b0eae3f9..b934e36892 100644
--- a/crates/openhuman-core/src/core/log_redaction.rs
+++ b/crates/openhuman-core/src/core/log_redaction.rs
@@ -16,23 +16,10 @@ static SECRET_PATTERNS: Lazy<Vec<(Regex, &'static str)>> = Lazy::new(|| {
vec![
// Matches "Bearer <token>" and redacts the token.
(Regex::new(r"(?i)(bearer\s+)\S+").unwrap(), "${1}[REDACTED]"),
- // A named credential key with a quoted value: `"password":"a b, c"`.
- // The value runs to its closing quote (escapes included), so spaces
- // and delimiters inside it can't end the match early.
- (
- Regex::new(r#"(?i)\b((?:access_|refresh_|id_|auth_|session_|bearer_)?token|api[_-]?key|client_secret|password)("?\s*[=:]\s*")(?:[^"\\]|\\.)*""#).unwrap(),
- "${1}${2}[REDACTED]\"",
- ),
- // Matches a named credential key and redacts its value: `api_key=…`,
- // `access_token=…`, and JSON-quoted `"token":"…"`. The key list is an
- // allowlist so `cancellation_token=` and the like stay intact.
- (
- Regex::new(r#"(?i)\b((?:access_|refresh_|id_|auth_|session_|bearer_)?token|api[_-]?key|client_secret|password)("?\s*[=:]\s*"?)[^\s"&,;}]+"#).unwrap(),
- "${1}${2}[REDACTED]",
- ),
// An `Authorization: Basic …` header, however short its credential.
(
- Regex::new(r#"(?i)(authorization["']?\s*[:=]\s*["']?basic\s+)[A-Za-z0-9+/]+=*"#).unwrap(),
+ Regex::new(r#"(?i)(authorization["']?\s*[:=]\s*["']?basic\s+)[A-Za-z0-9+/]+=*"#)
+ .unwrap(),
"${1}[REDACTED]",
),
// HTTP Basic credentials.
@@ -63,6 +50,38 @@ static SECRET_PATTERNS: Lazy<Vec<(Regex, &'static str)>> = Lazy::new(|| {
]
});
+/// A credential key: `token` with any snake_case prefix (`bot_token`,
+/// `access_token`), `api_key`, `client_secret` or `password`.
+const CREDENTIAL_KEY: &str = r"(?i)\b((?:[a-z0-9]+_)*token|api[_-]?key|client_secret|password)";
+
+/// Named credential keys and their values, most specific first. `cancellation_token`
+/// matches the key shape but is not a secret, so `scrub_secrets` leaves it alone.
+static NAMED_KEY_PATTERNS: Lazy<Vec<(Regex, &'static str)>> = Lazy::new(|| {
+ [
+ // A quoted value: `"password":"a b, c"`. It runs to its closing quote
+ // (escapes included), so spaces and delimiters inside can't end it early.
+ (r#"("?\s*[=:]\s*")(?:[^"\\]|\\.)*""#, "${1}${2}[REDACTED]\""),
+ // The same inside escaped JSON: `{\"token\":\"x y\"}`.
+ (
+ r#"(\\"\s*:\s*\\")(?:[^"\\]|\\[^"])*\\""#,
+ "${1}${2}[REDACTED]\\\"",
+ ),
+ // An unquoted value: `api_key=…`, `access_token: …`.
+ (
+ r#"(\\?"?\s*[=:]\s*\\?"?)(?:[^\s"\\&,;}]|\\[^"])+"#,
+ "${1}${2}[REDACTED]",
+ ),
+ ]
+ .into_iter()
+ .map(|(value, replacement)| {
+ (
+ Regex::new(&format!("{CREDENTIAL_KEY}{value}")).unwrap(),
+ replacement,
+ )
+ })
+ .collect()
+});
+
/// Replace substrings that look like secrets with `[REDACTED]`.
///
/// Intended for anything about to be written to a log sink or an error report;
@@ -70,6 +89,19 @@ static SECRET_PATTERNS: Lazy<Vec<(Regex, &'static str)>> = Lazy::new(|| {
/// diagnostic message intact (unlike a whole-value prefix redaction).
pub fn scrub_secrets(input: &str) -> String {
let mut result = input.to_string();
+ for (re, replacement) in NAMED_KEY_PATTERNS.iter() {
+ result = re
+ .replace_all(&result, |caps: &regex::Captures<'_>| {
+ if caps[1].eq_ignore_ascii_case("cancellation_token") {
+ caps[0].to_string()
+ } else {
+ let mut out = String::new();
+ caps.expand(replacement, &mut out);
+ out
+ }
+ })
+ .into_owned();
+ }
for (re, replacement) in SECRET_PATTERNS.iter() {
result = re.replace_all(&result, *replacement).into_owned();
}
diff --git a/crates/openhuman-core/src/core/log_redaction_tests.rs b/crates/openhuman-core/src/core/log_redaction_tests.rs
index cf42f845d9..3e5f8bc44f 100644
--- a/crates/openhuman-core/src/core/log_redaction_tests.rs
+++ b/crates/openhuman-core/src/core/log_redaction_tests.rs
@@ -75,3 +75,21 @@ fn scrubs_a_short_basic_authorization_header() {
"Authorization: Basic [REDACTED]"
);
}
+
+#[test]
+fn scrubs_any_snake_case_token_key_and_escaped_json() {
+ assert_eq!(scrub_secrets("bot_token=b0t12345"), "bot_token=[REDACTED]");
+ assert_eq!(
+ scrub_secrets(r#"body {\"token\":\"ghi 789\"} sent"#),
+ r#"body {\"token\":\"[REDACTED]\"} sent"#
+ );
+ assert_eq!(
+ scrub_secrets(r#"{\"api_key\": \"k1\", \"x\": 1}"#),
+ r#"{\"api_key\": \"[REDACTED]\", \"x\": 1}"#
+ );
+ // Still not a secret.
+ assert_eq!(
+ scrub_secrets("cancellation_token=abc"),
+ "cancellation_token=abc"
+ );
+}