Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(scripts): refuse every spelling of / as OPENWA_DATA_DIR

Yudhi Armyndharis26b6a16b79fb
diff --git a/scripts/restore.sh b/scripts/restore.sh
index 94f805ba9..93112f333 100755
--- a/scripts/restore.sh
+++ b/scripts/restore.sh
@@ -110,8 +110,13 @@ case "$DATA_DIR" in
exit 1
;;
esac
+# The trailing / comes off the prefix wherever a resolved directory is matched as one: the root resolves to
+# "/", "$dir"/* is then //*, which no path matches, and every spelling of / that the list above misses
+# (//, /., /./, /.. or a link to /) would be taken for a directory the workspace is not under. Trimmed, the
+# pattern is /*, so the root is refused here as a parent of the workspace. The / is kept inside the quotes:
+# bash 5 does not match "${dir%/}"/* against anything when the trim leaves nothing, and bash 3 does.
case "$RESOLVED_CWD/" in
- "$RESOLVED_DATA_DIR"/*)
+ "${RESOLVED_DATA_DIR%/}/"*)
log "ERROR: OPENWA_DATA_DIR must not be the workspace or one of its parent directories: $DATA_DIR"
exit 1
;;
@@ -144,13 +149,13 @@ replace_tree() {
exit 1
fi
case "$RESOLVED_CWD/" in
- "$resolved_target"/*)
+ "${resolved_target%/}/"*)
log "ERROR: refusing to replace $label target that contains the workspace: $target_dir"
exit 1
;;
esac
case "$RESOLVED_DATA_DIR/" in
- "$resolved_target"/*)
+ "${resolved_target%/}/"*)
log "ERROR: refusing to replace $label target that contains the data directory: $target_dir"
exit 1
;;
@@ -190,7 +195,7 @@ snapshot_external() {
target="$1"
external_snapshot=""
case "$(resolve_path "$target")" in
- "$RESOLVED_DATA_DIR"/*) return 0 ;;
+ "${RESOLVED_DATA_DIR%/}/"*) return 0 ;;
esac
[ -e "$target" ] || return 0
snapshot_dir="${OPENWA_RESTORE_SNAPSHOT_DIR:-$(dirname "$target")}"
diff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.sh
index 01a9701e1..878c1149e 100755
--- a/scripts/smoke-test-backup-restore.sh
+++ b/scripts/smoke-test-backup-restore.sh
@@ -2331,6 +2331,9 @@ refused_restore() {
# The / cases matter most: a restore run as root in the container (docker exec) is the one that would
# take the container's root. They run only where a check that broke would not act on / itself, which
# holds while / cannot be written: the run then stops at its writability check, or at the first copy.
+# The exception is a data dir that is a link to /, whose safety snapshot lands in the link's own
+# directory, so the (am) cases that name a root spelling put a cp ahead of the real one that stops at
+# any source resolving to / (see am_root below).
ROOT_SAFE=0
if [ "$(id -u)" -ne 0 ] && [ ! -w / ]; then
ROOT_SAFE=1
@@ -2374,6 +2377,47 @@ pass "(am) an OPENWA_DATA_DIR that is ., ./, .., ../, the workspace, a parent of
if [ "$ROOT_SAFE" -eq 1 ]; then
am_refused "$AM" "am: /" 'refusing unsafe OPENWA_DATA_DIR target: /' "$AM/work" "$ARCHIVE_AM" OPENWA_DATA_DIR=/
pass "(am) / is refused as OPENWA_DATA_DIR"
+ # Every other spelling of the root gets past the literal list. Were it not refused, the restore would
+ # snapshot the data dir with cp -pRH, that is copy / into a directory inside it, until the disk is
+ # full. So these runs have a cp first on PATH that stops at a source resolving to /, with exit 97 and
+ # no copy at all, and a refusal that failed to come would show as that exit.
+ REAL_CP="$(command -v cp)"
+ mkdir -p "$AM/cpguard"
+ cat >"$AM/cpguard/cp" <<EOF
+#!/bin/sh
+for arg in "\$@"; do
+ case "\$arg" in -*) continue ;; esac
+ real="\$(cd -P "\$arg" 2>/dev/null && pwd -P)"
+ if [ -n "\$real" ] && [ -z "\$(printf %s "\$real" | tr -d /)" ]; then
+ echo "cp: not copying the root directory (\$arg)" >&2
+ exit 97
+ fi
+done
+exec "$REAL_CP" "\$@"
+EOF
+ chmod +x "$AM/cpguard/cp"
+ # The shell passes over a cp it cannot run, on a noexec temp dir for one, and takes the real one, so
+ # show that the restore will find this cp (in a new shell, as the restore is: this one has cp hashed) and
+ # that it stops at /. The probe's destination can never be created, which keeps even a broken guard from
+ # copying anything.
+ if [ "$(PATH="$AM/cpguard:$PATH" sh -c 'command -v cp')" != "$AM/cpguard/cp" ]; then
+ fail "(am) the cp that guards the root cases cannot be run from $AM/cpguard"
+ fi
+ if "$AM/cpguard/cp" -pR / /dev/null/probe >/dev/null 2>&1; then
+ fail "(am) the cp that guards the root cases did not stop at /"
+ else
+ [ $? -eq 97 ] || fail "(am) the cp that guards the root cases did not stop at /"
+ fi
+ ln -s / "$AM/rootlink"
+ am_root() {
+ am_refused "$AM" "am: $1" "$AM_WORKSPACE" "$AM/work" "$ARCHIVE_AM" PATH="$AM/cpguard:$PATH" OPENWA_DATA_DIR="$2"
+ }
+ am_root "//" //
+ am_root "/." /.
+ am_root "/./" /./
+ am_root "/.." /..
+ am_root "link to /" "$AM/rootlink"
+ pass "(am) //, /., /./, /.. and a link to / are refused as OPENWA_DATA_DIR, before the archive is read"
else
echo "SKIP: (am) the / case, which needs a user who cannot write /"
fi