Skip to content

Commit 4e58bb8

Browse files
committed
Include macOS DARWIN_USER_TEMP_DIR in default temp zone and exempt children from /var protection
On macOS, getconf DARWIN_USER_TEMP_DIR lives under /var/folders/<id>/T/ (canonical /private/var/folders/<id>/T). Over non-interactive SSH where TMPDIR is /private/tmp or unset, files in DARWIN_USER_TEMP_DIR missed the default $TMPDIR:/tmp delete zone and fell through to trash_item(), where PREFIX_PROTECTED's /var rule rejected them as protected. In both bin/rm-safe (LuaJIT) and bin/rm-safe.bash (Bash 3.2+): - Query getconf DARWIN_USER_TEMP_DIR once on Darwin and include its real path in the default temp delete zone (still overridden by RM_SAFE_TEMP_DIRS). - Exempt paths whose real parent lies strictly inside DARWIN_USER_TEMP_DIR from /var protection in is_protected() (keeping the directory root itself, siblings, .. escapes, and Trash/log dirs protected). - Add regression tests in bin/test/rm-safe_test and bin/test/find_shim_test across all three lanes (luajit, bash-4+, bash-3.2).
1 parent dee5aba commit 4e58bb8

5 files changed

Lines changed: 322 additions & 33 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ guidance for macOS, common Linux sudoers setups, and NixOS.
1111
Features
1212
- Moves files/directories to OS trash (XDG trash on Linux, Finder Trash on macOS).
1313
- Moves verified-empty directories invoked through `rmdir` to Trash too.
14-
- Deletes outright, without logging, anything strictly inside the system temp directory (`$TMPDIR` and `/tmp`; `RM_SAFE_TEMP_DIRS`, colon-separated, replaces that list) or already in the Trash. Temporary files are disposable, and removing a Trash entry means it; its `.trashinfo` record goes with it. Directories still need `-r`, a symlink is removed without its target, and the check uses the real parent directory, so `..` and symlinked parents cannot move a path into or out of a zone.
14+
- Deletes outright, without logging, anything strictly inside the system temp directory (`$TMPDIR`, `/tmp`, and on macOS `DARWIN_USER_TEMP_DIR`; `RM_SAFE_TEMP_DIRS`, colon-separated, replaces that list) or already in the Trash. Temporary files are disposable, and removing a Trash entry means it; its `.trashinfo` record goes with it. Directories still need `-r`, a symlink is removed without its target, and the check uses the real parent directory, so `..` and symlinked parents cannot move a path into or out of a zone.
1515
- Converts a restricted terminal `find -delete` expression into safe Trash moves.
1616
- Requires explicit confirmation before `truncate` shrinks an existing file.
1717
- Requires explicit confirmation before `dd` writes to an existing `of=` target.

‎bin/rm-safe‎

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,50 @@ local function undo_picker(offset_arg)
10721072
return restore_trash_entry(selected[5], selected[4])
10731073
end
10741074

1075+
local function real_dir(path)
1076+
local buf = ffi.new("char[4096]")
1077+
if path == "" or ffi.C.realpath(path, buf) == nil then return nil end
1078+
return ffi.string(buf)
1079+
end
1080+
1081+
local DARWIN_TEMP_CHECKED = false
1082+
local DARWIN_TEMP_REAL = nil
1083+
local function darwin_user_temp_dir()
1084+
if DARWIN_TEMP_CHECKED then return DARWIN_TEMP_REAL end
1085+
DARWIN_TEMP_CHECKED = true
1086+
if OS ~= "Darwin" then return nil end
1087+
local raw = trim(capture_stdout("getconf DARWIN_USER_TEMP_DIR 2>/dev/null"))
1088+
local real = real_dir(raw)
1089+
if real and real ~= "/" then DARWIN_TEMP_REAL = real end
1090+
return DARWIN_TEMP_REAL
1091+
end
1092+
1093+
local function is_inside_darwin_temp(path)
1094+
local dtemp = darwin_user_temp_dir()
1095+
if not dtemp then return false end
1096+
local abs = resolve_path(path)
1097+
local parent = real_dir(dirname(abs))
1098+
if not parent then return false end
1099+
local full = path_join(parent, basename(abs))
1100+
return full:sub(1, #dtemp + 1) == dtemp .. "/"
1101+
end
1102+
10751103
local function is_protected(path)
1104+
-- Protect the entire log fragments subtree, not just one file.
1105+
if path == TRASH_BASE_DIR or path == TRASH_FILES_DIR or path == TRASH_INFO_DIR then
1106+
return true
1107+
end
1108+
if LOG_DIR ~= "" and (path == LOG_DIR or path:sub(1, #LOG_DIR + 1) == LOG_DIR .. "/") then
1109+
return true
1110+
end
1111+
1112+
-- macOS's per-user system temp directory lives under /var/folders/<id>/T; items
1113+
-- strictly inside it are disposable temp files, not protected /var system files,
1114+
-- even when RM_SAFE_TEMP_DIRS excludes it so they go to the Trash.
1115+
if is_inside_darwin_temp(path) then
1116+
return false
1117+
end
1118+
10761119
for _, pattern in ipairs(EXACT_PROTECTED) do
10771120
if pattern:sub(-2) == "/*" and pattern ~= "/*" then
10781121
local base = pattern:sub(1, -3)
@@ -1096,14 +1139,6 @@ local function is_protected(path)
10961139
end
10971140
end
10981141

1099-
-- Protect the entire log fragments subtree, not just one file.
1100-
if path == TRASH_BASE_DIR or path == TRASH_FILES_DIR or path == TRASH_INFO_DIR then
1101-
return true
1102-
end
1103-
if LOG_DIR ~= "" and (path == LOG_DIR or path:sub(1, #LOG_DIR + 1) == LOG_DIR .. "/") then
1104-
return true
1105-
end
1106-
11071142
return false
11081143
end
11091144

@@ -1259,23 +1294,23 @@ end
12591294
-- Paths already in the Trash, or in the system temp directory, are deleted outright (owner,
12601295
-- 2026-11-28): removing a Trash entry means it, and temporary files are disposable. Scripts that
12611296
-- clean their temp files no longer fill the Trash. RM_SAFE_TEMP_DIRS (colon-separated) replaces
1262-
-- the default $TMPDIR:/tmp list. Nothing is logged: there is nothing to undo, and logging the
1263-
-- deletion of old log fragments, which live in the Trash, would write new ones.
1264-
local function real_dir(path)
1265-
local buf = ffi.new("char[4096]")
1266-
if path == "" or ffi.C.realpath(path, buf) == nil then return nil end
1267-
return ffi.string(buf)
1268-
end
1269-
1297+
-- the default $TMPDIR:/tmp (plus macOS DARWIN_USER_TEMP_DIR) list. Nothing is logged: there is
1298+
-- nothing to undo, and logging the deletion of old log fragments, which live in the Trash, would
1299+
-- write new ones.
12701300
local DELETE_ZONES = nil
12711301
local function delete_zones()
12721302
if DELETE_ZONES then return DELETE_ZONES end
12731303
DELETE_ZONES = {}
1274-
local temp = os.getenv("RM_SAFE_TEMP_DIRS") or ((os.getenv("TMPDIR") or "") .. ":/tmp")
1304+
local override = os.getenv("RM_SAFE_TEMP_DIRS")
1305+
local temp = override ~= nil and override or ((os.getenv("TMPDIR") or "") .. ":/tmp")
12751306
for dir in temp:gmatch("[^:]+") do
12761307
local real = real_dir(dir)
12771308
if real then DELETE_ZONES[#DELETE_ZONES + 1] = { path = real, kind = "temp" } end
12781309
end
1310+
if override == nil then
1311+
local darwin_temp = darwin_user_temp_dir()
1312+
if darwin_temp then DELETE_ZONES[#DELETE_ZONES + 1] = { path = darwin_temp, kind = "temp" } end
1313+
end
12791314
local trash = real_dir(TRASH_BASE_DIR)
12801315
if trash then DELETE_ZONES[#DELETE_ZONES + 1] = { path = trash, kind = "trash" } end
12811316
return DELETE_ZONES
@@ -1409,8 +1444,9 @@ Undo offsets:
14091444
TRASH_MANUAL entry (so -1 is the default).
14101445
14111446
Deleted outright, not trashed or logged:
1412-
Paths inside the system temp directory ($TMPDIR and /tmp; RM_SAFE_TEMP_DIRS
1413-
replaces that list) or already in the Trash.
1447+
Paths inside the system temp directory ($TMPDIR, /tmp, and on macOS
1448+
DARWIN_USER_TEMP_DIR; RM_SAFE_TEMP_DIRS replaces that list) or already
1449+
in the Trash.
14141450
14151451
Trash location: %s
14161452
Files: %s

‎bin/rm-safe.bash‎

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -657,10 +657,75 @@ resolve_path() {
657657
printf '%s' "$resolved"
658658
}
659659

660+
_real_dir() { (cd -P -- "$1" 2>/dev/null && pwd -P); }
661+
662+
DARWIN_TEMP_CHECKED=false
663+
DARWIN_TEMP_REAL=""
664+
darwin_user_temp_dir() {
665+
if [[ $DARWIN_TEMP_CHECKED == true ]]; then
666+
printf '%s' "$DARWIN_TEMP_REAL"
667+
return 0
668+
fi
669+
DARWIN_TEMP_CHECKED=true
670+
if [[ $OS != "Darwin" ]]; then
671+
return 0
672+
fi
673+
local raw real
674+
raw=$(getconf DARWIN_USER_TEMP_DIR 2>/dev/null || true)
675+
raw=${raw%$'\n'}
676+
if [[ -n $raw ]]; then
677+
real=$(_real_dir "$raw") || real=""
678+
if [[ -n $real && $real != "/" ]]; then
679+
DARWIN_TEMP_REAL=$real
680+
fi
681+
fi
682+
printf '%s' "$DARWIN_TEMP_REAL"
683+
}
684+
685+
is_inside_darwin_temp() {
686+
local item=$1 dtemp dir base parent full
687+
if [[ $DARWIN_TEMP_CHECKED != true ]]; then
688+
darwin_user_temp_dir >/dev/null
689+
fi
690+
dtemp=$DARWIN_TEMP_REAL
691+
[[ -n $dtemp ]] || return 1
692+
while [[ $item == */ && $item != / ]]; do item=${item%/}; done
693+
if [[ $item == */* ]]; then
694+
dir=${item%/*}
695+
base=${item##*/}
696+
[[ -n $dir ]] || dir=/
697+
else
698+
dir=.
699+
base=$item
700+
fi
701+
if [[ $base == . || $base == .. ]]; then
702+
full=$(_real_dir "$item") || return 1
703+
else
704+
parent=$(_real_dir "$dir") || return 1
705+
full=${parent%/}/$base
706+
fi
707+
[[ $full == "$dtemp"/* ]]
708+
}
709+
660710
# Check if path is protected
661711
is_protected() {
662712
local path=$1
663713

714+
# Check if it's the trash itself
715+
# Protect the entire fragment log subtree, not just one file.
716+
[[ $path == "$TRASH_BASE_DIR" || $path == "$TRASH_FILES_DIR" ||
717+
$path == "$TRASH_INFO_DIR" ]] && return 0
718+
if [[ -n $LOG_DIR && ( $path == "$LOG_DIR" || $path == "$LOG_DIR"/* ) ]]; then
719+
return 0
720+
fi
721+
722+
# macOS's per-user system temp directory lives under /var/folders/<id>/T; items
723+
# strictly inside it are disposable temp files, not protected /var system files,
724+
# even when RM_SAFE_TEMP_DIRS excludes it so they go to the Trash.
725+
if is_inside_darwin_temp "$path"; then
726+
return 1
727+
fi
728+
664729
# Check exact patterns
665730
for pattern in "${EXACT_PROTECTED[@]}"; do
666731
if [[ $pattern == *"/*" && $pattern != "/*" ]]; then
@@ -680,14 +745,6 @@ is_protected() {
680745
[[ $path == "$prefix" || $path == "$prefix/"* ]] && return 0
681746
done
682747

683-
# Check if it's the trash itself
684-
# Protect the entire fragment log subtree, not just one file.
685-
[[ $path == "$TRASH_BASE_DIR" || $path == "$TRASH_FILES_DIR" ||
686-
$path == "$TRASH_INFO_DIR" ]] && return 0
687-
if [[ -n $LOG_DIR && ( $path == "$LOG_DIR" || $path == "$LOG_DIR"/* ) ]]; then
688-
return 0
689-
fi
690-
691748
return 1
692749
}
693750

@@ -925,9 +982,9 @@ get_unique_name() {
925982
# Paths already in the Trash, or in the system temp directory, are deleted outright (owner,
926983
# 2026-11-28): removing a Trash entry means it, and temporary files are disposable. Scripts that
927984
# clean their temp files no longer fill the Trash. RM_SAFE_TEMP_DIRS (colon-separated) replaces
928-
# the default $TMPDIR:/tmp list. Nothing is logged: there is nothing to undo, and logging the
929-
# deletion of old log fragments, which live in the Trash, would write new ones.
930-
_real_dir() { (cd -P -- "$1" 2>/dev/null && pwd -P); }
985+
# the default $TMPDIR:/tmp (plus macOS DARWIN_USER_TEMP_DIR) list. Nothing is logged: there is
986+
# nothing to undo, and logging the deletion of old log fragments, which live in the Trash, would
987+
# write new ones.
931988
DELETE_ZONES=()
932989
DELETE_ZONE_KINDS=()
933990
init_delete_zones() {
@@ -941,6 +998,15 @@ init_delete_zones() {
941998
DELETE_ZONES+=("$real")
942999
DELETE_ZONE_KINDS+=(temp)
9431000
done
1001+
if [[ ${RM_SAFE_TEMP_DIRS+x} != x ]]; then
1002+
if [[ $DARWIN_TEMP_CHECKED != true ]]; then
1003+
darwin_user_temp_dir >/dev/null
1004+
fi
1005+
if [[ -n $DARWIN_TEMP_REAL ]]; then
1006+
DELETE_ZONES+=("$DARWIN_TEMP_REAL")
1007+
DELETE_ZONE_KINDS+=(temp)
1008+
fi
1009+
fi
9441010
real=$(_real_dir "$TRASH_BASE_DIR") || real=""
9451011
if [[ -n $real ]]; then
9461012
DELETE_ZONES+=("$real")
@@ -1138,8 +1204,9 @@ show_help() {
11381204
TRASH_MANUAL entry (so -1 is the default).
11391205
11401206
Deleted outright, not trashed or logged:
1141-
Paths inside the system temp directory (\$TMPDIR and /tmp; RM_SAFE_TEMP_DIRS
1142-
replaces that list) or already in the Trash.
1207+
Paths inside the system temp directory (\$TMPDIR, /tmp, and on macOS
1208+
DARWIN_USER_TEMP_DIR; RM_SAFE_TEMP_DIRS replaces that list) or already
1209+
in the Trash.
11431210
11441211
Trash location: $base_dir
11451212
Files: $file_dir

‎bin/test/find_shim_test‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,58 @@ else
290290
((fails++))
291291
fi
292292

293+
# Test 31: find -delete in macOS DARWIN_USER_TEMP_DIR deletes outright even when TMPDIR is /tmp.
294+
((tests++))
295+
darwin_find_base=""
296+
darwin_find_cleanup=""
297+
case "$(cd -P -- "$test_dir" && pwd -P)" in
298+
/tmp|/tmp/*|/private/tmp|/private/tmp/*)
299+
if [[ -d /dev/shm && -w /dev/shm ]]; then
300+
darwin_find_base=$(mktemp -d /dev/shm/rm-safe-find-darwin.XXXXXX 2>/dev/null || true)
301+
darwin_find_cleanup=$darwin_find_base
302+
fi
303+
;;
304+
*)
305+
darwin_find_base="$test_dir/darwin-find-case"
306+
mkdir -p "$darwin_find_base"
307+
;;
308+
esac
309+
if [[ -z $darwin_find_base ]]; then
310+
echo "✓ find shadow Darwin DARWIN_USER_TEMP_DIR check skipped (no dir outside /tmp)"
311+
else
312+
df_home="$darwin_find_base/home"
313+
df_bin="$darwin_find_base/stub-bin"
314+
df_real="$darwin_find_base/private/var/folders/ab/cd/T"
315+
df_link="$darwin_find_base/var/folders/ab/cd/T"
316+
mkdir -p "$df_home" "$df_bin" "$df_real/cr-x"
317+
ln -s "$darwin_find_base/private/var" "$darwin_find_base/var"
318+
cat >"$df_bin/uname" <<'STUB'
319+
#!/bin/sh
320+
echo Darwin
321+
STUB
322+
cat >"$df_bin/getconf" <<STUB
323+
#!/bin/sh
324+
if [ "\$1" = "DARWIN_USER_TEMP_DIR" ]; then
325+
printf '%s/\n' "$df_link"
326+
exit 0
327+
fi
328+
exit 1
329+
STUB
330+
chmod +x "$df_bin/uname" "$df_bin/getconf"
331+
printf 'hi\n' >"$df_link/cr-x/f"
332+
env -u RM_SAFE_TEMP_DIRS HOME="$df_home" TMPDIR=/tmp REAL_FIND="$real_find" \
333+
PATH="$df_bin:$PATH_SAFE" "$FIND_SHIM" "$df_link/cr-x" -mindepth 1 -maxdepth 1 -type f -delete \
334+
>"$darwin_find_base/out.txt" 2>"$darwin_find_base/err.txt"
335+
df_rc=$?
336+
df_trashed=$(find "$df_home/.Trash/files" -mindepth 1 -maxdepth 1 2>/dev/null | wc -l | tr -d ' ')
337+
if [[ $df_rc -eq 0 && ! -e "$df_link/cr-x/f" && $df_trashed -eq 0 ]] \
338+
&& ! grep -q -e 'is protected' -e 'moved to the Trash' "$darwin_find_base/err.txt"; then
339+
echo "✓ find -delete removes files inside Darwin DARWIN_USER_TEMP_DIR outright"
340+
else
341+
echo "✗ find -delete failed in Darwin DARWIN_USER_TEMP_DIR (rc=$df_rc trashed=$df_trashed err=$(cat "$darwin_find_base/err.txt"))" >&2
342+
((fails++))
343+
fi
344+
[[ -n $darwin_find_cleanup ]] && "$REAL_RM" -rf "$darwin_find_cleanup"
345+
fi
346+
293347
shim_test_summary

0 commit comments

Comments
 (0)