The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.
fix(gateguard): gate ref- and history-destroying git commands (#3154, #3151) (#3170)
Affaan Mustafa07756cee1578
diff --git a/scripts/hooks/gateguard-fact-force.js b/scripts/hooks/gateguard-fact-force.jsindex 568cf58b2..bb800d28b 100644--- a/scripts/hooks/gateguard-fact-force.js+++ b/scripts/hooks/gateguard-fact-force.js@@ -462,10 +462,65 @@ function findGitSubcommand(tokens) {return null;}+/**+ * Branch names treated as shared history: a forced update of one of+ * these rewrites commits other clones build on, even when the push is+ * lease-checked.+ */+const SHARED_GIT_BRANCHES = new Set(['main', 'master', 'develop', 'trunk']);++/**+ * Decide whether the positional arguments of a `git push` name a shared+ * branch as the destination of a refspec. The first positional token is+ * the remote (unless the remote came from `--repo`); every later+ * positional token is a refspec whose destination is the part after+ * `:` (or the whole token when there is no `:`). A leading `+` force+ * marker is stripped. When no refspec is given the target is the+ * current branch, which the hook cannot know, so this returns false.+ *+ * @param {string[]} rest tokens after `push`+ * @returns {boolean}+ */+function pushTargetsSharedBranch(rest) {+ const valueConsuming = new Set(['-o', '--push-option', '--receive-pack', '--exec']);+ const positional = [];+ let remoteViaFlag = false;+ for (let i = 0; i < rest.length; i++) {+ const t = rest[i];+ if (t === '--repo') {+ remoteViaFlag = true;+ i += 1;+ continue;+ }+ if (t.startsWith('--repo=')) {+ remoteViaFlag = true;+ continue;+ }+ if (valueConsuming.has(t)) {+ i += 1;+ continue;+ }+ if (t.startsWith('-')) continue;+ positional.push(t);+ }+ // Unless the remote came from --repo, positional[0] is the remote and+ // the rest are refspecs.+ const refspecs = remoteViaFlag ? positional : positional.slice(1);+ for (const refspec of refspecs) {+ const cleaned = refspec.startsWith('+') ? refspec.slice(1) : refspec;+ const dst = cleaned.includes(':') ? cleaned.slice(cleaned.indexOf(':') + 1) : cleaned;+ const branch = dst.startsWith('refs/heads/') ? dst.slice('refs/heads/'.length) : dst;+ if (SHARED_GIT_BRANCHES.has(branch)) return true;+ }+ return false;+}+/*** Detect destructive `git` invocations: `reset --hard`, `checkout --`,- * `clean -f...`, `push --force` (but not `--force-with-lease`),- * `commit --amend`, `rm -rf`.+ * `clean -f...`, `push --force` (`--force-with-lease` only to a shared+ * branch), `commit --amend`, `rm -rf`, `branch -D`, `stash drop` /+ * `stash clear`, `reflog expire` / `reflog delete`, `update-ref -d`,+ * and `restore` against the worktree.** @param {string[]} tokens* @returns {boolean}@@ -532,7 +587,9 @@ function isDestructiveGit(tokens) {plusRefspecForce = true;}}- return bareForce || (plusRefspecForce && !withLease);+ if (bareForce || (plusRefspecForce && !withLease)) return true;+ // A lease-checked force still rewrites a shared branch's history.+ return withLease && pushTargetsSharedBranch(rest);}if (command === 'commit') {@@ -563,6 +620,53 @@ function isDestructiveGit(tokens) {});}+ if (command === 'branch') {+ // `git branch -D` (long spelling: `--delete --force`) deletes a+ // branch even when it is unmerged, orphaning its commits. Plain+ // `-d` refuses when unmerged, so it is safe to leave ungated.+ let del = false;+ let force = false;+ for (const t of rest) {+ if (t === '--delete') { del = true; continue; }+ if (t === '--force') { force = true; continue; }+ if (!t.startsWith('-') || t.startsWith('--')) continue;+ const body = t.slice(1);+ if (body.includes('D')) return true;+ if (body.includes('d')) del = true;+ if (body.includes('f')) force = true;+ }+ return del && force;+ }++ if (command === 'stash') {+ // `drop` destroys one stash entry, `clear` the entire stash.+ // `list`, `show`, `pop` and `apply` keep the entries recoverable.+ return rest[0] === 'drop' || rest[0] === 'clear';+ }++ if (command === 'reflog') {+ // `expire` and `delete` remove the recovery net that makes every+ // other gated git command recoverable.+ return rest[0] === 'expire' || rest[0] === 'delete';+ }++ if (command === 'update-ref') {+ // `git update-ref -d <ref>` deletes a ref directly.+ return rest.includes('-d') || rest.includes('--delete');+ }++ if (command === 'restore') {+ // `git restore <path>` overwrites the working tree from the index+ // by default, the modern spelling of gated `git checkout -- <path>`.+ // Only `--staged` alone is non-destructive (it leaves the file on+ // disk untouched); `--worktree` (the default target) is destructive.+ const has = (long, short) => rest.some(t =>+ t === long || (t.startsWith('-') && !t.startsWith('--') && t.slice(1).includes(short)));+ const staged = has('--staged', 'S');+ const worktree = has('--worktree', 'W');+ return worktree || !staged;+ }+return false;}diff --git a/tests/hooks/gateguard-fact-force.test.js b/tests/hooks/gateguard-fact-force.test.jsindex ec200b8e8..fc3ab3106 100644--- a/tests/hooks/gateguard-fact-force.test.js+++ b/tests/hooks/gateguard-fact-force.test.js@@ -1978,12 +1978,71 @@ function runTests() {else failed++;if (- test('allows git push --force-if-includes as a safety-checked variant', () => {- expectAllow('git push --force-with-lease --force-if-includes origin main', 'git push --force-if-includes');- })- )- passed++;- else failed++;+ test('allows git push --force-if-includes as a safety-checked variant on a non-shared branch', () => {+ expectAllow('git push --force-with-lease --force-if-includes origin feature-branch', 'git push --force-if-includes');+ })+ )+ passed++;+ else failed++;++ // --- Ref- and history-destroying git commands (issues #3154, #3151) ---++ const destructiveGitCases = [+ ['git branch -D feature', 'git branch -D'],+ ['git branch --delete --force feature', 'git branch --delete --force'],+ ['git branch -d -f feature', 'git branch -d -f'],+ ['git stash drop', 'git stash drop'],+ ['git stash drop stash@{0}', 'git stash drop stash@{0}'],+ ['git stash clear', 'git stash clear'],+ ['git reflog expire --expire=now --all', 'git reflog expire'],+ ['git reflog delete HEAD@{2}', 'git reflog delete'],+ ['git update-ref -d refs/heads/x', 'git update-ref -d'],+ ['git update-ref --delete refs/heads/x', 'git update-ref --delete'],+ ['git restore foo.ts', 'git restore <path>'],+ ['git restore .', 'git restore .'],+ ['git restore --worktree foo.ts', 'git restore --worktree'],+ ['git restore -W foo.ts', 'git restore -W'],+ ['git restore --staged --worktree foo.ts', 'git restore --staged --worktree'],+ ['git restore -s HEAD foo.ts', 'git restore --source without --staged'],+ ['git push --force-with-lease origin main', 'git push --force-with-lease to main'],+ ['git push --force-with-lease origin HEAD:main', 'git push --force-with-lease HEAD:main'],+ ['git push --force-with-lease origin +refs/heads/master:refs/heads/master', 'git push --force-with-lease +refs/heads/master'],+ ['git push --force-with-lease --force-if-includes origin main', 'git push --force-with-lease --force-if-includes to main'],+ ['git push --force-with-lease --repo origin main', 'git push --force-with-lease --repo to main']+ ];+ for (const [command, label] of destructiveGitCases) {+ if (+ test(`denies ${label} as destructive`, () => {+ expectDestructiveDeny(command, label);+ })+ )+ passed++;+ else failed++;+ }++ const safeGitCases = [+ ['git branch -d feature', 'git branch -d (refuses when unmerged)'],+ ['git branch -f feature', 'git branch -f (no delete)'],+ ['git stash list', 'git stash list'],+ ['git stash show', 'git stash show'],+ ['git reflog show', 'git reflog show'],+ ['git update-ref refs/heads/x abc1234', 'git update-ref without -d'],+ ['git restore --staged foo.ts', 'git restore --staged'],+ ['git restore -S foo.ts', 'git restore -S'],+ ['git restore --source=HEAD --staged foo.ts', 'git restore --source with --staged'],+ ['git push --force-with-lease origin feature-branch', 'git push --force-with-lease to feature branch'],+ ['git push --force-with-lease', 'git push --force-with-lease with no refspec'],+ ['git push --force-with-lease -o ci.skip origin feature-branch', 'git push --force-with-lease with push option']+ ];+ for (const [command, label] of safeGitCases) {+ if (+ test(`allows ${label}`, () => {+ expectAllow(command, label);+ })+ )+ passed++;+ else failed++;+ }// --- Review-round-2 findings ---