Skip to content

feat(guardrails): deny edits in a main checkout on its default branch - #16954

Merged
kyle-sexton merged 7 commits into
mainfrom
feat/16905-main-checkout-edit-guard
Nov 18, 2026
Merged

kyle-sexton merged 7 commits into
mainfrom
feat/16905-main-checkout-edit-guard

Conversation

@kyle-sexton

Copy link
Copy Markdown
Member

Refs: #16905

Summary

Sessions that start in a repository's main folder sometimes edit tracked files there instead of in a worktree, and no hook stopped it. A new guardrails guard, block-main-checkout-edit, now denies a Write, Edit or NotebookEdit to any file that is not git-ignored in a main checkout while it is on the remote default branch. The denial tells the session how to make the edit in a worktree. It denies and never asks, because a background session cannot answer a prompt.

New denial on update: the guard is on by default for everyone who installs guardrails. To turn it off for yourself, set block_main_checkout_edit_enabled to false (README, "How to set these"). The changelog fragment and README both say so.

Fix

  • The guard runs in the existing Write|Edit|NotebookEdit run-guards.sh row. A main checkout is one whose .git is a directory, or a gitdir file whose directory has no commondir. The default branch is read from the local refs/remotes/origin/HEAD, with no network call.
  • Exemptions follow the owner's answers relayed by the hub: git-ignored paths, paths outside a repository, linked worktrees, a detached HEAD, any other branch, and repositories with no origin/HEAD all pass. A repository that has an origin remote but no origin/HEAD passes with a stderr line naming git remote set-head origin --auto. A new file that is not ignored is denied like a tracked one (owner Q1). There is no per-repository opt-out (owner Q2).
  • The denial names the repository's AGENTS.md as the source of its worktree convention, gives both routes (/source-control:worktree create, or git worktree add without entering it in a headless session), and gives the path to repeat the edit at.
  • The README lists the known limit: writes made through shell commands are not seen.
  • Registered in hooks.json, plugin.json (kill switch), guard-requires.sh, the abort boundary's guard list and run-guards.test.sh's dispatch list.

Verification

  • bash plugins/guardrails/hooks/block-main-checkout-edit.test.sh: PASS=88 FAIL=0. It runs on real git fixtures, both alone and through run-guards.sh, and covers a denied edit and new file, NotebookEdit, a tracked file under an ignored pattern, an inherited GIT_DIR, and a gitdir-file main checkout. It also covers each exemption, a linked worktree, a detached head, a non-default branch, the kill switch and the registration.
  • run-guards.test.sh PASS=752, abort-boundary.test.sh PASS=391, coverage-manifest.test.sh 32/32. The guardrails-pretooluse-write-edit-spawns ratchet census still reads spawns=7 (ceiling 7): every check before git check-ignore uses bash builtins only.
  • shellcheck, shfmt -i 2, the pre-commit comment lint, and the pre-push check-affected pass.
  • Left out: Windows CI selection for the new suite. The live-session demonstration is the hub owner's to arrange.

Related

  • Refs melodic-software/dotfiles#1641 and melodic-software/control-hub#11 (the finding behind the issue).

Remaining

Not delivered

The two ## Remaining items above. This diff delivers the tests line: a denied edit, each exemption, a linked worktree, a detached head and a non-default branch in the main checkout.

🤖 Generated with Claude Code

https://claude.ai/code/session_01KeBVgexJUYJe9Na3shvSfs

kyle-sexton and others added 2 commits November 17, 2026 16:49
block-main-checkout-edit runs on the Write|Edit|NotebookEdit row through
run-guards.sh. It denies an edit to a file that is not git-ignored in a
repository's main checkout while HEAD is on the branch origin/HEAD names,
and the reason gives the steps to make the edit in a worktree. Linked
worktrees, other branches, a detached HEAD, git-ignored paths, paths
outside a repository and repositories with no origin/HEAD pass.

Every check before one git check-ignore reads files with bash builtins,
so the row's spawn count on a clean Write stays at 7.

On by default; block_main_checkout_edit_enabled turns it off.

Refs: #16905

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KeBVgexJUYJe9Na3shvSfs
@kyle-sexton

Copy link
Copy Markdown
Member Author

Owner of this pull request: the hub session "Plugin Presets". This build session reports to it; route questions about the exemption design and the live-session check there.

@kyle-sexton
kyle-sexton marked this pull request as ready for review November 17, 2026 21:53
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T01:07:16.065579Z 38f346c Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 587d235cf2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@github-actions

Copy link
Copy Markdown
Contributor

Lane run report

Field Value
Activity pr-refine/fix-docs
Model rung 0
Leg handoff
Run job success
Activity step success
Commits pushed none (read lane)
Run 44184525304

