From dd366da77fd301b4ac2dfb97a101693d021f51e5 Mon Sep 17 00:00:00 2001 From: T <60372079+mpaulosky@users.noreply.github.com> Date: Mon, 26 Oct 2026 09:52:16 -0700 Subject: [PATCH] chore: Apply repo-ci-baseline Template Co-Authored-By: Claude Sonnet 5.5 --- .github/scripts/post_claude_review.py | 116 +++++- .github/scripts/release_post.py | 14 +- .../scripts/tests/test_post_claude_review.py | 332 ++++++++++++++++++ .github/scripts/tests/test_release_post.py | 55 +++ .github/workflows/claude-review.yml | 52 ++- 5 files changed, 554 insertions(+), 15 deletions(-) diff --git a/.github/scripts/post_claude_review.py b/.github/scripts/post_claude_review.py index 72254c8..ba3d212 100755 --- a/.github/scripts/post_claude_review.py +++ b/.github/scripts/post_claude_review.py @@ -37,6 +37,12 @@ marker is how PR Auto-Merge and the skill's landing decision tell it apart from anything else posted under that login. +A comment may carry one GitHub suggestion, a fenced block the reader applies to the commented +line with one click, and only a single line (vet_suggestions): any other is turned into a +plain text fence. So is every suggestion in a comment moved to the nearest diff line, which +would replace the wrong line, and in a body finding, where none can be applied. A body +finding is indented under its bullet so its paragraphs stay with it. + Standard library only, like release_post.py; the gh CLI does the rest. """ @@ -46,6 +52,7 @@ import re import subprocess import sys +import unicodedata import urllib.parse # Accepted limit (repo-ci-baseline's ADR 0004): the marker proves the login, @@ -72,6 +79,58 @@ ) REDACTED = "[redacted]" +# CREDENTIAL scans from every `eyJ`, and a long run of token characters (the first segment of a JWT +# holds the letters e, y and J) is rescanned from each one: quadratic on text a diff can steer. +# So a token, with the dots joining its segments, that holds a run longer than MAX_TOKEN_RUN is +# replaced whole before CREDENTIAL looks at the text: no legitimate review text has one, and it +# leaves nothing of a long JWT (its signature included) behind. What is left has runs of at most +# MAX_TOKEN_RUN characters, which CREDENTIAL scans in time linear in the text. +MAX_TOKEN_RUN = 128 +TOKEN_CHAIN = re.compile(r"[A-Za-z0-9_-]+(?:\.[A-Za-z0-9_-]+)*") + + +def redact_credentials(text): + """The text with credential-shaped text replaced, and how many were replaced.""" + count = 0 + + def long_chain(match): + nonlocal count + chain = match.group() + if any(len(run) > MAX_TOKEN_RUN for run in chain.split(".")): + count += 1 + return REDACTED + return chain + + text = TOKEN_CHAIN.sub(long_chain, text) + text, found = CREDENTIAL.subn(REDACTED, text) + return text, count + found + + +MAX_PATH = 1024 + + +def redact_path(path): + """A finding's path with credential-shaped text replaced, and how many were replaced. + + Not the long-run rule: a file name is one token, and a long one (a generated test's name, a + hash-named fixture) is no credential, while a path that no longer matches the diff leaves + its finding outside it. A path past MAX_PATH is no real path and gets the full rule. + """ + if len(path) > MAX_PATH: + return redact_credentials(path) + return CREDENTIAL.subn(REDACTED, path) + +# A fence whose info string starts with `suggestion`, anywhere in a body (after a list or quote +# marker, or a lone CR, too): GitHub shows its content as a change the reader can apply with +# one click to the line the comment sits on, and the legacy `suggestion:-1+1` form changes more. +# Any whitespace but a line break may sit between the fence and the info string. +# A match starts only at the start of a run of fence characters, so a long run costs a linear scan. +SUGGESTION_FENCE = re.compile(r"(?i)((? length): # Not a line of the file: Claude got the number wrong. nearest = min(lines, key=lambda n: (abs(n - line), n)) note = f"_Claude cited line {line}, which isn't a line of this file, so this is on the nearest line in the diff._" - comments.append({"path": path, "line": nearest, "side": "RIGHT", "body": f"{note}\n\n{body}"}) + # A suggestion would replace the nearest line, not the one Claude meant. + comments.append({"path": path, "line": nearest, "side": "RIGHT", "body": f"{note}\n\n{defuse_suggestions(body)}"}) else: - off_diff.append(f"- `{path}:{line}`: {body}") + # In the review body a suggestion can't be applied, and a body of several + # paragraphs stays under its bullet when indented. + indented = defuse_suggestions(body).replace("\n", "\n ") + off_diff.append(f"- `{path}:{line}`: {indented}") parts = ["**Claude Review**", summary.strip()] if not findings: diff --git a/.github/scripts/release_post.py b/.github/scripts/release_post.py index ddf65c2..5cef69d 100644 --- a/.github/scripts/release_post.py +++ b/.github/scripts/release_post.py @@ -61,7 +61,9 @@ def _api(self, path, jq=None): return subprocess.run(command, check=True, capture_output=True, text=True).stdout def _list(self, path, jq=".[] | @json"): - return [json.loads(line) for line in self._api(path, jq).splitlines() if line.strip()] + # Split on newlines only: jq's @json leaves U+2028 and U+2029 unescaped, and + # splitlines() would cut a record at either one. + return [json.loads(line) for line in self._api(path, jq).split("\n") if line.strip()] def pull(self, number): return json.loads(self._api(f"pulls/{number}")) @@ -98,7 +100,11 @@ def version_key(tag): def commit_subject(commit): message = (commit.get("commit") or {}).get("message") or "" - return message.splitlines()[0].strip() if message.strip() else "(no message)" + # Cut at Markdown's line endings (CRLF, CR, LF) and nothing else: a CR left in the + # subject would start a new Markdown line, while splitlines() would also cut a + # subject at a U+2028 or U+2029 that Markdown reads as text. + first = re.split(r"\r\n|\r|\n", message, maxsplit=1)[0].strip() + return first if message.strip() else "(no message)" # A commit subject that starts a list item this way, or after the quote or list @@ -1334,7 +1340,9 @@ def update_blog_index(blog_dir, merged_date, title_line, post_name): # Keep the post rows, skipping the header and its separator. Matching the # header's first cell exactly keeps posts whose title mentions "Date". rows = [] - for line in existing.splitlines(): + # Cut at Markdown's line endings only: splitlines() would cut a row at a U+2028 or + # U+2029 in a title, and neither half would be kept. + for line in re.split(r"\r\n|\r|\n", existing): line = line.strip() if not (line.startswith("|") and line.endswith("|")): continue diff --git a/.github/scripts/tests/test_post_claude_review.py b/.github/scripts/tests/test_post_claude_review.py index 33e7819..3534f57 100644 --- a/.github/scripts/tests/test_post_claude_review.py +++ b/.github/scripts/tests/test_post_claude_review.py @@ -6,6 +6,7 @@ import subprocess import sys import textwrap +import time import unicodedata from pathlib import Path @@ -139,6 +140,146 @@ def test_a_line_past_the_end_of_a_changed_file_goes_on_its_nearest_diff_line(cap assert "::warning::" not in capsys.readouterr().out +SUGGESTED = "**Wrong default**\n\nIt is 0.\n\n```suggestion\nvalue = 1\n```" + + +def test_a_suggestion_on_the_cited_line_is_kept(): + gh = run(findings_json(("src/app.py", 22, SUGGESTED))) + + _, review = gh.posted[0] + assert review["comments"][0]["body"] == SUGGESTED + + +def test_a_suggestion_on_the_nearest_line_is_turned_into_plain_text(): + # The suggestion would replace the nearest diff line, not the line Claude meant. + gh = run(findings_json(("src/app.py", 598, SUGGESTED))) + + _, review = gh.posted[0] + body = review["comments"][0]["body"] + assert "```suggestion" not in body + assert "```text\nvalue = 1\n```" in body + + +@pytest.mark.parametrize( + ("body", "expected"), + [ + ("```suggestion\nx\n```", "```text\nx\n```"), + ("````suggestion\nx\n````", "````text\nx\n````"), + ("~~~ suggestion\nx\n~~~", "~~~text\nx\n~~~"), + ("```SUGGESTION\nx\n```", "```text\nx\n```"), + (" ```suggestion\nx\n```", " ```text\nx\n```"), + ("a `suggestion` word and ```suggestions``` inline", "a `suggestion` word and ```suggestions``` inline"), + ("```python\nsuggestion = 1\n```", "```python\nsuggestion = 1\n```"), + ], +) +def test_defuse_suggestions_changes_only_suggestion_fences(body, expected): + assert pcr.defuse_suggestions(body) == expected + + +@pytest.mark.parametrize( + "body", + [ + "> ```suggestion\nx\n> ```", + "- ```suggestion\n x\n ```", + "a\r```suggestion\nx\n```", + " ```suggestion\nx\n```", + "```suggestion foo\nx\n```", + "```suggestion:-1+1\nx\ny\n```", + "````\n```suggestion\nx\n```\n````", + "```suggestion\r\nx\r\n```", + "```\x0bsuggestion\nx\n```", + "```\x0csuggestion\nx\n```", + ], +) +def test_defuse_suggestions_catches_every_form_a_fence_can_take(body): + assert not pcr.SUGGESTION_FENCE.search(pcr.defuse_suggestions(body)) + + +def test_a_suggestion_fence_after_a_quote_marker_is_defused_to_text(): + assert pcr.defuse_suggestions("> ```suggestion\nx") == "> ```text\nx" + + +GOOD = "**T**\n\nWhy.\n\n```suggestion\n value = 1\n```" + + +@pytest.mark.parametrize( + "body", + [ + "No suggestion here.", + GOOD, + GOOD.replace("```", "~~~"), + "```python\nsuggestion = 1\n```", + "````suggestion\n```bash\n````", # content that can't close a longer fence + "```suggestion\n\tvalue = 1 \n```", # a tab and trailing spaces are the line's own + "```suggestion\n ```\n```", # four spaces make it indented text, not a closing fence + ], +) +def test_vet_suggestions_keeps_no_suggestion_or_one_good_line(body): + assert pcr.vet_suggestions(body) == body + + +@pytest.mark.parametrize( + "body", + [ + "```suggestion\na\nb\n```", # two lines + "```suggestion\n\n```", # nothing + "```suggestion\n \n```", # blank + "```suggestion:-1+1\nx\n```", # the multi-line form + "```suggestion\nx\n```\n\n```suggestion\ny\n```", # two blocks + "```suggestion\ntoken = [redacted]\n```", # a click would write the redaction + "``` suggestion\nx\n```", # not exactly the info string + "- ```suggestion\n x\n ```", # not on its own lines + "```suggestion\nx\r\n```", # a lone CR in the block + "```suggestion\n```\n```", # the content closes the block: a click would delete the line + "~~~suggestion\n~~~\n~~~", + "```suggestion\n ```\n```", + "````suggestion\n`````\n````", + "```suggestion\nvalue = 1\n", # never closed + "```suggestion\nvalue = 1\n~~~", # closed by the other marker + "```suggestion\n\u200b\n```", # a zero-width space is not a visible line + "```suggestion\nvalue\u202e = 1\n```", # a bidirectional override + "```suggestion\nvalue\x00 = 1\n```", + "```suggestion\nvalue\u2028= 1\n```", + "```suggestion\n\u3164\n```", # a Hangul filler renders blank + "```suggestion\n \u2800 \n```", # a blank Braille cell + "```suggestion\n\u0301\n```", # a lone combining mark + "```suggestion\nx\n````", # a closing fence longer than the opener + "```suggestion\nx\n ```", # a closing fence indented + "```\x0bsuggestion\nx\ny\n```", + ], +) +def test_vet_suggestions_defuses_anything_but_one_good_line(body): + vetted = pcr.vet_suggestions(body) + + assert vetted != body + assert not pcr.SUGGESTION_FENCE.search(vetted) + + +def test_an_on_diff_comment_goes_through_vet_suggestions(): + body = "**T**\n\n```suggestion\na\nb\n```" + _, review = run(findings_json(("src/app.py", 22, body))).posted[0] + + assert "```suggestion" not in review["comments"][0]["body"] + assert "```text\na\nb\n```" in review["comments"][0]["body"] + + +def test_a_finding_off_the_diff_keeps_a_table_and_a_list_under_its_bullet(): + body = "**T**\n\n| a | b |\n| - | - |\n| 1 | 2 |\n\n- one\n- two" + _, review = run(findings_json(("src/app.py", 10, body))).posted[0] + + for row in (" | a | b |", " | - | - |", " | 1 | 2 |", " - one", " - two"): + assert row in review["body"].split("\n") + + +def test_a_finding_off_the_diff_keeps_its_paragraphs_under_its_bullet_and_loses_its_suggestion(): + body = "**Title**\n\nWhat goes wrong.\n\n```suggestion\nx = 1\n```" + _, review = run(findings_json(("src/app.py", 10, body))).posted[0] + + assert "- `src/app.py:10`: **Title**\n \n What goes wrong." in review["body"] + assert "```suggestion" not in review["body"] + assert " ```text\n x = 1\n ```" in review["body"] + + def test_a_real_line_outside_the_hunks_still_holds_the_merge(): _, review = run(findings_json(("src/app.py", 10, "Unchanged code."))).posted[0] @@ -881,3 +1022,194 @@ def test_header_names_pr_automerge_among_what_adds_review_claude(): adders = [s for s in sentences if re.search(r"\badds?\b", s) and "review:claude" in s] assert adders, "the header has no sentence about what adds review:claude" assert any("PR Auto-Merge" in s for s in adders), adders + + +def test_the_prompt_asks_for_a_structured_summary_and_titled_findings(): + prompt = " ".join(WORKFLOW.read_text().split()) + + for section in ("**Overview**", "**Changes**", "**Advisory (not blocking)**", "**Not reviewed**"): + assert section in prompt + assert "Start each finding's body with a bold one-line title" in prompt + # The posting script that vets suggestions reaches a repo with the re-Apply, but a PR's review + # runs the base's script: the prompt asks for suggestions only in a later change. + assert "suggestion" not in prompt.lower() + + +@pytest.mark.parametrize( + "body", + [ + "```suggestion\n" + "a" * 200_000, + "`" * 200_000, + "~" * 200_000, + "`" * 100_000 + " " * 100_000, + "~" * 100_000 + "\t" * 100_000, + "```" + " " * 200_000, + ], + ids=["long line", "backticks", "tildes", "backticks then spaces", "tildes then tabs", "fence then spaces"], +) +def test_suggestion_handling_takes_linear_time_on_long_input(body): + start = time.monotonic() + pcr.vet_suggestions(body) + pcr.defuse_suggestions(body) + + assert time.monotonic() - start < 2 + + +def workflow_credentials(): + """The workflow answer check's credential definitions, run on their own.""" + script = answer_check_script() + start = script.index("CREDENTIAL = re.compile(") + namespace = {"re": re} + exec(script[start:script.index("\n\n\ndef answer_in", start)], namespace) + return namespace + + +def a_jwt(first=12, second=12, third=12, prefix="eyJ"): + return f"{prefix}{'a' * first}.eyJ{'b' * second}.{'c' * third}" + + +GLUED = "ghp_" + "A" * 36 + "_" + a_jwt() + + +@pytest.mark.parametrize( + "text", + [ + a_jwt(), + "x-" + a_jwt(), + GLUED, + "ghs_123_" + a_jwt(), + a_jwt(second=200), # a payload past MAX_TOKEN_RUN: the signature goes too + a_jwt(first=200), + a_jwt(third=300), + "eyJ" * 70_000, + "(" + a_jwt() + ")", + ], + ids=["jwt", "prefixed", "glued to ghp", "installation token", "long payload", "long header", "long signature", + "run of eyJ", "in parentheses"], +) +def test_no_credential_shaped_text_survives_redaction(text): + for redact_text in (pcr.redact_credentials, workflow_credentials()["redact_credentials"]): + redacted, count = redact_text(f"see {text} end") + + assert count >= 1 + assert redacted.startswith("see ") and redacted.endswith(" end") + assert not pcr.CREDENTIAL.search(redacted) + assert "bbbbbbbb" not in redacted and "cccccccc" not in redacted + + +@pytest.mark.parametrize("text", ["ghp_" + "A" * 36 + a_jwt(), "sk-ant-" + "a" * 30 + "-" + a_jwt()], ids=["ghp then jwt", "sk-ant then jwt"]) +def test_a_jwt_run_straight_onto_a_token_is_redacted_as_the_original_pattern_did(text): + # The token's alternative takes the JWT's first segment too, as it always has, so the rest + # (two segments) is no longer a JWT; the redaction must still act and never match a JWT. + redacted, count = pcr.redact_credentials(text) + + assert count >= 1 + assert not pcr.CREDENTIAL.search(redacted) + assert pcr.CREDENTIAL.sub("[redacted]", text) == redacted + + +def test_a_long_jwts_signature_is_replaced_with_its_payload(): + redacted, _ = pcr.redact_credentials("t=" + a_jwt(second=400, third=50)) + + assert redacted == "t=[redacted]" + + +def test_ordinary_text_with_short_words_is_left_alone(): + text = "Fix snake_case_names, dotted.names.here and a 64-char hash " + "0" * 64 + " in src/app.py:12." + + assert pcr.redact_credentials(text) == (text, 0) + + +def test_the_script_and_the_workflow_redact_alike(): + rng = random.Random(299) + pieces = [a_jwt(), a_jwt(second=200), GLUED, "ghs_9_" + a_jwt(), "sk-ant-" + "q" * 25, "x-", "eyJ", ".", " ", "/", "a" * 130, "ok"] + redact_workflow = workflow_credentials()["redact_credentials"] + for _ in range(300): + text = "".join(rng.choice(pieces) for _ in range(rng.randint(1, 6))) + + assert pcr.redact_credentials(text) == redact_workflow(text) + + +def test_the_redaction_leaves_nothing_the_original_pattern_matches(): + rng = random.Random(299) + pieces = [a_jwt(), a_jwt(first=8, second=8, third=8), a_jwt(second=200), GLUED, "ghp_" + "Z" * 40, "ghs_9_" + a_jwt(), + "github_pat_" + "a" * 22 + "_" + "b" * 59, "sk-ant-" + "q" * 25, "x-", "_", "-", ".", " ", "eyJ", "/"] + matched = 0 + for _ in range(3000): + text = "".join(rng.choice(pieces) for _ in range(rng.randint(1, 7))) + matched += bool(pcr.CREDENTIAL.search(text)) + redacted, _ = pcr.redact_credentials(text) + + assert not pcr.CREDENTIAL.search(redacted), text + assert matched > 1500 + + +@pytest.mark.parametrize( + "text", + [ + "eyJ" * 70_000, + "ghp_1_eyJ" * 25_000, + "eyJ" * 40_000 + "." + "eyJ" * 40_000, + "x-" * 100_000 + "eyJ", + ("eyJ" * 42 + "a" + ".") * 1_500, # many runs just under MAX_TOKEN_RUN, each full of eyJ + ("ghp_1_eyJ" * 14 + ".") * 1_500, + "a." * 100_000, + ], + ids=["eyJ run", "ghp eyJ run", "two eyJ runs", "long prefix", "many short eyJ runs", "many ghp runs", "many dots"], +) +def test_redacting_takes_linear_time_on_long_input(text): + redact_workflow = workflow_credentials()["redact_credentials"] + start = time.monotonic() + pcr.redact_credentials(text) + redact_workflow(text) + + assert time.monotonic() - start < 3 + + +LONG_NAME = "tests/Get_Articles_When_" + "A" * 130 + "_Returns_401.cs" + + +def test_a_long_file_name_in_a_finding_path_is_left_alone(): + summary, findings, count = pcr.redact("s", [{"path": LONG_NAME, "line": 3, "body": "b"}]) + + assert findings[0]["path"] == LONG_NAME + assert count == 0 + + +def test_a_credential_in_a_finding_path_is_still_redacted(): + path = "src/" + a_jwt() + ".cs" + _, findings, count = pcr.redact("s", [{"path": path, "line": 3, "body": "b"}]) + + assert "bbbbbbbb" not in findings[0]["path"] + assert count >= 1 + + +def test_a_path_past_the_limit_gets_the_full_rule(): + path = "x/" + "eyJ" * 70_000 + start = time.monotonic() + _, findings, _ = pcr.redact("s", [{"path": path, "line": 3, "body": "b"}]) + + assert findings[0]["path"] == "x/[redacted]" + assert time.monotonic() - start < 3 + + +def test_the_workflow_redacts_paths_like_the_script(): + redact = workflow_credentials()["redact_path"] + + assert redact(LONG_NAME) == (LONG_NAME, 0) + assert redact("src/" + a_jwt()) == pcr.redact_path("src/" + a_jwt()) + assert redact("x/" + "eyJ" * 1000) == pcr.redact_path("x/" + "eyJ" * 1000) + + +def test_the_workflows_redact_leaves_a_long_file_name_in_a_path_field_alone(): + script = answer_check_script() + namespace = workflow_credentials() + exec(script[script.index("def redact(value, key=None):"):script.index("def write_outputs")], namespace) + answer = {"summary": "see " + a_jwt(), "findings": [{"path": LONG_NAME, "line": 3, "body": "see " + a_jwt()}]} + + redacted, count = namespace["redact"](answer) + + assert redacted["findings"][0]["path"] == LONG_NAME + assert "bbbbbbbb" not in redacted["summary"] and "bbbbbbbb" not in redacted["findings"][0]["body"] + assert count == 2 + diff --git a/.github/scripts/tests/test_release_post.py b/.github/scripts/tests/test_release_post.py index 188929b..f9f921a 100644 --- a/.github/scripts/tests/test_release_post.py +++ b/.github/scripts/tests/test_release_post.py @@ -1823,3 +1823,58 @@ def test_a_reference_definition_keeps_its_label_and_title_as_written(text, expec ) def test_an_autolink_the_sanitizer_escapes_is_scanned_as_text(body, expected): assert rp.sanitize_markdown(body) == expected + + +def test_list_reads_a_record_whose_text_holds_unicode_line_separators(monkeypatch): + # jq's @json leaves U+2028 and U+2029 unescaped, and str.splitlines() splits on both. + records = [{"filename": "a.md", "patch": "x\u2028y\u2029z"}, {"filename": "b.md", "patch": "plain"}] + output = "".join(json.dumps(record, ensure_ascii=False) + "\n" for record in records) + github = rp.GitHub(REPO) + monkeypatch.setattr(github, "_api", lambda path, jq=None: output) + + assert github.files(42) == records + + +@pytest.mark.parametrize( + ("message", "expected"), + [ + ("Fix a\u2028b\r\n\nBody line", "Fix a\u2028b"), + ("Fix a\u2029b\nBody", "Fix a\u2029b"), + ("Fix\r# Heading", "Fix"), # a lone CR is a Markdown line ending + ("Fix\rBody", "Fix"), + ("Fix\r```\rmore", "Fix"), + ("Fix\r[x]: y", "Fix"), + ], +) +def test_commit_subject_ends_at_markdowns_line_endings_and_keeps_other_separators(message, expected): + assert rp.commit_subject({"commit": {"message": message}}) == expected + + +@pytest.mark.parametrize("separator", ["\u2028", "\u2029"]) +def test_a_subject_with_a_unicode_separator_renders_as_one_list_item(separator): + commits = [{"sha": "abc1234def", "commit": {"message": f"Fix a{separator}b\n\nBody"}}] + + assert rp.render_commits(commits).split("\n")[2] == f"- Fix a{separator}b (`abc1234`)" + + +def test_a_subject_with_a_lone_cr_renders_only_the_part_before_it(): + commits = [{"sha": "abc1234def", "commit": {"message": "Fix\r# Heading"}}] + + assert rp.render_commits(commits).split("\n")[2] == "- Fix (`abc1234`)" + + +@pytest.mark.parametrize("separator", ["\u2028", "\u2029"]) +def test_blog_index_keeps_a_row_whose_title_holds_a_unicode_separator(tmp_path, separator): + blog_dir = make_repo(tmp_path) / "docs" / "blogs" + first, second = "2026-09-20-pr-7-fix-a.md", "2026-09-24-pr-8-feat-next.md" + for name in (first, second): + (blog_dir / name).write_text("post\n", encoding="utf-8") + row = f"| 2026-09-20 | [fix: A{separator}B]({first}) | release,automation |" + + rp.update_blog_index(blog_dir, "2026-09-20", f"fix: A{separator}B", first) + rp.update_blog_index(blog_dir, "2026-09-24", "feat: Next", second) + + index = (blog_dir / "README.md").read_text(encoding="utf-8") + assert sum(f"({first})" in line for line in index.split("\n")) == 1 + assert row in index.split("\n") + diff --git a/.github/workflows/claude-review.yml b/.github/workflows/claude-review.yml index 3c870cb..df55df2 100644 --- a/.github/workflows/claude-review.yml +++ b/.github/workflows/claude-review.yml @@ -1684,7 +1684,19 @@ jobs: file (not in the diff) and make sure it is the line the finding is about. Say what goes wrong, in which situation, and how to fix it. - Answer with a short summary of the change and your verdict, and your findings. + Answer with a summary and your findings. Write the summary in Markdown, as short + sections a reader can skim: + + - **Overview**: two or three sentences on what the PR changes, and your verdict. + - **Changes**: a table with the columns File and What changed, one row for each + file or group of related files. + - **Advisory (not blocking)**: a bullet list of what the instructions above send + there. Leave the section out when there is nothing to list. + - **Not reviewed**: the diff pieces and files you couldn't read, as above. Leave + it out when you read everything. + + Start each finding's body with a bold one-line title, then say what goes wrong and + in which situation, then how to fix it. claude_args: >- --model claude-sonnet-5-5 --effort high @@ -1730,7 +1742,7 @@ jobs: # can push a change to a PR's scripts but not to this file, which a # pull_request run takes from the PR's merge commit. It also means a PR # cut before this check gets the check its workflow expects. - # post_claude_review.py's CREDENTIAL must match the one here. + # post_claude_review.py's CREDENTIAL, MAX_TOKEN_RUN and redact_credentials must match the ones here. - name: Check Claude's answer for secrets id: answer if: steps.secret.outputs.enabled == 'true' && steps.claude.outputs.structured_output != '' @@ -1761,6 +1773,34 @@ jobs: ) LEAK_WINDOW = 16 + # Mirrors post_claude_review.py: a token holding a run longer than MAX_TOKEN_RUN is replaced + # whole before CREDENTIAL looks, so a long run of `eyJ` isn't rescanned from each one. + MAX_TOKEN_RUN = 128 + TOKEN_CHAIN = re.compile(r"[A-Za-z0-9_-]+(?:\.[A-Za-z0-9_-]+)*") + + + def redact_credentials(text): + count = 0 + + def long_chain(match): + nonlocal count + chain = match.group() + if any(len(run) > MAX_TOKEN_RUN for run in chain.split(".")): + count += 1 + return "[redacted]" + return chain + + text = TOKEN_CHAIN.sub(long_chain, text) + text, found = CREDENTIAL.subn("[redacted]", text) + return text, count + found + + + def redact_path(path): + # Mirrors post_claude_review.py: a file name is no credential however long it is. + if len(path) > 1024: + return redact_credentials(path) + return CREDENTIAL.subn("[redacted]", path) + def answer_in(path): """Claude's answer from the execution file, or None when there's none.""" @@ -1908,14 +1948,14 @@ jobs: for finding in answer["findings"]]} - def redact(value): + def redact(value, key=None): if isinstance(value, str): - return CREDENTIAL.subn("[redacted]", value) + return redact_path(value) if key == "path" else redact_credentials(value) if isinstance(value, list): - pairs = [redact(item) for item in value] + pairs = [redact(item, key) for item in value] return [item for item, _ in pairs], sum(n for _, n in pairs) if isinstance(value, dict): - pairs = {key: redact(item) for key, item in value.items()} + pairs = {name: redact(item, name) for name, item in value.items()} return {key: item for key, (item, _) in pairs.items()}, sum(n for _, n in pairs.values()) return value, 0