diff --git a/docs/verification/gate-key-shape.md b/docs/verification/gate-key-shape.md new file mode 100644 index 00000000..582aafdd --- /dev/null +++ b/docs/verification/gate-key-shape.md @@ -0,0 +1,152 @@ +# Verification: gate-key-shape + +`lib/gate/gate-policy.sh` reads each quality-gate key with the line reader `_kit_toml_get`. A gate key written in a valid TOML form the reader cannot read came back empty, so the layer was skipped without a word. `[dev]` + `gate.proof_of_done = true` looked like "turn proof_of_done on" to anyone reading TOML, but the gate stayed off. + +## Differential, before the fix + +`gate-policy.sh enabled proof_of_done` on `origin/master` `adcf7915`, project file committed, no operator file, an empty kit root: + +| Project file | tomllib `proof_of_done` | Gate | Message | +|---|---|---|---| +| `kit.toml`: `[dev]` + `gate.proof_of_done = true` | true | off | none | +| `kit.toml`: `dev = { gate = { proof_of_done = true } }` | true | off | none | +| `kit.toml`: `["dev".gate]` + `proof_of_done = true` | true | off | none | +| `kit.toml`: `[dev.gate]` + `x = [` / ` [1]` / `]` + `proof_of_done = true` | true | off | none | +| `.kit.toml`: `gate.proof_of_done = true` | true | off | none | +| `kit.toml`: `[dev]` + `gate.proof_of_done = false`, operator says true | false | on | none | + +The same holds in the operator and kit-root files, and in the `--at ` copy the ship-gate floor reads. + +## Fix + +- **Shared check.** The left-to-right TOML scanner from the merge-command fix moves into `lib/config/kit-config.sh` as `_kit_toml_shape
`, and `_kit_mc_shape` becomes a one-line wrapper (`command str table`). The plain move left merge behavior unchanged: the same return code on all 9,231 merge corpus files in both sections. +- **Key scope.** The check guards only `
.`, so a sibling such as `[[gate.hard_path_exempt]]` stays fine. Values must be a bare `true` or `false`. +- **Scanner, after review.** It now also counts `{` and `}`, so a TOML 1.1 inline table spanning lines cannot hide a key line. A quoted header or an escaped key in an unrelated table (`[repo.labels."good first issue"]`) is accepted; one whose path could reach the guarded table, or an escaped header, still refuses. The merge check gets both changes too. +- **Gate policy.** Before resolving a key, `enabled()` checks every layer the key can come from: project `kit.toml` `[dev.gate]` and `.kit.toml` `[gate]` (or their `--at` copies), the operator file and the kit root. A layer that writes the key in an unreadable form makes the gate ON, with a stderr line naming the file, the offending line and the key. An odd `true` gets what it meant. An odd `false` cannot switch a gate off. The reader runs under `LC_ALL=C`, as the check does. + +``` +before: [dev] / gate.proof_of_done = true -> off, silent +after: gate-policy: /kit.toml line 2 cannot be read the way TOML reads it, and it may + set [dev.gate] proof_of_done; treating the gate as on (write a plain [dev.gate] table + with proof_of_done = true or false) +``` + +## Red run (new tests on the old code) + +`lib/gate/gate-policy.sh`, `lib/config/kit-config.sh` and `lib/config/merge-command.sh` from `origin/master`, with the new `tests/test-gate-key-shape.sh`. The negative controls pass here too. + +``` +Command: bash tests/run-all.sh --only test-gate-key-shape +Exit: 1 +Output: +== odd forms of a project key refuse loudly (gate ON) == +FAIL kit.toml: [dev] then a dotted gate key: rc=1 stderr=[] +FAIL kit.toml: [dev] then an inline gate table: rc=1 stderr=[] +FAIL kit.toml: top-level dev inline table: rc=1 stderr=[] +FAIL kit.toml: top-level dotted key: rc=1 stderr=[] +FAIL kit.toml: quoted header ["dev".gate]: rc=1 stderr=[] +FAIL kit.toml: quoted key in the table: rc=1 stderr=[] +FAIL kit.toml: a string where a boolean belongs: rc=0 stderr=[] +FAIL kit.toml: a nested element alone on its line before the key: rc=1 stderr=[] +FAIL kit.toml: a repeated key: rc=0 stderr=[] +FAIL kit.toml: the key as a sub-table: rc=1 stderr=[] +FAIL .kit.toml: dotted gate key: rc=1 stderr=[] +FAIL .kit.toml: inline gate table: rc=1 stderr=[] +FAIL .kit.toml: quoted header ["gate"]: rc=1 stderr=[] +FAIL kit.toml: an inline table spanning lines (TOML 1.1): rc=1 stderr=[] +FAIL kit.toml: an escaped header: rc=1 stderr=[] +FAIL kit.toml: an escaped key under [dev]: rc=1 stderr=[] +FAIL no line number: [] +== an odd form meant as false does not switch a gate off; it stays ON loudly == +FAIL odd false: rc=0 stderr=[] +== the operator and kit-root layers are checked too == +FAIL operator odd: rc=1 stderr=[] +FAIL root odd: rc=1 stderr=[] +== --at reads the base copy with the same rule == +FAIL --at base with a dotted gate key: rc=1 stderr=[] +== negative controls: plain shapes keep their meaning == +PASS no config at all: exit 1, quiet +PASS plain [dev.gate] true: exit 0, quiet +PASS plain [dev.gate] false, committed: exit 1, quiet +PASS legacy [gate] with [[gate.hard_path_exempt]] beside it: exit 1, quiet +PASS an odd form of another key leaves this key alone: exit 1, quiet +PASS a quoted header in an unrelated table: exit 1, quiet +PASS an escaped key in an unrelated table: exit 1, quiet +PASS a one-line inline table in the gate table: exit 1, quiet +PASS a default-on key with no config: exit 0, quiet +PASS multi-line strings, nested arrays and comments elsewhere: exit 0, quiet +--- +FAILS: 21 +Verdict: RED, as expected +``` + +## Green run + +``` +Command: bash tests/run-all.sh --only test-gate-key-shape +Exit: 0 +Output: +run-all: all 1 suites passed, 0 skipped for missing tooling +Verdict: PASS +``` + +``` +Command: bash tests/run-all.sh --time +Exit: 0 +Output: +run-all: --changed against adcf7915: 4 changed files -> 64 suites (55 named, the rest always-on) +run-all: all 64 suites passed, 0 skipped for missing tooling +Verdict: PASS +``` + +``` +Command: bash tests/run-all.sh --only (one run each) +Exit: 0 +Output: +test-merge-command rc=0 +test-gate-key-shape rc=0 +test-gate-opt-in rc=0 +test-gate-opt-out rc=0 +test-unified-kit-toml rc=0 +Verdict: PASS +``` + +## Fuzz against tomllib + +The merge-command corpora (9,231 files) were rewritten so that `merge` becomes `gate`, `command` becomes `proof_of_done`, and each quoted value becomes a bare `true` or `false`. For every file tomllib can parse, the reader's value must match tomllib's whenever the check accepts the file. + +| Section | Accepted | Disagree, with the check | Disagree, reader alone | +|---|---|---|---| +| `gate` | 6,626 | 0 | 833 | +| `dev.gate` | 6,311 | 0 | 1,078 | + +The merge corpora (the same 9,231 files, plus the 59,547-file key-form corpus) were re-judged for `merge.command` and `dev.merge.command` after the scanner change: 0 disagreements between check-accepted reads and tomllib. + +## Independent review + +A fresh-context security reviewer fuzzed about 4,650 valid documents plus 55 hand cases under macOS awk, gawk and mawk (and `/bin/bash` 3.2), and could not break the check under TOML 1.0. It confirmed old and new `_kit_mc_shape` agree on 700 random inputs. It found: + +| Finding | Now | +|---|---| +| A TOML 1.1 inline table spanning lines hid `proof_of_done = false` (same gap for merge, older than this change) | braces counted; test rows in both suites | +| A quoted header or escaped key in any unrelated table turned every gate ON (over-refusal) | accepted unless its path can reach the guarded table; negative controls in both suites | +| The message blamed the gate key, not the offending line | message names the line | +| `--at` temp files leak if the process is killed between `mktemp` and `rm` | not changed: two empty files, only on a kill | + +## Negative controls + +- The 10 control rows in the test pass on both `origin/master` and the fix: no config, plain `true`, plain `false` committed, legacy `[gate]` beside `[[gate.hard_path_exempt]]`, an odd form of another key, a quoted header and an escaped key in unrelated tables, a one-line inline table in the gate table, a default-on key, and multi-line strings, nested arrays and Vietnamese comments elsewhere. +- The merge suite gains a matching control (a quoted header and an escaped key in unrelated tables still read) and a TOML 1.1 row; all its rows pass. +- 40 real `kit.toml` / `.kit.toml` files (the local workspace, the operator file, this repo's own two) pass the check for all 7 gate keys, in every section they are read with, and give the same merge result as `origin/master`. + +## Cost + +`gate-policy.sh enabled` now scans each layer once. Measured in this repo (two 760-line files): 22 ms per call before, 48 to 56 ms after. In an adopted repo, only the kit root is long, so the added cost is about 10 to 15 ms per call. Each hook calls it a few times at most, well inside the 500 ms hook budget. + +## Not changed + +`lib/gate/lane-data.sh` reads `[gate] hard_path_canaries` and the exemption tables with its own validator, and `proof-ledger.sh` reads `gate.negative_control` (a string, not a gate switch). Neither goes through `enabled()`. + +## Rollback + +Revert the commit. It touches three shell files and adds one test file, and stores no state. diff --git a/lib/config/kit-config.sh b/lib/config/kit-config.sh index c73bfdc2..71cf81bc 100755 --- a/lib/config/kit-config.sh +++ b/lib/config/kit-config.sh @@ -70,6 +70,146 @@ _kit_toml_get() { ' "$file" } +# _kit_toml_shape
-- exit 0 when the file writes +#
. only in the one shape _kit_toml_get reads (or not at all), 1 otherwise. It looks +# at the raw lines, because the reader returns empty for the forms it cannot parse, and empty +# would read as "unset" or as the next layer's value. is a double-quoted value without a +# backslash or a `#`; is a bare true or false. Scope guards the whole section (any +# sub-table or array of tables under it refuses); scope guards only
., so a +# sibling such as `[[gate.hard_path_exempt]]` stays fine. +# The reader treats every line that opens with `[` as a header. So the check accepts such a line +# as a plain header (bare-key segments, blanks, an optional comment) or as a quoted header whose +# path cannot reach the guarded table, and refuses one inside a multi-line array, inline table or +# string where the reader would leave or enter the section by mistake; strings, arrays and inline +# tables are found by one left-to-right scan per line, as TOML reads them. It also refuses a +# multi-line string in the section, a blank other than space or tab, and an escaped key or header +# where its path could reach the guarded table. A sixth argument (any word) prints the line number +# of the first refusal. A key outside the section is judged by its full path (current table plus key), so `[dev]` +# + `merge.command` refuses like `dev.merge.command`, and so does any value on a parent (`dev = +# {`). Run the reader under LC_ALL=C too, as this check is, so a locale cannot change what counts +# as a blank. +_kit_toml_shape() { + local f="$1" sec="$2" kname="$3" kind="$4" scope="$5" where="${6:-}" + [ -s "$f" ] || return 0 + LC_ALL=C awk -v q="'" -v sec="$sec" -v kname="$kname" -v kind="$kind" -v scope="$scope" -v where="$where" ' + function bad() { if (where != "") print NR; exit 1 } + # key text with leading blanks and one layer of quotes around the first word removed + function firstword(k, w) { w = k; sub(/[ \t]*[.=].*$/, "", w); gsub(/"/, "", w); gsub(q, "", w); return w } + function lhs_norm(k, w) { w = k; sub(/[ \t]*=.*$/, "", w); gsub(/[ \t]/, "", w); gsub(/"/, "", w); gsub(q, "", w); return w } + # p is the guarded path or inside it; p is a parent of it (`dev` holds `dev.merge`) + function names(p) { return p == tgt || index(p, tgt ".") == 1 } + function holds(p) { return index(tgt, p ".") == 1 } + # the table name the reader gives a line that opens with `[` + function rname(l, h) { h = l; sub(/#.*/, "", h); gsub(/[][ \t]/, "", h); return h } + # a line opening with `[` that is not a header: the reader still takes it for one + function stray(l, h) { + h = rname(l); gsub(/"/, "", h); gsub(q, "", h) + if (insec || names(h) || holds(h)) bad() + } + # scan(l): walk the line once, left to right, the way TOML does: a basic string (escapes + # apply), a literal string (they do not), a multi-line string of either kind (ml, carried to + # the next line), a comment. Returns the change in array and inline-table depth outside + # strings (TOML 1.1 lets an inline table span lines). + function scan(l, i, n, c, c3, st, d) { + n = length(l); i = 1; st = 0; d = 0 + while (i <= n) { + c = substr(l, i, 1); c3 = substr(l, i, 3) + if (ml == 1) { + if (c == "\\") { i += 2; continue } + if (c3 == "\"\"\"") { ml = 0; i += 3; while (substr(l, i, 1) == "\"") i++; continue } + i++; continue + } + if (ml == 2) { + if (c3 == q q q) { ml = 0; i += 3; while (substr(l, i, 1) == q) i++; continue } + i++; continue + } + if (st == 1) { if (c == "\\") { i += 2; continue } if (c == "\"") st = 0; i++; continue } + if (st == 2) { if (c == q) st = 0; i++; continue } + if (c == "#") break + if (c3 == "\"\"\"") { ml = 1; i += 3; continue } + if (c3 == q q q) { ml = 2; i += 3; continue } + if (c == "\"") st = 1 + else if (c == q) st = 2 + else if (c == "[" || c == "{") d++ + else if (c == "]" || c == "}") d-- + i++ + } + return d + } + BEGIN { + tgt = (scope == "key") ? sec "." kname : sec + key = "[ \t]*[A-Za-z0-9_-]+([ \t]*[.][ \t]*[A-Za-z0-9_-]+)*[ \t]*" + header = "^[ \t]*([[]" key "[]]|[[][[]" key "[]][]])[ \t]*(#.*)?$" + } + { + line = $0; sub(/\r$/, "", line) + t = line; gsub(/[ \t]/, "", t) + if (t ~ /[[:space:]]/) bad() + if (insec && (ml || index(line, "\"\"\"") || index(line, q q q))) bad() + opens = (line ~ /^[ \t]*\[/) + if (ml) { + # the line starts inside a multi-line string; the reader still reads it + if (opens) stray(line) + depth += scan(line); if (depth < 0) depth = 0 + next + } + } + line ~ /^[ \t]*#/ { next } + { + cont = (depth > 0) + d = scan(line) + if (depth > 0) { + if (opens) stray(line) + depth += d; if (depth < 0) depth = 0 + if (opens) next + } else if (!opens) { + depth += d; if (depth < 0) depth = 0 + } + } + line ~ /^[ \t]*\[/ { + arr = (line ~ /^[ \t]*\[[ \t]*\[/) + if (line !~ header) { + # a quoted header: the reader keeps the quotes in the name, so it never matches the + # guarded table. Refuse it only where its TOML path could reach that table, or where an + # escape hides the path. + if (!index(line, "\"") && !index(line, q)) bad() + hn = rname(line); gsub(/"/, "", hn); gsub(q, "", hn) + if (index(line, "\\") || names(hn) || holds(hn)) bad() + insec = 0; cur = hn; next + } + hr = rname(line) + if (hr == sec && !arr) { if (seen++) bad(); insec = 1; ncmd = 0; next } + # an array of tables or a sub-table on the guarded path: the reader cannot read it; an array + # of tables on a parent (`[[dev]]`) moves the table out from under the path the reader reads + if (names(hr) || (arr && holds(hr))) bad() + insec = 0; cur = hr; next + } + { + # an array element line holds no key, but the reader still matches ` =` in the table + if (cont) { if (insec && line ~ ("^[ \t]*" kname "[ \t]*=")) bad(); next } + k = line; sub(/^[ \t]+/, "", k) + # an escaped key hides its path: refuse it where that path could reach the guarded table + if (index(k, "=")) { lk = k; sub(/=.*$/, "", lk); if (index(lk, "\\") && (insec || cur == "" || names(cur) || holds(cur))) bad() } + } + insec { + if (firstword(k) == kname && k ~ /[.=]/) { + if (++ncmd > 1) bad() + v = line + if (!sub("^[ \t]*" kname "[ \t]*=[ \t]*", "", v)) bad() + if (kind == "bool") { if (v !~ /^(true|false)[ \t]*(#.*)?$/) bad() } + else if (v !~ /^"[^"\\#]*"[ \t]*(#.*)?$/) bad() + } + next + } + { + if (scope == "table" && !index(sec, ".") && firstword(k) == sec && k ~ /[.=]/) bad() + if (!index(k, "=")) next + lh = lhs_norm(k); full = (cur == "") ? lh : cur "." lh + if (names(full) || holds(full)) bad() + } + ' "$f" +} + # _kit_toml_get_array
-- print each string element of a single-line or # multi-line TOML string array `key = ["a", "b"]` under [section], one element per line. # Returns 0 when was declared in
(even if the array is empty `[]`), 1 when absent diff --git a/lib/config/merge-command.sh b/lib/config/merge-command.sh index ee33a209..a8193e22 100644 --- a/lib/config/merge-command.sh +++ b/lib/config/merge-command.sh @@ -98,122 +98,8 @@ _kit_mc_base_ref() { # _kit_mc_shape [
] -- exit 0 when the file names
(default `merge`; # `dev.merge` for unified project kit.toml) only in the one shape the reader reads (or not at all), -# 1 otherwise. It looks at the raw lines, because the reader returns empty for the forms it cannot -# parse and empty would read as "unset". The reader treats every line that opens with `[` as a -# header. So the check accepts such a line only as a plain header (bare-key segments, blanks, an -# optional comment), and refuses one inside a multi-line array or string where the reader would -# leave or enter the merge table by mistake; strings and arrays are found by one left-to-right -# scan per line, as TOML reads them. It also refuses a multi-line string in the merge table, a -# blank other than space or tab, and a `\` in a key. A key outside the merge table is judged by -# its full path (current table plus key), so `[dev]` + `merge.command` refuses like -# `dev.merge.command`, and so does any value on a parent (`dev = {`). Both this check and the -# reader run under LC_ALL=C, so a locale cannot change what counts as a blank. -_kit_mc_shape() { - local f="$1" sec="${2:-merge}" - [ -s "$f" ] || return 0 - LC_ALL=C awk -v q="'" -v sec="$sec" ' - function bad() { exit 1 } - # key text with leading blanks and one layer of quotes around the first word removed - function firstword(k, w) { w = k; sub(/[ \t]*[.=].*$/, "", w); gsub(/"/, "", w); gsub(q, "", w); return w } - function lhs_norm(k, w) { w = k; sub(/[ \t]*=.*$/, "", w); gsub(/[ \t]/, "", w); gsub(/"/, "", w); gsub(q, "", w); return w } - # p is the merge table or inside it; p is a parent of it (`dev` holds `dev.merge`) - function names_sec(p) { return p == sec || index(p, sec ".") == 1 } - function holds_sec(p) { return index(sec, p ".") == 1 } - # the table name the reader gives a line that opens with `[` - function rname(l, h) { h = l; sub(/#.*/, "", h); gsub(/[][ \t]/, "", h); return h } - # a line opening with `[` that is not a header: the reader still takes it for one - function stray(l, h) { - h = rname(l); gsub(/"/, "", h); gsub(q, "", h) - if (insec || names_sec(h) || holds_sec(h)) bad() - } - # scan(l): walk the line once, left to right, the way TOML does: a basic string (escapes - # apply), a literal string (they do not), a multi-line string of either kind (ml, carried to - # the next line), a comment. Returns the change in array depth outside strings. - function scan(l, i, n, c, c3, st, d) { - n = length(l); i = 1; st = 0; d = 0 - while (i <= n) { - c = substr(l, i, 1); c3 = substr(l, i, 3) - if (ml == 1) { - if (c == "\\") { i += 2; continue } - if (c3 == "\"\"\"") { ml = 0; i += 3; while (substr(l, i, 1) == "\"") i++; continue } - i++; continue - } - if (ml == 2) { - if (c3 == q q q) { ml = 0; i += 3; while (substr(l, i, 1) == q) i++; continue } - i++; continue - } - if (st == 1) { if (c == "\\") { i += 2; continue } if (c == "\"") st = 0; i++; continue } - if (st == 2) { if (c == q) st = 0; i++; continue } - if (c == "#") break - if (c3 == "\"\"\"") { ml = 1; i += 3; continue } - if (c3 == q q q) { ml = 2; i += 3; continue } - if (c == "\"") st = 1 - else if (c == q) st = 2 - else if (c == "[") d++ - else if (c == "]") d-- - i++ - } - return d - } - BEGIN { - key = "[ \t]*[A-Za-z0-9_-]+([ \t]*[.][ \t]*[A-Za-z0-9_-]+)*[ \t]*" - header = "^[ \t]*([[]" key "[]]|[[][[]" key "[]][]])[ \t]*(#.*)?$" - } - { - line = $0; sub(/\r$/, "", line) - t = line; gsub(/[ \t]/, "", t) - if (t ~ /[[:space:]]/) bad() - if (insec && (ml || index(line, "\"\"\"") || index(line, q q q))) bad() - opens = (line ~ /^[ \t]*\[/) - if (ml) { - # the line starts inside a multi-line string; the reader still reads it - if (opens) stray(line) - depth += scan(line); if (depth < 0) depth = 0 - next - } - } - line ~ /^[ \t]*#/ { next } - { - cont = (depth > 0) - d = scan(line) - if (depth > 0) { - if (opens) stray(line) - depth += d; if (depth < 0) depth = 0 - if (opens) next - } else if (!opens) { - depth += d; if (depth < 0) depth = 0 - } - } - line ~ /^[ \t]*\[/ { - if (line !~ header) bad() - hr = rname(line); arr = (line ~ /^[ \t]*\[[ \t]*\[/) - if (hr == sec && !arr) { if (seen++) bad(); insec = 1; ncmd = 0; next } - # an array of tables or a sub-table: the reader cannot read it; an array of tables on a - # parent (`[[dev]]`) moves the merge table out from under the path the reader reads - if (names_sec(hr) || (arr && holds_sec(hr))) bad() - insec = 0; cur = hr; next - } - { - # an array element line holds no key, but the reader still matches `command =` in the table - if (cont) { if (insec && line ~ /^[ \t]*command[ \t]*=/) bad(); next } - k = line; sub(/^[ \t]+/, "", k) - if (index(k, "=")) { lk = k; sub(/=.*$/, "", lk); if (index(lk, "\\")) bad() } - } - insec { - if (firstword(k) == "command" && k ~ /[.=]/) { - if (++ncmd > 1) bad() - if (line !~ /^[ \t]*command[ \t]*=[ \t]*"[^"\\#]*"[ \t]*(#.*)?$/) bad() - } - next - } - { - if (sec == "merge" && firstword(k) == "merge" && k ~ /[.=]/) bad() - if (!index(k, "=")) next - lh = lhs_norm(k); full = (cur == "") ? lh : cur "." lh - if (names_sec(full) || holds_sec(full)) bad() - } - ' "$f" -} +# 1 otherwise. The whole table is guarded: see _kit_toml_shape in kit-config.sh for every rule. +_kit_mc_shape() { _kit_toml_shape "$1" "${2:-merge}" command str table; } # _kit_mc_resolve -- project kit.toml [dev.merge], # else project .kit.toml [merge], else operator kit.toml [merge], else kit-root [merge]. diff --git a/lib/gate/gate-policy.sh b/lib/gate/gate-policy.sh index 4ae6af00..a03c4a28 100755 --- a/lib/gate/gate-policy.sh +++ b/lib/gate/gate-policy.sh @@ -15,6 +15,9 @@ # working tree, so an uncommitted file would let the gated agent switch its own gate off # with no trace in the PR. An unknown key, a missing resolver, or a reader error means ON, # so a broken kit never silently drops a gate an operator turned on. +# A config file that writes the key in a TOML form the line reader cannot read (`[dev]` + +# `gate. = true`, a quoted header, an inline table) also means ON, with a stderr line naming +# the file: read as unset, an odd `true` would be ignored without a word. # # Exit-code contract for callers: 0 = on, 1 = off by config. Callers treat EVERY other exit # (a broken or unreadable copy of this script, 2/126/127) as on. @@ -43,6 +46,33 @@ enabled() { case " $KEYS " in *" $key "*) ;; *) return 0 ;; esac # shellcheck source=lib/config/kit-config.sh source "$GATE_DIR/../config/kit-config.sh" 2>/dev/null || return 0 + # The line reader runs under the same locale as the shape check below. + export LC_ALL=C + # A layer that writes the key in a form the line reader cannot read (`[dev]` + + # `gate. = true`, a quoted header, an inline table) would read as unset and be skipped + # without a word. Such a file turns the gate ON, loudly: an odd `true` gets what it meant, and + # an odd `false` cannot switch a gate off. + local tk="" tl="" pair sec f ln + local -a layers + if [ -n "$at" ]; then + tk="$(mktemp "${TMPDIR:-/tmp}/kit-gate-at.XXXXXX")" || return 0 + tl="$(mktemp "${TMPDIR:-/tmp}/kit-gate-at.XXXXXX")" || { rm -f "$tk"; return 0; } + kit_config_show_kit_at "$root" "$at" >| "$tk" || : >| "$tk" + kit_config_show_legacy_at "$root" "$at" >| "$tl" || : >| "$tl" + layers=("dev.gate:$tk" "gate:$tl") + else + layers=("dev.gate:$root/kit.toml" "gate:$root/.kit.toml") + fi + layers+=("gate:$(kit_config_operator)" "gate:$(kit_config_root)") + for pair in "${layers[@]}"; do + sec="${pair%%:*}"; f="${pair#*:}" + [ -s "$f" ] || continue + ln="$(_kit_toml_shape "$f" "$sec" "$key" bool key where)" && continue + case "$f" in "$tk") f="$at:kit.toml" ;; "$tl") f="$at:.kit.toml" ;; esac + echo "gate-policy: $f line ${ln:-?} cannot be read the way TOML reads it, and it may set [$sec] $key; treating the gate as on (write a plain [$sec] table with $key = true or false)" >&2 + rm -f "$tk" "$tl"; return 0 + done + rm -f "$tk" "$tl" # What the operator overlay and the kit root say, with the project file out of the picture. local def=false; case " $DEFAULT_ON " in *" $key "*) def=true ;; esac local rest; rest="$(KIT_PROJECT_ROOT=/nonexistent kit_config_get "gate.$key" "$def" 2>/dev/null)" || return 0 diff --git a/tests/test-gate-key-shape.sh b/tests/test-gate-key-shape.sh new file mode 100644 index 00000000..fcab7a2d --- /dev/null +++ b/tests/test-gate-key-shape.sh @@ -0,0 +1,108 @@ +#!/usr/bin/env bash +# test-gate-key-shape.sh -- a gate key written in a TOML form the line reader cannot read must not +# be ignored. Valid TOML such as `[dev]` + `gate.proof_of_done = true` used to read as unset, so a +# gate the project meant to turn on stayed off without a word. gate-policy.sh now refuses such a +# file loudly: the gate resolves ON and stderr names the file and the key. The plain shapes keep +# their meaning, and an odd form of one key does not touch another key. +# modules under test: lib/gate/gate-policy.sh lib/config/kit-config.sh +set -uo pipefail +KIT_DIR="$(cd "$(dirname "$0")/.." && pwd)" +TMP="$(mktemp -d "${TMPDIR:-/tmp}/kit-gate-shape.XXXXXX")" || exit 1; trap 'rm -rf "$TMP"' EXIT +# Pin both outer layers: an empty kit root (every key resolves to the code default) and no operator. +mkdir -p "$TMP/root" "$TMP/no-operator" +export KIT_CONFIG_ROOT="$TMP/root" KIT_CONFIG_OPERATOR="$TMP/no-operator" +POLICY="$KIT_DIR/lib/gate/gate-policy.sh" +fails=0 +pass(){ echo "PASS $*"; } +fail(){ echo "FAIL $*"; fails=$((fails+1)); } +gitc() { git -C "$1" init -q -b main 2>/dev/null; git -C "$1" config user.email t@t; git -C "$1" config user.name t; } + +# proj -- a repo that commits exactly this file; prints the repo path. +proj() { + local d="$TMP/$1"; rm -rf "$d"; mkdir -p "$d"; gitc "$d" + printf '%b\n' "$3" > "$d/$2" + git -C "$d" add -A; git -C "$d" commit -qm base + printf '%s\n' "$d" +} +# on