Skip to content

Commit 54c8ef3

Browse files
wesbillmanCarl
andauthored
fix(release): require exact-head approval for desktop tags (block#3973)
## Summary - require an exact-head trusted approval before desktop auto-tagging - remove rule-suite authorization that `GITHUB_TOKEN` cannot access - pin review pagination to `page=1` and test the deployed `gh` control flow ## Why The previous verifier unconditionally queried repository rule-suite endpoints with `github.token`. Those endpoints require Administration: read, which Actions `GITHUB_TOKEN` cannot receive. Its paginated list request also duplicated page one when no explicit page was supplied. This deliberately removes admin-bypass authorization rather than introducing a second credential during release recovery. Desktop release PRs must now have GitHub's overall `APPROVED` decision and a MEMBER/OWNER/COLLABORATOR approval attached to the exact candidate SHA. ## Validation - `scripts/test-desktop-release-authorization.sh` - `scripts/test-release-ref-contract.sh` - `bash -n scripts/verify-desktop-release-merge.sh scripts/verify-desktop-release-authorization.sh scripts/test-desktop-release-authorization.sh scripts/test-release-ref-contract.sh` - `git diff --check origin/main...HEAD` The new flow test uses a stub `gh` executable, asserts the exact `page=1` request, fails any rule-suite API call, and rejects stale-SHA, untrusted-author, changes-requested review, and non-approved aggregate-decision cases. Signed-off-by: Wes <[email protected]> Co-authored-by: Carl <c7ebe626f000404285d3686e1dc74cc07cc60a9754a150041ba132e14bd3e2ec@buzz.block.builderlab.xyz>
1 parent db7e84d commit 54c8ef3

7 files changed

Lines changed: 93 additions & 68 deletions

‎.github/workflows/auto-tag-on-release-pr-merge.yml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ jobs:
111111
PR_HEAD_REF: ${{ github.event.pull_request.head.ref }}
112112
PR_BASE_REF: ${{ github.event.pull_request.base.ref }}
113113
PR_HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }}
114-
MERGED_BY: ${{ github.event.pull_request.merged_by.login }}
115114
MERGE_SHA: ${{ github.event.pull_request.merge_commit_sha }}
116115
run: |
117116
VERSION="${VERSION#desktop-v}"

‎scripts/desktop-release-bypass-authorized.jq‎

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎scripts/fixtures/desktop-release-rule-suite-bypass.json‎

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
repo_root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
5+
tmp=$(mktemp -d)
6+
trap 'rm -rf "$tmp"' EXIT
7+
mkdir -p "$tmp/bin"
8+
cat >"$tmp/bin/gh" <<'GH'
9+
#!/usr/bin/env bash
10+
set -euo pipefail
11+
printf '%q ' "$@" >>"$GH_CALLS"
12+
printf '\n' >>"$GH_CALLS"
13+
14+
[[ "${1:-}" == api ]] || { echo "expected gh api" >&2; exit 91; }
15+
if [[ "${2:-}" == graphql ]]; then
16+
expected_query='query($owner:String!,$repo:String!,$number:Int!){repository(owner:$owner,name:$repo){pullRequest(number:$number){reviewDecision}}}'
17+
[[ "$#" -eq 12 && "$3" == -f && "$4" == "query=$expected_query" &&
18+
"$5" == -F && "$6" == owner=block &&
19+
"$7" == -F && "$8" == repo=buzz &&
20+
"$9" == -F && "${10}" == number=123 &&
21+
"${11}" == --jq && "${12}" == '.data.repository.pullRequest' ]] || {
22+
echo "GraphQL call does not match the deployed query contract" >&2; exit 92;
23+
}
24+
if [[ -n "${REVIEW_DECISION:-}" ]]; then printf '%s\n' "$REVIEW_DECISION"; else printf '%s\n' '{"reviewDecision":"APPROVED"}'; fi
25+
elif [[ "$#" -eq 4 && "$2" == --paginate && "$3" == --slurp && "$4" == "repos/block/buzz/pulls/123/reviews?per_page=100&page=1" ]]; then
26+
[[ "${GH_FAIL_REVIEWS:-false}" != true ]] || { echo "simulated reviews API failure" >&2; exit 94; }
27+
if [[ -n "${REVIEWS:-}" ]]; then printf '%s\n' "$REVIEWS"; else printf '%s\n' '[[],[{"state":"APPROVED","commit_id":"head","author_association":"MEMBER"}]]'; fi
28+
else
29+
echo "unexpected or malformed gh call: $*" >&2
30+
exit 95
31+
fi
32+
GH
33+
chmod +x "$tmp/bin/gh"
34+
35+
run_authorization() {
36+
(cd "$repo_root" && PATH="$tmp/bin:$PATH" GH_CALLS="$tmp/calls" GH_TOKEN=test \
37+
GITHUB_REPOSITORY=block/buzz PR_NUMBER=123 PR_HEAD_SHA=head \
38+
REVIEW_DECISION="${REVIEW_DECISION-}" REVIEWS="${REVIEWS-}" GH_FAIL_REVIEWS="${GH_FAIL_REVIEWS-false}" \
39+
scripts/verify-desktop-release-authorization.sh)
40+
}
41+
42+
: >"$tmp/calls"
43+
run_authorization
44+
! grep -Fq 'rule-suites' "$tmp/calls"
45+
46+
for invalid in \
47+
'[[{"state":"APPROVED","commit_id":"stale","author_association":"MEMBER"}]]' \
48+
'[[{"state":"APPROVED","commit_id":"head","author_association":"NONE"}]]' \
49+
'[[{"state":"CHANGES_REQUESTED","commit_id":"head","author_association":"MEMBER"}]]'; do
50+
: >"$tmp/calls"
51+
if REVIEWS="$invalid" run_authorization >/dev/null 2>&1; then
52+
echo "invalid approval was accepted: $invalid" >&2
53+
exit 1
54+
fi
55+
done
56+
57+
: >"$tmp/calls"
58+
if REVIEW_DECISION='{"reviewDecision":"CHANGES_REQUESTED"}' run_authorization >/dev/null 2>&1; then
59+
echo "changes-requested review decision was accepted" >&2
60+
exit 1
61+
fi
62+
63+
: >"$tmp/calls"
64+
if GH_FAIL_REVIEWS=true run_authorization >/dev/null 2>&1; then
65+
echo "reviews API failure was ignored" >&2
66+
exit 1
67+
fi
68+
69+
echo "desktop release authorization passed"

