Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
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 \
Expand Down Expand Up @@ -258,7 +259,7 @@
timeout-minutes: 5
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
# NOTE: persist-credentials is intentionally left at the default (true) —

Check warning on line 262 in .github/workflows/lint.yml

View workflow job for this annotation

GitHub Actions / Lint

262:9 [comments-indentation] comment not indented like content
# vci_resolve_reusable needs `git fetch origin` to authenticate against this
# private repo when resolving same-repo channel tags. Setting
# persist-credentials: false would silently push every such caller into the
Expand Down
41 changes: 41 additions & 0 deletions .github/workflows/persona-runner-reusable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
# restoring the ADR-0005 position; reverses #1650, fixes #1734). Chosen per
# persona from runtime.identity.credential, never a repo-wide default.
required: false
# GH_PAT_WORKFLOWS removed (#1650): the broadly-scoped workflows PAT is no

Check warning on line 92 in .github/workflows/persona-runner-reusable.yml

View workflow job for this annotation

GitHub Actions / Lint

92:7 [comments-indentation] comment not indented like content
# longer a posting fallback here — least privilege, and posting under it
# attributed advisories to a human owner account.

Expand All @@ -108,7 +108,7 @@
timeout-minutes: 15
permissions:
contents: read # checkout THIS repo's tooling + prompts. The agent reads
# the public source repo with github.token; the comment is

Check warning on line 111 in .github/workflows/persona-runner-reusable.yml

View workflow job for this annotation

GitHub Actions / Lint

111:24 [comments-indentation] comment not indented like content
# posted by the workflow with the persona's manifest PAT.
steps:
- name: Checkout persona tooling + prompts
Expand Down Expand Up @@ -177,6 +177,47 @@
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
Comment thread
don-petry marked this conversation as resolved.
Resolved

- name: Install Claude Code
if: steps.resolve.outputs.skip == 'false'
env:
Expand Down
130 changes: 130 additions & 0 deletions scripts/lib/persona-authz-preflight.sh
Original file line number Diff line number Diff line change
@@ -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 <admin> <maintain> <push>
# 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
}
Comment thread
codeant-ai[bot] marked this conversation as resolved.
Resolved

# authz_preflight_decision <probe_ok> <capability>
# The three-state classifier. <probe_ok> 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.
# <capability> 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 <decision>
# 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 <decision> <account> <credential> <repo>
# 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
}
104 changes: 104 additions & 0 deletions scripts/persona-authz-preflight.sh
Original file line number Diff line number Diff line change
@@ -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:-<unknown>}"
POSTING_CREDENTIAL="${POSTING_CREDENTIAL:-<unknown>}"

# 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)"
Comment thread
don-petry marked this conversation as resolved.
Resolved
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
Loading
Loading