fix(788): gate the vocabulary on a sentinel, not on a skippable if

Cold adversarial review found a fail-open in the first commit, reproduced here
before fixing: with `.*` in the positive list and the negative list written as a
scalar, `check-review-verdict.sh` classified an explicit `BLOCKED @ <head>` as
`positive`, exit 0. The merge-consent hook auto-grants on exactly that, and it
discards the classifier's stderr, so the only signal was invisible.

Root cause, measured rather than reasoned:

  * `${#arr[@]}` is nounset-safe only for a DECLARED-EMPTY array. On an unset
    name or a scalar it is a fatal unbound-variable error.
  * Under `set -u` that error inside a FUNCTION aborts the function, not the
    script — and in the `if ! validate; then exit 2; fi` form NEITHER branch is
    then taken, so the gate is skipped and execution continues.
  * `check-review-verdict.sh` deliberately runs without `set -e`, so the words
    reached the regex unvalidated. The write side has `set -e` and did die, so
    the asymmetry left the hole on precisely the side that authorizes merges.

The single-fault case needed no metacharacter and is the realistic wrong edit:
writing the list as a string degraded every negative verdict from `negative` to
`unknown` — DENY silently downgraded to ASK — with no non-zero exit anywhere.

A control-flow gate can be skipped by an abort; a value that was never assigned
cannot be read. So enforcement is now a DATA dependency: validation sets
`ETV_VERDICT_VOCABULARY_OK=1` as the last statement of its successful path, and
`etv_verdict_class`/`etv_verdict_alternation`/`etv_verdict_words_display` refuse
without it. The sentinel is reset to 0 at load time, so an inherited environment
variable cannot forge it. Array-ness is asserted with `declare -p` BEFORE any
`${#arr[@]}`, so the abort cannot occur in the first place — kept alongside the
sentinel for the accurate message, not as a second gate.

Separately, a top-level `exit` in a sourced library terminates the sourcing
script at the `source` line, so no check written after it can run: the reader
returned exit 0 with EMPTY stdout, breaking its "exactly one classification
word" contract. Both consumers now probe-source the library in a subshell and
require an end-of-file marker before sourcing it for real; that also catches a
syntax error and a truncated file.

All six failure modes now fail closed (reader exit 2, writer exit 1 having
posted nothing), and the shipped words classify exactly as before. Four of the
six new tests were witnessed RED against the unfixed predecessor 2d389a5bf; the
other two (unset array, truncated library) pin behaviour that already held and
are labelled as such rather than presented as fixes.

Also from the review: prose in the decision record, `docs/ci-cd.md` and two
in-script comments claimed validation was "run above, fatal" — the false premise
that shipped the hole. Corrected to name the sentinel as what enforces it. The
unresolved-symlink location dependency is recorded as a known limitation
(fail-closed today). De-flaked the unreadable-file test for a root runner, and
fixed a stale `check-review-verdict.sh:212` line reference plus a comment still
describing #788 as future work.

Not done, deliberately: no third guard on the both-patterns-match case. The
validator rejects a word in both lists, and test_check_review_verdict.py already
pins the dangerous direction; a third overlapping guard is the shape where
deleting any one leaves the suite green.

Verification: 987 passed / 2 skipped, ruff clean, shellcheck clean, catalog
regenerated, decisions-validate OK.

