Skip to content

The agent that grows with you

fix(debug): scrub short, colon-bearing and double-encoded support keys

kshitijk4poorf0b01a0080e6
diff --git a/hermes_cli/debug_redaction.py b/hermes_cli/debug_redaction.py
index fe787afd20b..44559848bfa 100644
--- a/hermes_cli/debug_redaction.py
+++ b/hermes_cli/debug_redaction.py
@@ -29,12 +29,15 @@ _RAW_URL_PARAM_RE = re.compile(
r"(?P<sep>[?#&;])(?P<key>[A-Za-z0-9_.~+%\-]+)="
r"(?P<value>[^?#&;\s\"'<>]*)"
)
+# ``%25`` prefixes the double-encoded forms (``%253F``/``%2526``/``%253D``) that a
+# URL nested inside an already-encoded redirect value carries.
+_ENCODED_URL_SEP = r"%(?:25)?(?:3f|23|26|3b)"
_ENCODED_URL_PARAM_RE = re.compile(
- r"(?P<sep>%3[fF]|%23|%26|%3[bB])"
- r"(?P<key>(?:(?!%3[dD]|%3[fF]|%23|%26|%3[bB])[^\s\"'<>]){1,160}?)"
- r"(?P<eq>%3[dD]|=)"
+ rf"(?P<sep>{_ENCODED_URL_SEP})"
+ rf"(?P<key>(?:(?!%(?:25)?3d|{_ENCODED_URL_SEP})[^\s\"'<>]){{1,160}}?)"
+ r"(?P<eq>%(?:25)?3d|=)"
r"(?P<value>.*?)"
- r"(?=(?:%3[fF]|%26|%23|%3[bB])|[\s\"'<>]|$)",
+ rf"(?={_ENCODED_URL_SEP}|[\s\"'<>]|$)",
re.IGNORECASE,
)
@@ -51,13 +54,26 @@ _QUOTED_HEADER_RE = re.compile(
r"(?P<quote>['\"])(?P<value>.*?)(?P=quote)",
re.IGNORECASE,
)
+# A key may carry inner colons (``sk-abc:xyz``, ``user:pass``); a trailing colon
+# is prose punctuation (``see: docs``). A letters-only word (``docs``, ``Note``,
+# ``HTTP``) is prose rather than a key: keys mix case, digits or symbols. The
+# word test is case-sensitive even inside the IGNORECASE regexes below.
+_KEY_CHARS = r"A-Za-z0-9._~+/\-"
+_KEY = rf"[{_KEY_CHARS}]+(?::[{_KEY_CHARS}]+)*=*(?![{_KEY_CHARS}=]|:[{_KEY_CHARS}])"
+_NOT_WORD = rf"(?!(?-i:[A-Z]?[a-z]+|[A-Z]+)(?![{_KEY_CHARS}=]|:[{_KEY_CHARS}]))"
+# 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.
+_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"(?!(?:{_HEADER_NAME_PATTERN})\b\s*[:=])"
+)
_BARE_HEADER_RE = re.compile(
rf"(?P<prefix>\b(?:{_HEADER_NAME_PATTERN})\b\s*[:=]\s*)"
- # agent.redact masks the scheme of ``x-api-key: Basic <key>`` to ``***`` and
- # keeps the key, so ``***`` before a same-line token-shaped value (not prose
- # and not another secret header) is a scheme too, and the key is the value.
- r"(?P<value>(?>(?:(?:Bearer|Basic|Digest)\s+|\*\*\*[ \t]+(?=[A-Za-z0-9._~+/-]{8,}=*(?![A-Za-z0-9._~+/=-]))"
- rf"(?!(?:{_HEADER_NAME_PATTERN})\b\s*[:=]))?)(?!\[REDACTED\])[^\s,}}\]]+)",
+ rf"(?P<value>(?>(?:{_SCHEME})?)(?!\[REDACTED\])[^\s,}}\]]+)",
re.IGNORECASE,
)
@@ -91,11 +107,11 @@ _HEADER_ARGV_RE = re.compile(
)
_PLAIN_HEADER_ARG_RE = re.compile(
rf"(?P<prefix>--header\s+(?:{_HEADER_NAME_PATTERN})\s+)"
- r"(?>(?:(?:Bearer|Basic|Digest)\s+)?)(?!\[REDACTED\])(?P<value>[^\s,}\]]+)",
+ rf"(?>(?:{_SCHEME})?)(?!\[REDACTED\])(?P<value>[^\s,}}\]]+)",
re.IGNORECASE,
)
_SUPPORT_BEARER_RE = re.compile(
- r"(?P<prefix>\bBearer\s+)[A-Za-z0-9._~+/-]{8,}=*",
+ rf"(?P<prefix>\bBearer\s+|\bBasic\s+{_NOT_WORD})(?=[^\s]{{8}}){_KEY}",
re.IGNORECASE,
)
@@ -112,8 +128,9 @@ def _redact_url_params(text: str) -> str:
def _encoded_sub(match: re.Match[str]) -> str:
if _decoded_compact_name(match.group("key")) not in _SENSITIVE_URL_PARAM_NAMES:
return match.group(0)
- replacement = "%2A%2A%2A" if match.group("eq").lower() == "%3d" else "***"
- return f"{match.group('sep')}{match.group('key')}{match.group('eq')}{replacement}"
+ eq = match.group("eq")
+ star = f"{eq[:-2]}2A" if eq != "=" else "*" # %2A, or %252A when double-encoded
+ return f"{match.group('sep')}{match.group('key')}{eq}{star * 3}"
# Running until stable handles a raw URL nested inside another query value
# without recursive parsing. The pass cap keeps malformed input bounded.
diff --git a/tests/tui_gateway/test_diagnostics_share_nous.py b/tests/tui_gateway/test_diagnostics_share_nous.py
index 07d28d030bd..00260a42de4 100644
--- a/tests/tui_gateway/test_diagnostics_share_nous.py
+++ b/tests/tui_gateway/test_diagnostics_share_nous.py
@@ -193,6 +193,19 @@ 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"):
+ kept = real_redact(f"x-api-key: *** {prose}")
+ assert kept.endswith(f" {prose}") and real_redact(kept) == kept
+ # Short, colon-bearing and double-encoded keys are keys, not prose.
+ for leaky, key in (
+ ("x-api-key: Bearer abc1234", "abc1234"),
+ ("-H 'x-auth-token: *** s3cr3t'", "s3cr3t"),
+ ("--header x-api-key Basic short", "short"),
+ ("x-api-key: *** sk-abc:xyz123456", "xyz123456"),
+ ("Basic user:passw0rd", "passw0rd"),
+ ("?r=x%2526access_token%253Dtok123456789", "tok123456789"),
+ ):
+ assert key not in real_redact(leaky)
def test_share_nous_linkless_success_is_a_failure(monkeypatch):