Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(loaders): redact structured credentials and omit private paths

Haozhe Wu115cf06c92fa
diff --git a/agent/backtest/loader_health.py b/agent/backtest/loader_health.py
index dedfccafe..9d2fec148 100644
--- a/agent/backtest/loader_health.py
+++ b/agent/backtest/loader_health.py
@@ -60,10 +60,14 @@ MAX_EVIDENCE = 3
EVIDENCE_TEXT_LIMIT = 200
_URL_RE = re.compile(r"[a-zA-Z][a-zA-Z0-9+.\-]*://\S+")
-_PATH_RE = re.compile(r"(?:[A-Za-z]:\\[^\s]+)|(?:\\\\[^\s]+)|(?:/[\w.@+.-]+)+/?")
+# Filesystem paths may contain spaces: partial replacement can expose names.
+# Omit those warnings entirely; slashes inside market symbols remain sanitizable.
+_FILESYSTEM_PATH_RE = re.compile(r"(?<![A-Za-z0-9])(?:/|[A-Za-z]:[\\/]|\\\\)")
+_PATH_RE = re.compile(r"/[^\s]+")
_CREDENTIAL_RE = re.compile(
- r"\b[\w-]*(?:token|secret|password|passwd|api[-_]?key|private[-_]?key)[\w-]*\s*[=:]\s*\S+"
- r"|\bbearer\s+\S+"
+ r"[\"']?\b[\w-]*(?:token|secret|password|passwd|api[-_]?key|private[-_]?key)[\w-]*[\"']?"
+ r"\s*[=:]\s*(?:\"(?:\\.|[^\"])*(?:\"|$)|'(?:\\.|[^'])*(?:'|$)|\S+)"
+ r"|\b(?:bearer|basic)\s+\S+"
r"|\b(?:gh[pousr]_|github_pat_|sk-|xox[baprs]-)[\w-]{8,}",
re.IGNORECASE,
)
@@ -76,13 +80,17 @@ def sanitize_evidence(text: str) -> str | None:
text: A log message emitted by a loader.
Returns:
- The message with URLs, filesystem paths (single-segment included) and
- credential-shaped values replaced by placeholders and whitespace
- collapsed, or ``None`` when the result still carries a URL or path
- separator — an unclassifiable message is dropped rather than reported,
- because this artifact is public.
+ The message with URLs and credential-shaped values redacted and
+ whitespace collapsed, or ``None`` for filesystem paths or when the
+ result still carries an unclassified URL or path separator. An
+ unclassifiable message is dropped because this artifact is public.
"""
- cleaned = " ".join(_CREDENTIAL_RE.sub("<redacted>", _URL_RE.sub("<url>", text or "")).split())
+ cleaned = _CREDENTIAL_RE.sub("<redacted>", _URL_RE.sub("<url>", text or ""))
+ if _FILESYSTEM_PATH_RE.search(cleaned):
+ return None
+ if "://" in cleaned:
+ return None
+ cleaned = " ".join(cleaned.split())
cleaned = _PATH_RE.sub("<path>", cleaned)[:EVIDENCE_TEXT_LIMIT].strip()
if not cleaned or any(marker in cleaned for marker in ("://", "/", "\\")):
return None
diff --git a/agent/tests/test_loader_health.py b/agent/tests/test_loader_health.py
index 18f97befe..234375eb2 100644
--- a/agent/tests/test_loader_health.py
+++ b/agent/tests/test_loader_health.py
@@ -249,8 +249,12 @@ def test_loader_evidence_is_sanitized(monkeypatch):
_stub_loader(monkeypatch, "stooq", fetch=fetch)
result = health.probe("stooq", TODAY)
report = json.dumps(result)
- assert result["evidence"] == ["GET <url> failed for <path> with <redacted> from <path>"]
- assert "example.com" not in report and "abc123" not in report and "/home/" not in report
+ assert "evidence" not in result
+ assert (
+ "example.com" not in report
+ and "abc123" not in report
+ and "/home/" not in report
+ )
def test_a_loader_that_logs_then_raises_still_reports_its_reason(monkeypatch):
@@ -266,13 +270,15 @@ def test_a_loader_that_logs_then_raises_still_reports_its_reason(monkeypatch):
assert result["evidence"] == ["tencent returned an unparsable body"]
-def test_sanitizer_keeps_single_segment_paths_instead_of_dropping_the_message():
- """ccxt, mootdx and tushare warn with a lone slash; dropping the line loses the cause."""
+def test_sanitizer_keeps_market_slashes_but_omits_filesystem_paths():
+ """Market symbols remain useful; filesystem paths are omitted, never partially redacted."""
assert (
- health.sanitize_evidence("CCXT failed for BTC-USDT: binance does not have market symbol BTC/USDT")
+ health.sanitize_evidence(
+ "CCXT failed for BTC-USDT: binance does not have market symbol BTC/USDT"
+ )
== "CCXT failed for BTC-USDT: binance does not have market symbol BTC<path>"
)
- assert health.sanitize_evidence("cache file /tmp missing, refetching") == "cache file <path> missing, refetching"
+ assert health.sanitize_evidence("cache file /tmp missing, refetching") is None
assert health.sanitize_evidence("retry 2/3 failed") == "retry 2<path> failed"
@@ -364,3 +370,71 @@ def test_evidence_survives_the_parent_report(monkeypatch):
"attempts": 2,
"evidence": ["stooq is serving an anti-bot challenge page instead of CSV data"],
}
+
+
+ "warning",
+ [
+ '{"api_key": "synthetic-canary-credential"}',
+ "{'token': 'synthetic-canary-credential'}",
+ 'password="synthetic canary credential" rejected',
+ "password='synthetic canary credential' rejected",
+ "Authorization: Basic synthetic-canary-credential",
+ 'password="synthetic canary credential',
+ "password='synthetic canary credential",
+ '{"api_key": "synthetic canary credential',
+ "password=synthetic,canary-credential",
+ "password=synthetic;canary-credential",
+ "password=synthetic}canary-credential",
+ ],
+)
+def test_sanitizer_handles_structured_and_quoted_credentials(warning):
+ sanitized = health.sanitize_evidence(warning)
+ assert sanitized is not None
+ assert "synthetic" not in sanitized
+ assert "credential" not in sanitized
+ assert "<redacted>" in sanitized
+
+
+ "path",
+ [
+ "/Users/Example User/private-key.pem",
+ "/tmp/[private].pem",
+ '"/Users/Example User/config.json"',
+ r"C:\Users\Example User\config.json",
+ r"\\fileserver\private share\config.json",
+ "(/Users/Example User/private-key.pem)",
+ "path:/Users/Example User/private-key.pem",
+ "path[/Users/Example User/private-key.pem]",
+ "C:/Users/Example User/private-key.pem",
+ ],
+)
+def test_filesystem_warnings_are_omitted_in_full(path):
+ assert health.sanitize_evidence(f"cache file {path} missing") is None
+
+
+ "message",
+ [
+ '{"api_key": "synthetic-canary-credential"}',
+ 'password="synthetic canary credential',
+ "password=synthetic,canary-credential",
+ ],
+)
+def test_real_tencent_warning_path_redacts_a_structured_exception(monkeypatch, message):
+ from backtest.loaders.tencent_loader import DataLoader
+
+ def fetch_one(self, *args, **kwargs):
+ raise ValueError(message)
+
+ monkeypatch.setattr(DataLoader, "_fetch_one", fetch_one)
+ monkeypatch.setattr(DataLoader, "is_available", lambda self: True)
+ from backtest.loaders.registry import LOADER_REGISTRY
+
+ monkeypatch.setitem(LOADER_REGISTRY, "tencent", DataLoader)
+ result = health.probe("tencent", TODAY)
+ assert result["status"] == "invalid"
+ assert result["attempts"] == 2
+ assert "synthetic" not in json.dumps(result)
+ assert "<redacted>" in result["evidence"][0]