diff --git a/src/loopzero/github.py b/src/loopzero/github.py index e40611b9..deda6a22 100644 --- a/src/loopzero/github.py +++ b/src/loopzero/github.py @@ -126,8 +126,8 @@ class Readiness: _read_scope: ContextVar[tuple[float, str | None, bool] | None] = ContextVar("github_reads", default=None) _api_error_diagnostics: ContextVar[bool] = ContextVar("github_api_error_diagnostics", default=False) _HTTP_STATUS_RE = re.compile(r"^HTTP/\S+\s+(\d{3})(?:\s+(.*))?$", re.IGNORECASE) -_GITHUB_TOKEN_RE = re.compile( - r"\b(?:github_pat_[A-Za-z0-9_]+|gh[pousr]_[A-Za-z0-9_]{10,})\b" +_CREDENTIAL_TOKEN_RE = re.compile( + r"\b(?:github_pat_[A-Za-z0-9_]+\b|gh[pousr]_[A-Za-z0-9_]{10,}\b|sk-[A-Za-z0-9_-]{8,})" ) @@ -220,7 +220,7 @@ def _redact_tokens(value: str, token: str = "") -> str: value = re.sub(r"(?i)\b(authorization)\s*[:=]\s*" r"(?:(?:basic|bearer|token)\s+)?[^\s,;]+", r"\1=[REDACTED]", value) value = re.sub(r"(?i)\bBearer\s+[^\s,;]+", "Bearer [REDACTED]", value) - value = _GITHUB_TOKEN_RE.sub("[REDACTED]", value) + value = _CREDENTIAL_TOKEN_RE.sub("[REDACTED]", value) value = re.sub(r"(?i)\b(access_token|auth_token|token)\s*[:=]\s*" r"[^\s,;]+", r"\1=[REDACTED]", value) value = re.sub(r"(?i)(https?://[^?\s]+)\?[^\s]+", r"\1?[REDACTED]", value) @@ -780,7 +780,7 @@ def finding_marker(severity: str, head_sha: str, finding: Finding) -> str: def _comment_body(f: Finding, head_sha: str, prefix: str = "") -> str: - text = f"{prefix}**{f.severity}: {f.title}**\n\n{f.body}".rstrip() + text = prefix + _CREDENTIAL_TOKEN_RE.sub("[REDACTED]", f"**{f.severity}: {f.title}**\n\n{f.body}").rstrip() if f.severity in BLOCKING: return f"{finding_marker(f.severity, head_sha, f)}\n{text}" return text @@ -832,7 +832,7 @@ def _review_body(result: ReviewResult, unplaced: list[Finding], prefix: str = "" lines.extend(_review_settings_lines(result, include_configured=settings_layout == "current")) if unplaced: lines.append("\nFindings without a file location:\n") - lines += [f"- **{f.severity}**: {f.title} — {f.body}".rstrip(" —") for f in unplaced] + lines += [_CREDENTIAL_TOKEN_RE.sub("[REDACTED]", f"- **{f.severity}**: {f.title} — {f.body}".rstrip(" —")) for f in unplaced] return "\n".join(lines) + _review_transcript(result, legacy_display) diff --git a/tests/test_review_recovery.py b/tests/test_review_recovery.py index d6e0202a..80c11391 100644 --- a/tests/test_review_recovery.py +++ b/tests/test_review_recovery.py @@ -42,14 +42,16 @@ def record_claude_runs(fake_bin): def lost_reply(wt, gh, fake_bin, monkeypatch, *, kind, verdict, interrupted=False, - mutate=None, timeout=False): + mutate=None, timeout=False, findings=None): history = [] if kind == "delta": history = [rev(head_of(wt), "primary")] (wt / "feature.py").write_text("print('fixed')\n") git(wt, "commit", "-qam", "repair") - head = support.prepare_review(wt, gh, fake_bin, history=history, post_id=17, - payload=APPROVE if verdict == 0 else CHANGES) + payload = APPROVE if verdict == 0 else CHANGES + if findings is not None: + payload = {**payload, "findings": findings} + head = support.prepare_review(wt, gh, fake_bin, history=history, post_id=17, payload=payload) record_claude_runs(fake_bin) original = github._api @@ -249,6 +251,28 @@ def read(endpoint): assert len([c for c in gh.calls if "--input" in c]) == 1 +def test_repost_recovers_review_with_redacted_findings(wt, gh, fake_bin, monkeypatch, capsys): + title = "Rotate sk-ant-FAKE0123" + findings = [ + {"severity": "important", "path": "feature.py", "line": 1, + "title": title, "body": "Remove sk-proj-Fake_only-0123 from settings."}, + {"severity": "suggestion", "path": None, "line": None, + "title": "Replace ghp_FAKE0123456789", "body": "See sk-FakeToken0123 in the log."}, + ] + head = lost_reply(wt, gh, fake_bin, monkeypatch, kind="primary", verdict=5, + interrupted=True, findings=findings) + assert run(capsys, "review")[0] == 130 + payload = support.posted_review(gh) + assert "**suggestion**: Replace [REDACTED] — See [REDACTED] in the log." in payload["body"] + comment, = payload["comments"] + assert (comment["path"], comment["line"], comment["side"]) == ("feature.py", 1, "RIGHT") + stable_id = github.finding_id(head, "feature.py", 1, title) + assert comment["body"].splitlines()[0].endswith(f"severity=important head={head} id={stable_id} -->") + assert "**important: Rotate [REDACTED]**\n\nRemove [REDACTED] from settings." in comment["body"] + assert run(capsys, "review", "--repost")[0] == 5 + assert_single_publication(gh, fake_bin) + + def test_interrupted_explicit_repost_recovers_repost_body(wt, gh, fake_bin, monkeypatch, capsys): head = lost_reply(wt, gh, fake_bin, monkeypatch, kind="primary", verdict=5, interrupted=True) original = github._api