Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions docs/verification/gate-key-shape.md
Original file line number Diff line number Diff line change
@@ -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 <base>` 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 <file> <section> <key> <str|bool> <table|key>`, 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 `<section>.<key>`, 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: <repo>/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 <base> 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 <suite> (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.
140 changes: 140 additions & 0 deletions lib/config/kit-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,146 @@ _kit_toml_get() {
' "$file"
}

# _kit_toml_shape <file> <section> <key> <str|bool> <table|key> -- exit 0 when the file writes
# <section>.<key> 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. <str> is a double-quoted value without a
# backslash or a `#`; <bool> is a bare true or false. Scope <table> guards the whole section (any
# sub-table or array of tables under it refuses); scope <key> guards only <section>.<key>, 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 `<key> =` 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 <file> <section> <key> -- 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 <key> was declared in <section> (even if the array is empty `[]`), 1 when absent
Expand Down
Loading
Loading