The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): redact dotted and Zhipu API keys
KoNit-K7b57cda6d9ad
diff --git a/agent/redact.py b/agent/redact.pyindex 6ef67ef4ae7..fbb20a0c212 100644--- a/agent/redact.py+++ b/agent/redact.py@@ -133,7 +133,8 @@ def _redact_enabled() -> bool:# Every pattern MUST start with a literal prefix: _PREFIX_SUBSTRINGS (the cheap# pre-screen gate) is derived from these literals and must stay false-negative-free._PREFIX_PATTERNS = [- r"sk-[A-Za-z0-9_-]{10,}", # OpenAI / OpenRouter / Anthropic (sk-ant-*)+ # Some provider-issued ``sk-`` keys contain dot-delimited body segments.+ r"sk-(?=[A-Za-z0-9_.-]{10,}(?![A-Za-z0-9_.-]))[A-Za-z0-9_-]+(?:\.[A-Za-z0-9_-]+)*",r"ghp_[A-Za-z0-9]{10,}", # GitHub PAT (classic)r"github_pat_[A-Za-z0-9_]{10,}", # GitHub PAT (fine-grained)r"gho_[A-Za-z0-9]{10,}", # GitHub OAuth access token@@ -564,6 +565,14 @@ def _compile_prefix_matcher(patterns: list) -> "re.Pattern[str]":_PREFIX_RE = _compile_prefix_matcher(_PREFIX_PATTERNS)+# Zhipu API keys use an unprefixed ``id.secret`` form. Keep this deliberately+# provider-shaped instead of applying a generic high-entropy dotted-token rule:+# both opaque segments are alphanumeric, the ID is 32--40 characters, and the+# credential suffix is at least six characters.+_ZHIPU_API_KEY_RE = re.compile(+ r"(?<![A-Za-z0-9_.-])([A-Za-z0-9]{32,40}\.[A-Za-z0-9]{6,})(?![A-Za-z0-9_.-])"+)+def _mask_control_split_tokens(text: str, mask_fn) -> str:"""Mask tokens whose body is split by control/zero-width characters.@@ -906,6 +915,10 @@ def redact_sensitive_text(text: str, *, force: bool = False, code_file: bool = Ftext = _mask_control_split_tokens(text, _prefix_sub)text = _PREFIX_RE.sub(lambda m: _prefix_sub(m.group(1)), text)+ if "." in text:+ _zhipu_sub = _mask_token_nonreusable if file_read else _mask_token+ text = _ZHIPU_API_KEY_RE.sub(lambda m: _zhipu_sub(m.group(1)), text)+if not code_file:text = _redact_assignments(text, mask_nonreusable=file_read)diff --git a/tests/agent/test_redact.py b/tests/agent/test_redact.pyindex a5f10ca87dc..a8bd6681749 100644--- a/tests/agent/test_redact.py+++ b/tests/agent/test_redact.py@@ -19,6 +19,26 @@ def _ensure_redaction_enabled(monkeypatch):class TestKnownPrefixes:+ def test_dotted_sk_and_zhipu_keys_are_fully_redacted(self):+ """Provider keys with internal dots must not leave a cleartext tail (#113901)."""+ dotted_sk = "sk-sp-" + "ABCDEFGH1234567890" + "." + "abcdefgh1234567890_XYZ-0987654321"+ zhipu = "50aaedFAKE1234567890abcdef1234567890abc" + "." + "ZpSh99"++ result = redact_sensitive_text(f"dotted={dotted_sk} zhipu={zhipu}")++ assert dotted_sk not in result+ assert "abcdefgh1234567890_XYZ" not in result+ assert zhipu not in result++ def test_zhipu_matcher_keeps_nearby_non_key_text(self):+ """The provider-specific id.secret shape must not become a generic dotted-token sweep."""+ text = "release=" + "a" * 31 + ".ZpSh99"++ assert redact_sensitive_text(text) == text++ def test_short_sk_token_remains_unredacted(self):+ assert redact_sensitive_text("sk-short") == "sk-short"+