Skip to content

The agent that grows with you

fix(redact): widen repr-field key class to mixed-case credential suffixes

Teknium92d4c0233e6f
diff --git a/agent/redact.py b/agent/redact.py
index ee91b2b3107..54b02a1e92f 100644
--- a/agent/redact.py
+++ b/agent/redact.py
@@ -402,6 +402,20 @@ _PYTHON_REPR_ENV_SUFFIXES = (
"_CREDENTIAL",
"_CREDENTIALS",
)
+# Casefolded credential suffixes for mixed/camel-case key names
+# (``UserPassword``, ``sessionToken``, ``clientApiKey``). Suffix-only so
+# ``token_count`` / ``password_policy`` metadata keys never match. Widened per
+# OpenHands/software-agent-sdk#4508.
+_PYTHON_REPR_CREDENTIAL_SUFFIXES = (
+ "apikey",
+ "api_key",
+ "token",
+ "secret",
+ "password",
+ "passwd",
+ "credential",
+ "credentials",
+)
_PYTHON_REPR_FIELD_RE = re.compile(
r"'(?P<key>[A-Za-z_][A-Za-z0-9_]*)'(?P<sep>\s*:\s*)"
r"(?:"
@@ -568,10 +582,19 @@ def _mask_token(token: str) -> str:
def _is_python_repr_secret_key(key: str) -> bool:
- """Return True for exact secret keys or uppercase env credential suffixes."""
- if key.casefold() in _PYTHON_REPR_SECRET_KEYS:
+ """Return True for exact secret keys or credential-suffixed key names."""
+ folded = key.casefold()
+ if folded in _PYTHON_REPR_SECRET_KEYS:
+ return True
+ if key.isupper() and key.endswith(_PYTHON_REPR_ENV_SUFFIXES):
return True
- return key.isupper() and key.endswith(_PYTHON_REPR_ENV_SUFFIXES)
+ # Mixed/camel-case keys ending in a credential word (``UserPassword``,
+ # ``sessionToken``, ``clientApiKey``) — the exact-set and uppercase-suffix
+ # rules above miss these. Suffix-only matching keeps metadata names like
+ # ``TOKEN_COUNT`` / ``PASSWORD_POLICY`` / ``SECRET_NAME`` untouched.
+ # Class widened per OpenHands/software-agent-sdk#4508 (their dict-entry
+ # redaction was uppercase-only and leaked mixed-case keys).
+ return folded.endswith(_PYTHON_REPR_CREDENTIAL_SUFFIXES)
def _redact_python_repr_fields(text: str) -> str:
diff --git a/tests/agent/test_redact.py b/tests/agent/test_redact.py
index 0c44d8652b0..b703ddd6a25 100644
--- a/tests/agent/test_redact.py
+++ b/tests/agent/test_redact.py
@@ -322,6 +322,28 @@ class TestPythonReprFields:
text = f"{{'{key}': 'public-metadata-value'}}"
assert redact_sensitive_text(text, force=True) == text
+ @pytest.mark.parametrize(
+ "key",
+ ["UserPassword", "sessionToken", "clientApiKey", "gh_token", "webhookSecret"],
+ )
+ def test_mixed_case_credential_suffix_key_is_redacted(self, key):
+ """Case-insensitive dict-entry class per OpenHands/software-agent-sdk#4508."""
+ secret = f"opaque-{key.lower()}-value-1234567890"
+ text = f"{{'{key}': '{secret}'}}"
+
+ result = redact_sensitive_text(text, force=True)
+
+ assert secret not in result
+ assert f"'{key}': '***'" in result
+
+ @pytest.mark.parametrize(
+ "key",
+ ["tokenizer", "secretary", "password_policy", "token_count", "keyring"],
+ )
+ def test_embedded_or_metadata_keyword_key_is_unchanged(self, key):
+ text = f"{{'{key}': 'ordinary-value-1234567890'}}"
+ assert redact_sensitive_text(text, force=True) == text
+
@pytest.mark.parametrize(
"value",
[