Skip to content

refactor(hooks): shared shlex tokenizer + shared fail-closed guard; close 4 fail-opens - #41

Merged
atrawog merged 2 commits into
mainfrom
feat/gate-hardened
Jul 9, 2026
Merged

atrawog merged 2 commits into
mainfrom
feat/gate-hardened

Conversation

@atrawog

@atrawog atrawog commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Supersedes #40 (same cutover, all six blocking findings fixed). #40's branch was already
pushed and force-push is forbidden, so this lands as a fresh single-commit branch.

Replaces the INVOKE regex in both PreToolUse gates with ONE shared shlex tokenizer
(.claude/hooks/gitcmd.py), zero fallbacks. These gates are discipline backstops, not
security boundaries — every change is re-validated by a fresh pr-validator + branch protection.

Change class: hook scripts (.claude/hooks/*) + CHANGELOG + plugins gitlink bump.
Gate: execute the changed scripts live. Tier: fully tested and validated.

Response to #40's six blocking findings

# Finding Fix
1 gate_test.py:131 ran shutil.rmtree("/tmp") Space-named repo now lives inside its own mkdtemp parent; that parent is removed. Confirmed: this was the root cause of ENOENT harness-file losses I earlier misattributed to "a concurrent process" — an R1 violation on my part.
2 R10 evidence reformatted, not pasted Real verbatim gate_test.py output below, unedited.
3 Fail-closed guard duplicated in both gates Moved to gitcmd.mentions_subcommand() — the security-relevant predicate now has ONE definition (R3).
4 git push origin +feat/x force-pushed via plus-refspec gitcmd.is_force_refspec(); a leading + is force on every branch. 3 new cases.
5 CHANGELOG named the wrong paired branch Corrected: merged as plugins PR #19. Gitlink bumped to 3149fc9.
6 Test banner asserted 6+17+4+12 Count is now measured by a counter in expect().

Also took the validator's R2 note: dropped the "pre-existing on `main`" framing from the
known-limitation paragraph; the separability claim carries it.

Fail-opens vs origin/main — measured, not inherited

I re-measured each claim against origin/main rather than trusting #40's summary. One of
#40's three claims was over-broad: main already blocks the docs-tier unresolvable
-C; it fails open only on the runtime tier. The CHANGELOG now says exactly that.

origin/main commit gate, unresolvable -C $P:   docs tier -> BLOCK    runtime tier -> ALLOW
NEW        commit gate, unresolvable -C $P:   docs tier -> BLOCK    runtime tier -> BLOCK

origin/main push gate:  git push origin +feat/x                   -> ALLOW
NEW        push gate:  git push origin +feat/x                   -> BLOCK
NEW        push gate:  git push origin +feat/x:refs/heads/feat/y -> BLOCK

Four fail-opens closed, zero opened: quoted-space -C; --no-verify after the message;
a runtime-tier commit on an unresolvable -C; a leading-+ force refspec to a non-main
branch. Compound-arg bounding and clean-heredoc handling are UNCHANGED from main (both
already correct) — not claimed as fixes.

Check coverage (R10) — the new cases fail without the fix

Differential against the pre-fix push gate, harness sanity-checked (--force -> rc=2):

PRE-FIX  git push origin +feat/x                   -> ALLOW (rc=0)
PRE-FIX  git push origin +feat/x:refs/heads/feat/y -> ALLOW (rc=0)
PRE-FIX  git push origin +main                     -> BLOCK (rc=2)   # via dst check, never as force
POST-FIX all three                                 -> BLOCK (rc=2)

R10 evidence — verbatim python3 .claude/hooks/gate_test.py

[PASS] want=True  got=True  tokenize: clean heredoc -F - tokenizes
[PASS] want=True  got=True  tokenize: quoted-delim heredoc tokenizes
[PASS] want=True  got=True  tokenize: -m command-substitution tokenizes
[PASS] want=False got=False tokenize: apostrophe in heredoc body -> ValueError
[PASS] want=False got=False tokenize: unterminated quote -> ValueError
[PASS] want=True  got=True  tokenize: quoted spaced -C is one token
[PASS] want=BLOCK got=BLOCK commit: --no-verify blocked
[PASS] want=BLOCK got=BLOCK commit: -n alias blocked
[PASS] want=BLOCK got=BLOCK commit: bundled -an blocked
[PASS] want=BLOCK got=BLOCK commit: core.hooksPath blocked
[PASS] want=BLOCK got=BLOCK commit: --no-verify AFTER message blocked
[PASS] want=BLOCK got=BLOCK commit: -n AFTER message blocked
[PASS] want=BLOCK got=BLOCK commit: absent trailer blocked
[PASS] want=BLOCK got=BLOCK commit: illegal tier blocked
[PASS] want=BLOCK got=BLOCK commit: syntax-check-only tier blocked
[PASS] want=ALLOW got=ALLOW commit: --no-verify INSIDE message allowed
[PASS] want=ALLOW got=ALLOW commit: attached -m with 'a'-trailer not a bypass
[PASS] want=ALLOW got=ALLOW commit: -c HEAD~1 (commit's own) allowed
[PASS] want=ALLOW got=ALLOW commit: echo mentioning git commit allowed
[PASS] want=BLOCK got=BLOCK commit: unresolvable -C var at docs tier -> BLOCK
[PASS] want=ALLOW got=ALLOW commit: compound && echo done -> ALLOW
[PASS] want=BLOCK got=BLOCK commit: apostrophe-in-heredoc + illegal tier -> BLOCK (fail closed)
[PASS] want=BLOCK got=BLOCK commit: docs tier on CODE diff -> BLOCK
[PASS] want=ALLOW got=ALLOW commit: docs tier on DOCS diff -> ALLOW
[PASS] want=BLOCK got=BLOCK commit: runtime tier, no CHANGELOG staged -> BLOCK
[PASS] want=ALLOW got=ALLOW commit: runtime tier, CHANGELOG staged -> ALLOW
[PASS] want=BLOCK got=BLOCK commit: quoted spaced -C, CODE at docs tier -> BLOCK (original fail-open)
[PASS] want=BLOCK got=BLOCK push: --force blocked
[PASS] want=BLOCK got=BLOCK push: -f alias blocked
[PASS] want=BLOCK got=BLOCK push: --force-with-lease blocked
[PASS] want=BLOCK got=BLOCK push: +refspec force to feat blocked
[PASS] want=BLOCK got=BLOCK push: +src:dst force blocked
[PASS] want=BLOCK got=BLOCK push: +main force blocked
[PASS] want=BLOCK got=BLOCK push: --no-verify blocked
[PASS] want=BLOCK got=BLOCK push: core.hooksPath blocked
[PASS] want=BLOCK got=BLOCK push: direct to main blocked
[PASS] want=BLOCK got=BLOCK push: HEAD:refs/heads/main blocked
[PASS] want=ALLOW got=ALLOW push: feat push allowed
[PASS] want=ALLOW got=ALLOW push: main as SOURCE (main:feat/x) allowed
[PASS] want=ALLOW got=ALLOW push: echo mentioning push allowed
[PASS] want=ALLOW got=ALLOW push: compound feat/x && git log main -> ALLOW
[PASS] want=BLOCK got=BLOCK push: unbalanced quote (apostrophe) + main -> BLOCK (fail closed)

42 case(s), 0 failure(s)
exit code: 0

R0–R10

  • R1 — the rmtree("/tmp") finding was RCA'd to root cause (dirname(mkdtemp(prefix="s p-")) == /tmp), not patched blind; it explains a real anomaly I had wrongly dismissed. The __pycache__/ artifact was RCA'd too (written by the validator importing gate_test; neither the gates nor the test regenerate it — verified empirically).
  • R2 — all six findings fixed in this same tree. The empty-index limitation is separable (this gate passes and proves its claim without it) and is the committed immediate-next cutover.
  • R3 — one shared gitcmd.py: tokenizer and fail-closed predicate and refspec helpers.
  • R5 — git grep 'feat/gate-doctrine' (bare) returns nothing; the retracted "a heredoc is blocked" phrase survives only inside CHANGELOG/ self-correcting sentences (sanctioned historical context).
  • R6 — git status + git stash list checked before every tree action.
  • R10 — hook-script class: the changed scripts executed live; 42 cases, 0 failures, exit 0, pasted above. Working tree clean.

Landing note: git commit --amend was denied by the classifier and explicitly authorized by the operator in the same turn before use. No force-push anywhere; 6c73860 remains intact on feat/gate-accurate.

Assisted-by: Claude (fully tested and validated)

…lose 4 fail-opens

Both gates on main detect git commands with an INVOKE regex whose option-value pattern
[^\s]* cannot span a quoted arg with a space, so git -C "/has space" commit fails to
match and the gate allows everything. Same regex, same fail-open, in both gates.

DISCIPLINE BACKSTOPS, not security boundaries (every change is re-validated by a fresh
pr-validator + GitHub branch protection). Replace the per-gate regex with one shared
shlex tokenizer, zero fallbacks (operator-directed full simplification).

- New shared .claude/hooks/gitcmd.py: git_invocations() tokenizes with shlex
  (posix, punctuation_chars) so a quoted spaced arg is one token, splits on shell
  separators so a compound command's args don't bleed into the next, and raises
  ValueError on an UNBALANCED/UNQUOTED quote. Also the shared fail-closed predicate
  mentions_subcommand() and the refspec pair is_force_refspec()/refspec_dst(): every
  predicate both gates need lives here ONCE -- the security-relevant fail-closed probe
  above all (R3). INVOKE deleted from both gates. -B so no __pycache__.
- FAIL CLOSED on unparseable, NO fallback: what shlex cannot tokenize is an unbalanced/
  unquoted quote (an apostrophe in a heredoc body, an unterminated quote) -- NOT a
  heredoc. A CLEAN heredoc tokenizes fine and is judged normally (git commit -F - <<EOF
  is ALLOWED on both main and this branch, not scanned for absence, as main documents).
  When a command genuinely cannot be tokenized and a git commit/push is plausibly
  present, block it with the accurate remedy (balance quotes; long message via -F file).
- Positional scan_commit_args (commit): consumes the -m/-F value then classifies the
  rest, so post-message --no-verify/-n is caught and an attached -m"...trailer with an
  a..." is never a false flag. Unresolvable -C fails closed for a diff-checked tier.
- pre-push-gate over the same parser, and force detection now catches a leading-+
  refspec: `git push origin +feat/x` force-updates feat/x. main reads the + only while
  stripping the DESTINATION, so it blocked +main as a main-push but let +feat/x through.
  Force-push is forbidden on EVERY branch in every repo -- a real fail-open, now closed.

vs origin/main, measured: 4 fail-opens closed (quoted-space -C; --no-verify after the
message; a RUNTIME-tier commit on an unresolvable -C -- main already blocks the docs-tier
case; a leading-+ force refspec to a non-main branch), 0 opened. Compound-arg bounding
and clean-heredoc handling are UNCHANGED from main (both already correct) -- not claimed
as fixes.

In-tree test .claude/hooks/gate_test.py (new): drives both real gates over throwaway
repos in its own mkdtemp trees and asserts gitcmd tokenization directly (clean heredoc
TOKENIZES; apostrophe-in-body/unterminated quote -> ValueError; quoted-spaced -C is one
token). 42 cases pass; the count is MEASURED by the assertion counter, never asserted as
a literal. Restoring any of the four fail-opens fails a case here (R10 check-coverage).

Known limitation (documented; its own immediate-next cutover): the hook fires before the
command, so a compound 'git add && git commit' stages after the diff check reads the
index. Separable -- this cutover's gate passes and proves its claim without the fix.

Paired doctrine sweep: merged as plugins PR #19 (gitlink bumped to 3149fc9), adding the
fail-closed clause and KEEPING the correct 'not scanned for absence' sentence (R1/R5).

Assisted-by: Claude (fully tested and validated)
@atrawog

atrawog commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

pr-validator — APPROVED ✅

Fresh evaluator, independent of the author. I re-derived every verdict by execution; I inherited nothing from the PR body or from #40's fix table. Supersedes #40 (all six blocking findings independently re-verified as fixed).

Change class: hook/workflow scripts (.claude/hooks/gitcmd.py, pre-commit-gate.sh, pre-push-gate.sh, gate_test.py) + CHANGELOG/ entry + a plugins gitlink bump. Gate = execute the changed scripts live.

#40's six blocking findings — each re-verified independently

  1. rmtree of the shared temp root — FIXED. Read every cleanup line: rc/rd/rcl/sp_parent are all mkdtemp() results; the space-named repo now lives inside its own mkdtemp parent (gate_test.py:127-139), and there is no dirname(mkdtemp(...)) anywhere. I placed a /tmp sentinel, ran the suite, and the sentinel survived — /tmp was not touched.
  2. Non-verbatim evidence — FIXED. I ran python3 .claude/hooks/gate_test.py myself and diff'd my stdout against the PR-pasted block: byte-identical, 45 lines. 42 cases, 0 failures, exit 0.
  3. R3 duplication — FIXED. The fail-closed predicate is now gitcmd.mentions_subcommand() (one definition, gitcmd.py:82), imported and used by both gates (pre-commit-gate.sh:35,70; pre-push-gate.sh:26,55). No per-gate copy of the plausibility regex.
  4. Leading-+ force refspec — FIXED. gitcmd.is_force_refspec() treats any refspec leading with + as force (pre-push-gate.sh:76-78). My own differential vs origin/main: git push origin +feat/x and +feat/x:refs/heads/feat/y were ALLOW on main (fail-open), BLOCK now.
  5. Stale cross-repo ref — FIXED. git grep 'feat/gate-doctrine' returns nothing (superproject and the bumped plugins tree). The paired change is correctly named plugins PR chore: bump sdk pointer for gofmt spec wire files (P1.12) #19, gitlink 3149fc9. The only "a heredoc is blocked" hit is the sanctioned self-correcting CHANGELOG sentence (line 29, R5 historical context).
  6. Asserted count — FIXED. expect() increments ran (gate_test.py:39); the banner prints the measured ran (line 161), not a literal.

Independent checks (not from the six)

  • Regression differential (main gates vs new gates), 35 adversarial commands: 0 fail-opens OPENED. The new gate is strictly stronger — it also blocks +feat/x force refspecs and a trailerless -am x that main allowed. Confirmed the author's refined claim: main blocks the docs-tier unresolvable -C but fails open on the runtime tier; the new gate blocks both.
  • Adversarial bypass probing: --no-verify/-n/-an/-nm (before and after the message), core.hooksPath (incl. uppercase), env-assign/sudo/keyword prefixes, force bundles (-fu/-uf), HEAD:refs/heads/main, feat/x:main — all blocked; message-internal --no-verify and $(...)/-F/heredoc messages correctly not scanned for absence.
  • Gitlink 3149fc9: confirmed on plugins real origin/main (its branch feat/gate-doctrine-v2 was merged + deleted) — a merged-commit pointer, never a feat branch. The bump is the minimal commit that includes the named paired sweep; its 3 ancestor cutovers (SDD/CUE gate, merge-authority, self-approval/autoMode doctrine) are already-merged, documentation-only, gate-strengthening skill/agent-spec changes — I read every diff (T4 heightened review) and found no weakening and no attack.
  • Security (T1–T4): no network/eval/base64/secret/exfiltration; subprocess invokes only git/bash with static arg lists; no scope-mismatch in the authored diff; no attempt to instruct or manipulate the validator; the hook/spec changes strengthen the gates.
  • Structure: ONE atomic commit (39127d0, parent = current origin/main); trailer Assisted-by: Claude (fully tested and validated); CHANGELOG/2026.190.1624.md staged; 6c73860 intact on feat/gate-accurate (no force-push); working tree clean.

Checklist

  • [PASS] 0. security & anti-tampering (T1–T4)
  • [PASS] 1. description complete (template filled, verbatim evidence)
  • [PASS] 2. change-class gate matches evidence (scripts executed live; no dry-run/rebuild-alone fraud)
  • [PASS] 3. attribution tier fully tested and validated earned (changed scripts ran live, output pasted + reproduced)
  • [PASS] 4. R0 skills honored (git-workflow / agents hooks-doctrine)
  • [PASS] 5. R1 RCA on every finding; ZERO warnings; no flake/transient framing
  • [PASS] 6. R2 no pre-existing/out-of-scope split (empty-index limit is genuinely separable + scoped next-cutover)
  • [PASS] 7. R3 no duplication (one shared gitcmd.py)
  • [PASS] 8. R4 no ad-hoc workaround (principled tokenizer, not a shim)
  • [PASS] 9. R5 hard cutover + grep self-test (INVOKE deleted from both gates; zero fallback path)
  • [PASS] 10. R6/R8/R9 [N/A — R8/R9 no generation/binary/dep surface; R6 the only destroy is on the test's own mkdtemp trees]
  • [PASS] 11. R10 hook class: live script execution, zero failures, check-coverage that fails without the fix
  • [PASS] 12. RDD/ADE/SDD [N/A — no candy/CUE/generated-Go/wire/params surface in the diff]
  • [PASS] 13. concurrency [N/A — PreToolUse hook scripts, no shared-state runtime path; no idle/serial cheat]
  • [PASS] 14. hard cutover — one atomic commit; no Phase-2/TODO in scope
  • [PASS] 15. kernel/plugin boundary law [N/A — no charly core / sdk Go change]
  • [PASS] 16. disposable-only autonomy (test destroys only its own mkdtemp trees; /tmp sentinel survived)
  • [PASS] 17. clean architecture [Go gates N/A — Python + markdown]; one shared module, no dead code
  • [PASS] 18. CHANGELOG present (runtime tier)

Decision: PASS. All six #40 findings are independently re-verified fixed; my own regression differential opens zero fail-opens and closes four; the gitlink bump and its ride-along doctrine changes are benign and gate-strengthening; the fully tested and validated tier is earned by evidence I reproduced byte-for-byte. Proceeding to finalize the merge-time CalVer, merge --rebase, and tag.

Assisted-by: Claude (analysed on a live system)

…e shared tokenizer)

Assisted-by: Claude (fully tested and validated)
@atrawog
atrawog merged commit 1013a81 into main Jul 9, 2026
@atrawog
atrawog deleted the feat/gate-hardened branch July 9, 2026 17:54
atrawog added a commit that referenced this pull request Jul 9, 2026
…osed and recreated (2026.190.1835)

`main` now gains exactly ONE commit per cutover, and a PR under review is UPDATED
IN PLACE instead of being closed and recreated. Both follow from one change of
merge method.

The problem: the landing flow merged --rebase and required the author's cutover to
be a single commit on feat/<slug>. Force-push is forbidden absolutely, so on a
CHANGES-REQUESTED verdict there was no way to update the branch in place -- an
amended commit diverges and can only be published by a force-push, and a second
change commit would land on main as a second commit. The only remaining move was to
close the PR and open a replacement, churning review history once per round (six
times across PRs #36-#41 for one hook cutover).

The fix -- atomicity is a property of main, not of the branch:

- scripts/apply-branch-protection.sh enforces SQUASH-ONLY merge settings
  (allow_squash_merge=true, allow_rebase_merge=false, merge-commit off,
  delete-branch on) across all 11 repos. required_linear_history, enforce_admins,
  allow_force_pushes=false, allow_deletions=false and the required
  charly/claude-validation status context are UNCHANGED: this changes the merge
  METHOD only and weakens no gate.
- The evaluator merges `gh pr merge --squash --delete-branch`, composing the squash
  message itself (subject + body + the author's Assisted-by trailer). The author's
  change, any review-round fix commits, and the merge-time version stamp fold into
  ONE commit on main.
- On CHANGES REQUESTED the author appends a fix commit and pushes fast-forward; the
  status resets and the evaluator re-runs. Closing a PR is reserved for work that
  will not land at all.
- The force-push ban is untouched and still absolute. `git commit --amend` on a
  PUSHED branch is now named explicitly as forbidden for the same reason.

Paired doctrine sweep: plugins gitlink bumped 3149fc9 -> 1043577 (docs-only) --
the skill/agent side (git-workflow, agents, pr-validator) plus the merge-authority
correction (authority is the superproject's committed permissions.allow pair; no
autoMode key exists in any settings file).

Independently validated by a fresh pr-validator (PR #42): bash -n clean; the changed
verify predicate reports drift (exit 1) against pre-change settings and compliant
(exit 0) after; every branch-protection invariant re-read live from the GitHub API
on charly/plugins/distro-fedora and confirmed UNCHANGED; plugins 1043577 confirmed
on plugins main and docs-only. Merge-time CalVer 2026.190.1835.

Assisted-by: Claude (fully tested and validated)
atrawog added a commit that referenced this pull request Jul 13, 2026
…-free eval VM beds (#33)

The superproject runtime half of #33 — the libvirt domain (and every per-domain resource: state dir, NVRAM, ssh key, seed ISO, ssh alias, ssh port, cloud-init instance-id) is keyed by the DEPLOY name (vmshared.VmDomainIdentity), not the shared kind:vm entity, with a per-deploy qcow2 overlay over a read-only entity base. Sibling beds sharing one entity via from: get distinct charly-<deploy> domains — collision-free by construction — so the long VM beds run CONCURRENTLY.

- --domain flag on charly vm create/start/stop/destroy/console; destroy-by-domain.
- Read side per-deploy (bedVmDomains / checkVmTarget / Runner.vmTargetName) so the libvirt:/spice: check verbs address charly-<deploy>.
- Base-build FLOCK (acquireVmBuildLock) + content-signature idempotent skip-if-fresh makes the concurrent vm build <entity> preflight safe (RDD-proven; the concurrent roster never rewrites a base a live per-domain overlay backs onto).
- Fixes the nested-child SSH-alias seam (deploykit.VmChildExecutor caller → 2-arg) the concurrent R10 caught.
- R2 fold-ins: plugin-vm (14) + plugin-preempt (3) tabwriter-errcheck + dead-code lint cleanup.

Submodule pointers: sdk → 2977d5c (v0.2026194.659, #35 merged); plugins → 55800b2 (v2026.194.0707, #41 merged). charly/go.mod require → v0.2026194.659.

BREAKING CHANGE: deploykit.VmChildExecutor drops the *BundleNode param; the sole caller (charly/bundle_add_cmd.go) is updated here.

R10: the FULL 4-bed concurrent eval-vm roster (fresh per-tree binary v2026.194.0736, all calver 2026.194.0741, simultaneous) — check-builder-vm PASS 9/210s, check-substrate PASS 9/187s, check-group PASS 5/263s (3-level group→vm→local), check-structkind PASS 5/265s (3-level structkind→vm→local); 4 distinct per-deploy domains + keys + seeds (validator-corroborated on disk), both deep-bed nested-member SSH seams GREEN, clean teardown (0 leftover domains). Validator re-ran gofmt/vet/build clean, golangci 0, go test green.

Assisted-by: Claude (fully tested and validated)
github-actions Bot pushed a commit that referenced this pull request Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant