Destructive-command check catches more force pushes and outside deletes, and stops asking about quoted spellings (#4604)

The destructive-command check now catches force pushes to main hidden
behind abbreviated or misplaced dry-run flags, and asks before deletes
that reach a system folder through a `cd` (including `cd ../../etc`) or
a device node under /dev.

Quoting no longer changes its decision: a quoted build-folder delete or
a quoted `gh` title or body gets the same answer as its unquoted
spelling, which was already allowed. Fixes #4628, fixes #4601.

Nothing to do; no command that main asks about or blocks becomes allowed
except those same-command spellings.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Co-authored-by: TayTaySwizzle <45181777+TayTaySwizzle@users.noreply.github.com>
Review-Finding: p1/red-team#1 reader=red-team
This commit is contained in:
authored and GitHub committed 2026-11-22 11:52:02 +00:00
1 parent 3cff6a47f6
commit 9055979608
6 files changed
+261 -13

No files matched your search

+84 -8
View File
@@ -102,8 +102,9 @@ if grep -qE '\$\{IFS\}|\$IFS|\$\(echo[^)]*base64[^)]*\)|base64[[:space:]]+(-d|--
fi
# --- Commit-message masking (#1060) ---
# Mask quoted -m/-am/--message argument content in git commit commands so that
# descriptive commit messages do not trigger false positives. The quote-aware
# Mask quoted -m/-am/--message argument content in git commit commands, and the
# quoted --title/--body of gh issue and gh pr create, comment and edit (#4601),
# so that descriptive text does not trigger false positives. The quote-aware
# scanner lives in mask-commit-message.awk beside this hook: as a heredoc inside
# a command substitution, its quote characters are mis-parsed by bash 3.2
# (macOS /bin/bash), which made this whole hook a syntax error there. A missing
@@ -114,7 +115,7 @@ PATTERN=""
CMD_MEDIUM="$CMD"
CMD_MEDIUM_LOWER="$CMD_LOWER"
_MASK_AWK="$_HOOK_DIR/mask-commit-message.awk"
case "$CMD" in *git*commit*) _MAYBE_COMMIT=1 ;; *) _MAYBE_COMMIT=0 ;; esac
case "$CMD" in *git*commit*|*gh*) _MAYBE_COMMIT=1 ;; *) _MAYBE_COMMIT=0 ;; esac
if [ "$_MAYBE_COMMIT" -eq 1 ] && [ -f "$_MASK_AWK" ]; then
if _cmd_masked=$(printf '%s' "$CMD" | awk -f "$_MASK_AWK" 2>/dev/null); then
CMD_MEDIUM="$_cmd_masked"
@@ -140,6 +141,9 @@ _RX_CACHE="^${_RX_HOME}/(\.cache|\.npm|\.bun/install/cache|\.cargo/registry|\.lo
_RX_ARTIFACT="^([^[:space:];&|#(${_BT}\$]*/)?(node_modules|\.next|dist|__pycache__|\.cache|build|\.turbo|coverage|\.pytest_cache|\.mypy_cache|\.ruff_cache|\.tox|\.nox|htmlcov|\.eggs|\.venv|venv|[^/[:space:]]+\.egg-info)(/|/\*)?\$"
_RX_ROOT="^(/+|/+\*|${_RX_HOME}(/+|/+\*)?|/(usr|etc|var|home|opt|bin|sbin|lib|lib64|boot|root|srv|System|Users|Library|Applications|private|mnt/c|c)(/+|/+\*)?|${_RX_HOME}/(Documents|Desktop|Downloads|Library|Pictures|Movies|Music|\.ssh|\.config|\.aws|\.gnupg|\.kube)(/+|/+\*)?)\$"
# A device node under /dev, and a system folder a cd can lead a relative delete into.
_RX_DEV_NODE='^/dev/(sd|nvme|hd|vd|xvd|mmcblk|loop|disk|rdisk|md|dm-|nbd|sr|mapper/|sg[0-9]|zram[0-9]|ram[0-9]|mtd|ubi)'
_RX_SYSDIR='^/(etc|usr|boot|bin|sbin|lib|lib32|lib64|root|opt|var/(lib|log|db|spool)|System|Library|private/etc)/'
_CAREFUL_CWD="${GSTACK_HOOK_CWD:-}"
[ -n "$_CAREFUL_CWD" ] || _CAREFUL_CWD="$PWD"
if command -v gstack_hook_normalize_path >/dev/null 2>&1; then
@@ -230,6 +234,12 @@ _careful_rm_safe() {
return 1
}
# _SQ is $1 with doubled slashes and /./ folded, the way the kernel reads it.
_careful_squash() {
_SQ="$1"
while [[ $_SQ == *//* || $_SQ == */./* ]]; do _SQ=${_SQ//\/\//\/}; _SQ=${_SQ//\/.\//\/}; done
}
# 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.
@@ -237,6 +247,8 @@ _careful_outside() {
local t="$1"
[ -n "$t" ] || return 1
case "/$t/" in */../*) return 0 ;; esac
[[ $t =~ $_RX_DEV_NODE ]] && return 0
_careful_squash "$t"; [[ $_SQ =~ $_RX_DEV_NODE ]] && return 0
case "$t" in /dev/*|/proc/*|/tmp|/tmp/|/var/tmp|/var/tmp/|/private/tmp|/private/tmp/) return 1 ;; esac
[[ $t =~ $_RX_TMP ]] && return 1
[[ $t =~ $_RX_CACHE ]] && return 1
@@ -254,13 +266,41 @@ _careful_outside() {
'~'|'~/'*) t="${HOME:-}${t#\~}" ;;
'~'*) return 0 ;;
/*) ;;
*) return 1 ;;
*)
[ "$_SCRATCH" -eq 0 ] && [ -n "$_CUR_DIR" ] || return 1
_careful_squash "$_CUR_DIR/$t"
if [[ $_CUR_DIR/ =~ $_RX_SYSDIR ]]; then t="$_CUR_DIR/$t"
elif [[ $_SQ =~ $_RX_DEV_NODE ]]; then return 0
elif [[ $_SQ =~ $_RX_SYSDIR ]]; then t="$_SQ"
else
# A cd that climbs with .. (cd ../../etc) lands where cd folds it.
_D="$_SQ"; _careful_fold
if [[ $_D =~ $_RX_DEV_NODE ]]; then return 0
elif [[ $_D =~ $_RX_SYSDIR ]]; then t="$_D"
else return 1; fi
fi ;;
esac
t="${t%/}"
if [ -n "$_CAREFUL_CWD" ] && { [ "$t" = "$_CAREFUL_CWD" ] || [ "${t#"$_CAREFUL_CWD"/}" != "$t" ]; }; then return 1; fi
return 0
}
# _D with . and .. folded the way cd reads an absolute path (logically).
_careful_fold() {
case "$_D" in /*) ;; *) return 0 ;; esac
local out="" part rest="${_D#/}"
while [ -n "$rest" ]; do
part="${rest%%/*}"
case "$rest" in */*) rest="${rest#*/}" ;; *) rest="" ;; esac
case "$part" in
''|.) ;;
..) out="${out%/*}" ;;
*) out="$out/$part" ;;
esac
done
_D="${out:-/}"
}
# _CUR_DIR is the folder a cd leads to, or empty when it is unknown.
_careful_cd() {
_CD_SEEN=1
@@ -450,19 +490,37 @@ _careful_push_default() {
return 0
}
# Each push is read twice: once with a push-option value counted as an operand
# (git push -f -o x main), and once without (git push -f -o ci.skip origin on
# main), so either reading can deny.
_careful_push() {
local j=0 t c r force=0 lease=0 dry=0 del=0 np=0 nr=0 repo=0 all=0 val=0
_careful_push_scan 0
_careful_push_scan 1
return 0
}
_careful_push_scan() {
local j=0 t c r force=0 lease=0 dry=0 del=0 np=0 nr=0 repo=0 all=0 val=0 pov=0 pw=0 d0=0 fd=0 dd=0 e0=0
local refs=()
while [ "$j" -lt "$na" ]; do
t="${a[$j]}"; j=$((j + 1))
if [ "$val" -eq 1 ]; then val=0; continue; fi
# git reads the word after -o as its push-option value, so it never starts a
# dry run: the value keeps the dry-run state before it. Its other effects
# still count, since an option or redirect before -o can take -o itself.
pw=0
if [ "$pov" -eq 1 ]; then
pov=0
pw=1; d0=$dry; e0=$dd
fi
case "$t" in
--force-with-lease*|--force-if-includes) lease=1 ;;
-f|--force) force=1 ;;
--mirror) force=1; all=1 ;;
--all|--branches) all=1 ;;
-n|--dry-run) dry=1 ;;
--no-dry-run) dry=0 ;;
--no-dr|--no-dry|--no-dry-|--no-dry-r|--no-dry-ru|--no-dry-run) dry=0; dd=0 ;;
--pu|--pus|--push|--push-|--push-o|--push-op|--push-opt|--push-opti|--push-optio|--push-option) pov=1 ;;
-d|--delete) del=1 ;;
--repo=*) repo=1 ;;
--repo) repo=1; val=1 ;;
@@ -471,14 +529,25 @@ _careful_push() {
# In a short-option group, letters after o are its push-option value.
c="${t#-}"
case "$c" in
*[!a-zA-Z]*) ;;
*[!a-zA-Z]*)
# A group with a digit (-f4, -4f, -fo-n) still forces, unless the same
# group holds its own -n (-fn4 is a dry run, as main reads it).
case "${c%%o*}" in
''|*[!a-zA-Z0-9]*) ;;
*)
[ "${c%%o*}o" != "$c" ] || pov=1
case "${c%%o*}" in *f*) fd=1 ;; esac
case "${c%%o*}" in *n*) dd=1 ;; esac ;;
esac ;;
*)
[ "${c%%o*}o" != "$c" ] || pov=1
c="${c%%o*}"
case "$c" in *f*) force=1 ;; esac
case "$c" in *n*) dry=1 ;; esac ;;
esac ;;
[0-9]'>'*|'>'*|'<'*) ;;
*)
if [ "$pw" -eq 1 ] && [ "$1" -eq 1 ]; then pov=0; [ "$d0" -eq 1 ] || dry=0; [ "$e0" -eq 1 ] || dd=0; continue; fi
np=$((np + 1))
# With --repo the first operand may be a refspec too: read it as one.
if [ "$np" -gt 1 ] || [ "$repo" -eq 1 ]; then
@@ -487,8 +556,10 @@ _careful_push() {
refs[$nr]="$r"; nr=$((nr + 1))
fi ;;
esac
if [ "$pw" -eq 1 ]; then pov=0; [ "$d0" -eq 1 ] || dry=0; [ "$e0" -eq 1 ] || dd=0; fi
done
[ "$dry" -eq 0 ] || return 0
if [ "$force" -eq 0 ] && [ "$fd" -eq 1 ] && [ "$dd" -eq 0 ]; then force=1; fi
if [ "$force" -eq 1 ] && [ -z "$_PUSH_DENY" ]; then _careful_push_default; fi
if [ "$force" -eq 1 ] || [ "$lease" -eq 1 ]; then _PUSH_FORCE=1; fi
if [ "$del" -eq 1 ]; then _PUSH_DELETE=1; fi
@@ -857,7 +928,12 @@ case "$CMD" in
*$'\n'*) : ;; # multi-line: fall through to the destructive checks
*..*) : ;; # a path climbing out with .. is never build output here
*rm*)
if grep -qE '^[[:space:]]*rm[[:space:]]+(-[a-zA-Z]*[rR][a-zA-Z]*[[:space:]]+|--recursive[[:space:]]+)(([^[:space:];&|#(`]*/)?(node_modules|\.next|dist|__pycache__|\.cache|build|\.turbo|coverage)[[:space:]]*)+$' <<< "$CMD" 2>/dev/null; then
# A target may also sit in one pair of quotes when nothing inside them
# differs from its unquoted spelling (#4628: rm -rf "/home/me/app/dist").
_RX_BUILD_NAMES='(node_modules|\.next|dist|__pycache__|\.cache|build|\.turbo|coverage)'
_RX_BUILD_Q="([A-Za-z0-9_./@+,:%=-]*/)?${_RX_BUILD_NAMES}"
_RX_BUILD_T="(([^[:space:];&|#(${_BT}]*/)?${_RX_BUILD_NAMES}|\"${_RX_BUILD_Q}\"|'${_RX_BUILD_Q}')"
if grep -qE "^[[:space:]]*rm[[:space:]]+(-[a-zA-Z]*[rR][a-zA-Z]*[[:space:]]+|--recursive[[:space:]]+)${_RX_BUILD_T}([[:space:]]+${_RX_BUILD_T})*[[:space:]]*\$" <<< "$CMD" 2>/dev/null; then
echo '{}'
exit 0
fi
+17 -4
View File
@@ -6,7 +6,9 @@
#
# Only git commit's own arguments are masked: the scanner splits the command
# at unquoted ; & | ( ) and newlines, and masks a message flag only in a
# segment whose first word is git and that has a commit word after it. The &
# segment whose first word is git and that has a commit word after it. The
# quoted --title/--body (-t/-b) of gh issue or gh pr create, comment and edit
# are masked the same way, in a segment that starts with those three words. The &
# of a redirection (2>&1, &>) does not split. Any pipe after a masked message
# prints the command unchanged, because git prints the message on stdout.
# Quotes, backslashes and comments follow bash: a backslash escapes the next
@@ -75,11 +77,19 @@ function flag(s, i, rest, m) {
if (match(rest, /^(--message|-am|-m)[ \t]+/)) return RLENGTH
return 0
}
# ghflag S I — length of the gh title or body flag (with its trailing blanks) at S[I], or 0
function ghflag(s, i, rest) {
rest = substr(s, i)
if (rest ~ /^--title=/) return 8
if (rest ~ /^--body=/) return 7
if (match(rest, /^(--title|--body|-t|-b)[ \t]+/)) return RLENGTH
return 0
}
{ cmd = (NR > 1) ? cmd "\n" $0 : $0 }
END {
s = cmd
n = length(s); sq = 0; dq = 0; bs = 0; i = 1
atword = 1; words = 0; git = 0; commit = 0; masked = 0; piped = 0; unsure = 0
atword = 1; words = 0; git = 0; gh = 0; commit = 0; masked = 0; piped = 0; unsure = 0
while (i <= n) {
c = substr(s, i, 1)
if (bs) { result = result c; bs = 0; i++; continue }
@@ -99,7 +109,7 @@ END {
if (c == "&" && (substr(s, i - 1, 1) ~ /[<>]/ || substr(s, i + 1, 1) == ">")) { atword = 0; result = result c; i++; continue }
if (c ~ /[;&|()\n]/) {
if (c == "|" && masked && substr(s, i + 1, 1) != "|" && substr(s, i - 1, 1) != "|") piped = 1
atword = 1; words = 0; git = 0; commit = 0
atword = 1; words = 0; git = 0; gh = 0; commit = 0
result = result c; i++; continue
}
if (c == " " || c == "\t") { atword = 1; result = result c; i++; continue }
@@ -107,8 +117,11 @@ END {
atword = 0; words++
rest = substr(s, i)
if (words == 1 && rest ~ /^git([ \t\n;&|()]|$)/) git = 1
else if (words == 1 && rest ~ /^gh([ \t\n;&|()]|$)/) gh = 1
else if (git && !commit && rest ~ /^commit([ \t\n;&|()]|$)/) commit = 1
else if (commit && (len = flag(s, i)) > 0) {
else if (gh == 1 && words == 2 && rest ~ /^(issue|pr)([ \t\n;&|()]|$)/) gh = 2
else if (gh == 2 && words == 3 && rest ~ /^(create|comment|edit)([ \t\n;&|()]|$)/) gh = 3
else if ((commit && (len = flag(s, i)) > 0) || (gh == 3 && (len = ghflag(s, i)) > 0)) {
result = result substr(s, i, len); i += len
j = handle_msg(s, n, i)
if (j > i) masked = 1
@@ -0,0 +1,4 @@
---
important: security
---
**The destructive-command check catches more force pushes and outside deletes, and stops asking about a few harmless spellings.** A force push to main is no longer read as a dry run when `--no-dry-run` is abbreviated (`git push -fn --no-dry origin main`), when the `-n` is really the value of `-o`/`--push-option` (`git push -f -o -n origin main`, also with `--push`), or when `-f` sits in a group with a digit (`-f4`, `-4f`, `-fo-n`). A push-option value is no longer read as the remote, so `git push -f -o ci.skip origin` on main is blocked like `git push -f origin`. A relative delete after `cd` into a system folder (`cd /etc/nginx && rm -f sites-enabled/default`, also `cd ../../etc`) and removing a device node under `/dev` (`rm -f /dev/loop0`, `/dev/sg0`, `/dev/zram0`) now ask, like their absolute-path forms. A push-option value never makes a push safer. It can't turn a push into a dry run, and a `--no-dry-run` after `-o` still undoes `-n`. Quoting no longer changes the decision: `rm -rf "/home/me/app/dist"` is allowed like `rm -rf /home/me/app/dist` (#4628), and the quoted title or body of `gh issue` or `gh pr` create, comment and edit is read as text, like its backslashed spelling (#4601).
+147
View File
@@ -279,6 +279,137 @@ const STRICT_CASES: Array<[string, string, Decision, Tree]> = [
['unchanged from 17303862 (control: a backslashed cd stays unread)', '\\cd /srv/demo && git push -f', 'ask', 'clean'],
];
// #4534 review notes: git reads an unambiguous prefix of --no-dry-run as the option itself, and the word after -o as
// its push-option value. Each exemption keeps its dangerous neighbours denied.
const NOTE_CASES: Array<[string, string, Decision, Tree]> = [
['an abbreviated --no-dry-run undoes -n', 'git push -fn --no-dry origin main', 'deny', 'clean'],
['an abbreviated --no-dry-run undoes -n', 'git push -fn --no-dr origin main', 'deny', 'clean'],
['an abbreviated --no-dry-run undoes -n', 'git push --force -n --no-dry-ru origin main', 'deny', 'clean'],
['an abbreviated --no-dry-run undoes -n (control: the last flag wins)', 'git push --no-dry -fn origin main', 'allow', 'clean'],
['an abbreviated --no-dry-run undoes -n (control: git refuses the ambiguous --no-d)', 'git push -fn --no-d origin main', 'allow', 'clean'],
['a push-option value never makes a push safer, as main reads it', 'git push -fn -o --no-dry-run origin main', 'deny', 'clean'],
['a push-option value never makes a push safer, as main reads it', 'git push -fno --no-dry-run origin main', 'deny', 'clean'],
['a push-option value never makes a push safer, as main reads it', 'git push -fn --push-option --no-dry-run origin main', 'deny', 'clean'],
['a push-option value never makes a push safer (an option that takes a value)', 'git push -n -f --receive-pack -o --no-dry-run origin main', 'deny', 'clean'],
['a push-option value never makes a push safer (an option that takes a value)', 'git push -n -f --exec -o --no-dry-run origin main', 'deny', 'clean'],
['a push-option value never makes a push safer (an option that takes a value)', 'git push -n -f --recurse-submodules -o --no-dry-run origin main', 'deny', 'clean'],
['a push-option value never makes a push safer (a redirect target)', 'git push -fn > -o --no-dry-run origin main', 'deny', 'clean'],
['a push-option value never makes a push safer (a redirect target)', 'git push -fn 2> -o --no-dry-run origin main', 'deny', 'clean'],
['a push-option value never makes a push safer (a redirect target)', 'git push -fn < --push-option --no-dry-run origin main', 'deny', 'clean'],
['the word after -o is its value (neighbour: no -n)', 'git push -f -o --no-dry-run origin main', 'deny', 'clean'],
['the word after -o is its value (neighbour: -o took another value)', 'git push -fn -o ci.skip --no-dry-run origin main', 'deny', 'clean'],
['the word after -o is its value (neighbour: value attached in the group)', 'git push -fnox --no-dry-run origin main', 'deny', 'clean'],
['the word after -o is its value (neighbour: a second --no-dry-run)', 'git push -fn -o --no-dry-run --no-dry origin main', 'deny', 'clean'],
['a -n read as the -o value is not a dry run', 'git push -f -o -n origin main', 'deny', 'clean'],
['a -n read as the -o value is not a dry run', 'git push -f -o --dry-run origin main', 'deny', 'clean'],
['a -n read as the -o value is not a dry run', 'git push -fo -n origin main', 'deny', 'clean'],
['a -n read as the -o value is not a dry run', 'git push -f -o -n4 origin main', 'deny', 'clean'],
['a -n read as the -o value is not a dry run (neighbour: the value still forces)', 'git push -o -fn --no-dry origin main', 'deny', 'clean'],
['a -n read as the -o value is not a dry run (control: -n before -o)', 'git push -fn -o ci.skip origin main', 'allow', 'clean'],
['a push-option value never takes a value itself', 'git push -n -o -o --no-dry-run -f origin main', 'deny', 'clean'],
['a push-option value never takes a value itself', 'git push -n -o --push --no-dry-run -f origin main', 'deny', 'clean'],
['a push-option value never takes a value itself', 'git push -n -fo -o --no-dry-run origin main', 'deny', 'clean'],
['an abbreviated --push-option takes its value', 'git push -f --push -n origin main', 'deny', 'clean'],
['an abbreviated --push-option takes its value', 'git push -f --push-opt --dry-run origin main', 'deny', 'clean'],
['an abbreviated --push-option takes its value (control: -n before it)', 'git push -fn --push ci.skip origin main', 'allow', 'clean'],
['a short group with a digit still reads -f', 'git push -f4 origin main', 'deny', 'clean'],
['a short group with a digit still reads -f', 'git push -4f origin main', 'deny', 'clean'],
['a short group with a digit still reads -f', 'git push -fo-n origin main', 'deny', 'clean'],
['a short group with a digit still reads -f', 'git push -f -4o -n origin main', 'deny', 'clean'],
['a short group with a digit still reads -f (control: -n before it)', 'git push -n -f4 origin main', 'allow', 'clean'],
];
// GEVA-560 re-measure at ab6a38b7: two outside-the-folder delete shapes slipped through, a relative delete after a cd
// into a system folder and a device-node removal under /dev. Both now ask; their scratch and in-folder twins do not.
const OUTSIDE_CASES: Array<[string, string, Decision, Tree]> = [
['a relative delete after a cd into a system folder', 'cd /etc/nginx && rm -f sites-enabled/default', 'ask', 'clean'],
['a relative delete after a cd into a system folder', 'cd /etc && rm -f hosts.bak', 'ask', 'clean'],
['a relative delete after a cd into a system folder', 'cd /usr/local/lib && rm -f libdemo.so', 'ask', 'clean'],
['a relative delete after a cd into a system folder (twin: a scratch folder)', 'cd /tmp/work && rm -f sites-enabled/default', 'allow', 'clean'],
['a relative delete after a cd into a system folder (twin: a subfolder of this one)', 'cd src && rm -f old.txt', 'allow', 'clean'],
['a relative delete after a cd into a system folder (twin: no delete)', 'cd /etc/nginx && cat nginx.conf', 'allow', 'clean'],
['a relative delete after a cd into a system folder (twin: an absolute scratch target)', 'cd /etc/nginx && rm -f /tmp/demo.txt', 'allow', 'clean'],
['a device node removed under /dev', 'for i in 0 1 2 3; do rm -f /dev/loop$i; done', 'ask', 'clean'],
['a device node removed under /dev', 'rm -f /dev/sdb1', 'ask', 'clean'],
['a device node removed under /dev', 'rm /dev/nvme0n1', 'ask', 'clean'],
['a device node removed under /dev (twin: shared memory)', 'rm -f /dev/shm/demo-lock', 'allow', 'clean'],
['a device node removed under /dev (twin: output sent to /dev/null)', 'rm -f demo.txt > /dev/null', 'allow', 'clean'],
['a device node removed under /dev, spelled with a doubled slash', 'rm -f /dev//sdb1', 'ask', 'clean'],
['a device node removed under /dev, spelled with a dot folder', 'rm -f /dev/./sda', 'ask', 'clean'],
['a device node removed after a cd into /dev', 'cd /dev && rm -f sda', 'ask', 'clean'],
['a device node removed after a cd into /dev (twin: shared memory)', 'cd /dev/shm && rm -f sdx-lock', 'allow', 'clean'],
['a device node removed under /dev (twin: shared memory with a doubled slash)', 'rm -f /dev//shm/x', 'allow', 'clean'],
['a device node removed under /dev (twin: a project folder named dev)', 'rm -f dev/sdcard.txt', 'allow', 'clean'],
['a relative delete after a cd into a system folder under /var', 'cd /var/lib && rm -f dpkg/status', 'ask', 'clean'],
['a relative delete after a cd into a system folder under /var', 'cd /var/log && rm -f syslog', 'ask', 'clean'],
['a relative delete after a cd into a system folder', 'cd /opt/homebrew && rm -f bin/brew', 'ask', 'clean'],
['a relative delete after a cd into a system folder, reached from /', 'cd / && rm -f etc/hosts', 'ask', 'clean'],
['a relative delete after a cd into a system folder (twin: /var/tmp is scratch)', 'cd /var/tmp/w && rm -f x', 'allow', 'clean'],
['a relative delete after a cd into a system folder (twin: /var/www is not a system folder)', 'cd /var/www && rm -f index.html', 'allow', 'clean'],
];
// #4604 review: four gaps a reader reached, each closed without allowing anything main asks about or denies.
const REVIEW_CASES: Array<[string, string, Decision, Tree]> = [
['a short group with its own -n is a dry run, as main reads it', 'git push -fn4 origin main', 'allow', 'clean'],
['a short group with its own -n is a dry run, as main reads it', 'git push -4fn origin main', 'allow', 'clean'],
['a short group with its own -n is a dry run (neighbour: -n in another group, as main reads it)', 'git push -n4 -f origin main', 'deny', 'clean'],
['a short group with its own -n is a dry run (neighbour: a separate -f)', 'git push -fn4 -f origin main', 'deny', 'clean'],
['a short group with its own -n is a dry run (neighbour: the group is a push-option value)', 'git push -o -fn4 origin main', 'deny', 'clean'],
['a later --no-dry-run undoes the -n in a short group with a digit', 'git push -fn4 --no-dry-run origin main', 'deny', 'clean'],
['a later --no-dry-run undoes the -n in a short group with a digit', 'git push -4fn --no-dry origin main', 'deny', 'clean'],
['a later --no-dry-run undoes the -n in a short group with a digit (neighbour: it is a push-option value)', 'git push -fn4 -o --no-dry-run origin main', 'deny', 'clean'],
['a cd that climbs with .. into a system folder', 'cd ../../../../../../../../etc && rm -f hosts', 'ask', 'clean'],
['a cd that climbs with .. into /dev', 'cd ../../../../../../../../dev && rm -f sda', 'ask', 'clean'],
['a cd that climbs with .. into a system folder (twin: the parent folder)', 'cd .. && rm -f stale.txt', 'allow', 'clean'],
['a cd that climbs with .. into a system folder (twin: scratch)', 'cd ../../../../../../../../tmp/w && rm -f x', 'allow', 'clean'],
['a push-option value is not the remote', 'git push -f -o ci.skip origin', 'deny', 'main'],
['a push-option value is not the remote (neighbour: as main reads it, the value may be the remote)', 'git push -f -o x main', 'deny', 'clean'],
['a push-option value is not the remote (control: another branch checked out)', 'git push -f -o ci.skip origin', 'ask', 'clean'],
['a push-option value is not the remote (control: a dry run)', 'git push -fn -o ci.skip origin', 'allow', 'main'],
['a device node removed under /dev', 'rm -f /dev/sg0', 'ask', 'clean'],
['a device node removed under /dev', 'rm -f /dev/zram0', 'ask', 'clean'],
['a device node removed under /dev', 'rm -f /dev/ram0', 'ask', 'clean'],
['a device node removed under /dev', 'rm -f /dev/mtd0', 'ask', 'clean'],
['a device node removed under /dev', 'rm -f /dev/ubi0', 'ask', 'clean'],
['a device node removed under /dev (twin: shared memory)', 'rm -f /dev/shm/sg0', 'allow', 'clean'],
];
// #4628 and #4601: a quoted spelling gets the decision main gives its unquoted or backslashed twin, which main
// allows. Negative controls: nothing else becomes allowed, and text that runs (`$( )`, bash -c, a later statement)
// is still read.
const SPELLING_CASES: Array<[string, string, Decision, Tree]> = [
['a quoted build folder is the same delete as unquoted', 'rm -rf "/home/me/app/dist"', 'allow', 'clean'],
['a quoted build folder is the same delete as unquoted', "rm -rf '/home/me/app/dist'", 'allow', 'clean'],
['a quoted build folder is the same delete as unquoted', 'rm -rf "/home/me/app/node_modules" "build"', 'allow', 'clean'],
['a quoted build folder is the same delete as unquoted (control: unquoted)', 'rm -rf /home/me/app/dist', 'allow', 'clean'],
['a quoted build folder (negative: not a build folder)', 'rm -rf "/home/me/app/src"', 'ask', 'clean'],
['a quoted build folder (negative: a variable)', 'rm -rf "$HOME/dist"', 'ask', 'clean'],
['a quoted build folder (negative: a quoted ~ is not home)', 'rm -rf "~/dist"', 'ask', 'clean'],
['a quoted build folder (negative: a quoted glob)', 'rm -rf "/home/me/d*st"', 'ask', 'clean'],
['a quoted build folder (negative: a space main never allows)', 'rm -rf "/home/me/Github Projects/app/dist"', 'ask', 'clean'],
['a quoted build folder (negative: a trailing slash main never allows)', 'rm -rf "/home/me/app/dist/"', 'ask', 'clean'],
['a quoted build folder (negative: ..)', 'rm -rf "/home/me/../me/app/dist"', 'ask', 'clean'],
['a quoted build folder (negative: a command substitution)', 'rm -rf "`pwd`/dist"', 'ask', 'clean'],
['a quoted build folder (negative: text after the quote)', 'rm -rf "/home/me/app/dist"x', 'ask', 'clean'],
['a quoted build folder (negative: a second target)', 'rm -rf "/home/me/app/dist" "/home/me/app/src"', 'ask', 'clean'],
['a quoted build folder (negative: a system folder beside it)', 'rm -rf "/home/me/app/dist" /etc', 'deny', 'clean'],
['a quoted build folder (negative: a statement inside the quotes)', 'rm -rf "dist;rm -rf ~"', 'deny', 'clean'],
['a quoted build folder (negative: two quoted names joined into one word)', 'rm -rf "dist""build"', 'ask', 'clean'],
['a quoted gh title is text', 'gh issue create -R garrytan/gstack --title "careful: force-push check fires on a plain git push when a later command in the same line has a +token or -f (e.g. date +%F)" --body-file report.md', 'allow', 'clean'],
['a quoted gh title is text', "gh issue create --title 'git push -f origin main' --body x", 'allow', 'clean'],
['a quoted gh body is text', 'gh pr create --title "x" --body "rm -rf /"', 'allow', 'clean'],
['a quoted gh body is text', 'gh pr comment 12 --body "git push -f origin main"', 'allow', 'clean'],
['a quoted gh title (negative: a command substitution runs)', 'gh issue create --title "$(git push -f origin main)"', 'deny', 'clean'],
['a quoted gh title (negative: a later statement)', 'gh issue create --title "x" && git push -f origin main', 'deny', 'clean'],
['a quoted gh title (negative: a later statement)', 'gh issue create --title "rm -rf ~/" --body x; rm -rf ~/', 'deny', 'clean'],
['a quoted gh title (negative: another gh subcommand)', 'gh issue list --search "git push -f origin main"', 'deny', 'clean'],
['a quoted gh title (negative: a word that is not the title)', "gh issue create --title x 'git push -f origin main'", 'deny', 'clean'],
['a quoted gh title (negative: piped on)', 'gh issue create --title "git push -f origin main" | cat', 'deny', 'clean'],
['a quoted gh title (negative: gh is not the command)', 'sudo gh issue create --title "git push -f origin main"', 'deny', 'clean'],
['a quoted gh title (negative: echo)', 'echo "git push -f origin main"', 'deny', 'clean'],
['a quoted gh title (negative: bash -c runs it)', 'bash -c "git push -f origin main"', 'deny', 'clean'],
];
let base = '';
const trees = {} as Record<Tree, string>;
@@ -373,6 +504,22 @@ describe('each new rule, with negative controls', () => {
expect(decide(command, tree).decision).toBe(expected);
});
test.each(NOTE_CASES)('%s: %s -> %s (%s tree)', (_item, command, expected, tree) => {
expect(decide(command, tree).decision).toBe(expected);
});
test.each(OUTSIDE_CASES)('%s: %s -> %s (%s tree)', (_item, command, expected, tree) => {
expect(decide(command, tree).decision).toBe(expected);
});
test.each(REVIEW_CASES)('%s: %s -> %s (%s tree)', (_item, command, expected, tree) => {
expect(decide(command, tree).decision).toBe(expected);
});
test.each(SPELLING_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();
@@ -115,6 +115,7 @@ describe('a start that ran while the skill loaded (GST-3157)', () => {
'a load before the command': (m: ReturnType<typeof moved>) => m.loads(m.skillLoad(undefined, { timestamp: '2026-10-04T10:27:00.000Z' })),
'question tuning off': (m: ReturnType<typeof moved>) => m.loads(m.skillLoad((m.result.content as string).replace('QUESTION_TUNING: true', 'QUESTION_TUNING: false'))),
'a start that failed at load': (m: ReturnType<typeof moved>) => m.loads(m.skillLoad('SKILL_START: failed at load (exit 1); run the start command below yourself with Bash, then continue.')),
'a start that timed out at load after printing its status': (m: ReturnType<typeof moved>) => m.loads(m.skillLoad(`${(m.result.content as string).split('GSTACK_INSTRUCTION_BEGIN:')[0]}SKILL_START: timed out after 15s at load; run the start command below yourself with Bash, then continue.`)),
'shell execution disabled by policy': (m: ReturnType<typeof moved>) => m.loads(m.skillLoad('[shell command execution disabled by policy]')),
'an un-run injection line': (m: ReturnType<typeof moved>) => m.loads(m.skillLoad(START_LINE)),
})) test(`a load-time start is refused with ${name}`, () => {
@@ -123,6 +124,13 @@ describe('a start that ran while the skill loaded (GST-3157)', () => {
expect(native(m.entry)).toBeNull();
});
test('a start that timed out at load and then ran through Bash is the one start', () => {
const m = moved();
m.loads(m.skillLoad(`${(m.result.content as string).split('GSTACK_INSTRUCTION_BEGIN:')[0]}SKILL_START: timed out after 15s at load; run the start command below yourself with Bash, then continue.`));
m.entry.input.native.tools.push(m.use, m.result);
expect(native(m.entry)).toMatchObject({ option: 'HOLD SCOPE', preambleToolUseId: m.use.toolUseId });
});
test('a start that failed at load and then ran through Bash is the one start', () => {
const m = moved();
m.loads(m.skillLoad('SKILL_START: failed at load (exit 1); run the start command below yourself with Bash, then continue.'));
+1 -1
View File
@@ -337,7 +337,7 @@ function structuredModeDecision(transcript: PlanCountTranscript, tools: NativePu
// loaded text (GST-3157); that load counts as the session's one start.
const loadStarts = (transcript.skillLoads ?? []).flatMap(load => {
if (load.sessionId !== opts.sessionId || load.skill !== opts.skillName || !timely(load.timestamp)) return [];
const output = loadedStartOutput(load.text), session = output === null ? null : startSession(output);
const output = loadedStartOutput(load.text), session = output === null || /^SKILL_START: /m.test(output) ? null : startSession(output);
return session ? [{ toolUseId: undefined, at: load.timestamp, session }] : [];
});
const starts = [...shellStarts, ...loadStarts];