fix(698): review round 4 — the PROTECTED guard was DEAD; define before use, fail closed, fix prescriptive docs
PR Gates / CI image pin matches docker/ci (pull_request) Successful in 16s
PR Gates / Docs update reminder (pull_request) Successful in 19s
PR Gates / decisions lifecycle (pull_request) Successful in 22s
review-verdict/h10 Awaiting review verdict for d4c6001
Review verdict / Set review-verdict status (pull_request_target) Successful in 13s
PR Gates / Script tests (pytest) (pull_request) Successful in 1m0s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 1m23s
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Successful in 22s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 8m38s
Build ErsatzTV Image / Functional E2E (curl + UI contracts) (pull_request) Successful in 16m30s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 18m18s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
PR Gates / CI image pin matches docker/ci (pull_request) Successful in 16s
PR Gates / Docs update reminder (pull_request) Successful in 19s
PR Gates / decisions lifecycle (pull_request) Successful in 22s
review-verdict/h10 Awaiting review verdict for d4c6001
Review verdict / Set review-verdict status (pull_request_target) Successful in 13s
PR Gates / Script tests (pytest) (pull_request) Successful in 1m0s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 1m23s
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Successful in 22s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 8m38s
Build ErsatzTV Image / Functional E2E (curl + UI contracts) (pull_request) Successful in 16m30s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 18m18s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
Round-3 cross-family review returned BLOCKED with 3 Mediums. The first was serious
and self-inflicted.
THE PROTECTED GUARD WAS A NO-OP. Round 3's `count_matching` / `count_not_matching`
helpers were defined AFTER the classification chain that calls them, so
`count_matching` was `command not found` on every run, `$( )` yielded an empty string,
`[ "" -gt 0 ]` errored, and the `elif` was simply skipped — the protected-path check
never executed at all. Confirmed by direct execution before fixing.
Three "protected path" tests stayed GREEN throughout, because a protected path is also
not a manifest and not docs-only, so the job still reached `pending` down a different
route. Asserting the STATE could not distinguish a working guard from a dead one. The
mutation battery missed it too: I had mutated the predicates, not their reachability.
Fixed three ways:
* helpers are defined immediately after `gh()`, before any use;
* the three counts are evaluated ONCE at TOP LEVEL and validated numeric, because
`exit 1` inside `$( )` leaves only the subshell and, with the substitution sitting
in a conditional, `set -e` never fires either — so a grep error had been silently
reading as "no match". A non-numeric result now aborts with nothing posted, and an
absent required check blocks the merge;
* the helpers return a non-numeric sentinel instead of trying to `exit`.
Verified: an invalid regex now exits 2 and posts NOTHING (previously it classified and
posted). Renaming the helper at its definition turns six tests red.
TESTS, aimed at the failure mode rather than the symptom:
* assert the DISCRIMINATOR (the job's `Decision:` reason line), not the outcome —
when several branches yield the same verdict, the verdict cannot tell you which ran.
A first draft of this test asserted the status description and failed against a
WORKING guard, because for `pending` the description is constant;
* a cheap stderr sweep for `command not found` / `integer expression expected` /
`unbound variable` across four representative PR shapes. Each of those makes an `if`
condition merely false while the job exits 0 and posts a plausible status, so this
catches a whole family of silently-skipped guards.
DOCS. The record and ci-cd.md still PRESCRIBED the here-string that round 3 removed —
following them would have reintroduced the temp-storage failure. Both now prescribe
counting, define-before-use, top-level evaluation and numeric validation. The workflow's
measurement paragraph still said the npm manifests "are included" three lines above the
note saying they are excluded; corrected.
379 tests pass.
Refs: #698
Decisions-Edit: yes
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -199,8 +199,7 @@ jobs:
|
||||
#
|
||||
# The set is measured, not guessed: across all 11 Renovate PRs this repo has ever had, the
|
||||
# paths touched were `Directory.Packages.props` (10 of them) and `.config/dotnet-tools.json`
|
||||
# (1). `web/package.json` / `web/package-lock.json` are included because Renovate manages the
|
||||
# SPA's dependencies too and a first such PR must not deadlock.
|
||||
# (1). The npm manifests are deliberately NOT included — see the BOT_MANIFESTS note below.
|
||||
#
|
||||
# Deliberately EXCLUDED, with the cost stated: `*.csproj` and any source file. The one
|
||||
# historical Renovate PR outside the set above is #20, which touched a `.csproj` AND two C#
|
||||
@@ -233,6 +232,56 @@ jobs:
|
||||
|
||||
gh() { curl -sf -H "Authorization: token $GITEA_TOKEN" "$@"; }
|
||||
|
||||
# DEFINED HERE, BEFORE ANY USE. An earlier round defined these AFTER the classification
|
||||
# chain that calls them, so `count_matching` was `command not found` on every run, the
|
||||
# PROTECTED branch silently never fired, and three "protected path" tests still passed —
|
||||
# they reached `pending` by another route, so the guard being dead was invisible.
|
||||
#
|
||||
# HOW THE PATH PREDICATES ARE EVALUATED, and why neither obvious spelling is used.
|
||||
#
|
||||
# `producer | grep -q…` is FORBIDDEN here: `grep -q` exits at its first match, the producer
|
||||
# then takes SIGPIPE and exits 141 once the list exceeds the pipe buffer, and under
|
||||
# `set -o pipefail` the pipeline is a FAILURE even though grep MATCHED — inverting the guard
|
||||
# for exactly the large PRs that matter. Reproduced with `A.cs` + 1900 docs paths (171KB,
|
||||
# inside the enumerator's 2000-file cap): `docs_only=yes`, status 141; and a `.gitea/` path
|
||||
# made `PROTECTED` MISS. That construct predates #698 and was live on `main`.
|
||||
#
|
||||
# A here-string (`grep -q… <<< "$files"`) fixes the SIGPIPE but bash materialises a large
|
||||
# here-string via TEMPORARY STORAGE, so it can fail when the runner's temp space is full or
|
||||
# unwritable — and because these run inside `if`/`!`, that failure would flip the predicate
|
||||
# the same way. Trading a buffer bug for an environmental one is not a fix.
|
||||
#
|
||||
# So: count with `grep -c`, which DRAINS stdin (no early exit, no SIGPIPE) over an ordinary
|
||||
# pipe (no temp file), and treat grep's own exit status honestly — `grep -c` exits 1 when the
|
||||
# count is zero, which is a legitimate answer, while anything >1 is a real error and must FAIL
|
||||
# THE JOB rather than silently read as "no match". `set -e` would not catch these on its own
|
||||
# because they sit inside command substitution in a conditional.
|
||||
count_matching() { # how many lines of $2 match $1
|
||||
local out st=0
|
||||
out=$(printf '%s\n' "$2" | grep -cE "$1") || st=$?
|
||||
# NOT `exit 1`: these run inside `$( )`, so an exit leaves only the SUBSHELL and, because
|
||||
# the substitution sits in a conditional, `set -e` does not fire either — the job would sail
|
||||
# on with the predicate silently reading as "no match". Emit a NON-NUMERIC sentinel instead
|
||||
# and let the caller, at top level, refuse to classify.
|
||||
if [ "$st" -gt 1 ]; then
|
||||
echo "::error::grep failed (status ${st}) evaluating a path predicate." >&2
|
||||
printf 'ERR'
|
||||
return 0
|
||||
fi
|
||||
printf '%s' "${out:-0}"
|
||||
}
|
||||
count_not_matching() { # how many lines of $2 do NOT match $1
|
||||
local out st=0
|
||||
out=$(printf '%s\n' "$2" | grep -cvE "$1") || st=$?
|
||||
if [ "$st" -gt 1 ]; then
|
||||
echo "::error::grep failed (status ${st}) evaluating a path predicate." >&2
|
||||
printf 'ERR'
|
||||
return 0
|
||||
fi
|
||||
printf '%s' "${out:-0}"
|
||||
}
|
||||
|
||||
|
||||
# --- Is there already a verdict for THIS sha? ----------------------------------------
|
||||
# NOTE the heading no longer says "never overwrite". It cannot promise that: the read below
|
||||
# and the POST at the end of this job are not atomic, so a human verdict posted in between is
|
||||
@@ -342,13 +391,29 @@ jobs:
|
||||
echo "Changed files (${count}, complete=${complete}):"
|
||||
printf '%s\n' "$files" | sed 's/^/ /'
|
||||
|
||||
# Evaluated ONCE, at TOP LEVEL, so a failure can actually stop the job. Evaluating them
|
||||
# inline inside the `if`/`elif` chain is what hid the two defects above: a bad status or a
|
||||
# missing function turned into an empty string, `[ "" -gt 0 ]` errored, and the branch was
|
||||
# simply skipped. A non-numeric result here is fatal and posts nothing — an absent required
|
||||
# check blocks the merge, which is the correct direction.
|
||||
n_protected=$(count_matching "$PROTECTED" "$files")
|
||||
n_not_manifest=$(count_not_matching "$BOT_MANIFESTS" "$files")
|
||||
n_not_docs=$(count_not_matching "$DOCS_ONLY" "$files")
|
||||
for v in "$n_protected" "$n_not_manifest" "$n_not_docs"; do
|
||||
case "$v" in
|
||||
''|*[!0-9]*)
|
||||
echo "::error::A path predicate returned '${v}' instead of a count — the classifier is not operating, so no ${CONTEXT} status will be written for ${SHA:0:7}."
|
||||
exit 1 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
exempt=no
|
||||
reason=""
|
||||
if [ "$complete" != yes ]; then
|
||||
reason="${enum_error} — no exemption"
|
||||
elif [ "${count:-0}" -eq 0 ]; then
|
||||
reason="no changed files could be read from the API — no exemption"
|
||||
elif [ "$(count_matching "$PROTECTED" "$files")" -gt 0 ]; then
|
||||
elif [ "$n_protected" -gt 0 ]; then
|
||||
reason="touches a protected path (gate/CI/hooks/scripts/ci-image) — exemptions do not apply"
|
||||
else
|
||||
# The two exemptions are evaluated as INDEPENDENT predicates rather than as a chain.
|
||||
@@ -369,53 +434,15 @@ jobs:
|
||||
# blocks the merge) but it would do so on the ORDINARY path — every non-bot PR — so the
|
||||
# gate would look broken rather than strict. `cmd || var=yes` is safe for the same
|
||||
# reason it is confusing; both are spelled out instead.
|
||||
# HOW THE PATH PREDICATES ARE EVALUATED, and why neither obvious spelling is used.
|
||||
#
|
||||
# `producer | grep -q…` is FORBIDDEN here: `grep -q` exits at its first match, the producer
|
||||
# then takes SIGPIPE and exits 141 once the list exceeds the pipe buffer, and under
|
||||
# `set -o pipefail` the pipeline is a FAILURE even though grep MATCHED — inverting the guard
|
||||
# for exactly the large PRs that matter. Reproduced with `A.cs` + 1900 docs paths (171KB,
|
||||
# inside the enumerator's 2000-file cap): `docs_only=yes`, status 141; and a `.gitea/` path
|
||||
# made `PROTECTED` MISS. That construct predates #698 and was live on `main`.
|
||||
#
|
||||
# A here-string (`grep -q… <<< "$files"`) fixes the SIGPIPE but bash materialises a large
|
||||
# here-string via TEMPORARY STORAGE, so it can fail when the runner's temp space is full or
|
||||
# unwritable — and because these run inside `if`/`!`, that failure would flip the predicate
|
||||
# the same way. Trading a buffer bug for an environmental one is not a fix.
|
||||
#
|
||||
# So: count with `grep -c`, which DRAINS stdin (no early exit, no SIGPIPE) over an ordinary
|
||||
# pipe (no temp file), and treat grep's own exit status honestly — `grep -c` exits 1 when the
|
||||
# count is zero, which is a legitimate answer, while anything >1 is a real error and must FAIL
|
||||
# THE JOB rather than silently read as "no match". `set -e` would not catch these on its own
|
||||
# because they sit inside command substitution in a conditional.
|
||||
count_matching() { # how many lines of $2 match $1
|
||||
local out st=0
|
||||
out=$(printf '%s\n' "$2" | grep -cE "$1") || st=$?
|
||||
if [ "$st" -gt 1 ]; then
|
||||
echo "::error::grep failed (status ${st}) evaluating a path predicate; refusing to classify." >&2
|
||||
exit 1
|
||||
fi
|
||||
printf '%s' "${out:-0}"
|
||||
}
|
||||
count_not_matching() { # how many lines of $2 do NOT match $1
|
||||
local out st=0
|
||||
out=$(printf '%s\n' "$2" | grep -cvE "$1") || st=$?
|
||||
if [ "$st" -gt 1 ]; then
|
||||
echo "::error::grep failed (status ${st}) evaluating a path predicate; refusing to classify." >&2
|
||||
exit 1
|
||||
fi
|
||||
printf '%s' "${out:-0}"
|
||||
}
|
||||
|
||||
# BOTS is a short fixed literal, so it cannot reach the pipe buffer; it is still written
|
||||
# with an explicit status capture so a grep error cannot read as "not a bot" by accident.
|
||||
is_bot=no
|
||||
bot_hits=$(printf '%s\n' "$BOTS" | tr ' ' '\n' | grep -cxF "$AUTHOR") || bot_hits=0
|
||||
if [ "${bot_hits:-0}" -gt 0 ]; then is_bot=yes; fi
|
||||
manifests_only=no
|
||||
if [ "$(count_not_matching "$BOT_MANIFESTS" "$files")" -eq 0 ]; then manifests_only=yes; fi
|
||||
if [ "$n_not_manifest" -eq 0 ]; then manifests_only=yes; fi
|
||||
docs_only=no
|
||||
if [ "$(count_not_matching "$DOCS_ONLY" "$files")" -eq 0 ]; then docs_only=yes; fi
|
||||
if [ "$n_not_docs" -eq 0 ]; then docs_only=yes; fi
|
||||
|
||||
if [ "$is_bot" = yes ] && [ "$manifests_only" = yes ]; then
|
||||
exempt=yes
|
||||
|
||||
+5
-2
@@ -824,8 +824,11 @@ exemption; its stdout is meaningless on any failure path and is never consumed.
|
||||
**Never write a classification guard as `producer | grep -q…` here.** Under `set -o pipefail`, `grep -q`
|
||||
exits at its first match, the producer takes SIGPIPE (141), and a MATCH is reported as a failed
|
||||
pipeline — inverting the guard for any PR whose path list exceeds the pipe buffer. That let a large PR
|
||||
be classified docs-only, and let one editing `.gitea/` skip the protected-path check entirely. Use a
|
||||
here-string (`grep -q… <<< "$files"`). Full detail: `ci.grep-q-pipefail-inversion`.
|
||||
be classified docs-only, and let one editing `.gitea/` skip the protected-path check entirely. A
|
||||
here-string is **also** wrong (bash spills a large one to temp storage, which fails the same way when
|
||||
temp is full). **Count** instead — `grep -c` drains stdin over an ordinary pipe — evaluate the counts
|
||||
once at top level rather than inline in an `if`, and fail closed on a non-numeric result. Full detail:
|
||||
`ci.grep-q-pipefail-inversion`.
|
||||
|
||||
That script takes the expected base branch as a **required 5th argument** and refuses to enumerate when
|
||||
the PR's live base does not match it, checked both before and after paging (ersatztv#698).
|
||||
|
||||
@@ -48,7 +48,7 @@ the link for rationale. Superseded/retired history lives in `archive/`. Regenera
|
||||
| `ci.functional-e2e-harness` | The `functional-e2e` CI job boots the PR's own code from source via `dotnet run` (`scripts/e2e-local.sh`) and runs deterministic assertions (`scripts/e2e-functional.sh`) as an advisory (non-blocking) job, not a `build` dependency or required check. Originally curl-only; since #445 the same job carries a second, headless-browser step for the contracts curl cannot express — see `ci.ui-e2e-harness`. | 2026-07-16 | [link](records/ci/functional-e2e-harness.md) |
|
||||
| `ci.gate-trigger-base-resolved` | The workflow that writes the branch-protection-required `review-verdict/h10` status triggers on `pull_request_target` with `branches: [main]`, never on plain `pull_request`. Gitea resolves a `pull_request` workflow DEFINITION from the PR's own head commit, so under that trigger a PR editing `.gitea/workflows/review-verdict.yml` ran its own rewritten copy and could post `h10=success` for itself; `pull_request_target` resolves the definition from the base instead. The `branches: [main]` filter is part of the rule, not a refinement of it: base resolution only relocates the rewrite from the head to the base, so without the filter a PR opened into an attacker-pushed base branch runs that branch's gate. `pull_request_target` is safe HERE only because this job never checks out or executes head-supplied code — it checks out `base.sha` and runs only that tree's scripts (`ci.shared-pr-file-enumeration`); reintroducing a head checkout under this trigger would be worse than the bug it fixed. This closes the rewrite route through THIS workflow and does NOT close the class: Gitea injects a write-capable `GITEA_TOKEN` into EVERY job, so any ref-resolved workflow — and a collaborator's own API token, since branch protection binds the context and not its issuer — can still forge `review-verdict/h10`. Tracked in #697; the exemption path has its own separate defects in #698. | 2026-07-28 | [link](records/ci/gate-trigger-base-resolved.md) |
|
||||
| `ci.gitea-milestone-filter-noop` | Never filter issues with the server-side `?milestones=<name>` parameter — fetch all open issues once and filter LOCALLY on each issue's `.milestone.title`. | 2026-07-21 | [link](records/ci/gitea-milestone-filter-noop.md) |
|
||||
| `ci.grep-q-pipefail-inversion` | In any script running under `set -o pipefail`, a security or classification predicate of the form `producer \| grep -q…` is FORBIDDEN. `grep -q` exits at its first match; the producer then takes SIGPIPE and exits 141 once the data exceeds the pipe buffer (~64K), so `pipefail` reports the pipeline as FAILED even though grep MATCHED — inverting the predicate exactly when the input is large. Use a here-string (`grep -q… <<< "$data"`), which is written to a temp file and cannot signal its writer, so the exit status is grep's alone. `grep -c` and `grep -v` WITHOUT `-q` are safe because they drain stdin. This applies to both the enforced gate `.gitea/workflows/review-verdict.yml` and the advisory hook `.claude/hooks/pretooluse-merge-consent.sh`. | 2026-07-29 | [link](records/ci/grep-q-pipefail-inversion.md) |
|
||||
| `ci.grep-q-pipefail-inversion` | In any script running under `set -o pipefail`, a security or classification predicate of the form `producer \| grep -q…` is FORBIDDEN: `grep -q` exits at its first match, the producer then takes SIGPIPE and exits 141 once the data exceeds the pipe buffer (~64K), so `pipefail` reports the pipeline as FAILED even though grep MATCHED — inverting the predicate exactly when the input is large. A here-string (`grep -q… <<< "$data"`) is ALSO forbidden: bash materialises a large here-string via temporary storage, so it fails when temp space is full or unwritable, and inside an `if`/`!` that failure flips the predicate the same way. COUNT instead — `n=$(printf '%s\n' "$data" \| grep -cE "$re")` — because `grep -c` drains stdin (no early exit, no SIGPIPE) over an ordinary pipe (no temp file). Read grep's status honestly: exit 1 means a zero count and is a legitimate answer, anything >1 is a real error. Evaluate the counts ONCE at TOP LEVEL, never inline inside an `if`/`elif` condition: inside `$( )` an `exit` leaves only the subshell and `set -e` does not fire, so an error silently reads as "no match". Validate that each result is numeric and fail closed if not. This applies to both the enforced gate `.gitea/workflows/review-verdict.yml` and the advisory hook `.claude/hooks/pretooluse-merge-consent.sh`. | 2026-07-29 | [link](records/ci/grep-q-pipefail-inversion.md) |
|
||||
| `ci.infra-shaped-red-under-load` | When a job dies inside a setup/cache step before your code compiles, check the runner host's load before diagnosing the diff, and never file a CI bug off one sample under pressure. | 2026-07-21 | [link](records/ci/infra-shaped-red-under-load.md) |
|
||||
| `ci.jq-version-contract` | Every shell gate that shells out to `jq` is authored to the jq 1.6-compatible subset, because the CI runner ships jq 1.6 while every developer Mac ships 1.8.x. `scripts/jq-preflight.sh` (no args) prints the parsed version and asserts a floor of 1.6 in every gate job's log; `scripts/jq-preflight.sh --expect 1.6` additionally pins and fails loudly, but ONLY in the `script-tests` job. `review-verdict.yml` never pins — it writes the branch-protection-required `review-verdict/h10` status, so a hard pin there would turn any jq bump into a repo-wide merge deadlock. | 2026-07-26 | [link](records/ci/jq-version-contract.md) |
|
||||
| `ci.killed-job-triage` | Never trust a job's `conclusion` field alone — read the log tail and require an `❌ Failure - Main …` marker before treating a red as a real failure. | 2026-07-21 | [link](records/ci/killed-job-triage.md) |
|
||||
|
||||
@@ -5,9 +5,9 @@ status: active
|
||||
since: '2026-07-29'
|
||||
supersedes: none
|
||||
superseded-by: none
|
||||
rule: 'In any script running under `set -o pipefail`, a security or classification predicate of the form `producer | grep -q…` is FORBIDDEN. `grep -q` exits at its first match; the producer then takes SIGPIPE and exits 141 once the data exceeds the pipe buffer (~64K), so `pipefail` reports the pipeline as FAILED even though grep MATCHED — inverting the predicate exactly when the input is large. Use a here-string (`grep -q… <<< "$data"`), which is written to a temp file and cannot signal its writer, so the exit status is grep''s alone. `grep -c` and `grep -v` WITHOUT `-q` are safe because they drain stdin. This applies to both the enforced gate `.gitea/workflows/review-verdict.yml` and the advisory hook `.claude/hooks/pretooluse-merge-consent.sh`.'
|
||||
rule: 'In any script running under `set -o pipefail`, a security or classification predicate of the form `producer | grep -q…` is FORBIDDEN: `grep -q` exits at its first match, the producer then takes SIGPIPE and exits 141 once the data exceeds the pipe buffer (~64K), so `pipefail` reports the pipeline as FAILED even though grep MATCHED — inverting the predicate exactly when the input is large. A here-string (`grep -q… <<< "$data"`) is ALSO forbidden: bash materialises a large here-string via temporary storage, so it fails when temp space is full or unwritable, and inside an `if`/`!` that failure flips the predicate the same way. COUNT instead — `n=$(printf ''%s\n'' "$data" | grep -cE "$re")` — because `grep -c` drains stdin (no early exit, no SIGPIPE) over an ordinary pipe (no temp file). Read grep''s status honestly: exit 1 means a zero count and is a legitimate answer, anything >1 is a real error. Evaluate the counts ONCE at TOP LEVEL, never inline inside an `if`/`elif` condition: inside `$( )` an `exit` leaves only the subshell and `set -e` does not fire, so an error silently reads as "no match". Validate that each result is numeric and fail closed if not. This applies to both the enforced gate `.gitea/workflows/review-verdict.yml` and the advisory hook `.claude/hooks/pretooluse-merge-consent.sh`.'
|
||||
signals: 'grep -q pipefail, exit 141, SIGPIPE in a shell guard, large PR classified docs-only, protected path guard skipped, printf pipe grep -q, classification inverts on big input, pipe buffer 64K shell predicate · paths: `.gitea/workflows/review-verdict.yml`, `.claude/hooks/pretooluse-merge-consent.sh`, `scripts/tests/test_pr_changed_files.py` · issues: #698, #649'
|
||||
mechanics: '`if ! grep -qvE "$RE" <<< "$files"` / `if grep -qE "$RE" <<< "$files"`; regression tests `test_a_LARGE_pr_*` in `scripts/tests/test_pr_changed_files.py` build 1900+ paths (~171KB) to cross the pipe buffer'
|
||||
mechanics: '`count_matching` / `count_not_matching` helpers in `review-verdict.yml`, DEFINED BEFORE FIRST USE, results precomputed into `n_protected`/`n_not_manifest`/`n_not_docs` at top level and validated numeric; regression tests `test_a_LARGE_pr_*` build 1900+ paths (~171KB) to cross the pipe buffer, `test_the_classify_step_runs_without_SHELL_ERRORS` catches a dead guard, `test_a_protected_path_is_rejected_BY_THE_PROTECTED_BRANCH` asserts the branch rather than the outcome'
|
||||
---
|
||||
|
||||
Found by cross-family adversarial review of the #698 PR, then reproduced directly. It is not a defect
|
||||
@@ -31,13 +31,29 @@ The second is the serious one: it reaches a green required status on a PR that r
|
||||
**no retarget, no bot account and no forged status** — nothing but a large PR. It is a more direct hole
|
||||
than any of the three routes #698 was filed for.
|
||||
|
||||
**Why a here-string rather than dropping `-q`.** Dropping `-q` also works (grep then drains stdin), but
|
||||
it relies on a subtle side effect and reads as a redundant `>/dev/null`. `<<<` removes the pipe
|
||||
entirely: bash materialises the string, so there is no writer to signal and the status is unambiguous.
|
||||
The cost is a temp file per evaluation, which is irrelevant at these sizes.
|
||||
**The here-string was the FIRST fix, and it was wrong.** `grep -q… <<< "$data"` does remove the
|
||||
SIGPIPE, and it shipped for one round. But bash materialises a large here-string through temporary
|
||||
storage, so it fails when temp space is full or unwritable — and because these predicates sit inside
|
||||
`if`/`!`, that failure flips them exactly as SIGPIPE did. It did not reproduce on macOS bash 3.2 and
|
||||
did on Linux bash 5.x, which is the environment CI runs; the disagreement is itself the argument for a
|
||||
construct that cannot fail either way. Counting with `grep -c` uses an ordinary pipe and drains stdin,
|
||||
so neither failure mode exists.
|
||||
|
||||
**The testing lesson, which generalises past this bug.** The whole class was invisible because every
|
||||
test used small inputs. A guard whose behaviour depends on a BUFFER THRESHOLD needs a test that crosses
|
||||
**Two follow-on traps, both found only by re-review.** First, the helpers were defined AFTER the
|
||||
classification chain that called them, so `count_matching` was `command not found` on every run and the
|
||||
`PROTECTED` branch never fired — while three "protected path" tests stayed green, because a protected
|
||||
path is also not a manifest and not docs-only, so the job reached `pending` down another route. Second,
|
||||
`exit 1` inside those helpers only left the command-substitution SUBSHELL, and since the substitution
|
||||
sat in a conditional, `set -e` never fired either. Hence the rule: define before use, evaluate once at
|
||||
top level, validate the result is numeric, and fail closed when it is not.
|
||||
|
||||
**Two testing lessons.** When several branches produce the SAME outcome, asserting the outcome cannot
|
||||
tell you which branch ran — assert the discriminator (here the `Decision:` reason line). And a cheap
|
||||
stderr sweep for `command not found` / `integer expression expected` / `unbound variable` catches a
|
||||
whole family of silently-skipped guards, because each of those makes an `if` condition merely false
|
||||
while the job exits 0 and posts a plausible status.
|
||||
|
||||
**The input-size lesson.** The whole class was invisible because every test used small inputs. A guard whose behaviour depends on a BUFFER THRESHOLD needs a test that crosses
|
||||
the threshold; otherwise the suite is measuring the wrong regime entirely and full coverage of the
|
||||
small regime proves nothing. The regression tests pair each large-input negative with a large-input
|
||||
POSITIVE control, so "large lists now fail closed" (a merge deadlock) cannot masquerade as a fix.
|
||||
|
||||
@@ -1384,3 +1384,63 @@ def test_positive_control_no_late_verdict_still_posts_normally(tmp_path):
|
||||
"""Without this, the test above would pass against a job that had simply stopped posting."""
|
||||
posted, _ = _run_classify(tmp_path, _emitting("docs/a.md"), status_mode="none")
|
||||
assert posted is not None and posted["state"] == "success"
|
||||
|
||||
|
||||
# --- The PROTECTED branch must actually EXECUTE, not merely coincide with the right answer --------
|
||||
#
|
||||
# Round-3 review found `count_matching` being called before its definition, so it was
|
||||
# `command not found` on every run and the PROTECTED branch never fired. Three "protected path" tests
|
||||
# passed anyway, because a protected path is also not a manifest and not docs-only, so the job reached
|
||||
# `pending` down a different route. Asserting the STATE could not see it; the guard was dead and the
|
||||
# suite was green.
|
||||
#
|
||||
# The lesson generalises: when several branches produce the same outcome, asserting the outcome cannot
|
||||
# tell you which branch ran. Assert the DISCRIMINATOR — here the reason string the branch writes.
|
||||
|
||||
def test_a_protected_path_is_rejected_BY_THE_PROTECTED_BRANCH(tmp_path):
|
||||
posted, r = _run_classify(tmp_path, _emitting("scripts/evil.sh", "docs/a.md"))
|
||||
assert posted is not None, f"the job posted nothing: {r.stderr[-2000:]}"
|
||||
assert posted["state"] == "pending"
|
||||
# The DISCRIMINATOR is the job's `Decision:` line, not the status description: for `pending` the
|
||||
# description is always "Awaiting review verdict for <sha>", identical no matter which branch
|
||||
# produced it. A first draft of this test asserted on the description and failed against a WORKING
|
||||
# guard — the assertion has to be aimed at something that actually differs per branch.
|
||||
assert "protected" in r.stdout.lower(), (
|
||||
"the PR was not exempted, but NOT via the protected-path branch — it reached the same verdict "
|
||||
f"by another route, so that guard may be dead. Decision log:\n{r.stdout[-800:]}")
|
||||
|
||||
|
||||
def test_a_protected_path_defeats_the_BOT_exemption_by_the_protected_branch(tmp_path):
|
||||
"""A bot PR whose every path IS a manifest, plus one protected path. Without a live PROTECTED
|
||||
branch this still lands on `pending` (the protected file is not a manifest), so again only the
|
||||
reason string distinguishes a working guard from a dead one."""
|
||||
posted, r = _run_classify(
|
||||
tmp_path, _emitting("Directory.Packages.props", ".gitea/workflows/renovate.yml"),
|
||||
author="renovate")
|
||||
assert posted is not None
|
||||
assert posted["state"] == "pending"
|
||||
assert "protected" in r.stdout.lower(), (
|
||||
f"the bot exemption was refused, but not by the PROTECTED branch:\n{r.stdout[-800:]}")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("paths,author", [
|
||||
(("docs/a.md",), "timothy"),
|
||||
(("Directory.Packages.props",), "renovate"),
|
||||
(("ErsatzTV/Program.cs",), "timothy"),
|
||||
(("scripts/evil.sh",), "timothy"),
|
||||
])
|
||||
def test_the_classify_step_runs_without_SHELL_ERRORS(tmp_path, paths, author):
|
||||
"""A cheap, general trap-catcher for the whole step.
|
||||
|
||||
`command not found`, `integer expression expected`, `unbound variable` — each of these silently
|
||||
skips a branch inside an `if`/`elif` (the condition just evaluates false) while the job exits 0 and
|
||||
posts a plausible status. That is precisely how the dead PROTECTED guard survived a green suite.
|
||||
Any of them appearing on stderr means some guard is not running, whatever the verdict looks like.
|
||||
"""
|
||||
_, r = _run_classify(tmp_path, _emitting(*paths), author=author)
|
||||
bad = [ln for ln in r.stderr.splitlines()
|
||||
if "command not found" in ln
|
||||
or "integer expression expected" in ln
|
||||
or "unbound variable" in ln
|
||||
or "syntax error" in ln]
|
||||
assert not bad, f"the classification step emitted shell errors, so a guard is not running: {bad}"
|
||||
|
||||
Reference in New Issue
Block a user