Refs: #788
Decisions-Edit: yes
Co-authored-by: Timothy <timothy@noreply.gitea.tblindustries.be>
This commit is contained in:
2026-08-26 21:28:39 +02:00
parent dd9cd113c3
commit 99caae7615
8 changed files with 288 additions and 23 deletions
+69 -4
View File
@@ -46,6 +46,24 @@
ETV_VERDICT_POSITIVE_WORDS=(mergeable approved lgtm)
ETV_VERDICT_NEGATIVE_WORDS=(blocked not-mergeable)
# --- The validation sentinel. -------------------------------------------------------------------
# Reset to 0 HERE, at load time, for two reasons. It defeats an inherited `ETV_VERDICT_VOCABULARY_OK=1`
# from the environment, and — the reason it exists — it makes "validated" a fact the DERIVED VIEWS
# can require, rather than something a caller is trusted to have checked.
#
# WHY A SENTINEL AND NOT A RETURN CODE. `etv_verdict_vocabulary_validate` used to be gated as
# `if ! etv_verdict_vocabulary_validate; then exit 2; fi`, and that is bypassable: under `set -u` an
# unbound-variable error inside a FUNCTION aborts the function but not the script, and in the
# `if ! f` form NEITHER branch is then taken — execution simply continues past the gate. Measured:
# with the negative list written as a scalar (`ETV_VERDICT_NEGATIVE_WORDS="blocked"`) and `.*` in the
# positive list, `check-review-verdict.sh` classified an explicit `BLOCKED @ <head>` as `positive`,
# exit 0, with only a stderr line the calling hook discards. That is the exact fail-open this whole
# file exists to make impossible.
#
# A control-flow gate can be skipped by an abort. A DATA dependency cannot: the words are now
# unobtainable unless validation ran all the way to its final line.
ETV_VERDICT_VOCABULARY_OK=0
# --- Validation. -------------------------------------------------------------------------------
# The read side interpolates these words into an EXTENDED REGULAR EXPRESSION. That makes the list an
# injection surface, and the failure direction is the dangerous one: a stray `.*` in the positive
@@ -57,11 +75,33 @@ ETV_VERDICT_NEGATIVE_WORDS=(blocked not-mergeable)
# Returns 0 when the vocabulary is usable, 1 otherwise (message on stderr). It never exits: the two
# consumers fail closed with DIFFERENT codes (`post` dies 1, `check` exits 2 so its callers treat it
# as an unreadable input), so the exit semantics belong to them, not here.
etv_verdict_vocabulary_validate() {
local word seen=' '
# True when $1 names a set variable carrying the ARRAY attribute. Checked before any `${#name[@]}`,
# because that expansion is nounset-safe ONLY for a declared-empty array: on an UNSET name or on a
# SCALAR it is a fatal unbound-variable error, which is precisely the abort that used to skip
# validation entirely. `declare -p` answers without expanding anything.
etv_verdict__is_array() {
local declaration flags
declaration=$(declare -p "$1" 2>/dev/null) || return 1
flags=${declaration#declare -}
flags=${flags%% *}
case "$flags" in
*a*) return 0 ;;
*) return 1 ;;
esac
}
etv_verdict_vocabulary_validate() {
local word seen=' ' name
ETV_VERDICT_VOCABULARY_OK=0
for name in ETV_VERDICT_POSITIVE_WORDS ETV_VERDICT_NEGATIVE_WORDS; do
if ! etv_verdict__is_array "$name"; then
printf 'review-verdict vocabulary: %s is not an array — declare it as %s=(word word), not as a string\n' "$name" "$name" >&2
return 1
fi
done
# `${#arr[@]}` is safe on an unset/empty array even under `set -u`, which `"${arr[@]}"` is not on
# bash 3.2 — so the count is checked BEFORE anything expands the arrays.
if [ "${#ETV_VERDICT_POSITIVE_WORDS[@]}" -eq 0 ] || [ "${#ETV_VERDICT_NEGATIVE_WORDS[@]}" -eq 0 ]; then
printf 'review-verdict vocabulary: the positive and negative lists must both be non-empty\n' >&2
return 1
@@ -88,6 +128,9 @@ etv_verdict_vocabulary_validate() {
seen="$seen$word "
done
# The ONLY assignment to 1 in this file, and it is the last statement of the successful path. Any
# earlier `return 1`, and any abort part-way through, leaves the sentinel at 0.
ETV_VERDICT_VOCABULARY_OK=1
return 0
}
@@ -96,6 +139,11 @@ etv_verdict_vocabulary_validate() {
# nothing. Case-insensitive, so the caller may pass the operator's raw argument.
etv_verdict_class() {
local token word
if [ "${ETV_VERDICT_VOCABULARY_OK:-0}" != 1 ]; then
printf 'review-verdict vocabulary: not validated — refusing to hand out verdict words\n' >&2
return 1
fi
token=$(printf '%s' "${1-}" | tr '[:upper:]' '[:lower:]')
for word in ${ETV_VERDICT_POSITIVE_WORDS[@]+"${ETV_VERDICT_POSITIVE_WORDS[@]}"}; do
@@ -121,6 +169,10 @@ etv_verdict_class() {
etv_verdict_alternation() {
local alternation='' word
local words=()
if [ "${ETV_VERDICT_VOCABULARY_OK:-0}" != 1 ]; then
printf 'review-verdict vocabulary: not validated — refusing to hand out verdict words\n' >&2
return 1
fi
case "${1-}" in
positive) words=(${ETV_VERDICT_POSITIVE_WORDS[@]+"${ETV_VERDICT_POSITIVE_WORDS[@]}"}) ;;
@@ -145,6 +197,10 @@ etv_verdict_alternation() {
# vocabulary — the same drift this file exists to remove, one layer out.
etv_verdict_words_display() {
local word index=0 total out=''
if [ "${ETV_VERDICT_VOCABULARY_OK:-0}" != 1 ]; then
printf 'review-verdict vocabulary: not validated — refusing to hand out verdict words\n' >&2
return 1
fi
set -- ${ETV_VERDICT_POSITIVE_WORDS[@]+"${ETV_VERDICT_POSITIVE_WORDS[@]}"} \
${ETV_VERDICT_NEGATIVE_WORDS[@]+"${ETV_VERDICT_NEGATIVE_WORDS[@]}"}
@@ -164,3 +220,12 @@ etv_verdict_words_display() {
printf '%s\n' "$out"
}
# --- Reached-the-end marker. --------------------------------------------------------------------
# A top-level `exit` in a SOURCED file terminates the sourcing script where it stands, so no check
# placed after the `source` can ever run — the reader returned exit 0 with empty stdout, violating
# its own "exactly one classification word" contract. Both consumers therefore source this file in a
# SUBSHELL first and require the marker below on stdout; a file that exits early never prints it.
# Defining a function is not enough (a truncated file can define every function and still stop
# short), so this is the LAST line and it must stay last.
etv_verdict_vocabulary_loaded() { printf 'etv-verdict-vocabulary-loaded\n'; }