fix(648): round 4 — the round-1 fail-open was still reachable, via an over-long number

Round 4 found the round-1 MECHANISM alive in round 3's regex. The pattern guaranteed the
operands were digits but not that they fit `test`'s integer range, so a 23-digit major made
`[ "$major" -lt "$min_major" ]` error with "integer expression expected" — and `set -e`
exempts a failing command in an `if` condition, so the conditional read false and the floor
was never asserted. Exit 0. That is precisely what the empty string did in round 1: same
shape, third occurrence, same predicate.

Bounding the runs with {1,9} was not sufficient on its own. The pattern is unanchored at the
end, so `jq-1.99999999999999999999999` simply matched the first 9 digits of the minor and
compared THAT — a mis-parse that passes the floor rather than an error that skips it. The
trailing `([^0-9]|$)` is what actually closes it.

Second hole: `[[:space:]]` matches NEWLINES, so round 3's "anchor" still scanned the whole
output. `jq\n2.34: cannot load shared library` matched `jq`, crossed the newline as
separator, and parsed 2.34. Now the first line only, with `[[:blank:]]`.

Third: the separator class `[-[:blank:]]{1,4}` could be walked across filler —
`jq -- 2.34 (real jq-1.6)` parsed as 2.34, `jq<TAB><TAB>9.9` as 9.9. It is now one of the two
forms real jq emits: `jq-1.6` or `jq version 1.6` (a blank separator REQUIRES the literal
word `version`).

Verified across a 20-case matrix: every legitimate form still parses to the right numbers
(jq-1.6, jq version 1.6, jq-1.7.1, jq-1.6-dirty, jq-1.10 numerically, jq-1.6 (Debian 1.6-2.1),
jq-v1.6, JQ-1.6, jq-1.6.0, CRLF), and every constructed attack fails closed. Four mutations,
each reddening exactly its own tests. The real jq 1.8.2 on this machine still reports cleanly.

Also: the log line now interpolates the first line, so a multi-line --version cannot split the
single grep-able line the no-arg mode exists to emit.

