Skip to content

The agent that grows with you

fix(debug): scrub any 8+ char key after a masked scheme, letters-only included

kshitijk4poorc50c77f0d43a
diff --git a/hermes_cli/debug_redaction.py b/hermes_cli/debug_redaction.py
index 06c9cbb74b3..96790b7b82b 100644
--- a/hermes_cli/debug_redaction.py
+++ b/hermes_cli/debug_redaction.py
@@ -66,12 +66,13 @@ _KEY = rf"[{_KEY_CHARS}]+(?::[{_KEY_CHARS}]+)*=*{_KEY_END}"
_NOT_WORD = rf"(?!(?-i:[A-Z]?[a-z]{{1,19}}|[A-Z]{{1,19}}){_KEY_END})"
# agent.redact masks the scheme of ``x-api-key: Basic <key>`` to ``***`` and
# keeps the key, so ``***`` is a scheme too and the key is the value when it
-# is key-shaped (not a word; 4+ chars before any colon, or 6+ mixing letters
-# and digits) or a short word ending the header
-# (``Basic short``). Prose after a masked value stays.
+# is 8+ key chars (any case, letters-only included: fail closed), key-shaped
+# (not a word; 4+ chars before any colon, or 6+ mixing letters and digits) or
+# a short word ending the header (``Basic short``). Shorter prose after a
+# masked value stays.
_SCHEME = (
r"(?:Bearer|Basic|Digest)\s+|\*\*\*[ \t]+"
- rf"(?={_NOT_WORD}(?=[^\s:]{{4}}|(?=\S*[A-Za-z])(?=\S*[0-9])\S{{6}}){_KEY}|[A-Za-z]{{4,7}}[ \t]*(?:[\r\n'\",;}}\]]|$))"
+ rf"(?=[{_KEY_CHARS}]{{8,}}=*(?![{_KEY_CHARS}=])|{_NOT_WORD}(?=[^\s:]{{4}}|(?=\S*[A-Za-z])(?=\S*[0-9])\S{{6}}){_KEY}|[A-Za-z]{{4,7}}[ \t]*(?:[\r\n'\",;}}\]]|$))"
rf"(?!(?:{_HEADER_NAME_PATTERN})\b\s*[:=])"
)
_BARE_HEADER_RE = re.compile(
diff --git a/tests/tui_gateway/test_diagnostics_share_nous.py b/tests/tui_gateway/test_diagnostics_share_nous.py
index 52d183dd8b5..2cd4d7435b5 100644
--- a/tests/tui_gateway/test_diagnostics_share_nous.py
+++ b/tests/tui_gateway/test_diagnostics_share_nous.py
@@ -193,7 +193,7 @@ def test_redacted_support_egress_scrubs_structured_values_and_errors(
# Prose after an already-masked value is not a key: kept, and a fixed point.
benign = real_redact("x-api-key: *** see docs")
assert benign.endswith(" see docs") and real_redact(benign) == benign
- for prose in ("see: documentation", "time: 12:30:45", "configured via env", "undefined"):
+ for prose in ("see: documentation", "time: 12:30:45"):
kept = real_redact(f"x-api-key: *** {prose}")
assert kept.endswith(f" {prose}") and real_redact(kept) == kept
# An RFC 6750 challenge is auth-params, not a token.
@@ -202,6 +202,7 @@ def test_redacted_support_egress_scrubs_structured_values_and_errors(
for leaky, key in (
("x-api-key: Bearer abc1234", "abc1234"),
("-H 'x-auth-token: *** s3cr3t'", "s3cr3t"),
+ ("x-api-key: *** qwertyuiopas", "qwertyuiopas"),
("--header x-api-key *** abc1234", "abc1234"),
("x-api-key: *** " + "q" * 32, "q" * 32),
("--header x-api-key *** " + "Q" * 32, "Q" * 32),