From 36aac2b46a2206bc72d0657b6ed73dda4610904a Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 03:02:52 +0100 Subject: [PATCH] Redact credentials in reviewer authentication diagnostics Architecture-Review: unchanged; the runner keeps existing credential forwarding and authentication failure behavior and scrubs native diagnostic text. Co-Authored-By: Codex --- src/loopzero/runners.py | 16 +++++++++++++--- tests/test_cli.py | 15 +++++++++++++++ tests/test_runners.py | 6 ++++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/loopzero/runners.py b/src/loopzero/runners.py index 3499b866..74ecd5e8 100644 --- a/src/loopzero/runners.py +++ b/src/loopzero/runners.py @@ -176,6 +176,16 @@ def _tail(text: str) -> str: return text[-TAIL_CHARS:] +def _auth_failure(family: str, diagnostic: str) -> RunnerAuthFailed: + names = CLAUDE_AUTH_ENV if family == "claude" else CODEX_AUTH_ENV + env = _proc.build_env(names) + values = {variant for value in env.values() if value for variant in (value, json.dumps(value)[1:-1])} + patterns = [*map(re.escape, sorted(values, key=len, reverse=True)), r"\bsk-[A-Za-z0-9_-]{8,}", + r"\beyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+"] + diagnostic = re.sub("|".join(patterns), "[REDACTED]", diagnostic) + return RunnerAuthFailed(f"{family}: CLI is not authenticated\n{_tail(diagnostic)}") + + def _looks_like_auth_failure(text: str) -> bool: return any(p.search(text) for p in _AUTH_PATTERNS) @@ -281,7 +291,7 @@ def _run( with contextlib.suppress(json.JSONDecodeError, ValueError): envelope = _extract_json_object(done.stdout) if _claude_envelope_auth_failure(envelope) or _looks_like_auth_failure(combined): - raise RunnerAuthFailed(f"{family}: CLI is not authenticated\n{_tail(combined)}") + raise _auth_failure(family, combined) if _subscription_exhausted(combined, envelope): raise RunnerUsageLimit(f"{family}: usage limit reached", _tail(combined)) if _looks_too_long(combined): @@ -1149,13 +1159,13 @@ def _review_claude( if not isinstance(envelope, dict): raise RunnerBadOutput("claude: result envelope is not an object", _tail(raw)) if _claude_envelope_auth_failure(envelope): - raise RunnerAuthFailed(f"claude: CLI is not authenticated\n{_tail(raw)}") + raise _auth_failure("claude", raw) if envelope.get("is_error") and _subscription_exhausted(raw, envelope): raise RunnerUsageLimit("claude: usage limit reached", _tail(raw)) if envelope.get("is_error") or envelope.get("subtype", "success") != "success": text = str(envelope.get("result", "")) if _looks_like_auth_failure(text): - raise RunnerAuthFailed(f"claude: CLI is not authenticated\n{_tail(text)}") + raise _auth_failure("claude", text) if _looks_too_long(text) or _looks_too_long(raw): raise RunnerPromptTooLong("claude: prompt is too long", _tail(raw)) raise RunnerBadOutput(f"claude: {envelope.get('subtype', 'error')}", _tail(raw)) diff --git a/tests/test_cli.py b/tests/test_cli.py index 3cd7131b..f5659007 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -2041,6 +2041,21 @@ def test_review_observes_new_codex_version_in_saved_receipt( assert result.provenance["cli_version"] == version +def test_review_redacts_native_authentication_diagnostics(wt, gh, fake_tool, capsys, monkeypatch, head): + arm_empty_primary_review_history(gh, head) + for family, env in (("claude", "ANTHROPIC_API_KEY"), ("codex", "OPENAI_API_KEY")): + monkeypatch.setenv(env, f"opaque-fake-{family}-credential") + fake_tool(family, "if [ \"$1\" = --version ]; then echo 'codex-cli 0.161.0'; exit 0; fi\n" + f'printf "Not logged in with %s (server echoed sk-FakeToken0123)\\n" "${env}" >&2\nexit 1\n') + code, _, err = run(capsys, "review") + assert code == 4 + assert err.count("Not logged in with [REDACTED] (server echoed [REDACTED])") == 2 + assert "opaque-fake-" not in err and "sk-FakeToken0123" not in err + for family, remedy in (("claude", "claude auth login"), ("codex", "codex login")): + assert f"{family}: CLI is not authenticated" in err + assert f"Fix: run `{remedy}`." in err + + @pytest.mark.parametrize("mixed_authors", [False, True]) def test_review_fails_with_three_line_errors_and_remedies_without_spending_budget( wt: Path, gh: FakeGh, capsys, monkeypatch, head, mixed_authors diff --git a/tests/test_runners.py b/tests/test_runners.py index 991d10ab..2c243368 100644 --- a/tests/test_runners.py +++ b/tests/test_runners.py @@ -690,7 +690,7 @@ def test_claude_bad_severity(fake_bin: Path, tmp_path: Path) -> None: CLAUDE_EXPIRED_OAUTH = { "is_error": True, "api_error_status": 401, "terminal_reason": "api_error", - "result": "Failed to authenticate. API Error: 401 OAuth access token has expired. Re-authenticate to continue.", + "result": "Failed to authenticate. API Error: 401 OAuth access token has expired. Re-authenticate to continue. Token sk-ant-FAKE0123.", } @@ -706,8 +706,10 @@ def test_claude_bad_severity(fake_bin: Path, tmp_path: Path) -> None: ]) def test_claude_auth_failure(fake_bin, tmp_path, output, exit_code, diagnostic): _fake_claude(fake_bin, output, exit_code=exit_code) - with pytest.raises(RunnerAuthFailed, match=diagnostic): + with pytest.raises(RunnerAuthFailed, match=diagnostic) as failure: _review("claude", tmp_path) + if output is CLAUDE_EXPIRED_OAUTH: + assert "Token [REDACTED]." in str(failure.value) @pytest.mark.parametrize("exit_code", [0, 1])