Skip to content

The agent that grows with you

fix(debug): scrub the key after a masked scheme on API-key headers

kshitijk4poor5381678d033e
diff --git a/hermes_cli/debug_redaction.py b/hermes_cli/debug_redaction.py
index a7c984514c5..99459d1db95 100644
--- a/hermes_cli/debug_redaction.py
+++ b/hermes_cli/debug_redaction.py
@@ -53,7 +53,11 @@ _QUOTED_HEADER_RE = re.compile(
)
_BARE_HEADER_RE = re.compile(
rf"(?P<prefix>\b(?:{_HEADER_NAME_PATTERN})\b\s*[:=]\s*)"
- r"(?P<value>(?>(?:(?:Bearer|Basic|Digest)\s+)?)(?!\[REDACTED\])[^\s,}\]]+)",
+ # agent.redact masks the scheme of ``x-api-key: Basic <key>`` to ``***`` and
+ # keeps the key, so ``***`` before a same-line value (not another secret
+ # header) is a scheme too, and the key after it is the value.
+ r"(?P<value>(?>(?:(?:Bearer|Basic|Digest)\s+|\*\*\*[ \t]+(?=[^\s,}\]])"
+ rf"(?!(?:{_HEADER_NAME_PATTERN})\b\s*[:=]))?)(?!\[REDACTED\])[^\s,}}\]]+)",
re.IGNORECASE,
)
diff --git a/tests/tui_gateway/test_diagnostics_share_nous.py b/tests/tui_gateway/test_diagnostics_share_nous.py
index c7399af8ab4..d508b2a0d66 100644
--- a/tests/tui_gateway/test_diagnostics_share_nous.py
+++ b/tests/tui_gateway/test_diagnostics_share_nous.py
@@ -175,6 +175,7 @@ def test_redacted_support_egress_scrubs_structured_values_and_errors(
raise RuntimeError(
"PUT https://upload.invalid/?X-Amz-Security-Token="
f"{canaries['error']} headers={{'X-API-Key': '{canaries['header']}'}} "
+ f"x-api-key: Basic {canaries['header']} "
+ real_redact(f"cookie={canaries['error']}; --api-key {canaries['error']} "
f"--header X-API-Key {canaries['error']}")
+ " --header X-API-Key Basic [REDACTED]"
@@ -187,6 +188,7 @@ def test_redacted_support_egress_scrubs_structured_values_and_errors(
assert canaries["header"] not in failure["error"]
assert "[REDACTED]]" not in failure["error"]
assert failure["error"].endswith(" --header X-API-Key Basic [REDACTED]")
+ assert real_redact(failure["error"]) == failure["error"]
def test_share_nous_linkless_success_is_a_failure(monkeypatch):