diff --git a/src/loopzero/cli.py b/src/loopzero/cli.py index 46a93e29..e6ac0373 100644 --- a/src/loopzero/cli.py +++ b/src/loopzero/cli.py @@ -421,9 +421,8 @@ def _validation_section(wt: Path, head: str, config: Config) -> str: lines = [f"Recorded for `{recorded[:12]}`" + ("" if recorded == head else " (not current head)")] if _check_report_problem(loaded, wt, config, head, require_pass=False): lines[0] += " (unverified, run `loopzero check`)" - lines += [ - f"- `{r.command}`: exit {r.exit_code}" for r in results - ] or ["- (no check commands configured)"] + lines += [f"- `{github.redact_credentials(r.command)}`: exit {r.exit_code}" + for r in results] or ["- (no check commands configured)"] return "\n".join(lines) diff --git a/src/loopzero/github.py b/src/loopzero/github.py index e85f5300..be045663 100644 --- a/src/loopzero/github.py +++ b/src/loopzero/github.py @@ -197,6 +197,15 @@ def _api_path(args: tuple[str, ...]) -> str: return "/" + path.lstrip("/") +def redact_credentials(value: str) -> str: + value = re.sub(r"""(?i)(https?://)[^\s/?#'"<>`]+@""", r"\1[REDACTED]@", value) + value = re.sub(r"""(?i)(\bauthorization(?:\\?['"])?[ \t]*:[ \t]*)(\\?['"])(?:(?!\2)(?:\\.|[^\\\r\n]))+\2""", + r"\1\2[REDACTED]\2", value) + return re.sub(r"""(?i)((?P['"])?\bauthorization[ \t]*:[ \t]*)""" + r"""(?(quote)(?:\\.|(?!(?P=quote))[^\\\r\n])+|(?:[a-z][\w-]*[ \t]+)?(?:\\.|[^\\\s'"`])+)""", + r"\1[REDACTED]", value) + + def _redact_tokens(value: str, token: str = "") -> str: for secret in (token, os.environ.get("GH_TOKEN", ""), os.environ.get("GITHUB_TOKEN", "")): diff --git a/tests/test_cli.py b/tests/test_cli.py index d86327d1..4d4e76ae 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -952,6 +952,50 @@ def test_pr_pushes_and_creates_draft( assert "- `echo ok`: exit 0" in body and "- `test -f README.md`: exit 0" in body +@pytest.mark.parametrize("refresh", [False, True]) +def test_pr_validation_redacts_private_check_commands(wt, gh, capsys, tmp_path, refresh): + command = ( + "git ls-remote 'https://x-access-token:ghs_FAKE0123@example.invalid/repo.git' " + "&& curl -H 'Authorization: Bearer opaque-fake-header' https://example.invalid/ " + r"&& curl -H Authorization:\ Bot\ opaque-fake-header https://example.invalid/ " + """&& curl -d '{"Authorization": "Bearer opaque-fake-header"}' https://example.invalid/ """ + r'''&& curl -d "{\"Authorization\": \"Bearer opaque-fake-header\"}" https://example.invalid/ ''' + """&& python -c "request(headers={'Authorization': 'Basic opaque-fake-header'})" """ + """&& curl -H 'Authorization: Digest username="fake-user", response="opaque-fake-header"' """ + "https://example.invalid/" + ) + private = tmp_path / "private.toml" + private.write_text(WORKFLOW.replace(json.dumps("echo ok"), json.dumps(command))) + cfg = config.load(private) + head = head_of(wt) + results = (CheckResult(command, 7, 0.1, ""), CheckResult("test -f README.md", 0, 0.1, "")) + cli._save_report(wt, CheckReport(head, False, results, cli.sandbox.settings_fingerprint(cfg, wt))) + + if refresh: + git(wt, "push", "-q", "-u", "origin", "lz/t1") + gh.respond_pr_list([pr_json(headRefOid=head)]) + gh.respond(f"api repos/{REPO}/pulls/7 --method PATCH", {}) + cli._refresh_pr_checks(wt, cfg) + else: + gh.respond(branch_history_key(), []) + gh.respond_pr_list([]) + gh.respond_pr_create(pr_json(headRefOid=head)) + assert run(capsys, "--config", str(private), "pr")[0] == 0 + + body = json.loads(gh.calls[-1]["--input"])["body"] + assert "ghs_FAKE0123" not in body and "opaque-fake-header" not in body + assert "git ls-remote 'https://[REDACTED]@example.invalid/repo.git'" in body + assert "curl -H 'Authorization: [REDACTED]' https://example.invalid/" in body + assert "curl -H Authorization:[REDACTED] https://example.invalid/" in body + assert """curl -d '{"Authorization": "[REDACTED]"}' https://example.invalid/""" in body + assert r'''curl -d "{\"Authorization\": \"[REDACTED]\"}" https://example.invalid/''' in body + assert """python -c "request(headers={'Authorization': '[REDACTED]'})" """ in body + assert "'Authorization: [REDACTED]' https://example.invalid/`: exit 7" in body + assert f"Recorded for `{head[:12]}`" in body and "(unverified" not in body + assert "- `test -f README.md`: exit 0" in body + assert cli._load_report(wt).results == results + + @pytest.mark.parametrize("duplicate", [None, "CLOSED", "OPEN"]) def test_pr_refuses_completed_branch_before_any_publication( wt: Path, gh: FakeGh, fake_tool, tmp_path: Path, capsys, duplicate: str | None,