‎scripts/test-release-ref-contract.sh‎

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,15 @@ grep -Fq 'git/refs' "$auto_tag"
6464
grep -Fq 'TAG_PREFIX="desktop-v"' "$auto_tag"
6565
grep -Fq 'target_sha=${{ github.event.pull_request.merge_commit_sha }}' "$auto_tag"
6666
grep -Fq 'scripts/verify-desktop-release-merge.sh' "$auto_tag"
67-
68-
bypass_filter="$repo_root/scripts/desktop-release-bypass-authorized.jq"
69-
bypass_fixture="$repo_root/scripts/fixtures/desktop-release-rule-suite-bypass.json"
70-
jq -e --argjson ruleset_id 13596885 -f "$bypass_filter" "$bypass_fixture" >/dev/null || {
71-
echo "real squash-bypass fixture was rejected" >&2
67+
"$repo_root/scripts/test-desktop-release-authorization.sh"
68+
if rg -q 'rule-suites|desktop-release-bypass-authorized|MERGED_BY' \
69+
"$repo_root/scripts/verify-desktop-release-merge.sh" \
70+
"$repo_root/scripts/verify-desktop-release-authorization.sh" \
71+
"$auto_tag"; then
72+
echo "desktop auto-tag still depends on unavailable rule-suite authorization" >&2
7273
exit 1
73-
}
74-
for mutation in \
75-
'.result = "pass"' \
76-
'(.rule_evaluations[] | select(.rule_type == "pull_request")).result = "pass"' \
77-
'(.rule_evaluations[] | select(.rule_type == "pull_request")).rule_source.id = 0' \
78-
'(.rule_evaluations[] | select(.rule_type == "pull_request")).enforcement = "evaluate"' \
79-
'del(.rule_evaluations[] | select(.rule_type == "pull_request"))'; do
80-
if jq "$mutation" "$bypass_fixture" | jq -e --argjson ruleset_id 13596885 -f "$bypass_filter" >/dev/null; then
81-
echo "bypass filter accepted invalid fixture mutation: $mutation" >&2
82-
exit 1
83-
fi
84-
done
74+
fi
75+
8576
review_filter="$repo_root/scripts/review-decision-approved.jq"
8677
for fixture in \
8778
'{"reviewDecision":"CHANGES_REQUESTED"}' \
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
: "${PR_HEAD_SHA:?}"
5+
: "${PR_NUMBER:?}"
6+
: "${GITHUB_REPOSITORY:?}"
7+
: "${GH_TOKEN:?}"
8+
9+
review="$(gh api graphql -f query='query($owner:String!,$repo:String!,$number:Int!){repository(owner:$owner,name:$repo){pullRequest(number:$number){reviewDecision}}}' -F owner="${GITHUB_REPOSITORY%/*}" -F repo="${GITHUB_REPOSITORY#*/}" -F number="$PR_NUMBER" --jq '.data.repository.pullRequest')"
10+
reviews="$(gh api --paginate --slurp "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/reviews?per_page=100&page=1")"
11+
valid_approvals="$(jq --arg sha "$PR_HEAD_SHA" '[.[][] | select(.state == "APPROVED" and .commit_id == $sha and (.author_association == "MEMBER" or .author_association == "OWNER" or .author_association == "COLLABORATOR"))] | length' <<<"$reviews")"
12+
if ! jq -e -f scripts/review-decision-approved.jq <<<"$review" >/dev/null || [[ "$valid_approvals" -eq 0 ]]; then
13+
echo "release lacks an exact-head approval" >&2
14+
exit 1
15+
fi