Failed GitHub API calls

Method Path Status
GET /repos/{repo}/git/ref/tags/* 404

@github-actions

Copy link
Copy Markdown
Contributor

Lane run report

Field Value
Activity pr-refine/apply-patch
Model rung 0
Leg base
Run job success
Activity step success
Commits pushed c4681ec
Run 44184525304

Failed GitHub API calls

Method Path Status
GET /repos/{repo}/git/ref/tags/* 404

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

claude-review reviewed 38f346caed3d0b7828db97a28b2b49d7323c7230: read 5 of 5 changed file(s); 0 finding(s), 0 as inline comments.

Incremental review: feat(guardrails): deny edits in a main checkout on its default branch

Scope: the change from c4681ec to 38f346c, five files: docs/catalog.md, plugins/guardrails/.claude-plugin/plugin.json, plugins/guardrails/README.md, block-main-checkout-edit.sh and its test. This lane already reviewed an earlier head, so this review reports blocking findings and manifest flags only. I found no blocking finding.

My earlier summary finding is fixed. It said the manifest description left out the new denial and had a stale guard count. The description now lists "main-checkout edits" under Blocks and gives "25 toggleable guards". The generated docs/catalog.md line carries the same text.

Hook changes I traced by hand (block-main-checkout-edit.sh):

  • Symlink resolution. The nearest existing ancestor directory is resolved with cd -P, and the shell then returns to its original directory. The tail that does not exist yet is added back on, and .git is checked again on the resolved path. So alias/b.txt is judged as src/b.txt, and a symlinked directory that leaves the repository passes. The cases I traced, root-level files, drive-letter paths and .. segments, all reach a sensible exit.
  • Repository lookup. It now starts from the resolved directory, so the reason text and MCE_REL use the real path.
  • Git calls. mce::git_exec is only ever called inside $(...), so its unset and exec stay inside the subshell. This matters if run-guards.sh sources the guard.
  • check-ignore exit codes. 0 still exempts the edit. 1 denies with the usual note. Any other code now denies and names git's first error line. The old behaviour, where an error exempted the edit, is gone.
  • Tests. They cover a symlinked directory (tracked file and new file), a checkout reached through a symlink, a symlink that leaves the repository, a planted core.fsmonitor plus include (with a control run showing plain git does fire it), an unowned git dir or work tree that runs no git (checked with a stub), and a git error that denies.

Security. The ownership gate, unsetting the GIT_* variables and the -c core.fsmonitor=false -c core.hooksPath=/dev/null overrides answer the CRITICAL thread that the owner settled. Judging that thread is the security lane's job, not this lane's.

Not verified. I did not run the tests. The PASS counts in the PR are the author's claim.

Closing issue. The PR context only says Refs: #16905 and has no closing-issue section, so there were no acceptance criteria to check.

Usage. The change adds no new process on the common path. The only git process is still the one check-ignore for an edit in a main checkout on its default branch. It uses local compute only.

Loosened checks. Verdict: fail. This is the default, because the prompt has no loosened_check_verdict: or loosened_check_patterns: line; patterns: 0. The change tightens the guard: a git error now denies instead of exempting the edit.

Advisory and manifest lines (1)
  • manifest description: pass (the Blocks list now names main-checkout edits and the count reads 25; the catalog line matches)

Verifier dropped 0 of 0 candidates.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

claude-security-review: 1 inline finding(s) on c4681ec044d8f752aa778a3f8a4edec48db8c43f; the summary is in this lane's pull request comment.

claude-lane: claude-security-review

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

claude-security-review reviewed 38f346caed3d0b7828db97a28b2b49d7323c7230: read 5 of 5 changed file(s); 0 finding(s), 0 as inline comments.

Security review: PR #16954, incremental (c4681ec → 38f346c)

This review covers only what changed since c4681ec, in 5 files: block-main-checkout-edit.sh, its test, the README row, plugin.json description and docs/catalog.md. I read every hunk of each, plus the whole hook at head. This lane already reviewed an earlier head, so this review reports blocking findings only.

No security issues found in this push.

The earlier CRITICAL finding is fixed. It was that a planted .git could run code through core.fsmonitor when git check-ignore ran (discussion_r4606485654). Its lines changed, and the head closes the path in three ways:

  • Ownership check before git runs. At L84-L85 the hook requires this user to own the work tree, the git dir and the git dir's config. If any check fails it exits 0 and no git runs. That stops another user's /tmp/.git or C:/.git. The ownership test runs on the symlink-resolved directory (L41-L52), and -O follows a .git symlink.
  • Git's own safe.directory check applies again. mce::git_exec (L87-L92) unsets GIT_DIR, GIT_WORK_TREE, GIT_INDEX_FILE, GIT_CONFIG_PARAMETERS and the other variables that tell git where the repository is. Git then finds the repository from -C "$MCE_ROOT" and runs that check itself.
  • Command-line overrides cover a user-owned but untrusted tree (for example an extracted archive). -c core.fsmonitor=false -c core.hooksPath=/dev/null --no-pager outrank the repository config, its includes and config.worktree.
    • I checked what else check-ignore and symbolic-ref can reach. Exclude files and includes are only read, never run. trace2.* is read only from system and global config. Neither command runs filters, hooks, aliases or auto-gc. I found no other repo-local key that runs a command on this path.
    • The new test plants an fsmonitor command both directly and through an include, asserts it never runs, and has a control check that proves the fixture fires under plain git.

Other changes:

  • Error handling now fails closed. A check-ignore error other than exit 1 used to allow the edit; now it denies (L125-L129). This widens nothing. The first line of git's stderr goes into the denial text shown to the session, which leaks nothing.
  • Test-only environment variable. GUARDRAILS_TEST_UNOWNED_PATH (L81) can only make a path count as not owned. That skips the guard and starts no git process, the same effect as the existing off switch. It gives an attacker no exec, leak or charge path.
  • Docs only. The README row and docs/catalog.md only describe the guard.

Spend: none.

critical-findings: 0

Manifest lines (1)
  • manifest description: pass

@github-actions github-actions Bot added the needs-human Human-in-the-loop required; autonomous sessions must not resolve items carrying this. label Nov 17, 2026
@github-actions

Copy link
Copy Markdown
Contributor

claude-security-review holds this pull request for a person (critical-security) at c4681ec044d8f752aa778a3f8a4edec48db8c43f.

CRITICAL: plugins/guardrails/hooks/block-main-checkout-edit.sh:62/95 is on by default. On an ordinary Write or Edit it runs git check-ignore with an explicit GIT_DIR against any ancestor .git, which skips safe.directory and lets a planted core.fsmonitor run code as the user. A separate verifier tried to refute the rating and confirmed it.

claude-lane: claude-security-review

kyle-sexton and others added 4 commits November 17, 2026 17:15
The manifest description now lists main-checkout edits on the default
branch among the blocks, and counts 25 guards, the rows of the README
guard table.

Refs: #16905

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KeBVgexJUYJe9Na3shvSfs
…ys off

A planted .git (another user's /tmp/.git, a C:/.git, an extracted archive)
could run its core.fsmonitor command when block-main-checkout-edit read the
index through git check-ignore, and the guard's explicit GIT_DIR skipped
git's safe.directory ownership check.

The guard now runs git only when this user owns the work tree, the git
directory and its config, and otherwise allows the edit with no git call.
Every git call unsets the locating GIT_* variables, so safe.directory applies
too, and passes --no-pager -c core.fsmonitor=false -c core.hooksPath=/dev/null,
which outrank the repository's config and its includes.

The target's existing directories are resolved through symlinks before the
repository is found, so alias/a.txt with alias -> src is judged as src/a.txt
instead of check-ignore failing beyond the symlink. A check-ignore error in
an owned repository now denies, naming the error, instead of exempting the
edit.

Refs: #16905

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KeBVgexJUYJe9Na3shvSfs
@kyle-sexton

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 38f346caed

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@kyle-sexton kyle-sexton removed the needs-human Human-in-the-loop required; autonomous sessions must not resolve items carrying this. label Nov 18, 2026
@kyle-sexton

Copy link
Copy Markdown
Member Author

Live check by the hub session at the owner's request, Claude Code 2.1.327, head 38f346c, in a throwaway repository on its default branch with this branch's guardrails loaded through --plugin-dir under --restricted:

  • An Edit of the tracked README.md was denied before any prompt; the session showed the BLOCKED: text with the three worktree steps and the file was unchanged (git status --short empty).
  • A Write to the git-ignored scratch/note.txt went through.

The security lane cleared its hold in its re-review of this head, so the needs-human label is removed.

@kyle-sexton
kyle-sexton added this pull request to the merge queue Nov 18, 2026
Merged via the queue into main with commit cec7aab Nov 18, 2026
93 of 95 checks passed
@kyle-sexton
kyle-sexton deleted the feat/16905-main-checkout-edit-guard branch November 18, 2026 01:50
@github-actions github-actions Bot added the merge-path: owner-agent Post-merge accounting record (README.md, Merge paths) label Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-path: owner-agent Post-merge accounting record (README.md, Merge paths)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant