From 88a19209e454fc425670e1a2a4d9328964a36b29 Mon Sep 17 00:00:00 2001 From: Han Ngo Date: Sun, 6 Dec 2026 07:16:21 +0700 Subject: [PATCH 1/6] fix(config): make the merge shape check read headers as the reader does The shape check and the TOML reader in merge-command.sh named table headers differently and judged keys outside the merge table by their own text. Valid TOML such as [dev] + merge.command, a quoted header, or dev = { merge = ... } passed the check and read as unset, so the kit merged plain and skipped the project merge guard. The check now finds headers exactly as the reader does and judges keys by full path; each such form refuses (exit 2). Proof: docs/verification/merge-shape-differential.md. --- docs/verification/merge-shape-differential.md | 96 +++++++++++++++++++ lib/config/merge-command.sh | 55 ++++++----- tests/test-merge-command.sh | 47 ++++++++- 3 files changed, 172 insertions(+), 26 deletions(-) create mode 100644 docs/verification/merge-shape-differential.md diff --git a/docs/verification/merge-shape-differential.md b/docs/verification/merge-shape-differential.md new file mode 100644 index 00000000..662f0518 --- /dev/null +++ b/docs/verification/merge-shape-differential.md @@ -0,0 +1,96 @@ +# 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 check now finds and names a header exactly as the reader does: a raw line that opens with `[`, with everything from `#` dropped. It refuses a quoted, array or sub-table header that names the merge table. Outside the table it judges a key by its full path (current table plus key). So `[dev]` + `merge.command`, `[dev]` + `merge = {...}` and `dev = { merge = ... }` refuse like the dotted form. + +## Red run (new tests on the old code) + +``` +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) + PASS kit.toml: [dev] keys plus the plain [dev.merge] shape still read (exit 0) + PASS kit.toml: other dev keys that mention merge are not the merge table (exit 1) +test-merge-command: 144 passed, 13 FAILED of 157 +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 (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 + +Both pass before and after the fix, so the fix does not over-refuse: + +- `[dev]` keys plus `[ 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). +- The repo's own `kit.toml` and `.kit.toml` pass the check (shape rc 0). + +## 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. diff --git a/lib/config/merge-command.sh b/lib/config/merge-command.sh index 466365f0..4f0dd5da 100644 --- a/lib/config/merge-command.sh +++ b/lib/config/merge-command.sh @@ -46,7 +46,10 @@ # 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 a quoted header (`["merge"]`, `[dev."merge"]`) and, in kit.toml, a +# key under a parent table that lands in `dev.merge` (`[dev]` + `merge.command`, `dev = { merge +# = ... }`). The check finds headers exactly as the reader does, so the two never disagree on +# which lines belong to the merge table. 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. @@ -95,7 +98,10 @@ _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". +# parse and empty would read as "unset". A header is found and named exactly as _kit_toml_get does +# (the raw line opens with `[`, everything from `#` on dropped), so the check and the reader always +# agree on which lines are the merge table. A key outside it is judged by its full path (the +# current table plus the key), so `[dev]` + `merge.command` is caught like `dev.merge.command`. _kit_mc_shape() { local f="$1" sec="${2:-merge}" [ -s "$f" ] || return 0 @@ -104,33 +110,34 @@ _kit_mc_shape() { # 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) - } - 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 + # 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 } + { line = $0; sub(/\r$/, "", line) } + line ~ /^[ \t]*#/ { next } + line ~ /^[ \t]*\[/ { + hr = line; sub(/#.*/, "", hr); arr = (hr ~ /^[ \t]*\[[ \t]*\[/); gsub(/[][ \t]/, "", hr) + hn = hr; gsub(/"/, "", hn); gsub(q, "", hn) + if (hr == sec && !arr) { if (seen++) bad(); insec = 1; ncmd = 0; next } + # quoted, an array of tables, or a sub-table: the reader cannot read it + if (names_sec(hn)) bad() + insec = 0; cur = hn; next } - { - 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() - } - next - } + { k = line; sub(/^[ \t]+/, "", k) } + 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)) bad() + # an inline table on a parent (`dev = { merge = ... }`) that mentions the next level down + if (holds_sec(full)) { nxt = substr(sec, length(full) + 2); sub(/\..*$/, "", nxt); if (index(k, nxt)) bad() } } ' "$f" } diff --git a/tests/test-merge-command.sh b/tests/test-merge-command.sh index 2c47500f..9f875f48 100644 --- a/tests/test-merge-command.sh +++ b/tests/test-merge-command.sh @@ -211,6 +211,7 @@ case "\$*" in case "\${MC_SLUG_MODE:-}" in kit_set) printf '[dev.merge]\ncommand = "bash scripts/kit-merge.sh"\n'; exit 0 ;; kit_dotted) printf 'dev.merge.command = "bash scripts/kit-merge.sh"\n'; exit 0 ;; + kit_parent) printf '[dev]\nmerge.command = "bash scripts/kit-merge.sh"\n'; exit 0 ;; set|dotted|none|hidden) echo "gh: Not Found (HTTP 404)" >&2; exit 1 ;; *) echo "gh: Bad Gateway (HTTP 502)" >&2; exit 1 ;; esac ;; @@ -218,14 +219,14 @@ case "\$*" in case "\${MC_SLUG_MODE:-}" in set|kit_set) printf '[merge]\ncommand = "bash scripts/pr-merge.sh"\n'; exit 0 ;; dotted) printf 'merge.command = "bash scripts/pr-merge.sh"\n'; exit 0 ;; - none|hidden|kit_dotted) echo "gh: Not Found (HTTP 404)" >&2; exit 1 ;; + none|hidden|kit_dotted|kit_parent) echo "gh: Not Found (HTTP 404)" >&2; exit 1 ;; *) echo "gh: Bad Gateway (HTTP 502)" >&2; exit 1 ;; esac ;; *"contents/?ref="*) # the directory listing of the same ref: readable for "none"/"set"/"dotted", also a 404 for "hidden" (no contents access) case "\${MC_SLUG_MODE:-}" in none|set|dotted) printf 'README.md\nsrc\n'; exit 0 ;; - kit_dotted) printf 'README.md\nkit.toml\n'; exit 0 ;; + kit_dotted|kit_parent) printf 'README.md\nkit.toml\n'; exit 0 ;; esac echo "gh: Not Found (HTTP 404)" >&2; exit 1 ;; esac @@ -248,6 +249,8 @@ chk "slug: unified kit.toml [dev.merge] wins over .kit.toml [merge]" "$rc" chk_has "slug: unified kit.toml [dev.merge] value" "$cmd" "bash scripts/kit-merge.sh" PATH="$TMPD/shim:$PATH" MC_SLUG_MODE=kit_dotted kit_merge_command_slug owner/name main >/dev/null 2>&1; rc=$? chk "slug: unified kit.toml dotted dev.merge.command refuses (exit 2)" "$([ "$rc" -eq 2 ]; echo $?)" +PATH="$TMPD/shim:$PATH" MC_SLUG_MODE=kit_parent kit_merge_command_slug owner/name main >/dev/null 2>&1; rc=$? +chk "slug: kit.toml [dev] with merge.command refuses (exit 2), not unset" "$([ "$rc" -eq 2 ]; echo $?)" # Unified kit.toml [dev.merge] in a git repo beats .kit.toml [merge] R_UNI="$(mc_repo_toml uni '[merge] @@ -258,6 +261,46 @@ cmd="$(kit_merge_command "$R_UNI")"; rc=$? chk "repo: unified kit.toml [dev.merge] wins over .kit.toml [merge]" "$rc" chk_has "repo: unified kit.toml [dev.merge] value" "$cmd" "bash scripts/unified-merge.sh" +# mc_repo_kit -- a repo whose default branch commits exactly this kit.toml. +mc_repo_kit() { + local c; c="$(mc_repo "$1" no ok)" + printf '%s\n' "$2" > "$c/kit.toml" + git -C "$c" add -A; git -C "$c" commit -qm "chore: write the key"; git -C "$c" push -q origin main + printf '%s\n' "$c" +} + +# The shape check and the reader must agree on where the merge table is. Each row is valid TOML +# that names the command, but the reader reads it as unset (the kit would merge plain) or reads a +# line the check never looked at. Every one refuses (exit 2). +KFORMN=0 +while IFS='|' read -r file label toml; do + KFORMN=$((KFORMN+1)); toml="$(printf '%b' "$toml")" + if [ "$file" = kit ]; then RF="$(mc_repo_kit "kform-$KFORMN" "$toml")"; else RF="$(mc_repo_toml "lform-$KFORMN" "$toml")"; fi + fo="$(kit_merge_command "$RF" 2>&1)"; rc=$? + chk "check/reader differential ($file: $label): refuses (exit 2)" "$([ "$rc" -eq 2 ]; echo $?)" +done <<'KFORMS' +kit|[dev] then a dotted merge.command|[dev]\nmerge.command = "bash scripts/pr-merge.sh" +kit|[dev] then an inline merge table|[dev]\nmerge = { command = "bash scripts/pr-merge.sh" } +kit|top-level dev inline table|dev = { merge = { command = "bash scripts/pr-merge.sh" } } +kit|quoted header ["dev".merge]|["dev".merge]\ncommand = "bash scripts/pr-merge.sh" +kit|quoted header [dev."merge"]|[dev."merge"]\ncommand = "bash scripts/pr-merge.sh" +kit|header comment with no blank, # in the value|[dev.merge]#note\ncommand = "bash scripts/pr-merge.sh #approved" +kit|header comment with no blank, single-quoted value|[dev.merge]#note\ncommand = 'bash scripts/pr-merge.sh' +kit|a quoted key that looks like a header|[dev.merge]\n"[x]" = 1\ncommand = 'bash scripts/pr-merge.sh' +legacy|quoted header ["merge"]|["merge"]\ncommand = "bash scripts/pr-merge.sh" +legacy|single-quoted header ['merge']|['merge']\ncommand = "bash scripts/pr-merge.sh" +legacy|header comment with no blank, # in the value|[merge]#note\ncommand = "bash scripts/pr-merge.sh #approved" +legacy|a quoted key that looks like a header|[merge]\n"[x]" = 1\ncommand = 'bash scripts/pr-merge.sh' +KFORMS +# Negative controls: the readable shape and unrelated dev keys keep their meaning. +RG="$(mc_repo_kit kform-good $'[dev]\nprofile = "x"\n\n[ dev.merge ] # the one shape\ncommand = "bash scripts/pr-merge.sh" # trailing')" +fo="$(kit_merge_command "$RG" 2>&1)"; rc=$? +chk "kit.toml: [dev] keys plus the plain [dev.merge] shape still read (exit 0)" "$rc" +chk_has "...and give the value" "$fo" "bash scripts/pr-merge.sh" +RG="$(mc_repo_kit kform-other $'[dev]\nmerge_own_prs = true\nlane = { normal = "x" }\n[dev.wrap]\nmerge_own_prs = true\n[dev.gate]\nmerge_gate = false')" +kit_merge_command "$RG" >/dev/null 2>&1; rc=$? +chk "kit.toml: other dev keys that mention merge are not the merge table (exit 1)" "$([ "$rc" -eq 1 ]; echo $?)" + # =========================================================================== echo "=== wrap merge --apply: the project command merges, the plain merge is never called ===" # =========================================================================== From cf50f76920bae9526204e85bedb6bd084f49d674 Mon Sep 17 00:00:00 2001 From: Han Ngo Date: Sun, 6 Dec 2026 07:33:28 +0700 Subject: [PATCH 2/6] fix(config): refuse every header and key form the merge reader misplaces An independent review found more valid TOML that passed the shape check and read as unset: nested-array lines, multi-line strings, escaped keys and headers, a two-line dev inline table, form-feed blanks. The check now accepts a line opening with [ only as a plain header, refuses multi-line strings, odd blanks and a backslash in a key, and refuses any value on a parent of the merge table. --- docs/verification/merge-shape-differential.md | 25 +++++++--- lib/config/merge-command.sh | 47 ++++++++++++------- tests/test-merge-command.sh | 14 +++++- 3 files changed, 61 insertions(+), 25 deletions(-) diff --git a/docs/verification/merge-shape-differential.md b/docs/verification/merge-shape-differential.md index 662f0518..e89b585a 100644 --- a/docs/verification/merge-shape-differential.md +++ b/docs/verification/merge-shape-differential.md @@ -22,10 +22,14 @@ The key is read from the default branch, so a PR cannot use this for its own mer ## Fix -The check now finds and names a header exactly as the reader does: a raw line that opens with `[`, with everything from `#` dropped. It refuses a quoted, array or sub-table header that names the merge table. Outside the table it judges a key by its full path (current table plus key). So `[dev]` + `merge.command`, `[dev]` + `merge = {...}` and `dev = { merge = ... }` refuse like the dotted form. +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) and refuses anything else: a quoted or escaped header, or a nested-array line. It also refuses, anywhere in the file, what the reader would misplace: a multi-line string (it can hide a header or hold one the reader enters), a blank other than space or tab, and a `\` in a key. Outside the merge table a key is judged by its full path (current table plus key), and any value on `dev` itself refuses. So `[dev]` + `merge.command`, `[dev]` + `merge = {...}` and `dev = { ... }` refuse like the dotted form. + +An independent review of the first version found more fail-open input: nested-array lines, multi-line strings, `\u` escapes in keys and headers, a `dev = {` inline table spanning lines, and form-feed blanks. It also found one regression: a carriage return inside a header. All of them are now test rows. The reviewer's 44-case corpus gives identical results under macOS awk, GNU Awk 5.4.2 and mawk 1.3.4. Every input the check accepts now reads the same in the reader and in `tomllib`. ## 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: the old code refused that form. It guards against the regression the review found in the first version of this fix. + ``` Command: bash tests/run-all.sh --only test-merge-command Exit: 1 @@ -43,9 +47,16 @@ Output: 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) - PASS kit.toml: [dev] keys plus the plain [dev.merge] shape still read (exit 0) - PASS kit.toml: other dev keys that mention merge are not the merge table (exit 1) -test-merge-command: 144 passed, 13 FAILED of 157 + 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) +test-merge-command: 145 passed, 22 FAILED of 167 Verdict: RED, as expected ``` @@ -83,9 +94,11 @@ Verdict: PASS Both pass before and after the fix, so the fix does not over-refuse: -- `[dev]` keys plus `[ dev.merge ] # comment` and `command = "..." # trailing` 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). -- The repo's own `kit.toml` and `.kit.toml` pass the check (shape rc 0). +- The repo's own `kit.toml` and `.kit.toml` and the operator `kit.toml` pass the check for both sections. +- 37 real `kit.toml` / `.kit.toml` files under the local workspace give the same check result before and after the fix, for both sections. +- Over-refusal this fix adds: any value on `dev` itself (`dev = { wrap = ... }`). The kit writes `[dev.*]` tables, so this form does not occur. A legacy `[wrap]` + `merge = ...` was already refused and is unchanged. ## Guard settings diff --git a/lib/config/merge-command.sh b/lib/config/merge-command.sh index 4f0dd5da..55760bff 100644 --- a/lib/config/merge-command.sh +++ b/lib/config/merge-command.sh @@ -46,10 +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. So are a quoted header (`["merge"]`, `[dev."merge"]`) and, in kit.toml, a -# key under a parent table that lands in `dev.merge` (`[dev]` + `merge.command`, `dev = { merge -# = ... }`). The check finds headers exactly as the reader does, so the two never disagree on -# which lines belong to the merge table. 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. @@ -98,10 +99,13 @@ _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". A header is found and named exactly as _kit_toml_get does -# (the raw line opens with `[`, everything from `#` on dropped), so the check and the reader always -# agree on which lines are the merge table. A key outside it is judged by its full path (the -# current table plus the key), so `[dev]` + `merge.command` is caught like `dev.merge.command`. +# 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 the rest: a quoted or escaped header, or a line of a multi-line +# array. It also refuses what the reader would misplace: a multi-line string (it can hide a header +# or hold one the reader enters), 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 = {`). _kit_mc_shape() { local f="$1" sec="${2:-merge}" [ -s "$f" ] || return 0 @@ -113,17 +117,28 @@ _kit_mc_shape() { # 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 } - { line = $0; sub(/\r$/, "", line) } + 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:]]/ || index(line, "\"\"\"") || index(line, q q q)) bad() + } line ~ /^[ \t]*#/ { next } line ~ /^[ \t]*\[/ { + if (line !~ header) bad() hr = line; sub(/#.*/, "", hr); arr = (hr ~ /^[ \t]*\[[ \t]*\[/); gsub(/[][ \t]/, "", hr) - hn = hr; gsub(/"/, "", hn); gsub(q, "", hn) if (hr == sec && !arr) { if (seen++) bad(); insec = 1; ncmd = 0; next } - # quoted, an array of tables, or a sub-table: the reader cannot read it - if (names_sec(hn)) bad() - insec = 0; cur = hn; next + # an array of tables or a sub-table: the reader cannot read it + if (names_sec(hr)) bad() + insec = 0; cur = hr; next + } + { + k = line; sub(/^[ \t]+/, "", k) + if (index(k, "=")) { lk = k; sub(/=.*$/, "", lk); if (index(lk, "\\")) bad() } } - { k = line; sub(/^[ \t]+/, "", k) } insec { if (firstword(k) == "command" && k ~ /[.=]/) { if (++ncmd > 1) bad() @@ -135,9 +150,7 @@ _kit_mc_shape() { 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)) bad() - # an inline table on a parent (`dev = { merge = ... }`) that mentions the next level down - if (holds_sec(full)) { nxt = substr(sec, length(full) + 2); sub(/\..*$/, "", nxt); if (index(k, nxt)) bad() } + if (names_sec(full) || holds_sec(full)) bad() } ' "$f" } diff --git a/tests/test-merge-command.sh b/tests/test-merge-command.sh index 9f875f48..fb336ec3 100644 --- a/tests/test-merge-command.sh +++ b/tests/test-merge-command.sh @@ -291,11 +291,21 @@ legacy|quoted header ["merge"]|["merge"]\ncommand = "bash scripts/pr-merge.sh" legacy|single-quoted header ['merge']|['merge']\ncommand = "bash scripts/pr-merge.sh" legacy|header comment with no blank, # in the value|[merge]#note\ncommand = "bash scripts/pr-merge.sh #approved" legacy|a quoted key that looks like a header|[merge]\n"[x]" = 1\ncommand = 'bash scripts/pr-merge.sh' +kit|a nested-array line inside the table|[dev.merge]\nchecks = [\n ["lint", "test"],\n]\ncommand = "bash scripts/pr-merge.sh" +kit|a nested-array line, then [dev] merge.command|[dev]\nx = [\n ["a"],\n]\nmerge.command = "bash scripts/pr-merge.sh" +kit|a multi-line string holding a header|[dev.merge]\nnote = """\n[x]\n"""\ncommand = "bash scripts/pr-merge.sh" +kit|an escaped key under [dev]|[dev]\n"m\\u0065rge".command = "bash scripts/pr-merge.sh" +kit|an escaped header|["dev"."m\\u0065rge"]\ncommand = "bash scripts/pr-merge.sh" +kit|a dev inline table over two lines|dev = { x = [\n 1,\n], merge = { command = "bash scripts/pr-merge.sh" } } +kit|a carriage return inside the header|[dev.merge\r]\ncommand = 'bash scripts/pr-merge.sh' +legacy|a form feed before the key|[merge]\n\fcommand = 'bash scripts/pr-merge.sh' +legacy|a multi-line string holding a command|[merge]\nnote = """\ncommand = "bash scripts/pr-merge.sh"\n""" +legacy|an escaped header|["m\\u0065rge"]\ncommand = "bash scripts/pr-merge.sh" KFORMS # Negative controls: the readable shape and unrelated dev keys keep their meaning. -RG="$(mc_repo_kit kform-good $'[dev]\nprofile = "x"\n\n[ dev.merge ] # the one shape\ncommand = "bash scripts/pr-merge.sh" # trailing')" +RG="$(mc_repo_kit kform-good $'[repo]\ndoor_paths = [\n "hooks/",\n "lib/gate/",\n]\n[dev]\nprofile = "x"\n\n[ dev.merge ] # the one shape\ncommand = "bash scripts/pr-merge.sh" # trailing')" fo="$(kit_merge_command "$RG" 2>&1)"; rc=$? -chk "kit.toml: [dev] keys plus the plain [dev.merge] shape still read (exit 0)" "$rc" +chk "kit.toml: a multi-line string array, [dev] keys and the plain [dev.merge] shape still read (exit 0)" "$rc" chk_has "...and give the value" "$fo" "bash scripts/pr-merge.sh" RG="$(mc_repo_kit kform-other $'[dev]\nmerge_own_prs = true\nlane = { normal = "x" }\n[dev.wrap]\nmerge_own_prs = true\n[dev.gate]\nmerge_gate = false')" kit_merge_command "$RG" >/dev/null 2>&1; rc=$? From 281b5bc32da94a99f26ece2ad749e76af0935d2b Mon Sep 17 00:00:00 2001 From: Han Ngo Date: Sun, 6 Dec 2026 07:46:01 +0700 Subject: [PATCH 3/6] fix(config): track arrays and strings in the merge shape check A second review found a nested array element alone on its line still read as a header, and that refusing every triple quote or nested array blocked plain configs. The check now tracks multi-line arrays and strings and refuses a stray [ line only where the reader would leave or enter the merge table. Check and reader run under LC_ALL=C so a locale cannot change what counts as a blank. --- docs/verification/merge-shape-differential.md | 33 +++++++++--- lib/config/merge-command.sh | 54 ++++++++++++++----- tests/test-merge-command.sh | 10 ++++ 3 files changed, 79 insertions(+), 18 deletions(-) diff --git a/docs/verification/merge-shape-differential.md b/docs/verification/merge-shape-differential.md index e89b585a..5c77e534 100644 --- a/docs/verification/merge-shape-differential.md +++ b/docs/verification/merge-shape-differential.md @@ -22,13 +22,28 @@ The key is read from the default branch, so a PR cannot use this for its own mer ## 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) and refuses anything else: a quoted or escaped header, or a nested-array line. It also refuses, anywhere in the file, what the reader would misplace: a multi-line string (it can hide a header or hold one the reader enters), a blank other than space or tab, and a `\` in a key. Outside the merge table a key is judged by its full path (current table plus key), and any value on `dev` itself refuses. So `[dev]` + `merge.command`, `[dev]` + `merge = {...}` and `dev = { ... }` refuse like the dotted form. +The reader treats every line that opens with `[` as a header. The check now: -An independent review of the first version found more fail-open input: nested-array lines, multi-line strings, `\u` escapes in keys and headers, a `dev = {` inline table spanning lines, and form-feed blanks. It also found one regression: a carriage return inside a header. All of them are now test rows. The reviewer's 44-case corpus gives identical results under macOS awk, GNU Awk 5.4.2 and mawk 1.3.4. Every input the check accepts now reads the same in the reader and in `tomllib`. +- accepts such a line only as a plain header (bare-key segments, blanks, an optional `#` comment); +- tracks multi-line arrays and strings, and refuses a `[` line inside one 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 (61+ inputs), 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` | + +In the final corpus every input the check accepts reads the same in the reader and in `tomllib`, with identical results across the three awks. ## 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: the old code refused that form. It guards against the regression the review found in the first version of this fix. +`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 @@ -56,7 +71,13 @@ Output: 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) -test-merge-command: 145 passed, 22 FAILED of 167 + 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) +test-merge-command: 147 passed, 28 FAILED of 175 Verdict: RED, as expected ``` @@ -94,10 +115,10 @@ Verdict: PASS Both pass before and after the fix, so the fix does not over-refuse: +- 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). -- The repo's own `kit.toml` and `.kit.toml` and the operator `kit.toml` pass the check for both sections. -- 37 real `kit.toml` / `.kit.toml` files under the local workspace give the same check result before and after the fix, for both sections. +- 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. - Over-refusal this fix adds: any value on `dev` itself (`dev = { wrap = ... }`). The kit writes `[dev.*]` tables, so this form does not occur. A legacy `[wrap]` + `merge = ...` was already refused and is unchanged. ## Guard settings diff --git a/lib/config/merge-command.sh b/lib/config/merge-command.sh index 55760bff..ad6cc3ea 100644 --- a/lib/config/merge-command.sh +++ b/lib/config/merge-command.sh @@ -100,16 +100,17 @@ _kit_mc_base_ref() { # `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 the rest: a quoted or escaped header, or a line of a multi-line -# array. It also refuses what the reader would misplace: a multi-line string (it can hide a header -# or hold one the reader enters), 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 = {`). +# 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. 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 } @@ -117,6 +118,14 @@ _kit_mc_shape() { # 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() + } + function count(s, t, n, i) { n = 0; while ((i = index(s, t)) > 0) { n++; s = substr(s, i + length(t)) } return n } BEGIN { key = "[ \t]*[A-Za-z0-9_-]+([ \t]*[.][ \t]*[A-Za-z0-9_-]+)*[ \t]*" header = "^[ \t]*([[]" key "[]]|[[][[]" key "[]][]])[ \t]*(#.*)?$" @@ -124,12 +133,33 @@ _kit_mc_shape() { { line = $0; sub(/\r$/, "", line) t = line; gsub(/[ \t]/, "", t) - if (t ~ /[[:space:]]/ || index(line, "\"\"\"") || index(line, q q q)) bad() + if (t ~ /[[:space:]]/) bad() + nml = count(line, "\"\"\"") + count(line, q q q) + if (insec && nml) bad() + if (inml) { + if (line ~ /^[ \t]*\[/) stray(line) + if (nml % 2) inml = 0 + next + } + # a full-line comment opens nothing; a `#` on a line that opens one may hide where it starts + if (nml % 2 && line !~ /^[ \t]*#/) { if (index(line, "#")) bad(); inml = 1 } } line ~ /^[ \t]*#/ { next } + { + # open array brackets, with strings and the comment taken out + s = line; gsub(/\\./, "", s); gsub(/"[^"]*"/, "", s); gsub(q "[^" q "]*" q, "", s); sub(/#.*/, "", s) + d = gsub(/\[/, "[", s) - gsub(/\]/, "]", s) + if (depth > 0) { + if (line ~ /^[ \t]*\[/) stray(line) + depth += d; if (depth < 0) depth = 0 + if (line ~ /^[ \t]*\[/) next + } else if (line !~ /^[ \t]*\[/) { + depth += d; if (depth < 0) depth = 0 + } + } line ~ /^[ \t]*\[/ { if (line !~ header) bad() - hr = line; sub(/#.*/, "", hr); arr = (hr ~ /^[ \t]*\[[ \t]*\[/); gsub(/[][ \t]/, "", hr) + 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 if (names_sec(hr)) bad() @@ -164,7 +194,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 @@ -215,8 +245,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 diff --git a/tests/test-merge-command.sh b/tests/test-merge-command.sh index fb336ec3..6f256f4e 100644 --- a/tests/test-merge-command.sh +++ b/tests/test-merge-command.sh @@ -301,6 +301,12 @@ kit|a carriage return inside the header|[dev.merge\r]\ncommand = 'bash scripts/p legacy|a form feed before the key|[merge]\n\fcommand = 'bash scripts/pr-merge.sh' legacy|a multi-line string holding a command|[merge]\nnote = """\ncommand = "bash scripts/pr-merge.sh"\n""" legacy|an escaped header|["m\\u0065rge"]\ncommand = "bash scripts/pr-merge.sh" +kit|a nested element alone on its line inside the table|[dev.merge]\nchecks = [\n [1]\n]\ncommand = "bash scripts/pr-merge.sh" +kit|a nested element alone on its line, then [dev] merge.command|[dev]\nx = [\n [1]\n]\nmerge.command = "bash scripts/pr-merge.sh" +kit|a comment holding a triple quote, then dev inline table|# use """ here\ndev = { merge = { command = "bash scripts/pr-merge.sh" } } +kit|an inline comment opening a triple quote|[x]\na = 1 # """\n[dev]\nmerge.command = "bash scripts/pr-merge.sh" +kit|a multi-line string in another table holding the header|[project]\nd = """\n[dev.merge]\n"""\ncommand = "bash scripts/pr-merge.sh" +legacy|a nested date element alone on its line|[merge]\nchecks = [\n [2024-01-01]\n]\ncommand = "bash scripts/pr-merge.sh" KFORMS # Negative controls: the readable shape and unrelated dev keys keep their meaning. RG="$(mc_repo_kit kform-good $'[repo]\ndoor_paths = [\n "hooks/",\n "lib/gate/",\n]\n[dev]\nprofile = "x"\n\n[ dev.merge ] # the one shape\ncommand = "bash scripts/pr-merge.sh" # trailing')" @@ -310,6 +316,10 @@ chk_has "...and give the value" "$fo" "bash scripts/pr-merge.sh" RG="$(mc_repo_kit kform-other $'[dev]\nmerge_own_prs = true\nlane = { normal = "x" }\n[dev.wrap]\nmerge_own_prs = true\n[dev.gate]\nmerge_gate = false')" kit_merge_command "$RG" >/dev/null 2>&1; rc=$? chk "kit.toml: other dev keys that mention merge are not the merge table (exit 1)" "$([ "$rc" -eq 1 ]; echo $?)" +RG="$(mc_repo_kit kform-elsewhere $'# ghi chú: cài đặt và à\n[project]\ndescription = """\nmulti [x]\n"""\npattern = \'\'\'a|b\'\'\'\n[dev.lanes]\nmatrix = [\n ["a", "b"],\n [1]\n]\n[dev.merge]\ncommand = "bash scripts/pr-merge.sh" # à')" +fo="$(kit_merge_command "$RG" 2>&1)"; rc=$? +chk "kit.toml: multi-line strings, nested arrays and non-ASCII comments outside the merge table still read (exit 0)" "$rc" +chk_has "...and give the value" "$fo" "bash scripts/pr-merge.sh" # =========================================================================== echo "=== wrap merge --apply: the project command merges, the plain merge is never called ===" From 7316d77b7d4fb5baaaa6ee47a81d7ee67bb0af98 Mon Sep 17 00:00:00 2001 From: Han Ngo Date: Sun, 6 Dec 2026 07:57:45 +0700 Subject: [PATCH 4/6] fix(config): scan TOML strings left to right in the merge check A third review showed quote counting cannot follow TOML strings: a triple quote inside a one-line string, mixed delimiters, a backslash ending a literal string or a double quote inside one still hid a key or a header from the check. One left-to-right scan per line now tracks basic, literal and both multi-line string kinds, comments, and array depth. --- docs/verification/merge-shape-differential.md | 13 +++- lib/config/merge-command.sh | 60 +++++++++++++------ tests/test-merge-command.sh | 5 ++ 3 files changed, 58 insertions(+), 20 deletions(-) diff --git a/docs/verification/merge-shape-differential.md b/docs/verification/merge-shape-differential.md index 5c77e534..3c54af30 100644 --- a/docs/verification/merge-shape-differential.md +++ b/docs/verification/merge-shape-differential.md @@ -25,19 +25,21 @@ The key is read from the default branch, so a PR cannot use this for its own mer 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); -- tracks multi-line arrays and strings, and refuses a `[` line inside one only where the reader would leave or enter the merge table by mistake (inside the table, or naming the table or a parent); +- 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 (61+ inputs), under macOS awk, GNU Awk 5.4.2 and mawk 1.3.4. +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 | In the final corpus every input the check accepts reads the same in the reader and in `tomllib`, with identical results across the three awks. @@ -77,7 +79,12 @@ Output: 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) -test-merge-command: 147 passed, 28 FAILED of 175 + 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) +test-merge-command: 147 passed, 33 FAILED of 180 Verdict: RED, as expected ``` diff --git a/lib/config/merge-command.sh b/lib/config/merge-command.sh index ad6cc3ea..489806cd 100644 --- a/lib/config/merge-command.sh +++ b/lib/config/merge-command.sh @@ -102,9 +102,10 @@ _kit_mc_base_ref() { # 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. 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 +# 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() { @@ -125,7 +126,35 @@ _kit_mc_shape() { h = rname(l); gsub(/"/, "", h); gsub(q, "", h) if (insec || names_sec(h) || holds_sec(h)) bad() } - function count(s, t, n, i) { n = 0; while ((i = index(s, t)) > 0) { n++; s = substr(s, i + length(t)) } return n } + # 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]*(#.*)?$" @@ -134,26 +163,23 @@ _kit_mc_shape() { line = $0; sub(/\r$/, "", line) t = line; gsub(/[ \t]/, "", t) if (t ~ /[[:space:]]/) bad() - nml = count(line, "\"\"\"") + count(line, q q q) - if (insec && nml) bad() - if (inml) { - if (line ~ /^[ \t]*\[/) stray(line) - if (nml % 2) inml = 0 + 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 } - # a full-line comment opens nothing; a `#` on a line that opens one may hide where it starts - if (nml % 2 && line !~ /^[ \t]*#/) { if (index(line, "#")) bad(); inml = 1 } } line ~ /^[ \t]*#/ { next } { - # open array brackets, with strings and the comment taken out - s = line; gsub(/\\./, "", s); gsub(/"[^"]*"/, "", s); gsub(q "[^" q "]*" q, "", s); sub(/#.*/, "", s) - d = gsub(/\[/, "[", s) - gsub(/\]/, "]", s) + d = scan(line) if (depth > 0) { - if (line ~ /^[ \t]*\[/) stray(line) + if (opens) stray(line) depth += d; if (depth < 0) depth = 0 - if (line ~ /^[ \t]*\[/) next - } else if (line !~ /^[ \t]*\[/) { + if (opens) next + } else if (!opens) { depth += d; if (depth < 0) depth = 0 } } diff --git a/tests/test-merge-command.sh b/tests/test-merge-command.sh index 6f256f4e..abca04c0 100644 --- a/tests/test-merge-command.sh +++ b/tests/test-merge-command.sh @@ -307,6 +307,11 @@ kit|a comment holding a triple quote, then dev inline table|# use """ here\ndev kit|an inline comment opening a triple quote|[x]\na = 1 # """\n[dev]\nmerge.command = "bash scripts/pr-merge.sh" kit|a multi-line string in another table holding the header|[project]\nd = """\n[dev.merge]\n"""\ncommand = "bash scripts/pr-merge.sh" legacy|a nested date element alone on its line|[merge]\nchecks = [\n [2024-01-01]\n]\ncommand = "bash scripts/pr-merge.sh" +kit|a triple single quote inside a one-line string|[dev]\na = "'''"\nmerge.command = "bash scripts/pr-merge.sh" +kit|a triple single quote inside a multi-line basic string|[dev]\na = """\n'''\n"""\nmerge.command = "bash scripts/pr-merge.sh" +kit|a comment with triples on the closing line|[dev]\na = """\ntext\n""" # don'''t\nmerge.command = "bash scripts/pr-merge.sh" +kit|a literal string ending in a backslash before a bracket string|[dev.merge]\nchecks = ['x\\', ']',\n [1]\n]\ncommand = "bash scripts/pr-merge.sh" +kit|a double quote inside a literal string|[dev.merge]\nchecks = ['a"b', "]",\n [1]\n]\ncommand = "bash scripts/pr-merge.sh" KFORMS # Negative controls: the readable shape and unrelated dev keys keep their meaning. RG="$(mc_repo_kit kform-good $'[repo]\ndoor_paths = [\n "hooks/",\n "lib/gate/",\n]\n[dev]\nprofile = "x"\n\n[ dev.merge ] # the one shape\ncommand = "bash scripts/pr-merge.sh" # trailing')" From 4fc84014b9d526dae8e305e3f5e257f0ae9400f3 Mon Sep 17 00:00:00 2001 From: Han Ngo Date: Sun, 6 Dec 2026 08:47:55 +0700 Subject: [PATCH 5/6] fix(config): refuse a parent array of tables, skip array lines A fresh fuzzing review found no fail-open in about 69,000 generated files. It found one low-impact case ([[dev]] before [dev.merge]) and a new over-refusal: array element lines with = or a backslash were judged as keys. Both fixed, with test rows. --- docs/verification/merge-shape-differential.md | 11 ++++++++--- lib/config/merge-command.sh | 8 ++++++-- tests/test-merge-command.sh | 5 +++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/docs/verification/merge-shape-differential.md b/docs/verification/merge-shape-differential.md index 3c54af30..56b55ffd 100644 --- a/docs/verification/merge-shape-differential.md +++ b/docs/verification/merge-shape-differential.md @@ -40,8 +40,9 @@ A fresh-context security reviewer attacked the fix with a growing corpus (129 in | 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 | -In the final corpus every input the check accepts reads the same in the reader and in `tomllib`, with identical results across the three awks. +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) @@ -84,7 +85,10 @@ Output: 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) -test-merge-command: 147 passed, 33 FAILED of 180 + 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 ``` @@ -126,7 +130,8 @@ Both pass before and after the fix, so the fix does not over-refuse: - 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. -- Over-refusal this fix adds: any value on `dev` itself (`dev = { wrap = ... }`). The kit writes `[dev.*]` tables, so this form does not occur. A legacy `[wrap]` + `merge = ...` was already refused and is unchanged. +- Array element lines such as regex lists (`"^\\s*foo="`, `"merge=x"`) are not read as keys (exit 0). +- 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 `[dev.*]` tables, so this form does not occur. A legacy `[wrap]` + `merge = ...` was already refused and is unchanged. ## Guard settings diff --git a/lib/config/merge-command.sh b/lib/config/merge-command.sh index 489806cd..ee33a209 100644 --- a/lib/config/merge-command.sh +++ b/lib/config/merge-command.sh @@ -174,6 +174,7 @@ _kit_mc_shape() { } line ~ /^[ \t]*#/ { next } { + cont = (depth > 0) d = scan(line) if (depth > 0) { if (opens) stray(line) @@ -187,11 +188,14 @@ _kit_mc_shape() { 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 - if (names_sec(hr)) bad() + # 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() } } diff --git a/tests/test-merge-command.sh b/tests/test-merge-command.sh index abca04c0..075c4cca 100644 --- a/tests/test-merge-command.sh +++ b/tests/test-merge-command.sh @@ -312,6 +312,7 @@ kit|a triple single quote inside a multi-line basic string|[dev]\na = """\n'''\n kit|a comment with triples on the closing line|[dev]\na = """\ntext\n""" # don'''t\nmerge.command = "bash scripts/pr-merge.sh" kit|a literal string ending in a backslash before a bracket string|[dev.merge]\nchecks = ['x\\', ']',\n [1]\n]\ncommand = "bash scripts/pr-merge.sh" kit|a double quote inside a literal string|[dev.merge]\nchecks = ['a"b', "]",\n [1]\n]\ncommand = "bash scripts/pr-merge.sh" +kit|an array of tables on the parent|[[dev]]\n[dev.merge]\ncommand = "bash scripts/pr-merge.sh" KFORMS # Negative controls: the readable shape and unrelated dev keys keep their meaning. RG="$(mc_repo_kit kform-good $'[repo]\ndoor_paths = [\n "hooks/",\n "lib/gate/",\n]\n[dev]\nprofile = "x"\n\n[ dev.merge ] # the one shape\ncommand = "bash scripts/pr-merge.sh" # trailing')" @@ -325,6 +326,10 @@ RG="$(mc_repo_kit kform-elsewhere $'# ghi chú: cài đặt và à\n[project]\nd fo="$(kit_merge_command "$RG" 2>&1)"; rc=$? chk "kit.toml: multi-line strings, nested arrays and non-ASCII comments outside the merge table still read (exit 0)" "$rc" chk_has "...and give the value" "$fo" "bash scripts/pr-merge.sh" +RG="$(mc_repo_kit kform-arrays $'[dev]\nignore = [\n "^\\\\s*foo=",\n "merge=x",\n]\n[dev.merge]\nallow = [\n "command = not a key",\n]\ncommand = "bash scripts/pr-merge.sh"')" +fo="$(kit_merge_command "$RG" 2>&1)"; rc=$? +chk "kit.toml: array element lines with = or a backslash are not keys (exit 0)" "$rc" +chk_has "...and give the value" "$fo" "bash scripts/pr-merge.sh" # =========================================================================== echo "=== wrap merge --apply: the project command merges, the plain merge is never called ===" From 730f9df889757ae5c09cf528975a956727c520d3 Mon Sep 17 00:00:00 2001 From: Han Ngo Date: Sun, 6 Dec 2026 09:00:17 +0700 Subject: [PATCH 6/6] docs(verification): correct which merge controls pass on master --- docs/verification/merge-shape-differential.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/verification/merge-shape-differential.md b/docs/verification/merge-shape-differential.md index 56b55ffd..117a8017 100644 --- a/docs/verification/merge-shape-differential.md +++ b/docs/verification/merge-shape-differential.md @@ -124,14 +124,14 @@ Verdict: PASS ## Negative controls -Both pass before and after the fix, so the fix does not over-refuse: +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"`) are not read as keys (exit 0). -- 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 `[dev.*]` tables, so this form does not occur. A legacy `[wrap]` + `merge = ...` was already refused and is unchanged. +- 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