fix(778): * is the only wildcard — a rule name's regex metacharacters were live
PR Gates / CI image pin matches docker/ci (pull_request) Successful in 10s
Build ErsatzTV Image / Delimiter ban (release path) (pull_request) Successful in 21s
PR Gates / Docs update reminder (pull_request) Successful in 14s
PR Gates / decisions lifecycle (pull_request) Successful in 24s
PR Gates / Fix proofs (Proves trailers) (pull_request) Successful in 17s
review-verdict/h10 Awaiting review verdict for 01de6cb
Review verdict / Set review-verdict status (pull_request_target) Successful in 32s
PR Gates / Script tests (pytest) (pull_request) Successful in 4m51s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 8m36s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 6m11s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Skipped
Build ErsatzTV Image / Functional E2E (curl + UI contracts) (pull_request) Canceled after 4m27s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Canceled after 0s
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Canceled after 0s

Found by attacking my own glob fallback from the previous commit before the reviewer
got to it, which is the round where this PR's defects have landed every time.

The 404 fallback matched a branch-protection rule by substituting `*` into a raw
regex, leaving every other metacharacter active. Verified directly: a rule named
`main.x` matched the base `mainax`, and `a+b` matched `aab`. The direction is the one
that matters — a spurious match to some OTHER rule that happens to require
`review-verdict/h10` reports this base as protected when nothing governs it, so a
consent gate answers yes on evidence about a different branch.

Each literal segment is now escaped before the pattern is assembled, so the wildcard
survives and nothing else does. Verified across 11 rule/base pairs: metacharacters are
literal, `*` still spans, exact and non-matches unaffected.

The regression test needed two goes to stop being vacuous, both times for the same
reason the rest of this PR keeps hitting: the stub never 404'd for the new mode, so
the run denied earlier via the by-name lookup and never reached the fallback at all. It
now goes red against the unescaped predecessor.

refs #778

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-19 18:01:42 +02:00
co-authored by Claude Opus 5
parent e5996b7c1e
commit 01de6cbd8b
2 changed files with 32 additions and 2 deletions
+8 -1
View File
@@ -416,7 +416,14 @@ else
bp_list=$(cat "$bp_file" 2>/dev/null || true)
if [ "$bp_code" = "200" ] && printf '%s' "$bp_list" | jq -e 'type == "array"' >/dev/null 2>&1; then
# Match a rule by exact name or by its glob, the same way Gitea applies them.
bp=$(printf '%s' "$bp_list" | jq --arg b "$base_ref" -c 'map(select((.branch_name // .rule_name // "") as $n | $n == $b or ($b | test("^" + ($n | gsub("\\*"; ".*")) + "$")))) | first // empty' 2>/dev/null || true)
#
# `*` is the ONLY wildcard: every other regex metacharacter in a rule name is escaped before
# the pattern is built. Substituting `*` into a raw regex made a rule named `main.x` match the
# base `mainax`, and `a+b` match `aab` — a spurious match to some OTHER rule that happens to
# require `review-verdict/h10` would report this base as protected when nothing governs it,
# which is a false-open in a consent gate. Escaping is done per literal segment, so the
# wildcard survives and nothing else does.
bp=$(printf '%s' "$bp_list" | jq --arg b "$base_ref" -c 'map(select((.branch_name // .rule_name // "") as $n | (($n | split("*") | map(gsub("(?<c>[.+?^${}()|\\[\\]\\\\])"; "\\\\" + .c)) | join(".*")) as $re | ($n == $b) or ($b | test("^" + $re + "$"))))) | first // empty' 2>/dev/null || true)
if [ -n "${bp//[[:space:]]/}" ]; then
bp_code=200 # a rule DOES cover this base; fall through and inspect it
else
@@ -65,6 +65,11 @@ if url.rstrip("/").endswith("/branch_protections"):
# The LIST endpoint, consulted only after a 404 on the by-name lookup. Gitea keys the by-name
# endpoint on the RULE name, so a base covered by a glob rule 404s there while being protected.
mode = (state / "bp").read_text().strip()
if mode == "REGEX-META-RULE":
# A rule whose NAME contains regex metacharacters, governing a DIFFERENT branch. Substituting
# `*` into a raw regex made `mai.` match `main`; the rule must be treated literally.
respond(json.dumps([{"branch_name": "mai.", "enable_status_check": True,
"status_check_contexts": ["review-verdict/h10"]}]))
if mode == "GLOB-RULE":
respond(json.dumps([{"branch_name": "m*", "enable_status_check": True,
"status_check_contexts": ["review-verdict/h10"]}]))
@@ -80,7 +85,7 @@ if "/branch_protections/" in url:
if mode == "TRANSPORT-ERROR":
# curl itself fails: no body, and `-w` prints 000.
respond("", "000")
if mode in ("NOT-FOUND", "GLOB-RULE", "LIST-UNREADABLE"):
if mode in ("NOT-FOUND", "GLOB-RULE", "LIST-UNREADABLE", "REGEX-META-RULE"):
respond('{"message":"not found"}', "404")
if mode == "FORBIDDEN":
respond('{"message":"token does not have at least one of required scope(s)"}', "403")
@@ -533,3 +538,21 @@ def test_the_base_ref_is_URL_ENCODED_in_the_protection_lookup():
does not exist, producing a 404 this code would read as 'unprotected'."""
src = HOOK.read_text()
assert "@uri" in src, "the base ref must be percent-encoded before going in a URL path segment"
def test_a_rule_name_with_REGEX_METACHARACTERS_does_not_match_a_different_base(hook):
"""The false-open in the glob fallback: `*` must be the only wildcard.
Substituting `*` into a raw regex left every other metacharacter live, so a rule named `mai.`
matched the base `main` (and `a+b` matched `aab`). A spurious match to some OTHER rule that
happens to require `review-verdict/h10` reports this base as protected when nothing governs it —
a consent gate answering yes on evidence about a different branch. Verified directly before the
fix: `main.x` matched `mainax`.
Here the only rule is `mai.`, which governs a branch that is not `main`, so nothing protects the
base and the gate must deny rather than grant.
"""
hook.set_branch_protection("REGEX-META-RULE")
reason = hook.reason()
assert "deny" in reason, (
"a rule named 'mai.' was regex-matched against base 'main' and read as protection")