fix(tools): bound Gitea read-back to this write; verify approve/reject state (#865)
All checks were successful
ci/woodpecker/pr/ci Pipeline was successful
All checks were successful
ci/woodpecker/pr/ci Pipeline was successful
Review remediation for two correctness holes in the #865 fix: BLOCKER 1 — issue-comment.sh read-back was body-only across all history: if `tea comment` silently no-opped (the #865 bug) while an identically bodied comment already existed from a prior run, the read-back matched the OLD comment and falsely reported success. Now record the pre-write maximum comment id as a boundary and require a comment with id > boundary AND exact body match; monotonic Gitea ids make id > boundary mean "created by this write". Fails closed otherwise. BLOCKER 2 — pr-review.sh approve/reject trusted tea's exit code for the review STATE (same never-trust-exit-zero defect class as #865). Removed the TODO deferral and added a real bounded read-back: record the max review id before `tea pr approve`/`reject`, then require a review with id > boundary, the expected state (APPROVED / REQUEST_CHANGES), and commit_id equal to the PR's current head. Fails closed if absent. Tests: extended test-pr-review-gitea-comment.sh to model and assert the new review-state read-back (guardrails preserved, assertions added). Added test-issue-comment-readback.sh proving the pre-existing-identical-body false positive now fails closed and a genuinely new comment verifies. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -192,6 +192,171 @@ PY
|
||||
return 0
|
||||
}
|
||||
|
||||
# Resolve and cache the Gitea REST endpoint + token for the current remote.
|
||||
# Populates GITEA_API_BASE and GITEA_API_TOKEN. Returns non-zero (with a
|
||||
# clear stderr message) on any resolution failure.
|
||||
gitea_resolve_api() {
|
||||
local host configured_url repo
|
||||
|
||||
host=$(get_remote_host)
|
||||
GITEA_API_TOKEN=$(get_gitea_token "$host") || {
|
||||
echo "Error: Gitea token not found for review read-back verification" >&2
|
||||
return 1
|
||||
}
|
||||
configured_url=$(get_gitea_url_for_host "$host") || {
|
||||
echo "Error: Configured Gitea URL not found for review read-back verification" >&2
|
||||
return 1
|
||||
}
|
||||
repo=$(get_gitea_repo_slug_for_url "$configured_url") || {
|
||||
echo "Error: Could not resolve Gitea owner/repository relative to configured URL" >&2
|
||||
return 1
|
||||
}
|
||||
GITEA_API_BASE="${configured_url%/}/api/v1/repos/$repo"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Print the maximum existing review id on a PR (0 if none). This is the
|
||||
# pre-write BOUNDARY: Gitea pull-review ids are monotonic, so any review
|
||||
# submitted by a subsequent `tea pr approve`/`reject` has an id strictly
|
||||
# greater than this value. Bounding the read-back this way is what turns the
|
||||
# check into a genuine write-verification rather than a match against any
|
||||
# historical review — the same never-trust-exit-zero discipline #865 requires
|
||||
# for comments, applied to the review STATE itself.
|
||||
gitea_max_review_id() {
|
||||
local pr_number="$1" response_file status
|
||||
|
||||
response_file=$(mktemp "${TMPDIR:-/tmp}/mosaic-pr-review-boundary.XXXXXX")
|
||||
trap 'rm -f "$response_file"' RETURN
|
||||
|
||||
if ! status=$(curl -sS -o "$response_file" -w '%{http_code}' \
|
||||
-H "Authorization: token $GITEA_API_TOKEN" \
|
||||
"$GITEA_API_BASE/pulls/$pr_number/reviews"); then
|
||||
echo "Error: Gitea review boundary read transport failed" >&2
|
||||
return 1
|
||||
fi
|
||||
if [[ "$status" != "200" ]]; then
|
||||
echo "Error: Gitea review boundary read failed with HTTP $status" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
python3 - "$response_file" <<'PY'
|
||||
import json
|
||||
import sys
|
||||
|
||||
try:
|
||||
with open(sys.argv[1], encoding="utf-8") as response:
|
||||
reviews = json.load(response)
|
||||
if not isinstance(reviews, list):
|
||||
raise ValueError("response is not a review list")
|
||||
ids = [r.get("id") for r in reviews if isinstance(r, dict) and isinstance(r.get("id"), int)]
|
||||
print(max(ids) if ids else 0)
|
||||
except (OSError, json.JSONDecodeError, TypeError, ValueError) as error:
|
||||
print(f"Error: could not compute Gitea review boundary: {error}", file=sys.stderr)
|
||||
raise SystemExit(1)
|
||||
PY
|
||||
}
|
||||
|
||||
# Independently verify that `tea pr approve`/`reject` produced a durable review
|
||||
# record — never trust tea's exit code alone (#865, same defect class). Require
|
||||
# a review that was created by THIS action: its id must be strictly greater
|
||||
# than the pre-write boundary, its state must equal the expected state
|
||||
# (APPROVED / REQUEST_CHANGES), and it must have been submitted against the
|
||||
# PR's current head commit. Prints the matched review id on success; fails
|
||||
# closed (non-zero, clear stderr) if no such review is found.
|
||||
#
|
||||
# Args: $1 = PR number, $2 = expected state (APPROVED|REQUEST_CHANGES),
|
||||
# $3 = pre-write boundary review id.
|
||||
gitea_verify_review_submitted() {
|
||||
local pr_number="$1" expected_state="$2" boundary="$3"
|
||||
local pr_response_file reviews_response_file status head_sha review_id
|
||||
|
||||
pr_response_file=$(mktemp "${TMPDIR:-/tmp}/mosaic-pr-review-head.XXXXXX")
|
||||
reviews_response_file=$(mktemp "${TMPDIR:-/tmp}/mosaic-pr-review-state.XXXXXX")
|
||||
trap 'rm -f "$pr_response_file" "$reviews_response_file"' RETURN
|
||||
|
||||
# Resolve the PR's current head commit so the review can be pinned to it.
|
||||
if ! status=$(curl -sS -o "$pr_response_file" -w '%{http_code}' \
|
||||
-H "Authorization: token $GITEA_API_TOKEN" \
|
||||
"$GITEA_API_BASE/pulls/$pr_number"); then
|
||||
echo "Error: Gitea PR head read transport failed" >&2
|
||||
return 1
|
||||
fi
|
||||
if [[ "$status" != "200" ]]; then
|
||||
echo "Error: Gitea PR head read failed with HTTP $status" >&2
|
||||
return 1
|
||||
fi
|
||||
head_sha=$(python3 - "$pr_response_file" <<'PY'
|
||||
import json
|
||||
import sys
|
||||
|
||||
try:
|
||||
with open(sys.argv[1], encoding="utf-8") as response:
|
||||
pr = json.load(response)
|
||||
head_sha = pr.get("head", {}).get("sha") if isinstance(pr, dict) else None
|
||||
if not isinstance(head_sha, str) or not head_sha:
|
||||
raise ValueError("missing PR head sha")
|
||||
except (OSError, json.JSONDecodeError, AttributeError, TypeError, ValueError) as error:
|
||||
print(f"Error: could not resolve PR head commit: {error}", file=sys.stderr)
|
||||
raise SystemExit(1)
|
||||
print(head_sha)
|
||||
PY
|
||||
) || return 1
|
||||
|
||||
if ! status=$(curl -sS -o "$reviews_response_file" -w '%{http_code}' \
|
||||
-H "Authorization: token $GITEA_API_TOKEN" \
|
||||
"$GITEA_API_BASE/pulls/$pr_number/reviews"); then
|
||||
echo "Error: Gitea review read-back transport failed" >&2
|
||||
return 1
|
||||
fi
|
||||
if [[ "$status" != "200" ]]; then
|
||||
echo "Error: Gitea review read-back failed with HTTP $status" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
review_id=$(EXPECTED_STATE="$expected_state" BOUNDARY_REVIEW_ID="$boundary" EXPECTED_HEAD_SHA="$head_sha" \
|
||||
python3 - "$reviews_response_file" <<'PY'
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
try:
|
||||
with open(sys.argv[1], encoding="utf-8") as response:
|
||||
reviews = json.load(response)
|
||||
if not isinstance(reviews, list):
|
||||
raise ValueError("response is not a review list")
|
||||
expected_state = os.environ["EXPECTED_STATE"]
|
||||
boundary = int(os.environ["BOUNDARY_REVIEW_ID"])
|
||||
expected_head = os.environ["EXPECTED_HEAD_SHA"]
|
||||
# Require all of: created-after-boundary (this action's write), the
|
||||
# expected review state, and pinned to the PR's current head commit.
|
||||
# The monotonic id boundary is what proves "submitted by this action"
|
||||
# rather than matching some pre-existing historical review.
|
||||
matches = [
|
||||
r for r in reviews
|
||||
if isinstance(r, dict)
|
||||
and isinstance(r.get("id"), int)
|
||||
and r.get("id") > boundary
|
||||
and r.get("state") == expected_state
|
||||
and r.get("commit_id") == expected_head
|
||||
]
|
||||
if not matches:
|
||||
raise ValueError(
|
||||
f"no {expected_state} review created by this action found "
|
||||
"(id > boundary, expected state, current head); "
|
||||
"tea may have silently failed (#865 defect class)"
|
||||
)
|
||||
review_id = max(r["id"] for r in matches)
|
||||
except (OSError, json.JSONDecodeError, KeyError, TypeError, ValueError) as error:
|
||||
print(f"Error: Gitea review persistence verification failed: {error}", file=sys.stderr)
|
||||
raise SystemExit(1)
|
||||
print(review_id)
|
||||
PY
|
||||
) || return 1
|
||||
|
||||
echo "$review_id"
|
||||
return 0
|
||||
}
|
||||
|
||||
if [[ "$PLATFORM" == "github" ]]; then
|
||||
case $ACTION in
|
||||
approve)
|
||||
@@ -225,6 +390,12 @@ elif [[ "$PLATFORM" == "gitea" ]]; then
|
||||
repo=$(get_repo_slug)
|
||||
host=$(get_remote_host)
|
||||
login=$(get_gitea_login_for_host "$host")
|
||||
# Resolve the REST endpoint and record the pre-write review-id
|
||||
# boundary BEFORE the write, so the read-back can require a
|
||||
# strictly-newer review created by THIS action (never trust tea's
|
||||
# exit code alone — #865 defect class applies to the review state).
|
||||
gitea_resolve_api || exit 1
|
||||
review_boundary=$(gitea_max_review_id "$PR_NUMBER") || exit 1
|
||||
# tea v0.11.1 defines no --comment/-comment flag on `pr approve`;
|
||||
# route any review body via the durable comment API instead (#835).
|
||||
TEA_ARGS=(pr approve "$PR_NUMBER" --repo "$repo" --login "$login")
|
||||
@@ -235,14 +406,11 @@ elif [[ "$PLATFORM" == "gitea" ]]; then
|
||||
TEA_ARGS+=(--login "$LOGIN_OVERRIDE")
|
||||
fi
|
||||
tea "${TEA_ARGS[@]}"
|
||||
echo "Approved Gitea PR #$PR_NUMBER"
|
||||
# TODO(#865): this trusts tea's exit code for the approval STATE
|
||||
# itself (no read-back of the review's approved status via the
|
||||
# Gitea REST API). Only the optional accompanying COMMENT text
|
||||
# below is independently read-back verified. Add a review-state
|
||||
# read-back (e.g. GET /repos/{repo}/pulls/{pr}/reviews) if the
|
||||
# approval state itself needs the same durable-provenance
|
||||
# guarantee as comments.
|
||||
review_id=$(gitea_verify_review_submitted "$PR_NUMBER" "APPROVED" "$review_boundary") || {
|
||||
echo "Error: could not verify an APPROVED review landed on Gitea PR #$PR_NUMBER via bounded read-back; treating tea's exit code as untrustworthy (#865)." >&2
|
||||
exit 1
|
||||
}
|
||||
echo "Approved and verified Gitea PR #$PR_NUMBER (review ID $review_id)"
|
||||
if [[ -n "$COMMENT" ]]; then
|
||||
comment_id=$(gitea_post_verified_comment "$PR_NUMBER" "$COMMENT") || exit 1
|
||||
echo "Added and verified review comment on Gitea PR #$PR_NUMBER (comment ID $comment_id)"
|
||||
@@ -256,6 +424,10 @@ elif [[ "$PLATFORM" == "gitea" ]]; then
|
||||
repo=$(get_repo_slug)
|
||||
host=$(get_remote_host)
|
||||
login=$(get_gitea_login_for_host "$host")
|
||||
# Record the pre-write review-id boundary BEFORE the write (see the
|
||||
# approve path above for the rationale).
|
||||
gitea_resolve_api || exit 1
|
||||
review_boundary=$(gitea_max_review_id "$PR_NUMBER") || exit 1
|
||||
# tea v0.11.1 defines no --comment/-comment flag on `pr reject`;
|
||||
# route the review body via the durable comment API instead (#835).
|
||||
TEA_ARGS=(pr reject "$PR_NUMBER" --repo "$repo" --login "$login")
|
||||
@@ -266,11 +438,11 @@ elif [[ "$PLATFORM" == "gitea" ]]; then
|
||||
TEA_ARGS+=(--login "$LOGIN_OVERRIDE")
|
||||
fi
|
||||
tea "${TEA_ARGS[@]}"
|
||||
echo "Requested changes on Gitea PR #$PR_NUMBER"
|
||||
# TODO(#865): this trusts tea's exit code for the rejection STATE
|
||||
# itself (no read-back of the review's rejected/changes-requested
|
||||
# status via the Gitea REST API). Only the required accompanying
|
||||
# COMMENT text below is independently read-back verified.
|
||||
review_id=$(gitea_verify_review_submitted "$PR_NUMBER" "REQUEST_CHANGES" "$review_boundary") || {
|
||||
echo "Error: could not verify a REQUEST_CHANGES review landed on Gitea PR #$PR_NUMBER via bounded read-back; treating tea's exit code as untrustworthy (#865)." >&2
|
||||
exit 1
|
||||
}
|
||||
echo "Requested changes and verified on Gitea PR #$PR_NUMBER (review ID $review_id)"
|
||||
comment_id=$(gitea_post_verified_comment "$PR_NUMBER" "$COMMENT") || exit 1
|
||||
echo "Added and verified review comment on Gitea PR #$PR_NUMBER (comment ID $comment_id)"
|
||||
;;
|
||||
|
||||
Reference in New Issue
Block a user