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
142 changes: 142 additions & 0 deletions docs/verification/merge-shape-differential.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# Verification: merge-shape-differential

`lib/config/merge-command.sh` has two parsers for the project merge command. `_kit_mc_shape` (the check) refuses forms the reader cannot read. `_kit_toml_get` (the reader) produces the value `kit_merge_exec` runs. They found and named table headers differently, and the check judged keys outside the merge table by their own text, not by their full path. So valid TOML that names the command could pass the check and still read as unset. Unset means `wrap merge`, `wrap land`, `mega-merge` and `stack-merge` fall back to the plain squash, which skips the project's merge guard. The contract says such forms refuse with exit 2.

## Differential, before the fix

Probe at `origin/master` `1facce9d`: shape rc (0 = accepted), reader value, Python `tomllib` value.

| Form (section) | Check | Reader | tomllib | Origin |
|---|---|---|---|---|
| `[dev]` + `merge.command = "guard.sh"` (dev.merge) | 0 | empty | guard.sh | PR 1277 |
| `[dev]` + `merge = { command = "guard.sh" }` | 0 | empty | guard.sh | PR 1277 |
| `dev = { merge = { command = "guard.sh" } }` | 0 | empty | guard.sh | PR 1277 |
| `["dev".merge]` / `[dev."merge"]` + `command = ...` | 0 | empty | guard.sh | PR 1277 |
| `["merge"]` / `['merge']` + `command = ...` (merge) | 0 | empty | g.sh | before 1277 |
| `[merge]#c` + `command = "g.sh #x"` | 0 | g.sh (cut) | g.sh #x | before 1277 |
| `[merge]#c` + `command = 'g.sh'` | 0 | 'g.sh' | g.sh | before 1277 |

The first five rows fail open: the check accepts, the reader says unset, the kit merges plain. The last two run a value the check never looked at: it skipped a header with `#` right after `]`, which the reader enters. A quoted key such as `"[x]" = 1` inside the table had the reverse effect: the check took it for a header and stopped looking, the reader did not.

The key is read from the default branch, so a PR cannot use this for its own merge. A config edit that looks like a no-op to anyone reading TOML (moving `command` under `[dev]`) would turn the guard off for every later merge, with no error.

## Fix

The reader treats every line that opens with `[` as a header. The check now:

