Skip to content

Use Garry Tan's exact Claude Code setup: 23 opinionated tools that serve as CEO, Designer, Eng Manager, Release Manager, Doc Engineer, and QA

TypeScript

Destructive-command check blocks four more ways past its disk-wipe and force-push checks (GST-3207) (#4534)

Garry Tanab6a38b76753
diff --git a/careful/bin/check-careful.sh b/careful/bin/check-careful.sh
index b9896eba6..e855884aa 100755
--- a/careful/bin/check-careful.sh
+++ b/careful/bin/check-careful.sh
@@ -161,9 +161,6 @@ _STASH_POP=0
_GIT_SUB=-1
_HI_RM_ROOT=0
_HI_DISK=0
-_WIPEFS_SEEN=0
-_WIPEFS_SAFE=0
-_STMT_N=0
_RM_UNSAFE=0
_OUTSIDE=0
_PUSH_DENY=""
@@ -364,76 +361,41 @@ _careful_del() {
return 0
}
-# wipefs -a erases every signature on its targets. _RX_WIPEFS denies any
-# wipefs with -a, --a, --al or --all; this scan only proves the one exemption
-# on top of it. An invocation is proven when wipefs leads its statement (after
-# sudo or NAME=value words); every target, with options read the way getopt
-# reads them, is a plain literal path outside /dev with a disk-image suffix
-# (.img, .iso, .raw, .qcow2, .vhd, .vhdx, .vmdk, .dmg) that already exists as
-# a regular file, not a symlink, in a known folder; and no redirect goes into
-# /dev other than /dev/null, /dev/stdout or /dev/stderr. An unproven
-# invocation with -a in any spelling is HIGH here too.
+# wipefs -a erases every signature on its targets. _RX_WIPEFS below denies
+# any wipefs with -a, --a, --al or --all in the command text; this scan reads
+# the options the way getopt does, so a spelling the regex can't see
+# (wipefs -$'a' /dev/sdb) is HIGH too. No target is exempt: GEVA-560's replay
+# of 254,322 agent commands holds no wipefs -a at all, so an image-file
+# exemption bought nothing, and each fact it had to prove was a way past the
+# deny.
_careful_wipefs() {
- local k="$1" t g j=0 all=0 hit=0 tgt=0 val=0 red=0 opts=1
- _WIPEFS_SEEN=$((_WIPEFS_SEEN + 1))
- while [ "$j" -lt $((k - 1)) ]; do
- case "${T[$j]}" in sudo|[A-Za-z_]*=*) ;; *) hit=1 ;; esac
- j=$((j + 1))
- done
+ local k="$1" t g val=0
while [ "$k" -lt "$n" ]; do
t="${T[$k]}"; k=$((k + 1))
if [ "$val" -eq 1 ]; then val=0; continue; fi
+ # A redirect and its target are not options: wipefs > -- -a still wipes.
case "$t" in
'') continue ;;
- '<'|'>'|'>>'|[0-9]'>'|[0-9]'>>'|[0-9]'<') red=1; continue ;;
+ '<'|'>'|'>>'|[0-9]'>'|[0-9]'>>'|[0-9]'<') val=1; continue ;;
+ [0-9]'>'*|'>'*|[0-9]'<'*|'<'*) continue ;;
esac
- if [ "$red" -eq 0 ]; then
- case "$t" in [0-9]'>'*|'>'*|[0-9]'<'*|'<'*) red=1; t="${t##*[<>]}" ;; esac
- fi
- if [ "$red" -eq 1 ]; then
- red=0
- _careful_join_dir "$_CUR_DIR" "$t"
- case "$_D" in
- /dev/null|/dev/stdout|/dev/stderr) ;;
- ''|/dev|/dev/*|*/dev/*) hit=1 ;;
- esac
- continue
- fi
- if [ "$opts" -eq 1 ]; then
- case "$t" in
- --) opts=0; continue ;;
- --a|--al|--all) all=1; continue ;;
- --offset|--types|--output) val=1; continue ;;
- --*) continue ;;
- -?*)
- g="${t#-}"
- while [ -n "$g" ]; do
- case "$g" in
- a*) all=1 ;;
- [toO]) val=1; break ;;
- [toO]*) break ;;
- esac
- g="${g#?}"
- done
- continue ;;
- esac
- fi
case "$t" in
- *[!A-Za-z0-9._/+@%,:~-]*|*/dev/*|dev/*) hit=1 ;;
- *.img|*.iso|*.raw|*.qcow2|*.vhd|*.vhdx|*.vmdk|*.dmg)
- _careful_join_dir "$_CUR_DIR" "$t"
- case "$_D" in
- ''|/dev/*) hit=1 ;;
- *) if [ -f "$_D" ] && [ ! -L "$_D" ]; then tgt=1; else hit=1; fi ;;
- esac ;;
- *) hit=1 ;;
+ --) break ;;
+ --a|--al|--all) _HI_DISK=1 ;;
+ --offset|--types|--output) val=1 ;;
+ --*) ;;
+ -?*)
+ g="${t#-}"
+ while [ -n "$g" ]; do
+ case "$g" in
+ a*) _HI_DISK=1 ;;
+ [toO]) val=1; break ;;
+ [toO]*) break ;;
+ esac
+ g="${g#?}"
+ done ;;
esac
done
- if [ "$hit" -eq 0 ] && [ "$tgt" -eq 1 ]; then
- _WIPEFS_SAFE=$((_WIPEFS_SAFE + 1))
- elif [ "$all" -eq 1 ]; then
- _HI_DISK=1
- fi
return 0
}
@@ -500,6 +462,7 @@ _careful_push() {
--mirror) force=1; all=1 ;;
--all|--branches) all=1 ;;
-n|--dry-run) dry=1 ;;
+ --no-dry-run) dry=0 ;;
-d|--delete) del=1 ;;
--repo=*) repo=1 ;;
--repo) repo=1; val=1 ;;
@@ -729,7 +692,6 @@ _careful_scan() {
continue ;;
$'\006') _PIPE=1; _SCRATCH="$_PL_S"; _CUR_DIR="$_PL_D"; continue ;;
esac
- case "$seg" in *[![:space:]]*) _STMT_N=$((_STMT_N + 1)) ;; esac
case "$seg" in
*rm*|*git*|*find*|*rsync*|*shred*|*unlink*|*cd*|*pushd*|*popd*|*wipefs*) ;;
*) continue ;;
@@ -748,7 +710,7 @@ _careful_scan() {
case "${T[$i]}" in
cd) if [ $((i + 1)) -lt "$n" ]; then _careful_cd "${T[$((i + 1))]}"; else _careful_cd ""; fi ;;
pushd|popd) _CD_SEEN=1; _SCRATCH=0; _CUR_DIR="" ;;
- wipefs|*/wipefs) _careful_wipefs $((i + 1)) ;;
+ wipefs|*/wipefs|'\wipefs') _careful_wipefs $((i + 1)) ;;
rm|'\rm'|*/rm|*':-rm'|*':=rm'|*':+rm') if [ "$i" -ne "$_GIT_SUB" ]; then _careful_rm $((i + 1)); fi ;;
git|*/git) _careful_git $((i + 1)) ;;
find|rsync|shred|unlink) _careful_del "${T[$i]}" $((i + 1)) ;;
@@ -823,21 +785,56 @@ if [ -n "$_PUSH_DENY" ]; then
fi
# wipefs with -a in any spelling (-a, a short group holding a, --a, --al,
# --all), however its targets arrive: on the line, from xargs or from stdin.
-# The one exemption: every statement in the command is a wipefs invocation
-# the statement scan proved, and the command holds no other wipefs word.
-_RX_WIPEFS="(^|[[:space:];&|(/'\"${_BT}])wipefs['\"]?([[:space:]]${_NS})?(-a|[[:space:]'\"]-[[:alnum:]]*a)"
+_RX_WIPEFS="(^|[[:space:];&|(/'\"${_BT}\\])wipefs['\"]*([[:space:]]${_NS})?(-a|[[:space:]'\"]-[[:alnum:]]*a)"
_WIPEFS_HI=0
case "$CMD_MEDIUM" in
- *wipefs*)
- if [[ $CMD_MEDIUM =~ $_RX_WIPEFS ]]; then
- _WIPEFS_HI=1
- _W_REST="${CMD_MEDIUM//wipefs/}"
- if [ "$_WIPEFS_SAFE" -gt 0 ] && [ "$_WIPEFS_SAFE" -eq "$_WIPEFS_SEEN" ] && [ "$_WIPEFS_SAFE" -eq "$_STMT_N" ] && [ $(( (${#CMD_MEDIUM} - ${#_W_REST}) / 6 )) -eq "$_WIPEFS_SEEN" ]; then _WIPEFS_HI=0; fi
- fi ;;
+ *wipefs*) if [[ $CMD_MEDIUM =~ $_RX_WIPEFS ]]; then _WIPEFS_HI=1; fi ;;
+esac
+# A redirect into a whole disk counts with its quotes taken out (>'/dev'/sdb),
+# and through a folder that is a symlink to /dev (d/sdb with d -> /dev): the
+# folder is resolved on disk, not read as text.
+_RX_DISK_REDIR=">[|]?[[:space:]]*(/dev/|([^[:space:];&|<>()\$${_BT}]*/)?)(sd|nvme|disk|rdisk|hd|vd|xvd|mmcblk)[a-z0-9]*([^[:alnum:]_.-]|\$)"
+_HI_REDIR=0
+_REDIR_OVER=0
+_RX_DISK_REDIR_ANY=">[|]?[[:space:]]*[^[:space:];&|<>()]*(sd|nvme|disk|rdisk|hd|vd|xvd|mmcblk)"
+case "$CMD_MEDIUM" in
+ *'>'*)
+ _rest=""
+ # grep lists each distinct redirect once, so repeated text costs one probe.
+ # Every redirect written into /dev/ is read with no limit; past 64 folders
+ # to look up on disk the scan stops and the command asks.
+ if [[ $CMD_MEDIUM =~ $_RX_DISK_REDIR_ANY ]]; then
+ _rest=$(printf '%s\n' "$CMD_MEDIUM" | tr -d "\"'" | LC_ALL=C grep -oE "$_RX_DISK_REDIR" | LC_ALL=C sort -u) || _rest=""
+ fi
+ _RX_DEV_LINE=$'(^|\n)>[|]?[[:space:]]*/dev/'
+ if [[ $_rest =~ $_RX_DEV_LINE ]]; then _HI_REDIR=1; fi
+ _probes=0
+ while IFS= read -r _m; do
+ [ "$_HI_REDIR" -eq 0 ] || break
+ [[ $_m =~ $_RX_DISK_REDIR ]] || continue
+ _d="${BASH_REMATCH[1]}"
+ case "$_d" in
+ /dev/) _HI_REDIR=1 ;;
+ *)
+ if [ "$_probes" -ge 64 ]; then _REDIR_OVER=1; break; fi
+ _probes=$((_probes + 1))
+ _d="${_d%/}"
+ case "$_d" in /*) ;; '') _d="$_CAREFUL_CWD" ;; *) _d="$_CAREFUL_CWD/$_d" ;; esac
+ if [[ "$(cd -P -- "$_d" 2>/dev/null && pwd -P)" =~ ^/+dev/*$ ]]; then _HI_REDIR=1; fi ;;
+ esac
+ done <<EOF_REDIR
+$_rest
+EOF_REDIR
+ ;;
esac
-if [ "$_HI_DISK" -eq 1 ] || [ "$_WIPEFS_HI" -eq 1 ] || [[ $CMD_MEDIUM =~ $_RX_DISK ]]; then
+if [ "$_HI_DISK" -eq 1 ] || [ "$_WIPEFS_HI" -eq 1 ] || [ "$_HI_REDIR" -eq 1 ] || [[ $CMD_MEDIUM =~ $_RX_DISK ]]; then
_careful_deny high_disk "Writing to, formatting or wiping a whole disk is blocked by gstack's destructive-command check. If the user truly means it, they run it themselves."
fi
+if [ "$_REDIR_OVER" -eq 1 ]; then
+ _careful_log_fire disk_redirect_scan_cap
+ gstack_hook_decision ask "[careful] This command writes to more than 64 folders under disk-like names, more than the destructive-command check looks up, so it could not be fully checked. Approve only if you know what it does."
+ exit 0
+fi
# --- Check for safe exceptions (one standalone rm of build artifacts) ---
# Match the complete command. Parsing only the last rm is unsafe because shell
diff --git a/changelog.d/careful-stricter-wipefs-redirects-comments.md b/changelog.d/careful-stricter-wipefs-redirects-comments.md
new file mode 100644
index 000000000..a0b65dc2a
--- /dev/null
+++ b/changelog.d/careful-stricter-wipefs-redirects-comments.md
@@ -0,0 +1,4 @@
+---
+important: regression
+---
+- **The destructive-command check closes three ways past its disk-wipe and force-push blocks.** `wipefs -a` is now blocked on every target, image files included, and also when spelled `\wipefs` or with doubled quotes. A redirect into a disk is blocked when its path is quoted (`>'/dev'/sdb`) or goes through a folder that links to `/dev`, and `mmcblk` and `rdisk` devices count. `git push -fn --no-dry-run` is read as a real force-push. Nothing that was asked about or blocked before is allowed now.
diff --git a/test/careful-replay.test.ts b/test/careful-replay.test.ts
index 3a24e34cf..714d3f777 100644
--- a/test/careful-replay.test.ts
+++ b/test/careful-replay.test.ts
@@ -211,7 +211,7 @@ const PARSER_CASES: Array<[string, string, Decision, Tree]> = [
['dry run in another statement', 'aws s3 rm s3://example-invalid/demo --recursive --dryrun; terraform destroy', 'ask', 'clean'],
['dry run in another statement', 'terraform destroy -var name=plan', 'ask', 'clean'],
['push dry run in a flag group', 'git push -nf origin main', 'allow', 'clean'],
- ['wipefs on a file', 'wipefs -a demo.img', 'allow', 'clean'],
+ ['wipefs on a file (GST-3207: no image exemption)', 'wipefs -a demo.img', 'deny', 'clean'],
['wipefs on a file (control)', 'wipefs -a /dev/sdb', 'deny', 'clean'],
['SQL shape in a search or text', "grep 'truncate demo;' fixtures.sql", 'allow', 'clean'],
['SQL shape in a search or text', "echo 'truncate demo;'", 'allow', 'clean'],
@@ -235,7 +235,7 @@ const PARSER_CASES: Array<[string, string, Decision, Tree]> = [
['round 2: cd after time', 'time cd B && git reset --hard', 'ask', 'clean'],
['round 2: cd then a discard (control)', 'cd B && git clean -fdx', 'ask', 'clean'],
['round 2: an image suffix under /dev', 'wipefs -a /dev/sdb.img', 'deny', 'clean'],
- ['round 2: wipefs option values', 'wipefs -t ext4 -a demo.img', 'allow', 'clean'],
+ ['round 2: wipefs option values (GST-3207: no image exemption)', 'wipefs -t ext4 -a demo.img', 'deny', 'clean'],
['wipefs in the regex again: targets from xargs', 'ls /dev/sd* | xargs wipefs -a', 'deny', 'clean'],
['wipefs in the regex again: targets from stdin', 'echo /dev/sdb | xargs wipefs --all', 'deny', 'clean'],
['wipefs in the regex again: --a', 'wipefs --a /dev/sdb', 'deny', 'clean'],
@@ -244,13 +244,39 @@ const PARSER_CASES: Array<[string, string, Decision, Tree]> = [
['wipefs in the regex again: attached value in a group', 'wipefs -aftvfat /dev/sdb', 'deny', 'clean'],
['wipefs in the regex again: image suffix under /dev', 'wipefs --al /dev/sdb.img', 'deny', 'clean'],
['wipefs in the regex again: image names from xargs', 'ls *.img | xargs wipefs -a', 'deny', 'clean'],
- ['wipefs in the regex again: attached value on an image (control)', 'wipefs -a -tvfat demo.img', 'allow', 'clean'],
- ['wipefs in the regex again: several images (control)', 'wipefs --all disk.qcow2 build/b.iso', 'allow', 'clean'],
+ ['wipefs in the regex again: attached value on an image (GST-3207: no image exemption)', 'wipefs -a -tvfat demo.img', 'deny', 'clean'],
+ ['wipefs in the regex again: several images (GST-3207: no image exemption)', 'wipefs --all disk.qcow2 build/b.iso', 'deny', 'clean'],
['wipefs round 2: an image name that is a symlink', 'wipefs -a alias.img', 'deny', 'clean'],
['wipefs round 2: a symlink made in the same command', 'ln -sf /dev/sdb d.img; wipefs -a d.img', 'deny', 'clean'],
['wipefs round 2: output redirected into a disk', 'wipefs -a demo.img > /dev/mmcblk0', 'deny', 'clean'],
['wipefs round 2: a quoted spelling beside a proven wipe', "wipefs -a demo.img; wi''pefs -a /dev/sdb", 'deny', 'clean'],
- ['wipefs round 2: errors sent to /dev/null (control)', 'wipefs -a demo.img 2>/dev/null', 'allow', 'clean'],
+ ['wipefs round 2: errors sent to /dev/null (GST-3207: no image exemption)', 'wipefs -a demo.img 2>/dev/null', 'deny', 'clean'],
+];
+
+// GST-3207: checks that only get stricter than 17303862: wipefs -a on every target, disk redirects with quotes or
+// through a link to /dev, and --no-dry-run. Every row but the controls was allowed or asked at 17303862;
+// [item, command, expected, tree].
+const STRICT_CASES: Array<[string, string, Decision, Tree]> = [
+ ['wipefs: no image exemption', "wipefs -a demo.img >'/dev'/sdb", 'deny', 'clean'],
+ ['wipefs: no image exemption', 'wipefs -a demo.img" #" /dev/sda', 'deny', 'clean'],
+ ['wipefs: no image exemption', 'wipefs -a demo.img > devlink/sdb', 'deny', 'clean'],
+ ['wipefs: no image exemption (control: without -a it only lists)', 'wipefs demo.img', 'allow', 'clean'],
+ ['wipefs: a longer word is not wipefs (control)', 'echo wipefs-archive && wipefs -a /dev/sdb', 'deny', 'clean'],
+ ['wipefs: a backslash before the name', '\\wipefs -a /dev/sdb', 'deny', 'clean'],
+ ['quoted /dev redirect', "echo hi >'/dev'/sdb", 'deny', 'clean'],
+ ['quoted /dev redirect', 'echo hi > "/dev/mmcblk0"', 'deny', 'clean'],
+ ['redirect through a folder that links to /dev', 'echo hi > devlink/sdb', 'deny', 'clean'],
+ ['redirect through a folder that links to /dev (control: not a disk name)', 'echo hi > devlink/null', 'allow', 'clean'],
+ ['redirect through a folder that links to /dev (control: a real folder)', 'echo hi > build/sdb', 'allow', 'clean'],
+ ['push dry run undone later', 'git push -fn --no-dry-run origin main', 'deny', 'clean'],
+ ['push dry run undone later', 'git push --force -n --no-dry-run origin main', 'deny', 'clean'],
+ ['push dry run undone later (control: the last flag wins)', 'git push --no-dry-run -fn origin main', 'allow', 'clean'],
+ ['wipefs: a redirect is not an option end', 'wipefs > -- -$\'a\' /dev/sdb', 'deny', 'clean'],
+ ['wipefs: a backslashed name with a getopt spelling', '\\wipefs -$\'a\' /dev/sdb', 'deny', 'clean'],
+ ['wipefs: a longer word still denies, as at 17303862', 'echo wipefs-archive', 'deny', 'clean'],
+ ['unchanged from 17303862 (control: words after a quoted # stay unread)', 'echo \'git push -f origin main # --dry-run\'', 'deny', 'clean'],
+ ['unchanged from 17303862 (control: words after a quoted # stay unread)', 'echo \'git restore # --staged\'', 'ask', 'dirty'],
+ ['unchanged from 17303862 (control: a backslashed cd stays unread)', '\\cd /srv/demo && git push -f', 'ask', 'clean'],
];
let base = '';
@@ -273,6 +299,7 @@ function makeTree(kind: Tree): string {
fs.writeFileSync(path.join(dir, image), '');
}
fs.symlinkSync('/dev/null', path.join(dir, 'alias.img'));
+ fs.symlinkSync('/dev', path.join(dir, 'devlink'));
git(['add', '.']);
git(['commit', '-q', '-m', 'init']);
git(['update-ref', 'refs/remotes/origin/main', 'HEAD']);
@@ -342,6 +369,25 @@ describe('each new rule, with negative controls', () => {
expect(decide(command, tree).decision).toBe(expected);
});
+ test.each(STRICT_CASES)('%s: %s -> %s (%s tree)', (_item, command, expected, tree) => {
+ expect(decide(command, tree).decision).toBe(expected);
+ });
+
+ test('a long command full of redirect-like words is still checked well inside the guard hook\'s 8 s timeout', () => {
+ const body = Array.from({ length: 12000 }, (_, i) => `a > d${i}/sdb`).join(' ');
+ const started = Date.now();
+ expect(decide(`echo "${body}"; rm -rf src`, 'clean').decision).toBe('ask');
+ expect(Date.now() - started).toBeLessThan(6000);
+ });
+
+ test('a redirect into a disk is still found behind many other redirects', () => {
+ const decoys = (n: number, dir: (i: number) => string) => Array.from({ length: n }, (_, i) => `echo > ${dir(i)}/sdb${i};`).join(' ');
+ expect(decide(`${decoys(70, () => 'x')} echo hi >'/dev'/sdb`, 'clean').decision).toBe('deny');
+ expect(decide(Array.from({ length: 20000 }, (_, i) => `>'/dev'/sd${i}`).join(' '), 'clean').decision).toBe('deny');
+ expect(decide(`${decoys(70, (i) => `d${i}`)} echo hi > devlink/sdb`, 'clean').decision).toBe('ask');
+ expect(decide(`${decoys(60, (i) => `d${i}`)} echo hi > build/sdb`, 'clean').decision).toBe('allow');
+ });
+
test('a 200 KB command is still checked well inside the guard hook\'s 8 s timeout', () => {
const body = Array.from({ length: 4000 }, (_, i) => `const x${i} = f(a, b) && (c || d); // ${i}`).join('\n');
const started = Date.now();