None of these are reachable from a real jq build. They are recorded and fixed because the
guard's own stated invariant — never assert a floor against something it did not parse — was
still violable three rounds in, and the follow-up PR moves this exact code into the
branch-protection-required check.
This commit is contained in:
2026-07-26 22:21:07 +02:00
parent 4e094637c6
commit 807ebbd38e
2 changed files with 81 additions and 3 deletions
+30 -3
View File
@@ -109,17 +109,44 @@ fi
# warning line or a date prefix as the version — `2026.07.26 jq-1.6` parsed as 2026.07, which sails
# over the floor. Anchoring keeps every legitimate form (`jq-1.6`, `jq version 1.6`, `jq-1.7.1`,
# `jq-1.6-dirty`, `jq-1.6 (Debian 1.6-2.1)`) and rejects the rest, which then fails closed below.
if [[ "$raw" =~ ^[[:space:]]*[Jj][Qq][[:space:]-]+(version[[:space:]]+)?v?([0-9]+)\.([0-9]+) ]]; then
# FIRST LINE ONLY, and bounded everywhere. Both bounds are load-bearing; this is the third round on
# this one predicate and each previous version failed for a variant of the same reason.
#
# * First line only. `[[:space:]]` matches NEWLINES, so an "anchored" pattern still scanned the
# whole output: `jq\n2.34: cannot load` matched `jq`, crossed the newline as separator, and
# parsed 2.34 — fail-open, the round-2 bug narrowed but not closed. `[[:blank:]]` (space/tab
# only) plus a first-line slice confines the match to the line that can actually carry a version.
# * Bounded digit runs. This is the round-1 mechanism resurrected. The regex guaranteed the
# operands were digits but not that they fit in `test`'s integer range, so a 23-digit major made
# `[ "$major" -lt "$min_major" ]` error with "integer expression expected" — and `set -e` exempts
# a failing command in an `if` condition, so the conditional read false and THE FLOOR WAS NEVER
# ASSERTED, exit 0. Exactly what the empty-string case did in round 1. `{1,9}` keeps every
# operand inside a 32-bit integer, so the comparison can no longer error.
# * Bounded separator runs, so the pattern cannot be walked across arbitrary filler.
first=${raw%%$'\n'*}
first=${first%$'\r'}
# The separator is one of the two forms real jq actually emits — `jq-1.6` or `jq version 1.6` — not
# "any run of dashes and blanks". A permissive class let the pattern be walked across filler:
# `jq -- 2.34 (real jq-1.6)` parsed as 2.34, and `jq<TAB><TAB>9.9` as 9.9. A blank separator now
# REQUIRES the literal word `version`, which is the only context a real build puts one in.
#
# The trailing `([^0-9]|$)` is what actually bounds the digit runs. `{1,9}` alone does not: the regex
# is unanchored at the end, so `jq-1.99999999999999999999999` simply matched the first 9 digits of
# the minor and compared THAT — a mis-parse that passes the floor. Requiring a non-digit (or
# end-of-string) after the minor makes an over-long run fail to match at all, so it fails closed.
if [[ "$first" =~ ^[[:blank:]]*[Jj][Qq](-v?|[[:blank:]]+version[[:blank:]]+v?)([0-9]{1,9})\.([0-9]{1,9})([^0-9]|$) ]]; then
major="${BASH_REMATCH[2]}"
minor="${BASH_REMATCH[3]}"
else
echo "jq-preflight: could not parse a major.minor version out of '${raw}'. Refusing to assert a floor against an unparsed version — that would silently pass." >&2
echo "jq-preflight: could not parse a major.minor version out of '${first}'. Refusing to assert a floor against an unparsed version — that would silently pass." >&2
exit 1
fi
# THIS LINE IS THE POINT of the no-arg mode: the jq version CI actually used is in the job log, so a
# future divergence can be diagnosed from the log alone rather than by guessing at the runner image.
echo "jq-preflight: jq version in use = ${raw} (parsed ${major}.${minor}; floor ${MIN_VERSION})"
# `$first`, not `$raw`: a multi-line `--version` would split this across lines, breaking the single
# grep-able log line that is the entire point of the no-arg mode.
echo "jq-preflight: jq version in use = ${first} (parsed ${major}.${minor}; floor ${MIN_VERSION})"
min_major=${MIN_VERSION%%.*}
min_minor=${MIN_VERSION#*.}
+51
View File
@@ -212,10 +212,61 @@ def test_a_number_that_is_not_the_VERSION_is_not_accepted_as_one(preflight, vers
assert "could not parse" in r.stderr
@pytest.mark.parametrize("version_line", [
"jq-99999999999999999999999.0",
"jq-1.99999999999999999999999",
])
def test_an_OUT_OF_RANGE_digit_run_fails_closed(preflight, version_line):
"""The round-1 fail-open mechanism, resurrected via an over-long number.
A regex that guarantees *digits* does not guarantee they fit `test`'s integer range. With a
23-digit major, `[ "$major" -lt "$min_major" ]` errors with "integer expression expected" — and
`set -e` exempts a failing command in an `if` condition, so the conditional read false and THE
FLOOR WAS NEVER ASSERTED, exit 0. Identical in shape to the empty-string case that started this.
Bounding the run with `{1,9}` alone was NOT enough either: the pattern is unanchored at the end,
so an over-long minor just matched its first 9 digits and compared that instead — a mis-parse
that passes. The trailing non-digit requirement is what actually closes it.
"""
preflight.with_jq(version_line)
r = preflight.run()
assert r.returncode == 1, f"{version_line!r} exited 0 — the floor was not asserted"
@pytest.mark.parametrize("version_line", [
"jq\n2.34: cannot load shared library", # NEWLINE crossed by a [[:space:]] separator
"jq\n\n\n99.9",
"jq -- 2.34 (real jq-1.6)", # filler walked by a permissive separator class
"jq\t\t9.9",
])
def test_a_number_AFTER_the_jq_token_is_not_reachable_across_filler(preflight, version_line):
"""`[[:space:]]` matches NEWLINES, so the round-3 'anchor' still scanned the whole output —
`jq\\n2.34: cannot load` matched `jq`, crossed the newline as separator, and parsed 2.34.
Parsing the first line only, and requiring one of the two separator forms real jq emits
(`jq-1.6` or `jq version 1.6`), confines the match to a real version token."""
preflight.with_jq(version_line)
r = preflight.run()
assert r.returncode == 1, f"{version_line!r} was accepted as a version"
def test_the_observability_line_stays_on_ONE_line(preflight):
"""The no-arg mode exists to put a single grep-able version line in the job log; interpolating a
multi-line `--version` would split it."""
preflight.with_jq("jq-1.6\ntrailing noise")
r = preflight.run()
assert r.returncode == 0, r.stderr
version_lines = [ln for ln in r.stdout.splitlines() if "version in use" in ln]
assert len(version_lines) == 1
assert "trailing noise" not in r.stdout
@pytest.mark.parametrize("version_line,expected", [
("jq-1.6 (Debian 1.6-2.1)", "1.6"), # distro packaging suffix
("jq-1.10", "1.10"), # two-digit minor: must compare numerically, not lexically
("jq-1.7.1", "1.7"),
("jq-1.6.0", "1.6"),
("jq-v1.6", "1.6"),
("JQ-1.6", "1.6"),
])
def test_legitimate_forms_still_parse_to_the_right_version(preflight, version_line, expected):
preflight.with_jq(version_line)