- accepts such a line only as a plain header (bare-key segments, blanks, an optional `#` comment);
- finds strings, comments and array brackets with one left-to-right scan per line, the way TOML reads them (basic strings with escapes, literal strings without, both multi-line kinds carried across lines);
- refuses a `[` line inside a multi-line array or string only where the reader would leave or enter the merge table by mistake (inside the table, or naming the table or a parent);
- refuses a multi-line string inside the merge table, a blank other than space or tab, and a `\` in a key;
- judges a key outside the merge table by its full path (current table plus key), and refuses any value on `dev` itself. So `[dev]` + `merge.command`, `[dev]` + `merge = {...}` and `dev = { ... }` refuse like the dotted form;
- runs, with the reader, under `LC_ALL=C`, so a locale cannot change what counts as a blank.

## Independent review

A fresh-context security reviewer attacked the fix with a growing corpus (129 inputs at the end), under macOS awk, GNU Awk 5.4.2 and mawk 1.3.4.

| Round | Found | Now |
|---|---|---|
| 1 | nested-array lines, multi-line strings, `\u` escapes in keys and headers, a two-line `dev = {`, form-feed blanks; a regression (carriage return inside a header) | refused, test rows |
| 2 | a nested element alone on its line (` [1]`) still read as a header; triple quotes and nested arrays refused anywhere (over-refusal); `[[:space:]]` depended on the locale (mawk refused `à` in a comment) | refused where it matters, allowed elsewhere, `LC_ALL=C` |
| 3 | quote counting could not track TOML strings: `'''` inside a one-line string, mixed `"""` and `'''`, triples in a closing-line comment, a backslash ending a literal string, a `"` inside a literal string | a left-to-right scanner per line, test rows |
| 4 (fresh reviewer, fuzzing) | no fail-open in about 69,000 generated files (the same harness finds about 730 on master); one low-impact case: `[[dev]]` before `[dev.merge]`; new over-refusal of array element lines with `=` or `\` (regex lists) | `[[parent]]` refused; array element lines are not keys, test rows |

Final state: the round-4 corpora (68,580 files, both sections) give zero disagreements between check-accepted reads and `tomllib`. The same harness flags failures on `origin/master`, so the zero is a real result. macOS awk, gawk and mawk agree on every corpus they ran.

## Red run (new tests on the old code)

`lib/config/merge-command.sh` from `origin/master` with the new tests. The "carriage return inside the header" row passes here because the old code refused that form; it guards the regression the review found in the first version of this fix. The negative controls pass here too.

```
Command: bash tests/run-all.sh --only test-merge-command
Exit: 1
Output:
FAIL slug: kit.toml [dev] with merge.command refuses (exit 2), not unset
FAIL check/reader differential (kit: [dev] then a dotted merge.command): refuses (exit 2)
FAIL check/reader differential (kit: [dev] then an inline merge table): refuses (exit 2)
FAIL check/reader differential (kit: top-level dev inline table): refuses (exit 2)
FAIL check/reader differential (kit: quoted header ["dev".merge]): refuses (exit 2)
FAIL check/reader differential (kit: quoted header [dev."merge"]): refuses (exit 2)
FAIL check/reader differential (kit: header comment with no blank, # in the value): refuses (exit 2)
FAIL check/reader differential (kit: header comment with no blank, single-quoted value): refuses (exit 2)
FAIL check/reader differential (kit: a quoted key that looks like a header): refuses (exit 2)
FAIL check/reader differential (legacy: quoted header ["merge"]): refuses (exit 2)
FAIL check/reader differential (legacy: single-quoted header ['merge']): refuses (exit 2)
FAIL check/reader differential (legacy: header comment with no blank, # in the value): refuses (exit 2)
FAIL check/reader differential (legacy: a quoted key that looks like a header): refuses (exit 2)
FAIL check/reader differential (kit: a nested-array line inside the table): refuses (exit 2)
FAIL check/reader differential (kit: a nested-array line, then [dev] merge.command): refuses (exit 2)
FAIL check/reader differential (kit: a multi-line string holding a header): refuses (exit 2)
FAIL check/reader differential (kit: an escaped key under [dev]): refuses (exit 2)
FAIL check/reader differential (kit: an escaped header): refuses (exit 2)
FAIL check/reader differential (kit: a dev inline table over two lines): refuses (exit 2)
FAIL check/reader differential (legacy: a form feed before the key): refuses (exit 2)
FAIL check/reader differential (legacy: a multi-line string holding a command): refuses (exit 2)
FAIL check/reader differential (legacy: an escaped header): refuses (exit 2)
FAIL check/reader differential (kit: a nested element alone on its line inside the table): refuses (exit 2)
FAIL check/reader differential (kit: a nested element alone on its line, then [dev] merge.command): refuses (exit 2)
FAIL check/reader differential (kit: a comment holding a triple quote, then dev inline table): refuses (exit 2)
FAIL check/reader differential (kit: an inline comment opening a triple quote): refuses (exit 2)
FAIL check/reader differential (kit: a multi-line string in another table holding the header): refuses (exit 2)
FAIL check/reader differential (legacy: a nested date element alone on its line): refuses (exit 2)
FAIL check/reader differential (kit: a triple single quote inside a one-line string): refuses (exit 2)
FAIL check/reader differential (kit: a triple single quote inside a multi-line basic string): refuses (exit 2)
FAIL check/reader differential (kit: a comment with triples on the closing line): refuses (exit 2)
FAIL check/reader differential (kit: a literal string ending in a backslash before a bracket string): refuses (exit 2)
FAIL check/reader differential (kit: a double quote inside a literal string): refuses (exit 2)
FAIL check/reader differential (kit: an array of tables on the parent): refuses (exit 2)
FAIL kit.toml: array element lines with = or a backslash are not keys (exit 0)
FAIL ...and give the value
test-merge-command: 147 passed, 36 FAILED of 183
Verdict: RED, as expected
```

## Green run

```
Command: bash tests/run-all.sh --only test-merge-command --time
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: all 25 suites passed, 0 skipped for missing tooling
Verdict: PASS
```

```
Command: bash tests/run-all.sh --only <suite> (one run each)
Exit: 0
Output:
test-unified-kit-toml rc=0
test-mega-merge rc=0
test-wrap-merge rc=0
test-wrap-land rc=0
test-gate-opt-in rc=0
Verdict: PASS
```

## Negative controls

These pass on the fix, so it does not over-refuse. All but the array-element control also pass on `origin/master`:

- Multi-line strings, `'''` strings, nested arrays (one element alone on its line) and a Vietnamese comment outside the merge table still read (exit 0).
- A `[repo]` multi-line string array, `[dev]` keys, `[ dev.merge ] # comment` and `command = "..." # trailing` still read (exit 0).
- `[dev] merge_own_prs`, `[dev] lane = { ... }`, `[dev.wrap] merge_own_prs`, `[dev.gate] merge_gate` are not the merge table (exit 1).
- 38 real `kit.toml` / `.kit.toml` files (the local workspace plus the operator file) give the same check result before and after the fix, for both sections.
- Array element lines such as regex lists (`"^\\s*foo="`, `"merge=x"`, `"command = not a key"` inside `[dev.merge]`) are not read as keys (exit 0). `origin/master` refuses this fixture (its two rows are in the red run), so the fix reads it where master refused it.
- Over-refusal this fix adds: any value on `dev` itself (`dev = { wrap = ... }`), a quoted header anywhere (`[tool."a b"]`), a multi-line string or nested multi-line array inside the merge table. The kit writes plain `[dev.*]` tables, so these forms do not occur in its own configs. A legacy `[wrap]` + `merge = ...` was already refused and is unchanged.

## Guard settings

`[dev.gate]` keys have one reader (`_kit_project_get`) and no separate check, so there is no check/runner split. An unreadable form (`[dev]` + `gate.proof_of_done = false`) reads as empty and falls back to the operator or kit-root value, so a project cannot switch a gate off that way. The same form cannot switch a default-off gate on either: it is ignored without a warning. That is a separate config-UX gap, outside this file.

## Rollback

Revert the commit. The change touches one shell function and its tests, and stores no state.
128 changes: 104 additions & 24 deletions lib/config/merge-command.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@
# value without a backslash or a `#`. The dotted form (`merge.command = ...`), an inline table
# (`merge = { ... }`), `[[merge]]`, a triple-quoted or single-quoted value, a repeated key and a
# `#` inside the value are all REFUSED (exit 2): the reader cannot read them, and unset would
# mean the plain merge. An empty value is a plain "unset".
# mean the plain merge. So are, anywhere in the file, a header that is not plain (quoted,
# escaped, or a multi-line array line opening with `[`), a multi-line string, a `\` in a key and
# a blank other than space or tab; and, in kit.toml, a key under a parent table that lands in
# `dev.merge` (`[dev]` + `merge.command`) or any value on `dev` itself (`dev = { ... }`). An empty
# value is a plain "unset".
# The value must also be one simple command: no `;` `&` `|` `<` `>` backtick `$(` or newline.
# That denylist guards against mistakes. It is NOT a security boundary: whoever can write the
# value can also write the script it names. The PR number is appended as the last argument.
Expand Down Expand Up @@ -95,42 +99,118 @@ _kit_mc_base_ref() {
# _kit_mc_shape <toml-file> [<section>] -- exit 0 when the file names <section> (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".
# 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
awk -v q="'" -v sec="$sec" '
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 }
/^[ \t]*#/ { next }
{
line = $0
h = line; sub(/[ \t]+#.*$/, "", h); gsub(/[ \t\r]/, "", h); gsub(/"/, "", h); gsub(q, "", h)
# 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
}
h ~ /^\[/ {
if (h == "[[" sec "]]" || index(h, "[[" sec ".") == 1) bad()
if (h == "[" sec "]") { if (seen++) bad(); insec = 1; ncmd = 0; next }
if (index(h, "[" sec ".") == 1) bad()
insec = 0; next
BEGIN {
key = "[ \t]*[A-Za-z0-9_-]+([ \t]*[.][ \t]*[A-Za-z0-9_-]+)*[ \t]*"
header = "^[ \t]*([[]" key "[]]|[[][[]" key "[]][]])[ \t]*(#.*)?$"
}
{
k = line; sub(/^[ \t]+/, "", k)
if (!insec) {
if (sec == "merge") {
if (firstword(k) == "merge" && k ~ /[.=]/) bad()
} else if (index(k, "=")) {
lh = lhs_norm(k)
if (lh == sec || index(lh, sec ".") == 1) bad()
}
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()
sub(/\r$/, "", line)
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"
}
Expand All @@ -144,7 +224,7 @@ _kit_mc_resolve() {
sec="${pair%%:*}"; f="${pair#*:}"
[ -s "$f" ] || continue
_kit_mc_shape "$f" "$sec" || { echo "merge command: $f names [$sec] in a form the kit cannot read (use a plain [$sec] table with command = \"...\", no # or backslash in the value); refusing to merge" >&2; return 2; }
v="$(_kit_toml_get "$f" "$sec" command)"
v="$(LC_ALL=C _kit_toml_get "$f" "$sec" command)"
[ -z "$v" ] || { printf '%s' "$v"; return 0; }
done
return 1
Expand Down Expand Up @@ -195,8 +275,8 @@ kit_merge_command() {
rm -f "$tmpk" "$tmpl"
v="$(_kit_mc_resolve /nonexistent /nonexistent)"; r=$?
if [ "$r" -ne 1 ]; then _kit_mc_finish "$r" "$v"; return $?; fi
if [ -n "$(_kit_toml_get "$repo/kit.toml" dev.merge command)" ] || ! _kit_mc_shape "$repo/kit.toml" dev.merge \
|| [ -n "$(_kit_toml_get "$repo/.kit.toml" merge command)" ] || ! _kit_mc_shape "$repo/.kit.toml" merge; then
if [ -n "$(LC_ALL=C _kit_toml_get "$repo/kit.toml" dev.merge command)" ] || ! _kit_mc_shape "$repo/kit.toml" dev.merge \
|| [ -n "$(LC_ALL=C _kit_toml_get "$repo/.kit.toml" merge command)" ] || ! _kit_mc_shape "$repo/.kit.toml" merge; then
echo "merge command: $repo names [merge] but origin's default branch cannot be read to verify it (run git fetch); refusing to merge" >&2
return 2
fi
Expand Down
Loading
Loading