Skip to content

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

fix(logging): redact whole quoted values, short Basic headers, and an @ in a URL's query

M3gA-Mind795e6ceb7e5f
diff --git a/crates/openhuman-core/src/config/schema/storage.rs b/crates/openhuman-core/src/config/schema/storage.rs
index 84fca80ae9..511affaa15 100644
--- a/crates/openhuman-core/src/config/schema/storage.rs
+++ b/crates/openhuman-core/src/config/schema/storage.rs
@@ -79,19 +79,22 @@ impl std::fmt::Debug for StorageConfig {
/// query string and fragment dropped (they can carry tokens, such as MongoDB's
/// `authMechanismProperties`).
pub fn redact_url(url: &str) -> String {
- // Userinfo ends at the LAST `@`, so an unencoded `?`, `#` or `/` in the
- // password can't end it early. A query that holds an `@` hides the host
- // too, which errs on the safe side.
- let (mut out, rest) = match url.split_once("://") {
- Some((scheme, rest)) => match rest.rsplit_once('@') {
- Some((_, host)) => (format!("{scheme}://***@"), host),
- None => (format!("{scheme}://"), rest),
- },
- None => (String::new(), url),
+ // Userinfo follows `//` (with or without a scheme) and ends at the LAST
+ // `@`, so an unencoded `?`, `#` or `/` in the password can't end it early.
+ // A `?` or `#` before that `@` could as well be a query holding an `@`:
+ // everything after `//` is hidden then.
+ let (head, rest) = url.find("//").map_or(("", url), |at| url.split_at(at + 2));
+ let (userinfo, host) = match rest.rsplit_once('@') {
+ Some((userinfo, host)) => (Some(userinfo), host),
+ None => (None, rest),
};
- let end = rest.find(['?', '#']).unwrap_or(rest.len());
- out.push_str(&rest[..end]);
- if end < rest.len() {
+ if userinfo.is_some_and(|userinfo| userinfo.contains(['?', '#'])) {
+ return format!("{head}***");
+ }
+ let end = host.find(['?', '#']).unwrap_or(host.len());
+ let mask = if userinfo.is_some() { "***@" } else { "" };
+ let mut out = format!("{head}{mask}{}", &host[..end]);
+ if end < host.len() {
out.push_str("?***");
}
out
diff --git a/crates/openhuman-core/src/config/schema/storage_tests.rs b/crates/openhuman-core/src/config/schema/storage_tests.rs
index b86675c077..7223e40e49 100644
--- a/crates/openhuman-core/src/config/schema/storage_tests.rs
+++ b/crates/openhuman-core/src/config/schema/storage_tests.rs
@@ -54,11 +54,32 @@ fn journal_retention_defaults_to_thirty_days_and_reads_from_toml() {
#[test]
fn redact_hides_userinfo_when_the_password_has_a_query_char() {
+ assert_eq!(
+ redact_url("mongodb://app:p/[email protected]/oh"),
+ "mongodb://***@db.internal/oh"
+ );
+ // A `?` or `#` before the `@` is ambiguous (password or query): hide all.
for url in [
"mongodb://app:[email protected]/oh",
"mongodb://app:p#[email protected]/oh",
- "mongodb://app:p/[email protected]/oh",
] {
- assert_eq!(redact_url(url), "mongodb://***@db.internal/oh", "{url}");
+ assert_eq!(redact_url(url), "mongodb://***", "{url}");
}
}
+
+#[test]
+fn redact_hides_a_query_value_that_holds_an_at() {
+ assert_eq!(
+ redact_url("mongodb://db/oh?password=@hunter2"),
+ "mongodb://***"
+ );
+ assert_eq!(
+ redact_url("mongodb://db/x?token=prefix@SECRET"),
+ "mongodb://***"
+ );
+}
+
+#[test]
+fn redact_hides_userinfo_of_a_scheme_relative_url() {
+ assert_eq!(redact_url("//app:[email protected]"), "//***@db.internal");
+}
diff --git a/crates/openhuman-core/src/core/log_redaction.rs b/crates/openhuman-core/src/core/log_redaction.rs
index 2389faad1e..51b0eae3f9 100644
--- a/crates/openhuman-core/src/core/log_redaction.rs
+++ b/crates/openhuman-core/src/core/log_redaction.rs
@@ -16,6 +16,13 @@ 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.
@@ -23,6 +30,11 @@ static SECRET_PATTERNS: Lazy<Vec<(Regex, &'static str)>> = Lazy::new(|| {
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(),
+ "${1}[REDACTED]",
+ ),
// HTTP Basic credentials.
(
Regex::new(r"(?i)(basic\s+)[A-Za-z0-9+/=]{8,}").unwrap(),
diff --git a/crates/openhuman-core/src/core/log_redaction_tests.rs b/crates/openhuman-core/src/core/log_redaction_tests.rs
index 0871003c1b..cf42f845d9 100644
--- a/crates/openhuman-core/src/core/log_redaction_tests.rs
+++ b/crates/openhuman-core/src/core/log_redaction_tests.rs
@@ -55,3 +55,23 @@ fn scrubs_snake_case_and_json_quoted_tokens() {
"cancellation_token=abc"
);
}
+
+#[test]
+fn scrubs_a_quoted_value_through_its_closing_quote() {
+ let out = scrub_secrets(r#"{"password":"correct horse, battery; p&ss \"x\" y","user":"a"}"#);
+ for leaked in ["horse", "battery", "p&ss", " y\""] {
+ assert!(!out.contains(leaked), "{leaked} in {out}");
+ }
+ assert!(out.ends_with(r#","user":"a"}"#), "{out}");
+ // An unterminated quoted value is still redacted.
+ let out = scrub_secrets(r#""token":"abc123"#);
+ assert!(!out.contains("abc123"), "{out}");
+}
+
+#[test]
+fn scrubs_a_short_basic_authorization_header() {
+ assert_eq!(
+ scrub_secrets("Authorization: Basic dTpw"),
+ "Authorization: Basic [REDACTED]"
+ );
+}