Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(scripts): keep a restored file off a refilled directory

Yudhi Armyndharis1613e51618a1
diff --git a/scripts/restore.sh b/scripts/restore.sh
index 2e288ba7b..405446d30 100755
--- a/scripts/restore.sh
+++ b/scripts/restore.sh
@@ -478,19 +478,32 @@ fi
if [ -f "$STAGE/.api-key" ]; then
archive_target BOOTSTRAP_KEY_FILE "$ADMIN_KEY_FILE" file "$DATA_DIR/.api-key"
fi
+# Written into the data dir after the trees, at fixed names, so no key moves them. A refilled tree can still
+# stand on one of them, and a copy onto a directory goes into it, through any link the archive put there.
+for fixed in .env.generated database.sql; do
+ if [ -f "$STAGE/$fixed" ]; then
+ TARGET_KEYS+=("$fixed")
+ TARGET_PATHS+=("$(resolve_path "$DATA_DIR/$fixed")")
+ TARGET_KINDS+=(file)
+ TARGET_CHOSEN+=(0)
+ fi
+done
i=0
while [ "$i" -lt "${#TARGET_KEYS[@]}" ]; do
j=0
while [ "$j" -lt "${#TARGET_KEYS[@]}" ]; do
if [ "${TARGET_KINDS[j]}" = dir ] && [ "${TARGET_CHOSEN[i]}${TARGET_CHOSEN[j]}" != 00 ]; then
+ where=""
case "${TARGET_PATHS[i]}" in
- "${TARGET_PATHS[j]%/}/"?*)
- key="${TARGET_KEYS[i]}"
- [ "${TARGET_CHOSEN[i]}" -eq 1 ] || key="${TARGET_KEYS[j]}"
- refuse_archive_path "$key" "${TARGET_PATHS[i]}" \
- "puts the ${TARGET_KEYS[i]} target inside the ${TARGET_KEYS[j]} directory, which is refilled from the archive"
- ;;
+ "${TARGET_PATHS[j]%/}/"?*) where=inside ;;
+ "${TARGET_PATHS[j]}") [ "${TARGET_KINDS[i]}" != file ] || where=on ;;
esac
+ if [ -n "$where" ]; then
+ key="${TARGET_KEYS[i]}"
+ [ "${TARGET_CHOSEN[i]}" -eq 1 ] || key="${TARGET_KEYS[j]}"
+ refuse_archive_path "$key" "${TARGET_PATHS[i]}" \
+ "puts the ${TARGET_KEYS[i]} target $where the ${TARGET_KEYS[j]} directory, which is refilled from the archive"
+ fi
fi
j=$((j + 1))
done
diff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.sh
index df17d87e6..6bf99c8f7 100755
--- a/scripts/smoke-test-backup-restore.sh
+++ b/scripts/smoke-test-backup-restore.sh
@@ -2865,5 +2865,34 @@ if [ ! -L "$AT/linked/data/sessions" ] || [ "$(cat "$AT/disk/sessions/session-1/
fi
pass "(at) the environment, ./.env and the defaults still decide where the restore writes"
+echo ""
+echo "==> (au) a file the restore writes is never put on a directory it refills from the archive"
+# A copy onto a directory goes into it, under the file's own name, through any link the archive left there under
+# that name. Both ways out of the data dir that the strict-descendant rule above does not see: a path from the
+# archive that is the key file and the sessions dir at once, and a sessions dir that is a file the restore writes
+# at a fixed name after the trees, such as .env.generated and database.sql.
+# The restores above that succeeded left their safety snapshots; at_refused looks for a new one.
+find "${AT:?}" -name '*.pre-restore-*' -prune -exec rm -rf {} +
+at_archive same 'SESSION_DATA_PATH=./data/shared\nBOOTSTRAP_KEY_FILE=./data/shared\n' .api-key "$AT/victim/precious.txt"
+at_refused "au: key file on the refilled sessions" \
+ "BOOTSTRAP_KEY_FILE from the archive's .env.generated puts the BOOTSTRAP_KEY_FILE target on the SESSION_DATA_PATH directory" \
+ "$AT/same.tar.gz"
+at_archive generated 'SESSION_DATA_PATH=./data/.env.generated\n' .env.generated "$AT/victim/precious.txt"
+at_refused "au: sessions on the generated configuration" \
+ "SESSION_DATA_PATH from the archive's .env.generated puts the .env.generated target on the SESSION_DATA_PATH directory" \
+ "$AT/generated.tar.gz"
+at_archive dump 'SESSION_DATA_PATH=./data/database.sql\n' database.sql "$AT/victim/precious.txt"
+rm -rf "${AT:?}/stage" && mkdir -p "$AT/stage"
+tar -xzf "$AT/dump.tar.gz" -C "$AT/stage"
+printf 'select 1;\n' >"$AT/stage/database.sql"
+tar -czf "$AT/dump.tar.gz" -C "$AT/stage" .
+at_refused "au: sessions on the PostgreSQL dump" \
+ "SESSION_DATA_PATH from the archive's .env.generated puts the database.sql target on the SESSION_DATA_PATH directory" \
+ "$AT/dump.tar.gz"
+if [ "$(cat "$AT/victim/precious.txt")" != "at-precious" ]; then
+ fail "(au) a refused restore wrote through a link the archive planted"
+fi
+pass "(au) a file written after the trees cannot land on a directory the archive's configuration names"
+
echo ""
echo "All smoke tests passed!"