From 568accb700cac5fe0a2f6c89c7c5495651181bca Mon Sep 17 00:00:00 2001 From: don-petry <36422719+don-petry@users.noreply.github.com> Date: Sun, 20 Sep 2026 20:39:15 +0000 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20implement=20issue=20#1776=20?= =?UTF-8?q?=E2=80=94=20Authorization=20preflight:=20fail=20loudly=20when?= =?UTF-8?q?=20a=20persona=20credential=20authenticates=20but=20cannot=20wr?= =?UTF-8?q?ite=20(split=20from=20#1734=20AC=20#5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/lint.yml | 1 + .github/workflows/persona-runner-reusable.yml | 41 ++++ scripts/lib/persona-authz-preflight.sh | 130 ++++++++++ scripts/persona-authz-preflight.sh | 104 ++++++++ tests/test_persona_authz_preflight.sh | 227 ++++++++++++++++++ 5 files changed, 503 insertions(+) create mode 100644 scripts/lib/persona-authz-preflight.sh create mode 100644 scripts/persona-authz-preflight.sh create mode 100644 tests/test_persona_authz_preflight.sh diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 2767ba39b..b9f6ac69e 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -171,6 +171,7 @@ jobs: tests/verify_persona_teams.bats \ tests/persona_runner.bats \ tests/resolve_persona_identity.bats \ + tests/test_persona_authz_preflight.sh \ tests/persona_reach_check.bats \ tests/test_dev_lead_concurrency_guard.bats \ tests/test_validate_workflow_schedules.bats \ diff --git a/.github/workflows/persona-runner-reusable.yml b/.github/workflows/persona-runner-reusable.yml index e43f2b0d5..70e8f9a3b 100644 --- a/.github/workflows/persona-runner-reusable.yml +++ b/.github/workflows/persona-runner-reusable.yml @@ -177,6 +177,47 @@ jobs: echo "credential=${credential}" >> "$GITHUB_OUTPUT" echo "resolved posting identity for '${PERSONA}': ${account} (${credential})" + - name: Authorization preflight — fail before paying for the engine + # #1776 (split from #1734 AC #5). The identity step above proved the + # credential is one we hold a PAT for and RESOLVED which account it acts + # as; it did NOT prove that account can WRITE. A credential can + # authenticate correctly and still lack authorization to comment — the + # exact break that reached `gh api .../comments`, returned 403, and + # discarded a paid advisory (#1734), and that silently dropped a + # pr-review approval on PR #1788. This step catches that class HERE, + # before the engine call below, so paid model work is never produced and + # then lost (AC #3, option a — the strongest position). + # + # The probe is NON-DESTRUCTIVE (AC #2): a single `GET /repos/{owner}/{repo}` + # read of `.permissions` for the posting identity — no throwaway content is + # ever written. It fails CLOSED only on a definite negative (readable + # surface, no write bit) and fails OPEN (warn + proceed) on any + # indeterminate result — a 5xx, rate limit, or unreadable surface must not + # gate a legitimate advisory (AC #4). Missing/invalid secret is NOT handled + # here — it takes the existing early-exit path (the wrapper skips when no + # PAT is present; the post step fails closed on it). The credential is + # derived per-persona from the manifest (via the identity step), so every + # persona is covered without an enumerated list (AC #6). + id: authz_preflight + if: steps.resolve.outputs.skip == 'false' + env: + # Same manifest-driven PAT selection as the post step — the token that + # WILL post is the token we probe. The agent step never sees these. + BOT_PAT: ${{ secrets.DON_PETRY_BOT_GH_PAT }} + OWNER_PAT: ${{ secrets.GH_PAT_DON_PETRY }} + POSTING_ACCOUNT: ${{ steps.identity.outputs.account }} + POSTING_CREDENTIAL: ${{ steps.identity.outputs.credential }} + SOURCE_REPO: ${{ inputs.source_repo }} + run: | + set -euo pipefail + if [ "$POSTING_CREDENTIAL" = "DON_PETRY_BOT_GH_PAT" ]; then + GH_TOKEN="${BOT_PAT}" + else + GH_TOKEN="${OWNER_PAT}" + fi + export GH_TOKEN + bash scripts/persona-authz-preflight.sh + - name: Install Claude Code if: steps.resolve.outputs.skip == 'false' env: diff --git a/scripts/lib/persona-authz-preflight.sh b/scripts/lib/persona-authz-preflight.sh new file mode 100644 index 000000000..977d93225 --- /dev/null +++ b/scripts/lib/persona-authz-preflight.sh @@ -0,0 +1,130 @@ +# shellcheck shell=bash +# scripts/lib/persona-authz-preflight.sh — persona authorization-preflight +# DECISION layer (#1776, split from #1734 AC #5). +# +# The gap this closes: a persona's declared posting credential can AUTHENTICATE +# correctly and still lack authorization to WRITE. Every prior check verifies +# authentication and naming (resolve-persona-identity.sh, verify-persona-identity.sh, +# the runner's wrong-identity guard); none verified authorization. That let a +# valid-but-unauthorized token reach `gh api .../comments` and return 403 only +# AFTER the paid engine call — the advisory was produced and then discarded +# (#1734, run 34183239687), and a pr-review approval silently never landed on +# PR #1788 while the surrounding code treated it as success. +# +# These are PURE functions: given the already-gathered facts of a non-destructive +# capability probe (`GET /repos/{owner}/{repo}` reading `.permissions` for the +# authenticated identity), decide one of three outcomes. They NEVER touch the +# network or write content — the gh probe and the ::error::/::warning:: side +# effects are the wrapper's job (scripts/persona-authz-preflight.sh). +# +# The three states are deliberately distinct (AC #1, #4): +# AUTHORIZED — the permission surface is readable AND a write bit is set: +# proceed silently. +# UNAUTHORIZED — the permission surface is readable AND no write bit is set: +# a DEFINITE negative. Fail loudly; the diagnostic names the +# account, the credential secret, and the missing capability. +# INDETERMINATE — the surface could not be read (5xx, rate limit, 401, network, +# or absent `.permissions`): WARN and proceed. Fail OPEN — a +# transient blip must never be reported as "cannot write" nor +# gate a legitimate advisory (AC #4). This is the delicate rule: +# a preflight that blocks advisories on a hiccup is worse than +# the bug it prevents. +# +# A missing/invalid secret is NOT this layer's concern: it is handled earlier +# (the wrapper skips the probe when no PAT is present; the post step already fails +# closed on it). This layer answers only "authenticated — can it write?". +# +# Detection scope — what the `.permissions` proxy can and cannot detect (AC #2): +# CAN: a credential whose account has NO repository write (push/maintain/admin +# are all false on a readable surface) — the #1734 / PR #1788 class, where +# write access to a private org repo is exactly what commenting requires. +# This is the authenticated-but-unauthorized break the issue targets. +# CANNOT: finer-grained gaps below the repo-write bit (e.g. a fine-grained PAT +# granted "Issues: read" but not "Issues: write", or org/branch +# restrictions) — those do not always surface in `.permissions`. Such a +# result reads as INDETERMINATE (unknown/unreadable) and fails OPEN, never +# as a false negative. The probe is deliberately conservative: it only +# gates on an unambiguous "no write" and treats everything else as +# proceed. It is a cheap, non-destructive early warning, not a proof of +# comment-write; the post step's own failure handling (#1775) remains the +# backstop for anything the proxy cannot see. + +# authz_write_capability +# Map the three write-implying booleans from a repo's `.permissions` object to a +# capability signal. Each argument is the string GitHub returns ("true"/"false") +# or empty when the object was absent/unreadable. +# yes — at least one of admin/maintain/push is "true" (can write/comment) +# no — the object was present (>=1 arg non-empty) and no write bit is set +# unknown — every argument is empty (no `.permissions` object to read) +# Only a literal "true" counts as a capability; any other value is not-write, so +# a garbled field can never be mistaken for a grant. +authz_write_capability() { + local admin="${1:-}" maintain="${2:-}" push="${3:-}" + if [ "$admin" = "true" ] || [ "$maintain" = "true" ] || [ "$push" = "true" ]; then + echo "yes" + elif [ -n "$admin" ] || [ -n "$maintain" ] || [ -n "$push" ]; then + echo "no" + else + echo "unknown" + fi +} + +# authz_preflight_decision +# The three-state classifier. is "yes" only when the +# `GET /repos/{owner}/{repo}` read itself succeeded; anything else (a 4xx/5xx, +# rate limit, network error, empty) means the surface was NOT readable. +# is the authz_write_capability output. +# probe_ok != "yes" -> INDETERMINATE (unreadable surface; fail open) +# capability == "yes" -> AUTHORIZED +# capability == "no" -> UNAUTHORIZED (the definite negative) +# capability == "unknown" -> INDETERMINATE (readable but no perms; fail open) +# Crucially, an unreadable surface is INDETERMINATE even if a stale capability +# argument says "no" — a read failure is never a definite negative (AC #4). +authz_preflight_decision() { + local probe_ok="${1:-}" capability="${2:-}" + if [ "$probe_ok" != "yes" ]; then + echo "INDETERMINATE" + return 0 + fi + case "$capability" in + yes) echo "AUTHORIZED" ;; + no) echo "UNAUTHORIZED" ;; + *) echo "INDETERMINATE" ;; + esac +} + +# authz_preflight_should_fail +# Exit 0 (the preflight SHOULD fail the run) ONLY for a definite negative. +# AUTHORIZED and INDETERMINATE both proceed — the fail-open contract (AC #4). +authz_preflight_should_fail() { + [ "${1:-}" = "UNAUTHORIZED" ] +} + +# authz_preflight_diagnostic +# Build the human-readable line for a decision. The UNAUTHORIZED message is the +# load-bearing one: it names the account, the credential SECRET, and the missing +# capability (repository write), and states explicitly that the token +# authenticated — so this is authorization, not a missing secret. A diagnostic +# that said only "permission denied" is exactly what made #1734 take a human to +# diagnose (AC #1). The wrapper prepends ::error:: / ::warning:: / notice. +authz_preflight_diagnostic() { + local decision="${1:-}" account="${2:-}" credential="${3:-}" repo="${4:-}" + case "$decision" in + AUTHORIZED) + printf "authorization preflight: '%s' (credential %s) can write to %s — proceeding" \ + "$account" "$credential" "$repo" + ;; + UNAUTHORIZED) + printf "authorization preflight FAILED: '%s' (credential %s) authenticated but CANNOT write to %s. Missing capability: repository write (push/maintain/admin) — needed to post the advisory comment. This is an authorization failure, not a missing or expired secret: the token is valid but the account lacks write permission, so the advisory would be produced at cost and then discarded (the #1734 / PR #1788 failure class). Grant %s write access to %s or fix the credential mapping." \ + "$account" "$credential" "$repo" "$account" "$repo" + ;; + INDETERMINATE) + printf "authorization preflight indeterminate for '%s' (credential %s) on %s: the permission surface could not be read (transient API error, rate limit, or absent permissions). Failing OPEN — proceeding without gating the advisory (a blip must not block a legitimate advisory)" \ + "$account" "$credential" "$repo" + ;; + *) + printf "authorization preflight: unknown decision '%s' for '%s' (credential %s) on %s" \ + "$decision" "$account" "$credential" "$repo" + ;; + esac +} diff --git a/scripts/persona-authz-preflight.sh b/scripts/persona-authz-preflight.sh new file mode 100644 index 000000000..a71333718 --- /dev/null +++ b/scripts/persona-authz-preflight.sh @@ -0,0 +1,104 @@ +#!/usr/bin/env bash +# persona-authz-preflight.sh — non-destructive authorization preflight for the +# persona runner (#1776, split from #1734 AC #5). +# +# Runs in persona-runner-reusable.yml AFTER the posting identity is resolved and +# BEFORE the paid engine call, so an authenticated-but-unauthorized credential is +# caught before the model is invoked and the advisory is produced and lost — the +# strongest position (AC #3, option a). CI-only placement cannot catch runtime +# revocation and, lacking the cross-repo posting PATs, cannot run a live probe at +# all, so the runtime preflight is required regardless. +# +# The probe is NON-DESTRUCTIVE (AC #2): it never creates, edits, or deletes real +# content. It issues a single capability query — `GET /repos/{owner}/{repo}` — and +# reads `.permissions` (admin/maintain/push) for the authenticated posting +# identity. No throwaway comment/issue/branch is written anywhere. +# +# The DECISION is pure and unit-tested in scripts/lib/persona-authz-preflight.sh. +# This wrapper is the network + side-effect layer: gather the probe facts, map +# them to a decision, and act — fail CLOSED (exit 1, ::error::) only on a definite +# negative; fail OPEN (exit 0, ::warning::) on any indeterminate result (AC #4). +# +# Env vars consumed: +# GH_TOKEN — the posting PAT already selected from the manifest +# credential by the calling step (never a repo-wide +# default). EMPTY means the missing/invalid-secret case, +# which is handled earlier — the preflight skips (see below). +# SOURCE_REPO — owner/repo of the item the advisory would be posted to +# POSTING_ACCOUNT — the login GH_TOKEN authenticates as (from the manifest) +# POSTING_CREDENTIAL — the secret name holding that PAT (from the manifest) +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +# shellcheck source=scripts/lib/persona-authz-preflight.sh +source "${SCRIPT_DIR}/lib/persona-authz-preflight.sh" + +SOURCE_REPO="${SOURCE_REPO:-}" +POSTING_ACCOUNT="${POSTING_ACCOUNT:-}" +POSTING_CREDENTIAL="${POSTING_CREDENTIAL:-}" + +# Missing/invalid secret is NOT this preflight's job (AC #1) — it is handled +# earlier: the post step fails closed on an unset PAT and on a wrong identity. +# Running the probe with no token would only produce a 401 we would (correctly) +# treat as indeterminate, so skip cleanly and let the existing path own it. +if [ -z "${GH_TOKEN:-}" ]; then + echo "::notice::authz-preflight: no PAT for credential '${POSTING_CREDENTIAL}' — deferring to the existing missing-secret handling (not this preflight's concern)" + exit 0 +fi + +if [ -z "$SOURCE_REPO" ]; then + echo "::notice::authz-preflight: no source repo to probe — skipping" + exit 0 +fi + +# --- Non-destructive capability probe -------------------------------------- +# A single read of the repo's permission surface for the authenticated identity. +errfile="$(mktemp)" +trap 'rm -f "$errfile"' EXIT +probe_ok="yes" +resp="" +if ! resp="$(gh api "repos/${SOURCE_REPO}" 2>"$errfile")"; then + probe_ok="no" # 4xx/5xx/rate-limit/network — an unreadable surface (fail open) +fi + +admin="" ; maintain="" ; push="" +if [ "$probe_ok" = "yes" ]; then + # `.permissions` may be absent for some token/endpoint combinations; default it + # to {} so a missing object yields empty fields (capability "unknown"), never a + # jq error. A jq failure (malformed JSON) also degrades to an unreadable surface. + if perms_tsv="$(printf '%s' "$resp" | jq -r ' + (.permissions // {}) as $p + | [$p.admin, $p.maintain, $p.push] + | map(if . == null then "" else tostring end) + | @tsv' 2>/dev/null)"; then + IFS=$'\t' read -r admin maintain push <<< "$perms_tsv" + else + probe_ok="no" + fi +fi + +capability="$(authz_write_capability "$admin" "$maintain" "$push")" +decision="$(authz_preflight_decision "$probe_ok" "$capability")" +diag="$(authz_preflight_diagnostic "$decision" "$POSTING_ACCOUNT" "$POSTING_CREDENTIAL" "$SOURCE_REPO")" + +case "$decision" in + AUTHORIZED) + echo "::notice::${diag}" + ;; + INDETERMINATE) + # Fail OPEN (AC #4): a transient/unreadable result must not gate the advisory. + if [ "$probe_ok" != "yes" ]; then + echo "::warning::authz-preflight: permission surface for ${SOURCE_REPO} was unreadable: $(tr '\n' ' ' < "$errfile")" + fi + echo "::warning::${diag}" + ;; + *) + # UNAUTHORIZED — the definite negative. Fail CLOSED, loudly (AC #1). + echo "::error::${diag}" + ;; +esac + +if authz_preflight_should_fail "$decision"; then + exit 1 +fi +exit 0 diff --git a/tests/test_persona_authz_preflight.sh b/tests/test_persona_authz_preflight.sh new file mode 100644 index 000000000..4a7106cf7 --- /dev/null +++ b/tests/test_persona_authz_preflight.sh @@ -0,0 +1,227 @@ +#!/usr/bin/env bats +# Unit tests for the persona authorization-preflight decision layer +# (scripts/lib/persona-authz-preflight.sh, issue #1776). +# +# #1776 (split from #1734 AC #5): a persona's declared posting credential can +# AUTHENTICATE correctly yet lack authorization to WRITE — the exact break that +# destroyed an advisory (403 on #1723) and silently dropped a pr-review approval +# on PR #1788. Every existing check verifies authentication and naming; nothing +# verified authorization. This library is the pure DECISION layer for a +# non-destructive capability probe: given the already-gathered facts of a +# `GET /repos/{owner}/{repo}` `.permissions` read, decide one of three outcomes — +# definite-negative (readable surface, no write bit) -> FAIL loudly +# indeterminate (unreadable surface / absent perms) -> WARN and proceed +# positive (a write bit is set) -> proceed silently +# It NEVER touches the network or writes content — the gh probe and the +# ::error::/::warning:: side-effects are the wrapper's job +# (scripts/persona-authz-preflight.sh). AC #4's fail-OPEN-on-indeterminate rule +# is the delicate one: a 5xx, a rate limit, or an unreadable surface must NOT be +# reported as "cannot write" and must not gate a legitimate advisory. +# +# Run with: bats tests/test_persona_authz_preflight.sh + +setup() { + source "$(dirname "$BATS_TEST_FILENAME")/../scripts/lib/persona-authz-preflight.sh" +} + +# --------------------------------------------------------------------------- +# authz_write_capability — map the .permissions booleans to a capability signal +# --------------------------------------------------------------------------- + +@test "write capability is 'yes' when push is true" { + run authz_write_capability false false true + [ "$status" -eq 0 ] + [ "$output" = "yes" ] +} + +@test "write capability is 'yes' when maintain is true (push false)" { + run authz_write_capability false true false + [ "$output" = "yes" ] +} + +@test "write capability is 'yes' when admin is true" { + run authz_write_capability true false false + [ "$output" = "yes" ] +} + +@test "write capability is 'no' when the surface is present but every write bit is false" { + run authz_write_capability false false false + [ "$output" = "no" ] +} + +@test "write capability is 'unknown' when the permissions object was absent (all empty)" { + run authz_write_capability "" "" "" + [ "$output" = "unknown" ] +} + +@test "a single present-but-false bit still classifies as 'no' (read-only collaborator)" { + # pull:true only would arrive here as admin/maintain/push all 'false' + run authz_write_capability false false false + [ "$output" = "no" ] +} + +# --------------------------------------------------------------------------- +# authz_preflight_decision — the three-state classifier (AC #1, #4) +# --------------------------------------------------------------------------- + +@test "positive: probe readable AND can write -> AUTHORIZED" { + run authz_preflight_decision yes yes + [ "$output" = "AUTHORIZED" ] +} + +@test "definite-negative: probe readable AND cannot write -> UNAUTHORIZED" { + run authz_preflight_decision yes no + [ "$output" = "UNAUTHORIZED" ] +} + +@test "indeterminate: probe NOT readable (5xx/rate-limit/401) -> INDETERMINATE even if capability is 'no'" { + # A read failure must never be read as a definite negative — AC #4. + run authz_preflight_decision no no + [ "$output" = "INDETERMINATE" ] +} + +@test "indeterminate: probe readable but permissions object absent (capability unknown) -> INDETERMINATE" { + run authz_preflight_decision yes unknown + [ "$output" = "INDETERMINATE" ] +} + +@test "indeterminate: an empty/garbage probe status is never a definite negative" { + run authz_preflight_decision "" no + [ "$output" = "INDETERMINATE" ] +} + +# --------------------------------------------------------------------------- +# authz_preflight_should_fail — only a definite negative gates (AC #4) +# --------------------------------------------------------------------------- + +@test "should_fail is true ONLY for UNAUTHORIZED" { + run authz_preflight_should_fail UNAUTHORIZED + [ "$status" -eq 0 ] +} + +@test "should_fail is false for AUTHORIZED" { + run authz_preflight_should_fail AUTHORIZED + [ "$status" -ne 0 ] +} + +@test "should_fail is false for INDETERMINATE (fail open)" { + run authz_preflight_should_fail INDETERMINATE + [ "$status" -ne 0 ] +} + +# --------------------------------------------------------------------------- +# authz_preflight_diagnostic — the loud message names account/secret/capability +# --------------------------------------------------------------------------- + +@test "UNAUTHORIZED diagnostic names the account, the credential secret, and the missing capability" { + run authz_preflight_diagnostic UNAUTHORIZED don-petry GH_PAT_DON_PETRY petry-projects/.github-private + [ "$status" -eq 0 ] + [[ "$output" == *"don-petry"* ]] + [[ "$output" == *"GH_PAT_DON_PETRY"* ]] + [[ "$output" == *"petry-projects/.github-private"* ]] + # names the missing capability, not a bare "permission denied" + [[ "$output" == *"write"* ]] + # distinguishes authorization from a missing secret (the #1734 diagnosis gap) + [[ "$output" == *"authenticated"* ]] +} + +@test "UNAUTHORIZED diagnostic does not masquerade as a missing/invalid secret" { + run authz_preflight_diagnostic UNAUTHORIZED don-petry GH_PAT_DON_PETRY petry-projects/.github-private + [[ "$output" != *"missing or invalid secret"* ]] +} + +@test "INDETERMINATE diagnostic says it is proceeding (fail open), not blocking" { + run authz_preflight_diagnostic INDETERMINATE don-petry GH_PAT_DON_PETRY petry-projects/.github-private + [[ "$output" == *"proceed"* ]] + # an indeterminate result must never claim the credential cannot write + [[ "$output" != *"cannot write"* ]] +} + +@test "AUTHORIZED diagnostic names the account and confirms write" { + run authz_preflight_diagnostic AUTHORIZED don-petry GH_PAT_DON_PETRY petry-projects/.github-private + [[ "$output" == *"don-petry"* ]] + [[ "$output" == *"write"* ]] +} + +# --------------------------------------------------------------------------- +# Wrapper integration (scripts/persona-authz-preflight.sh) with a stubbed `gh` +# — proves the network layer maps probe outcomes to the right exit code end to +# end. A stub `gh` on PATH lets these run offline and deterministically. +# --------------------------------------------------------------------------- + +WRAPPER() { + # Run the real wrapper with a fake `gh` shadowing the real one on PATH. All + # inputs are passed via the environment the test exports before calling. + PATH="$STUB_BIN:$PATH" \ + bash "$(dirname "$BATS_TEST_FILENAME")/../scripts/persona-authz-preflight.sh" +} + +_stub_gh() { + # $1 = exit code the stub returns; remaining args echoed as stdout (the JSON). + local rc="$1"; shift + cat > "$STUB_BIN/gh" < Date: Sun, 20 Sep 2026 20:48:38 +0000 Subject: [PATCH 2/2] fix(reviews): address review comments [skip ci-relay] --- .github/workflows/persona-runner-reusable.yml | 16 ++++++++++++++++ scripts/lib/persona-authz-preflight.sh | 18 +++++++++++------- scripts/persona-authz-preflight.sh | 10 +++++----- tests/test_persona_authz_preflight.sh | 11 ++++++++--- 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/.github/workflows/persona-runner-reusable.yml b/.github/workflows/persona-runner-reusable.yml index 70e8f9a3b..1e38c2596 100644 --- a/.github/workflows/persona-runner-reusable.yml +++ b/.github/workflows/persona-runner-reusable.yml @@ -216,6 +216,22 @@ jobs: GH_TOKEN="${OWNER_PAT}" fi export GH_TOKEN + + # Verify the token authenticates as the manifest identity BEFORE probing + # write access — otherwise a writable token for the WRONG account passes + # the capability probe and triggers the paid engine, only to be rejected + # by the post step's identity check afterward (the engine work is already + # spent). A definitely-wrong login fails closed here; an unreadable login + # (transient/rate-limit) is left to the post step's fail-closed check, + # matching this preflight's fail-open-on-indeterminate contract. + if [ -n "${GH_TOKEN}" ]; then + actual_login="$(gh api user -q .login 2>/dev/null || true)" + if [ -n "$actual_login" ] && [ "$actual_login" != "$POSTING_ACCOUNT" ]; then + echo "::error::authz-preflight: posting token authenticates as '${actual_login}', not the manifest identity '${POSTING_ACCOUNT}' (credential '${POSTING_CREDENTIAL}') — refusing to run the engine under the wrong identity" + exit 1 + fi + fi + bash scripts/persona-authz-preflight.sh - name: Install Claude Code diff --git a/scripts/lib/persona-authz-preflight.sh b/scripts/lib/persona-authz-preflight.sh index 977d93225..db39ecdf3 100644 --- a/scripts/lib/persona-authz-preflight.sh +++ b/scripts/lib/persona-authz-preflight.sh @@ -49,20 +49,24 @@ # comment-write; the post step's own failure handling (#1775) remains the # backstop for anything the proxy cannot see. -# authz_write_capability -# Map the three write-implying booleans from a repo's `.permissions` object to a +# authz_write_capability [triage] +# Map the comment-implying booleans from a repo's `.permissions` object to a # capability signal. Each argument is the string GitHub returns ("true"/"false") # or empty when the object was absent/unreadable. -# yes — at least one of admin/maintain/push is "true" (can write/comment) -# no — the object was present (>=1 arg non-empty) and no write bit is set +# yes — at least one of admin/maintain/push/triage is "true". triage is +# included because the triage role grants issue/PR comment access — +# exactly what posting the advisory needs — so a triage-only identity +# must not be rejected as a false negative (it can produce and post +# its advisory). +# no — the object was present (>=1 arg non-empty) and no such bit is set # unknown — every argument is empty (no `.permissions` object to read) # Only a literal "true" counts as a capability; any other value is not-write, so # a garbled field can never be mistaken for a grant. authz_write_capability() { - local admin="${1:-}" maintain="${2:-}" push="${3:-}" - if [ "$admin" = "true" ] || [ "$maintain" = "true" ] || [ "$push" = "true" ]; then + local admin="${1:-}" maintain="${2:-}" push="${3:-}" triage="${4:-}" + if [ "$admin" = "true" ] || [ "$maintain" = "true" ] || [ "$push" = "true" ] || [ "$triage" = "true" ]; then echo "yes" - elif [ -n "$admin" ] || [ -n "$maintain" ] || [ -n "$push" ]; then + elif [ -n "$admin" ] || [ -n "$maintain" ] || [ -n "$push" ] || [ -n "$triage" ]; then echo "no" else echo "unknown" diff --git a/scripts/persona-authz-preflight.sh b/scripts/persona-authz-preflight.sh index a71333718..dfb2301a1 100644 --- a/scripts/persona-authz-preflight.sh +++ b/scripts/persona-authz-preflight.sh @@ -53,7 +53,7 @@ fi # --- Non-destructive capability probe -------------------------------------- # A single read of the repo's permission surface for the authenticated identity. -errfile="$(mktemp)" +errfile="$(mktemp)" || { echo "::error::authz-preflight: failed to create temporary file" >&2; exit 1; } trap 'rm -f "$errfile"' EXIT probe_ok="yes" resp="" @@ -61,23 +61,23 @@ if ! resp="$(gh api "repos/${SOURCE_REPO}" 2>"$errfile")"; then probe_ok="no" # 4xx/5xx/rate-limit/network — an unreadable surface (fail open) fi -admin="" ; maintain="" ; push="" +admin="" ; maintain="" ; push="" ; triage="" if [ "$probe_ok" = "yes" ]; then # `.permissions` may be absent for some token/endpoint combinations; default it # to {} so a missing object yields empty fields (capability "unknown"), never a # jq error. A jq failure (malformed JSON) also degrades to an unreadable surface. if perms_tsv="$(printf '%s' "$resp" | jq -r ' (.permissions // {}) as $p - | [$p.admin, $p.maintain, $p.push] + | [$p.admin, $p.maintain, $p.push, $p.triage] | map(if . == null then "" else tostring end) | @tsv' 2>/dev/null)"; then - IFS=$'\t' read -r admin maintain push <<< "$perms_tsv" + IFS=$'\t' read -r admin maintain push triage <<< "$perms_tsv" else probe_ok="no" fi fi -capability="$(authz_write_capability "$admin" "$maintain" "$push")" +capability="$(authz_write_capability "$admin" "$maintain" "$push" "$triage")" decision="$(authz_preflight_decision "$probe_ok" "$capability")" diag="$(authz_preflight_diagnostic "$decision" "$POSTING_ACCOUNT" "$POSTING_CREDENTIAL" "$SOURCE_REPO")" diff --git a/tests/test_persona_authz_preflight.sh b/tests/test_persona_authz_preflight.sh index 4a7106cf7..aaabd31bd 100644 --- a/tests/test_persona_authz_preflight.sh +++ b/tests/test_persona_authz_preflight.sh @@ -44,8 +44,13 @@ setup() { [ "$output" = "yes" ] } +@test "write capability is 'yes' when triage is true (triage grants comment access)" { + run authz_write_capability false false false true + [ "$output" = "yes" ] +} + @test "write capability is 'no' when the surface is present but every write bit is false" { - run authz_write_capability false false false + run authz_write_capability false false false false [ "$output" = "no" ] } @@ -101,12 +106,12 @@ setup() { @test "should_fail is false for AUTHORIZED" { run authz_preflight_should_fail AUTHORIZED - [ "$status" -ne 0 ] + [ "$status" -eq 1 ] } @test "should_fail is false for INDETERMINATE (fail open)" { run authz_preflight_should_fail INDETERMINATE - [ "$status" -ne 0 ] + [ "$status" -eq 1 ] } # ---------------------------------------------------------------------------