Repository navigation
feat: implement issue #1776 — Authorization preflight: fail loudly when a persona credential authenticates but cannot write (split from #1734 AC #5) #1858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
|
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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)" | ||
|
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 | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.