mirror of
https://github.com/garrytan/gstack.git
synced 2026-12-01 04:13:55 +01:00
/careful no longer asks before deleting a file inside another worktree of the same repo (#4827) (#4834)
/careful no longer asks before every plain `rm` of a file inside another `git worktree` of the same repository, so people who run several worktrees side by side stop clicking through a prompt on each cleanup. It keeps the safety check quiet only where it was wrong (gstack's goal: guards that stop real mistakes, not routine work), and every other delete still asks. Nothing to do. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> Review-Finding: p1/specialist-batched#1 reader=testing Review-Finding: p1/adversarial-native#1 reader=adversarial-native Review-Finding: p1/adversarial-native#2 reader=adversarial-native Review-Finding: p2/red-team#1 reader=red-team
This commit is contained in:
1 parent
8aafd408e2
commit
a48df18f51
4 files changed
+232
-3
No files matched your search
@@ -177,6 +177,13 @@ _S=""
|
||||
_R=""
|
||||
_RISK=""
|
||||
_D=""
|
||||
_WT_N=-1
|
||||
_WT_ROOT=()
|
||||
_WT_GIT=""
|
||||
_WT_CWD=""
|
||||
_WT_HOME=""
|
||||
_WT_SOLO=1
|
||||
_WT_GRANT=0
|
||||
|
||||
# Up to two layers of quotes, any mix: trap 'rm -rf "$T"' leaves $T.
|
||||
_Q="[\"']"
|
||||
@@ -240,6 +247,90 @@ _careful_squash() {
|
||||
while [[ $_SQ == *//* || $_SQ == */./* ]]; do _SQ=${_SQ//\/\//\/}; _SQ=${_SQ//\/.\//\/}; done
|
||||
}
|
||||
|
||||
# The worktrees of the session's own repository (#4827), read once from
|
||||
# git worktree list --porcelain -z and resolved on disk with cd -P. A bare or
|
||||
# prunable entry never counts, and neither does a worktree that is /, the home
|
||||
# folder or a folder holding it, a personal folder, or a system folder. None
|
||||
# count when git fails or the session folder is inside none of them.
|
||||
_careful_wt_add() {
|
||||
local r="$1" p
|
||||
[ -n "$r" ] || return 0
|
||||
if command -v gstack_hook_normalize_path >/dev/null 2>&1; then gstack_hook_normalize_path "$r"; r="$GSTACK_HOOK_PATH"; fi
|
||||
case "$r" in /*) ;; *) return 0 ;; esac
|
||||
case "$r" in *"$_NL"*) return 0 ;; esac
|
||||
p=$(cd -P -- "$r" 2>/dev/null && pwd -P) || return 0
|
||||
p="${p%/}"
|
||||
[ -n "$p" ] || return 0
|
||||
case "$_WT_HOME/" in "$p"/*) return 0 ;; esac
|
||||
case "${p#"$_WT_HOME"/}" in Documents|Desktop|Downloads|Library|Pictures|Movies|Music|.config|.ssh|.ssh/*|.aws|.aws/*|.gnupg|.gnupg/*|.kube|.kube/*) return 0 ;; esac
|
||||
[[ $p =~ $_RX_ROOT ]] && return 0
|
||||
[[ $p/ =~ $_RX_SYSDIR ]] && return 0
|
||||
_WT_ROOT[$_WT_N]="$p"; _WT_N=$((_WT_N + 1))
|
||||
return 0
|
||||
}
|
||||
_careful_worktrees() {
|
||||
[ "$_WT_N" -lt 0 ] || return 0
|
||||
_WT_N=0
|
||||
local rec root="" skip=0 i=0 in=0
|
||||
_WT_HOME=$(cd -P -- "${HOME:-/}" 2>/dev/null && pwd -P) || _WT_HOME=/
|
||||
_WT_HOME="${_WT_HOME%/}"
|
||||
_WT_CWD=$(cd -P -- "$_CAREFUL_CWD" 2>/dev/null && pwd -P) || return 0
|
||||
_WT_GIT=$(unset GIT_DIR GIT_WORK_TREE GIT_COMMON_DIR; GIT_OPTIONAL_LOCKS=0 git -C "$_CAREFUL_CWD" rev-parse --path-format=absolute --git-common-dir </dev/null 2>/dev/null) || return 0
|
||||
_WT_GIT=$(cd -P -- "$_WT_GIT" 2>/dev/null && pwd -P) || return 0
|
||||
[ -n "$_WT_GIT" ] && [ "$_WT_GIT" != / ] || return 0
|
||||
while IFS= read -r -d '' rec; do
|
||||
case "$rec" in
|
||||
'worktree '*) [ "$skip" -eq 1 ] || _careful_wt_add "$root"; root="${rec#worktree }"; skip=0 ;;
|
||||
bare|prunable|'prunable '*) skip=1 ;;
|
||||
esac
|
||||
done < <(unset GIT_DIR GIT_WORK_TREE GIT_COMMON_DIR; GIT_OPTIONAL_LOCKS=0 git -C "$_CAREFUL_CWD" worktree list --porcelain -z </dev/null 2>/dev/null)
|
||||
[ "$skip" -eq 1 ] || _careful_wt_add "$root"
|
||||
while [ "$i" -lt "$_WT_N" ]; do
|
||||
case "$_WT_CWD/" in "${_WT_ROOT[$i]}"/*) in=1 ;; esac
|
||||
i=$((i + 1))
|
||||
done
|
||||
[ "$in" -eq 1 ] || _WT_N=0
|
||||
return 0
|
||||
}
|
||||
|
||||
# An absolute delete target strictly inside a worktree of the session's own
|
||||
# repository, checked on disk (#4827). Never a glob, a quote, a variable, a ..
|
||||
# or a symlink; never a .git entry or anything in the git folder; never a
|
||||
# worktree's root, the session folder or a folder holding one of them. Only an
|
||||
# rm whose every option is one of -f -i -I -v -d (or their long names), or an
|
||||
# unlink, grants it (_WT_GRANT), and only when every statement in
|
||||
# the command is one (_WT_SOLO): the disk is read before the command runs, so
|
||||
# nothing else in it, before or beside the delete, may move a folder or make a
|
||||
# link.
|
||||
_RX_WT_PATH='^/[A-Za-z0-9._/@+,:%=-]+$'
|
||||
_careful_in_worktree() {
|
||||
local t="$1" dir base p r i=0 in=0
|
||||
[[ $t =~ $_RX_WT_PATH ]] || return 1
|
||||
_careful_squash "$t"; t="${_SQ%/}"
|
||||
case "/$t/" in */../*|*/./*) return 1 ;; esac
|
||||
case "$t/" in */.[Gg][Ii][Tt]/*) return 1 ;; esac
|
||||
base="${t##*/}"; dir="${t%/*}"
|
||||
[ -n "$base" ] && [ -n "$dir" ] || return 1
|
||||
[ ! -L "$t" ] || return 1
|
||||
_careful_worktrees
|
||||
[ "$_WT_N" -gt 0 ] || return 1
|
||||
if [ -d "$t" ]; then
|
||||
p=$(cd -P -- "$t" 2>/dev/null && pwd -P) || return 1
|
||||
else
|
||||
p=$(cd -P -- "$dir" 2>/dev/null && pwd -P) || return 1
|
||||
p="${p%/}/$base"
|
||||
fi
|
||||
case "$p/" in */.[Gg][Ii][Tt]/*|"$_WT_GIT"/*) return 1 ;; esac
|
||||
case "$_WT_GIT/" in "$p"/*) return 1 ;; esac
|
||||
case "$_WT_CWD/" in "$p"/*) return 1 ;; esac
|
||||
while [ "$i" -lt "$_WT_N" ]; do
|
||||
r="${_WT_ROOT[$i]}"; i=$((i + 1))
|
||||
case "$r/" in "$p"/*) return 1 ;; esac
|
||||
case "$p" in "$r"/*) in=1 ;; esac
|
||||
done
|
||||
[ "$in" -eq 1 ]
|
||||
}
|
||||
|
||||
# A delete target outside the session's folder: an absolute or ~ path that is
|
||||
# not scratch or a cache, a path climbing out with .., or a word led by a
|
||||
# variable that may be unset.
|
||||
@@ -282,6 +373,7 @@ _careful_outside() {
|
||||
esac
|
||||
t="${t%/}"
|
||||
if [ -n "$_CAREFUL_CWD" ] && { [ "$t" = "$_CAREFUL_CWD" ] || [ "${t#"$_CAREFUL_CWD"/}" != "$t" ]; }; then return 1; fi
|
||||
[ "$_WT_GRANT" -eq 1 ] && [ "$_WT_SOLO" -eq 1 ] && _careful_in_worktree "$t" && return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -317,7 +409,7 @@ _careful_cd() {
|
||||
}
|
||||
|
||||
_careful_rm() {
|
||||
local k="$1" t rec=0 late=0 nt=0 first=1 q="" cp=1
|
||||
local k="$1" t rec=0 late=0 nt=0 first=1 q="" cp=1 plain=1
|
||||
local tg=()
|
||||
# rm opening a quoted string (trap 'rm -rf "$T"' EXIT) ends where it closes.
|
||||
case "${RW[$((k - 1))]}" in \'*) q="'" ;; \"*) q='"' ;; esac
|
||||
@@ -335,6 +427,10 @@ _careful_rm() {
|
||||
# A subcommand named rm (aws s3 rm s3://b --recursive) is not rm: a real
|
||||
# recursive rm leads with its flags, as the original family required.
|
||||
if [ "$first" -eq 1 ]; then first=0; case "$t" in -*) ;; *) [ "$cp" -eq 1 ] || rec=2 ;; esac; fi
|
||||
case "$t" in
|
||||
--|--force|--verbose|--dir|--interactive|--interactive=*) ;;
|
||||
--*|-*[!fivdI]*) plain=0 ;;
|
||||
esac
|
||||
case "$t" in
|
||||
--recursive) late=1; [ "$rec" -eq 2 ] || rec=1 ;;
|
||||
-*) case "${t#-}" in *[!a-zA-Z]*|'') ;; *[rR]*) late=1; [ "$rec" -eq 2 ] || rec=1 ;; esac ;;
|
||||
@@ -351,7 +447,9 @@ _careful_rm() {
|
||||
if [ "$rec" -eq 1 ]; then
|
||||
_careful_rm_safe "$t" || _RM_UNSAFE=1
|
||||
fi
|
||||
if [ "$rec" -eq 0 ] && [ "$plain" -eq 1 ]; then _WT_GRANT=1; fi
|
||||
if [ "$_OUTSIDE" -eq 0 ] && _careful_outside "$t"; then _OUTSIDE=1; fi
|
||||
_WT_GRANT=0
|
||||
done
|
||||
return 0
|
||||
}
|
||||
@@ -365,7 +463,9 @@ _careful_del() {
|
||||
[ -n "$t" ] || continue
|
||||
case "$tool" in
|
||||
shred|unlink)
|
||||
case "$t" in -*) ;; *) if _careful_outside "$t"; then hit=1; fi ;; esac ;;
|
||||
[ "$tool" = unlink ] && _WT_GRANT=1
|
||||
case "$t" in -*) ;; *) if _careful_outside "$t"; then hit=1; fi ;; esac
|
||||
_WT_GRANT=0 ;;
|
||||
rsync)
|
||||
case "$t" in
|
||||
--delete*) del=1 ;;
|
||||
@@ -736,6 +836,10 @@ _careful_scan() {
|
||||
local segs=( $s )
|
||||
IFS=$' \t\n'
|
||||
case "$CMD_MEDIUM" in *cd*|*pushd*|*popd*) track=1 ;; esac
|
||||
for seg in ${segs[@]+"${segs[@]}"}; do
|
||||
set -- $seg
|
||||
case "${1:-rm}" in rm|unlink|/bin/rm|/usr/bin/rm|/bin/unlink|/usr/bin/unlink|$'\004'|$'\005') ;; *) _WT_SOLO=0; break ;; esac
|
||||
done
|
||||
# for over the whole array: indexing it is linear per access on bash 3.2.
|
||||
for seg in ${segs[@]+"${segs[@]}"}; do
|
||||
# With no cd, pushd or popd anywhere, the separators change no folder state.
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
### Fixed
|
||||
|
||||
- **The destructive-command check no longer asks before deleting a file inside another `git worktree` of the same repository, or elsewhere in the session's own worktree.** A session started in `~/Projects/app` that ran `rm /Users/me/Projects/app-192/src/debug.spec.ts` got "deletes files outside this session's folder" on every delete, even in bypass mode (#4827). A plain `rm` or `unlink` of a path now counts as inside when, resolved on disk, it sits strictly inside a worktree that `git worktree list` reports for the session's own repository, and every statement in the command is such a delete. Everything else still asks, including a worktree's root or a folder holding one, `.git` and the git folder, `..`, symlinks, globs, other repositories, `rm -r`, `find`, `rsync --delete`, `shred`, a command that runs anything before the delete, and `git worktree remove --force`.
|
||||
+1
-1
@@ -1501,7 +1501,7 @@ Four skills that add safety rails to any Claude Code session. They work via Clau
|
||||
Say "be careful" or run `/careful` when you're working near production, running destructive commands, or just want a safety net. Every Bash and PowerShell command gets checked against known-dangerous patterns:
|
||||
|
||||
- `rm -rf` / `rm -r` of anything that isn't scratch — recursive delete
|
||||
- `rm`, `find -delete`, `rsync --delete`, `shred` or `unlink` outside the session folder, or through a variable that may be unset — files git can't bring back
|
||||
- `rm`, `find -delete`, `rsync --delete`, `shred` or `unlink` outside the session folder, or through a variable that may be unset — files git can't bring back (a plain `rm` or `unlink` of a file inside another `git worktree` of the same repository doesn't ask)
|
||||
- SQL `DROP TABLE` / `DROP DATABASE` / `TRUNCATE` run by a database client — data loss
|
||||
- `git push --force` / `-f` / `+ref` / `--force-with-lease` and remote branch deletes — history rewrite
|
||||
- `git reset --hard`, `checkout -- paths`, `checkout -f`, `switch -f`, `restore`, `clean -f` — only when the tree really has uncommitted work they would discard; `stash drop` and `worktree remove --force` always
|
||||
|
||||
@@ -1274,6 +1274,128 @@ describe('check-careful.sh PowerShell and cmd coverage (#3067)', () => {
|
||||
// The freeze fix (#3623) made the shared extractor keep trailing newlines and
|
||||
// map lone surrogates on PATH fields only; a command must come out exactly as
|
||||
// before, on both extractor branches.
|
||||
// #4827: deletes inside another worktree of the session's own repository. The
|
||||
// fixture lives outside /tmp, which the check already treats as scratch, so
|
||||
// every ask below is the check's real decision. POSIX symlinks and paths.
|
||||
describe.skipIf(process.platform === 'win32')('check-careful.sh sibling git worktrees (#4827)', () => {
|
||||
const real = (p: string) => fs.realpathSync(p);
|
||||
const scratchTmp = /^\/(private\/)?(tmp|var\/tmp)(\/|$)/.test(real(os.tmpdir()));
|
||||
function withWorktrees(fn: (b: string) => void) {
|
||||
const b = real(fs.mkdtempSync(path.join(scratchTmp ? os.homedir() : os.tmpdir(), '.gstack-careful-wt-')));
|
||||
try {
|
||||
const git = (cwd: string, args: string[]) => {
|
||||
const r = gitArgvIn(cwd, args, 10_000);
|
||||
if (r.status !== 0) throw new Error(`git ${args.join(' ')}: ${r.stderr}`);
|
||||
};
|
||||
for (const d of ['app/src', 'other/src', 'outside', 'plain']) fs.mkdirSync(path.join(b, d), { recursive: true });
|
||||
fs.writeFileSync(path.join(b, 'app', 'src', 'a.txt'), 'a\n');
|
||||
fs.writeFileSync(path.join(b, 'other', 'src', 'a.txt'), 'a\n');
|
||||
fs.writeFileSync(path.join(b, 'outside', 'secret'), 's\n');
|
||||
for (const repo of ['app', 'other']) {
|
||||
git(path.join(b, repo), ['init', '-q', '-b', 'main']);
|
||||
git(path.join(b, repo), ['add', '.']);
|
||||
git(path.join(b, repo), ['commit', '-q', '-m', 'init']);
|
||||
}
|
||||
git(path.join(b, 'app'), ['worktree', 'add', '-q', path.join(b, 'app-192'), '-b', 'feat']);
|
||||
git(path.join(b, 'app'), ['worktree', 'add', '-q', path.join(b, 'app', '.claude', 'worktrees', 'x'), '-b', 'nested']);
|
||||
git(path.join(b, 'app'), ['worktree', 'add', '-q', path.join(b, 'gone'), '-b', 'gone']);
|
||||
fs.rmSync(path.join(b, 'gone'), { recursive: true, force: true });
|
||||
fs.mkdirSync(path.join(b, 'gone', 'src'), { recursive: true });
|
||||
git(path.join(b, 'app'), ['worktree', 'prune']);
|
||||
fs.symlinkSync(path.join(b, 'outside'), path.join(b, 'app-192', 'escape'));
|
||||
fs.symlinkSync(path.join(b, 'outside', 'secret'), path.join(b, 'app-192', 'link-file'));
|
||||
fn(b);
|
||||
} finally {
|
||||
fs.rmSync(b, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
const decide = (b: string, cwd: string, command: string, env: Record<string, string> = {}) => {
|
||||
const { exitCode, output } = runHook(CAREFUL_SCRIPT, carefulInput(command.replaceAll('B/', `${b}/`)), env, path.join(b, cwd));
|
||||
expect(exitCode).toBe(0);
|
||||
return output.hookSpecificOutput?.permissionDecision ?? 'allow';
|
||||
};
|
||||
|
||||
test('a file delete inside a sibling worktree, either way round or from a nested worktree, is allowed', () => {
|
||||
withWorktrees((b) => {
|
||||
for (const [cwd, command] of [
|
||||
['app', 'rm B/app-192/src/a.txt'],
|
||||
['app', 'rm -f B/app-192/src/debug.spec.ts'],
|
||||
['app', 'rm -fv --interactive=never -- B/app-192/src/a.txt'],
|
||||
['app', 'unlink B/app-192/src/a.txt'],
|
||||
['app', 'rm B/app-192/src/a.txt && rm -f B/app-192/src/b.txt; unlink B/app-192/src/c.txt'],
|
||||
['app-192', 'rm B/app/src/a.txt'],
|
||||
['app/.claude/worktrees/x', 'rm -f B/app/src/a.txt'],
|
||||
['app-192', 'rm B/app/.claude/worktrees/x/src/a.txt'],
|
||||
]) {
|
||||
expect([command, decide(b, cwd, command)]).toEqual([command, 'allow']);
|
||||
}
|
||||
});
|
||||
}, 30_000);
|
||||
|
||||
test('roots, .git, the git folder, .., symlinks, globs, recursive deletes, find, rsync, shred and commands that run anything else still ask', () => {
|
||||
withWorktrees((b) => {
|
||||
for (const [cwd, command] of [
|
||||
['app', 'rm -f B/app-192'],
|
||||
['app', 'rm -f B/app-192/'],
|
||||
['app', 'rm -rf B/app-192/src'],
|
||||
['app', 'rm --rec B/app-192/src'],
|
||||
['app', 'rm --r -f B/app-192/src'],
|
||||
['app', 'rm --recurs B/app-192/src'],
|
||||
['app', 'rm -fx B/app-192/src/a.txt'],
|
||||
['app', 'rm --no-preserve-root B/app-192/src/a.txt'],
|
||||
['app', 'find B/app-192 -delete'],
|
||||
['app', 'rsync -a --delete ./src/ B/app-192'],
|
||||
['app-192', 'rm -f B/app'],
|
||||
['app-192', 'rm -f B/app/.claude/worktrees/x'],
|
||||
['app-192', 'find B/app/.claude -delete'],
|
||||
['app/.claude/worktrees/x', 'rm -f B/app/.claude'],
|
||||
['app', 'rm B/app-192/.git'],
|
||||
['app', 'rm B/app-192/.GIT'],
|
||||
['app-192', 'rm B/app/.git/config'],
|
||||
['app-192', 'rm -f B/app/.git/worktrees/app-192/HEAD'],
|
||||
['app', 'rm B/app-192/src/../../outside/secret'],
|
||||
['app', 'rm B/app-192/escape/secret'],
|
||||
['app', 'rm B/app-192/link-file'],
|
||||
['app', 'shred -u B/app-192/escape/secret'],
|
||||
['app', 'rm B/app-192/src/*.txt'],
|
||||
['app', 'rm -f B/app-192/.g*'],
|
||||
['app', 'rm $W/src/a.txt'],
|
||||
['app', 'git worktree remove --force B/app-192'],
|
||||
['app', 'find B/app-192/src -name "*.log" -delete'],
|
||||
['app', 'find -L B/app-192/src -delete'],
|
||||
['app', 'find B/app-192/src -exec rm {} +'],
|
||||
['app', 'rsync -a --delete ./src/ B/app-192/src/'],
|
||||
['app', 'shred -u B/app-192/src/a.txt'],
|
||||
['app', 'sudo rm B/app-192/src/a.txt'],
|
||||
['app', 'echo done && rm B/app-192/src/a.txt'],
|
||||
['app', 'mv B/app-192/src B/app-192/old && ln -s ~ B/app-192/src && rm B/app-192/src/.bashrc'],
|
||||
['app', 'rm B/app-192/src/a.txt; ln -s ~ B/app-192/d; rm B/app-192/d/.bashrc'],
|
||||
['app', 'rm B/app-192/d/.bashrc & mv B/app-192/d B/app-192/e'],
|
||||
['app', 'rm -f B/app-192/src/a.txt | cat'],
|
||||
]) {
|
||||
expect([command, decide(b, cwd, command)]).toEqual([command, 'ask']);
|
||||
}
|
||||
});
|
||||
}, 30_000);
|
||||
|
||||
test('another repository, a pruned worktree, a non-git folder, GIT_DIR, a home-folder repository and a worktree that is a personal folder grant nothing', () => {
|
||||
withWorktrees((b) => {
|
||||
expect(decide(b, 'app', 'rm B/other/src/a.txt')).toBe('ask');
|
||||
expect(decide(b, 'other', 'rm B/app-192/src/a.txt')).toBe('ask');
|
||||
expect(decide(b, 'app', 'rm B/gone/src/a.txt')).toBe('ask');
|
||||
expect(decide(b, 'plain', 'rm B/app-192/src/a.txt')).toBe('ask');
|
||||
expect(decide(b, 'plain', 'rm B/app-192/src/a.txt', { GIT_DIR: path.join(b, 'app', '.git') })).toBe('ask');
|
||||
expect(decide(b, 'app/src', 'rm B/app-192/src/a.txt', { HOME: path.join(b, 'app') })).toBe('ask');
|
||||
for (const dir of ['Documents', '.ssh']) {
|
||||
const r = gitArgvIn(path.join(b, 'app'), ['worktree', 'add', '-q', path.join(b, dir), '-b', `wt-${dir}`], 10_000);
|
||||
expect(r.status).toBe(0);
|
||||
expect(decide(b, 'app', `rm B/${dir}/src/a.txt`, { HOME: b })).toBe('ask');
|
||||
}
|
||||
expect(decide(b, 'app', 'rm B/app-192/src/a.txt', { HOME: b })).toBe('allow');
|
||||
});
|
||||
}, 30_000);
|
||||
});
|
||||
|
||||
describe('shared extractor keeps /careful command values unchanged (#3623)', () => {
|
||||
test.skipIf(process.platform === 'win32').each(['python3', 'node'])('with %s failing, a command still drops trailing newlines and nothing else', (broken) => {
|
||||
const shim = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-extract-shim-'));
|
||||
|
||||
Reference in new issue
Block a user