‎scripts/verify-desktop-release-merge.sh‎

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@ set -euo pipefail
33

44
: "${PR_HEAD_SHA:?}"
55
: "${MERGE_SHA:?}"
6-
: "${MERGED_BY:?}"
76
: "${VERSION:?}"
87
: "${PR_NUMBER:?}"
98
: "${GH_TOKEN:?}"
109

11-
# This ID is the release-authority policy anchor. A bypass of another ruleset
12-
# must never authorize a desktop release.
13-
readonly DEFAULT_RULESET_ID=13596885
1410
required_checks=(
1511
"Desktop E2E Integration"
1612
"Desktop"
@@ -47,41 +43,7 @@ git merge-base --is-ancestor "$MERGE_SHA" origin/main || { echo "squash commit i
4743
git checkout --detach "$PR_HEAD_SHA"
4844
scripts/desktop_release.py validate --candidate "$PR_HEAD_SHA" --version "$VERSION" --repo "$GITHUB_REPOSITORY"
4945

50-
review="$(gh api graphql -f query='query($owner:String!,$repo:String!,$number:Int!){repository(owner:$owner,name:$repo){pullRequest(number:$number){reviewDecision}}}' -F owner="${GITHUB_REPOSITORY%/*}" -F repo="${GITHUB_REPOSITORY#*/}" -F number="$PR_NUMBER" --jq '.data.repository.pullRequest')"
51-
reviews="$(gh api --paginate --slurp "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/reviews?per_page=100")"
52-
valid_approvals="$(jq --arg sha "$PR_HEAD_SHA" '[.[][] | select(.state == "APPROVED" and .commit_id == $sha and (.author_association == "MEMBER" or .author_association == "OWNER" or .author_association == "COLLABORATOR"))] | length' <<<"$reviews")"
53-
review_authorized=false
54-
if jq -e -f scripts/review-decision-approved.jq <<<"$review" >/dev/null && [[ "$valid_approvals" -gt 0 ]]; then
55-
review_authorized=true
56-
fi
57-
58-
# Rule suites are GitHub's durable record that a permitted bypass actor landed
59-
# this exact main update. The suite does not identify the matching bypass grant,
60-
# so the Default ruleset's bypass list is itself the release-authority policy.
61-
bypass_authorized=false
62-
for attempt in {1..5}; do
63-
suites="$(gh api --paginate --slurp "repos/$GITHUB_REPOSITORY/rulesets/rule-suites?ref=refs/heads/main&per_page=100")"
64-
mapfile -t suite_ids < <(jq -r --arg before "$base_sha" --arg after "$MERGE_SHA" --arg actor "$MERGED_BY" '
65-
.[][] | select(.ref == "refs/heads/main" and .before_sha == $before and .after_sha == $after and .actor_name == $actor and .result == "bypass") | .id
66-
' <<<"$suites")
67-
if [[ "${#suite_ids[@]}" -gt 1 ]]; then
68-
echo "multiple rule suites matched the release landing" >&2
69-
exit 1
70-
fi
71-
if [[ "${#suite_ids[@]}" -eq 1 ]]; then
72-
suite="$(gh api "repos/$GITHUB_REPOSITORY/rulesets/rule-suites/${suite_ids[0]}")"
73-
if jq -e --argjson ruleset_id "$DEFAULT_RULESET_ID" -f scripts/desktop-release-bypass-authorized.jq <<<"$suite" >/dev/null; then
74-
bypass_authorized=true
75-
fi
76-
break
77-
fi
78-
[[ "$attempt" -eq 5 ]] || sleep "$attempt"
79-
done
80-
81-
[[ "$review_authorized" == true || "$bypass_authorized" == true ]] || {
82-
echo "release lacks an exact-head approval or authorized Default-ruleset bypass" >&2
83-
exit 1
84-
}
46+
scripts/verify-desktop-release-authorization.sh
8547

8648
checks="$(gh api --paginate --slurp "repos/$GITHUB_REPOSITORY/commits/$PR_HEAD_SHA/check-runs?per_page=100")"
8749
for required in "${required_checks[@]}"; do

0 commit comments

Comments
 (0)