Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(redaction): scrub env-style credential names and URL userinfo

tdc80f7cd45b6
diff --git a/agent/src/tools/redaction.py b/agent/src/tools/redaction.py
index 528f350f0..303f5831a 100644
--- a/agent/src/tools/redaction.py
+++ b/agent/src/tools/redaction.py
@@ -427,7 +427,7 @@ _TEXT_VALUE = r"\"[^\"\n]*\"|'[^'\n]*'|[^\s,;{}\[\]()\"']+"
#: Every quantifier applies to a bounded character class, with no nesting and
#: no overlapping alternatives, so matching stays linear (no ReDoS surface).
_TEXT_KV_PATTERN = re.compile(
- r"(?P<q>[\"']?)\b(?P<name>" + _TEXT_CREDENTIAL_KEYS + r")\b(?P=q)"
+ r"(?P<q>[\"']?)(?P<name>" + _TEXT_CREDENTIAL_KEYS + r")\b(?P=q)"
r"(?P<sep>\s*[:=]\s*)"
r"(?![\"']?" + re.escape(_REDACTED) + r"[\"']?|bearer\b)"
r"(?P<value>" + _TEXT_VALUE + r")",
@@ -442,6 +442,11 @@ _TEXT_BEARER_PATTERN = re.compile(
re.IGNORECASE,
)
+#: URL userinfo (``scheme://user:password@host``). It is a credential with no
+#: key label, so the key-based pattern never reaches it. The class stops at the
+#: first ``@`` after ``://``, so an ``@`` later in a path is left alone.
+_TEXT_URL_USERINFO_PATTERN = re.compile(r"(?<=://)[^\s/?#@]+@")
+
def _sub_credential_pair(match: re.Match[str]) -> str:
"""Rebuild one ``key=value`` match with the value redacted.
@@ -506,6 +511,7 @@ def redact_text(text: object) -> str:
if not s:
return s
s = _TEXT_BEARER_PATTERN.sub(r"\g<scheme> " + _REDACTED, s)
+ s = _TEXT_URL_USERINFO_PATTERN.sub(_REDACTED + "@", s)
s = _TEXT_KV_PATTERN.sub(_sub_credential_pair, s)
# Bare tokens with no key label in front of them.
return _TEXT_TOKEN_PATTERN.sub(_REDACTED, s)
diff --git a/agent/tests/test_redaction.py b/agent/tests/test_redaction.py
index fd222203b..99960ad56 100644
--- a/agent/tests/test_redaction.py
+++ b/agent/tests/test_redaction.py
@@ -150,9 +150,34 @@ _CREDENTIAL_SHAPES = (
("authorization=raw-token-value", "authorization=[redacted]"),
("Authorization: Bearer eyJhbGciOi.J9-_x==", "Authorization: Bearer [redacted]"),
("bearer abc123", "bearer [redacted]"),
+ # Env-style names: the label is a suffix of a longer identifier, so the
+ # leading word boundary used to reject it (``_`` is a word character).
+ ("TUSHARE_TOKEN=abc123", "TUSHARE_TOKEN=[redacted]"),
+ ("export OPENAI_API_KEY=abc123", "export OPENAI_API_KEY=[redacted]"),
+ ("MY_PASSWORD=hunter2", "MY_PASSWORD=[redacted]"),
+ ("DB_SECRET: abc123", "DB_SECRET: [redacted]"),
)
+#: URL userinfo (``scheme://user:password@host``) carries a credential with no
+#: key label, so the key-based pattern never sees it.
+_URL_USERINFO_SHAPES = (
+ (
+ "connect failed: https://user:[email protected]:8080/x",
+ "connect failed: https://[redacted]@proxy.local:8080/x",
+ ),
+ (
+ "GET http://bot:[email protected]/v1 returned 404",
+ "GET http://[redacted]@hooks.example.com/v1 returned 404",
+ ),
+)
+
+
[email protected](("raw", "expected"), _URL_USERINFO_SHAPES)
+def test_redact_text_scrubs_url_userinfo(raw: str, expected: str) -> None:
+ assert redact_text(raw) == expected
+
+
@pytest.mark.parametrize(("raw", "expected"), _CREDENTIAL_SHAPES)
def test_redact_text_scrubs_credential_shapes(raw: str, expected: str) -> None:
assert redact_text(raw) == expected
@@ -176,6 +201,8 @@ def test_redact_text_is_idempotent(raw: str) -> None:
"no separator after this token",
"tokens: 1204 in / 318 out", # plural = a count, not a credential
"api_keys: 3 configured",
+ "max_tokens=4096 total_input_tokens: 74812",
+ "tokenizer=bert-base version=2",
],
)
def test_redact_text_leaves_benign_output_readable(raw: str) -> None: