Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(scripts): keep restore paths from an archive in its data dir

Yudhi Armyndharis2fe68d7aadd1
diff --git a/docs/11-operational-runbooks.md b/docs/11-operational-runbooks.md
index 0d851170c..d095840ef 100644
--- a/docs/11-operational-runbooks.md
+++ b/docs/11-operational-runbooks.md
@@ -801,13 +801,17 @@ OPENWA_DATA_DIR=/srv/openwa/data \
> first, then `./.env`, then `<data dir>/.env.generated`. Settings made through Dashboard >
> Infrastructure therefore apply without being restated on the command line. A restore reads that
> third layer from the archive's `.env.generated` when the archive carries one, because that copy
-> replaces the target's and is the one the restored app reads. A relative `./data/...` path in
-> `./.env` or `.env.generated`, such as the `PLUGINS_DIR=./data/plugins` that `.env.example` sets or
-> the `STORAGE_LOCAL_PATH=./data/media` the app writes on first run, names a path in the app's data
-> directory and is read under `OPENWA_DATA_DIR`, as are the `./data` database and state defaults; a
-> path from the environment is read against the current directory. When operating directly on the
-> host mount, a path recorded inside the container (`/app/data/...`) is not host-visible, so override
-> it in the environment.
+> replaces the target's and is the one the restored app reads. A path that copy names has to stay in
+> the data directory unless it is the default (a leftover `./uploads` included): the restore stops,
+> naming the key and before it copies anything, at one that leads elsewhere, whether outside the
+> directory, through `..`, a link or a link that does not resolve, or inside or around another
+> directory it refills from the archive. Set that key in the environment or `./.env` to restore
+> there. A relative `./data/...` path in `./.env` or `.env.generated`, such as the
+> `PLUGINS_DIR=./data/plugins` that `.env.example` sets or the `STORAGE_LOCAL_PATH=./data/media` the
+> app writes on first run, names a path in the app's data directory and is read under
+> `OPENWA_DATA_DIR`, as are the `./data` database and state defaults; a path from the environment is
+> read against the current directory. When operating directly on the host mount, a path recorded
+> inside the container (`/app/data/...`) is not host-visible, so override it in the environment.
>
> A leftover `STORAGE_LOCAL_PATH=./uploads` (which v0.2.0 to v0.7.3 wrote to `.env.generated`), in
> `./.env` or `.env.generated`, is read as `<OPENWA_DATA_DIR>/media` whenever `OPENWA_DATA_DIR` is
@@ -903,8 +907,9 @@ tar -tzf "$(ls -t ./backups/openwa-backup-*.tar.gz | head -n 1)"
Use the repo's `scripts/restore.sh`. It restores `main.sqlite` (auth/audit) and a SQLite data store;
for PostgreSQL it stages `database.sql` for the explicit import below. It also restores
-whatsapp-web.js/Baileys auth state, local media, plugins, and generated secret/config files. It snapshots
-the current data dir first so a bad restore can be undone:
+whatsapp-web.js/Baileys auth state, local media, plugins, and generated secret/config files, all as the
+archive holds them, so restore only archives you made. It snapshots the current data dir first so a
+bad restore can be undone:
```bash
# 1. Stop the app (so files are quiescent)
diff --git a/docs/14-migration-guide.md b/docs/14-migration-guide.md
index 62c609ad0..71c587f68 100644
--- a/docs/14-migration-guide.md
+++ b/docs/14-migration-guide.md
@@ -890,6 +890,7 @@ skips: check `pg_index.indisvalid` and drop the invalid index before building it
| `next` | The first boot rewrites every legacy `messages.type` value in one `UPDATE`, inside the transaction of the other pending migrations: on PostgreSQL about a minute per million legacy rows, with the port closed. Only a database whose conversion kept being cancelled on `0.25.0` still holds a large backlog | Count them with `SELECT count(*) FROM messages WHERE type IN ('chat', 'buttons_response', 'list_response', 'template_button_reply', 'ptt', 'vcard', 'multi_vcard')`; above a few million, run the migrations first (section 14.5), or raise `startupProbe.failureThreshold` to cover about a minute per million legacy rows (measured with twelve indexes on the table) on top of the index builds |
| `next` | A production boot refuses a `METRICS_TOKEN` that is the `.env.example` placeholder or a weak value the other secrets are checked against (`test`, `change-me`, `changeme`, `password`, `secret`, `admin`, `demo` and a few more), trimmed and case-insensitive | Set a long random token, or unset `METRICS_TOKEN` to keep `/api/metrics` disabled (it answers `404`); there is no length floor, so a short value that is not on the list still boots |
| `next` | A session delete removes its messages, outbox rows and delivery failures in batches (2000 rows on SQLite, 500 on PostgreSQL): on SQLite 300,000 to 600,000 messages take about 20 to 60 s, against about 10 s on `0.25.0`, which froze the whole process. A failure part-way leaves the session with fewer rows | Give the delete a client or proxy timeout that covers a large session, and repeat it after a failure: the repeat finishes the job |
+| `next` | `restore.sh` stops before it copies anything, naming the key, when a path from the archive's own `.env.generated` leads out of the data directory | Set that key in the environment or `./.env` to restore there; the default paths and a leftover `./uploads` restore as before |
| `0.25.0` | Once teardown begins (SIGTERM, SIGINT, `POST /api/infra/restart`) every request except `/api/health`, `/api/health/*`, the dashboard's HTML pages and CORS preflights answers `503` with `Connection: close`, `GET /api/infra/health` included | Probe `/api/health/live` for liveness that survives teardown, and retry a `503` once the instance is up again |
| `0.25.0` | `stop_grace_period` (Docker) and `terminationGracePeriodSeconds` (Kubernetes) go from 45 to 75 s, and a stop that runs 67 s past the shutdown delay exits with status 1 instead of 137 | Raise a pinned 45 s value to at least 75 s, plus the excess of `SHUTDOWN_DELAY_MS` over 3 s and of `WEBHOOK_SHUTDOWN_DRAIN_MS` over 5 s; on Helm upgrade with `--reset-then-reuse-values` (3.14 or later) or `-f values.yaml`, since plain `--reuse-values` keeps the 0.24.0 defaults; a container that drops all capabilities and starts as root must add `KILL` to the capabilities 0.24.0 shipped, or a stop never reaches the app; update exit-code alerts |
| `0.25.0` | `400` instead of `201` or `200` for a phone number WhatsApp reports as unregistered (Baileys; `503` when the lookup gets no answer) and for `vote-poll` with no matching option text (whatsapp-web.js) | Handle `400` (`POLL_OPTION_NOT_FOUND` carries `validOptions`) and `503` on these routes; an empty `options` list still clears a vote |
diff --git a/scripts/restore.sh b/scripts/restore.sh
index 2b6a8008f..2e288ba7b 100755
--- a/scripts/restore.sh
+++ b/scripts/restore.sh
@@ -29,7 +29,11 @@
# PLUGIN_STATE_DIR root whose plugins/ holds the plugin registry and ctx.storage (default: the
# data dir)
# BOOTSTRAP_KEY_FILE where the plaintext admin key goes (default: <data dir>/.api-key)
-# These paths resolve through the same layers as the databases.
+# These paths resolve through the same layers as the databases. One that
+# comes from the archive's .env.generated has to stay in the data dir, unless it
+# is the default: the restore stops at one that leads elsewhere, naming the key,
+# before it copies anything. Set the key in the environment or ./.env to restore
+# outside the data dir.
# OPENWA_RESTORE_SNAPSHOT_DIR
# where the safety snapshots go (default: next to the data dir, and next to
# each target outside it); needed when a parent is read-only, as in the
@@ -341,7 +345,9 @@ trap 'rm -rf "$STAGE"' EXIT
log "Extracting $ARCHIVE"
# Refuse archive path names that could escape STAGE. This restore command accepts archives produced by
-# backup.sh; path validation is an additional traversal guard, not a general untrusted-tar verifier.
+# backup.sh and installs their keys, plugins and configuration as they are; it is not a general
+# untrusted-tar verifier. Where it writes does not rest on the archive, though: a member name that could
+# escape STAGE is refused here, and a path from its .env.generated that leads out of the data dir below.
while IFS= read -r entry; do
case "$entry" in
/* | ../* | */../* | */..)
@@ -354,10 +360,10 @@ tar -xzf "$ARCHIVE" -C "$STAGE"
# Targets resolve exactly like the app: an explicit environment value, then ./.env, then the
# dashboard's .env.generated, else the app's ./data defaults under OPENWA_DATA_DIR. They may
-# legitimately live outside it. The archive's .env.generated replaces the target's further down, so
-# when it carries one, that copy is the third layer: the restored app reads its paths, and state
-# placed where the replaced file pointed would never be opened. Resolved before anything is
-# validated or written.
+# legitimately live outside it, where the environment or ./.env says so (see archive_target). The
+# archive's .env.generated replaces the target's further down, so when it carries one, that copy is
+# the third layer: the restored app reads its paths, and state placed where the replaced file
+# pointed would never be opened. Resolved before anything is validated or written.
if [ -f "$STAGE/.env.generated" ]; then
OPENWA_GENERATED_ENV="$STAGE/.env.generated"
log "Paths not set in the environment or ./.env come from the archive's .env.generated."
@@ -397,6 +403,100 @@ for member in sessions baileys media plugin-packages plugin-state; do
fi
done
+# The environment and ./.env are the operator's own; the archive's .env.generated is not, yet the paths it names
+# are where the restore empties directories and writes files. Such a path has to lie inside the data dir as the
+# kernel resolves it, unless it is where the app goes without it (a default, which the operator's own links may
+# lead elsewhere). Inside the data dir, two ways out remain closed: a link that does not resolve, which a copy
+# follows wherever it points, and a target inside or around a directory refilled from the archive, whose links
+# are in place before the other target is written. Setting the key in the environment or ./.env lifts this.
+from_archive() {
+ [ "$OPENWA_GENERATED_ENV" = "$STAGE/.env.generated" ] && [ -z "$(printenv "$1" 2>/dev/null || true)" ] &&
+ ! openwa_env_file_value ./.env "$1" >/dev/null 2>&1 &&
+ openwa_env_file_value "$OPENWA_GENERATED_ENV" "$1" >/dev/null 2>&1
+}
+
+refuse_archive_path() {
+ log "ERROR: $1 from the archive's .env.generated $3: $2"
+ log " nothing has been restored yet; to restore there, set $1 in the environment or ./.env and re-run"
+ exit 1
+}
+
+TARGET_KEYS=()
+TARGET_PATHS=()
+TARGET_KINDS=()
+TARGET_CHOSEN=()
+# archive_target <key> <path> <file|dir> <default>...: note a target the restore replaces, refusing it when the
+# archive's .env.generated chose it and it leads out of the data dir.
+archive_target() {
+ local key="$1" path="$2" kind="$3" resolved chosen=0 p
+ shift 3
+ resolved="$(resolve_path "$path")"
+ if from_archive "$key"; then
+ chosen=1
+ for p in "$@"; do
+ [ "$resolved" != "$(resolve_path "$p")" ] || chosen=0
+ done
+ fi
+ if [ "$chosen" -eq 1 ]; then
+ case "$resolved" in
+ "${RESOLVED_DATA_DIR%/}/"?*) ;;
+ *) refuse_archive_path "$key" "$path" "is not inside the data directory" ;;
+ esac
+ p="$path"
+ while [ ! -e "$p" ]; do
+ [ ! -L "$p" ] || refuse_archive_path "$key" "$path" "goes through a link that does not resolve ($p)"
+ p="$(dirname "$p")"
+ done
+ fi
+ TARGET_KEYS+=("$key")
+ TARGET_PATHS+=("$resolved")
+ TARGET_KINDS+=("$kind")
+ TARGET_CHOSEN+=("$chosen")
+}
+if [ -f "$STAGE/main.sqlite" ]; then
+ archive_target MAIN_DATABASE_NAME "$MAIN_DB" file "${DATA_DIR%/}/main.sqlite"
+fi
+if [ -f "$STAGE/openwa.sqlite" ]; then
+ archive_target DATABASE_NAME "$DATA_DB" file "${DATA_DIR%/}/openwa.sqlite"
+fi
+if [ -d "$STAGE/sessions" ]; then
+ archive_target SESSION_DATA_PATH "$SESSIONS_DIR" dir "$DATA_DIR/sessions"
+fi
+if [ -d "$STAGE/baileys" ]; then
+ archive_target BAILEYS_AUTH_DIR "$BAILEYS_DIR" dir "$DATA_DIR/baileys"
+fi
+# A leftover ./uploads stays where the app itself keeps media (openwa_media_dir), so it counts as a default.
+if [ -d "$STAGE/media" ]; then
+ archive_target STORAGE_LOCAL_PATH "$MEDIA_DIR" dir "$DATA_DIR/media" ./uploads
+fi
+if [ -d "$STAGE/plugin-packages" ]; then
+ archive_target PLUGINS_DIR "$PLUGIN_PACKAGES_DIR" dir "$DATA_DIR/plugins"
+fi
+if [ -d "$STAGE/plugin-state" ]; then
+ archive_target PLUGIN_STATE_DIR "$PLUGIN_STATE_DIR" dir "$DATA_DIR/plugins"
+fi
+if [ -f "$STAGE/.api-key" ]; then
+ archive_target BOOTSTRAP_KEY_FILE "$ADMIN_KEY_FILE" file "$DATA_DIR/.api-key"
+fi
+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
+ 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"
+ ;;
+ esac
+ fi
+ j=$((j + 1))
+ done
+ i=$((i + 1))
+done
+
# A backup taken without sqlite3 .backup carries this marker: the database snapshots were
# plain-copied from a possibly-live app and may be torn. Warn loudly and continue — unless
# --strict makes it fatal. Refuse BEFORE touching any existing state.
diff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.sh
index df9e42ed9..df17d87e6 100755
--- a/scripts/smoke-test-backup-restore.sh
+++ b/scripts/smoke-test-backup-restore.sh
@@ -93,6 +93,10 @@
# (as) restore refuses a MAIN_DATABASE_NAME, DATABASE_NAME or BOOTSTRAP_KEY_FILE that names an
# existing directory (the workspace's parent, the data dir, another directory, a link to one),
# before it copies, writes into or changes the mode of it
+# (at) restore refuses a path from the archive's .env.generated that leads out of the data dir (outside
+# it, through .., a link or a link that does not resolve, or inside or around a directory refilled
+# from the archive) before it copies anything, and restores there when the environment or ./.env
+# sets that key; a default reached through the operator's link is still followed
#
# Usage: ./scripts/smoke-test-backup-restore.sh
# Requires: bash, tar, node (restore.sh path resolution). sqlite3 is optional (see (c) and (k)).
@@ -347,13 +351,13 @@ echo "==> (f) data/.env.generated supplies paths the environment does not"
# alone then archives the abandoned file and exits 0 — a backup that only reveals itself as wrong
# during a restore. A missing default would at least fail loudly; a stale one does not.
F="$WORK/f"
-mkdir -p "$F/state" "$F/live" "$F/data" "$F/extract" "$F/restore/state"
-make_fixture "$F/live/auth.sqlite" "foxtrot-live-main"
-make_fixture "$F/live/store.sqlite" "foxtrot-live-data"
+mkdir -p "$F/state/live" "$F/data" "$F/extract" "$F/restore"
+make_fixture "$F/state/live/auth.sqlite" "foxtrot-live-main"
+make_fixture "$F/state/live/store.sqlite" "foxtrot-live-data"
make_fixture "$F/data/main.sqlite" "STALE-main"
make_fixture "$F/data/openwa.sqlite" "STALE-data"
printf 'DATABASE_TYPE=sqlite\nMAIN_DATABASE_NAME=%s\nDATABASE_NAME=%s\n' \
- "$F/live/auth.sqlite" "$F/live/store.sqlite" >"$F/state/.env.generated"
+ "$F/state/live/auth.sqlite" "$F/state/live/store.sqlite" >"$F/state/.env.generated"
(
cd "$F"
OPENWA_DATA_DIR="$F/state" BACKUP_DIR="$F/out" "$BACKUP" >/dev/null
@@ -369,21 +373,22 @@ fi
# restore.sh must read the same layer, from the file in effect AFTER the restore: the archive's
# .env.generated replaces the target's, so databases placed where the target's old file pointed
# would be ones the restored app never opens. Here the live databases are gone, as in a disaster
-# recovery, and the target's own file still names other paths.
-rm -f "$F/live/auth.sqlite" "$F/live/store.sqlite"
+# recovery, and the data dir's own file now names other paths. (at) covers an archive path outside
+# the data dir.
+rm -f "$F/state/live/auth.sqlite" "$F/state/live/store.sqlite"
printf 'DATABASE_TYPE=sqlite\nMAIN_DATABASE_NAME=%s\nDATABASE_NAME=%s\n' \
- "$F/restore/auth.sqlite" "$F/restore/store.sqlite" >"$F/restore/state/.env.generated"
+ "$F/state/other/auth.sqlite" "$F/state/other/store.sqlite" >"$F/state/.env.generated"
(
cd "$F/restore"
- OPENWA_DATA_DIR="$F/restore/state" "$RESTORE" "$ARCHIVE_F" >/dev/null
+ OPENWA_DATA_DIR="$F/state" "$RESTORE" "$ARCHIVE_F" >/dev/null
)
-if [ "$(db_fingerprint "$F/live/auth.sqlite")" != "foxtrot-live-main" ]; then
+if [ "$(db_fingerprint "$F/state/live/auth.sqlite")" != "foxtrot-live-main" ]; then
fail "(f) restore ignored the MAIN_DATABASE_NAME in the archive's data/.env.generated"
fi
-if [ "$(db_fingerprint "$F/live/store.sqlite")" != "foxtrot-live-data" ]; then
+if [ "$(db_fingerprint "$F/state/live/store.sqlite")" != "foxtrot-live-data" ]; then
fail "(f) restore ignored the DATABASE_NAME in the archive's data/.env.generated"
fi
-if [ -e "$F/restore/auth.sqlite" ] || [ -e "$F/restore/store.sqlite" ]; then
+if [ -e "$F/state/other/auth.sqlite" ] || [ -e "$F/state/other/store.sqlite" ]; then
fail "(f) restore wrote the databases where the replaced data/.env.generated pointed"
fi
# An explicit environment value must still win — that is the app's precedence, not ours to change.
@@ -2745,5 +2750,120 @@ for target in 'MAIN_DATABASE_NAME auth/audit DB' 'DATABASE_NAME data store' 'BOO
done
pass "(as) MAIN_DATABASE_NAME, DATABASE_NAME and BOOTSTRAP_KEY_FILE naming a directory are refused before anything is copied or changed"
+echo ""
+echo "==> (at) a path from the archive's .env.generated stays in the data dir unless the operator names the key"
+# The archive's own .env.generated says where the restore empties directories and writes files, and the archive
+# need not be one the operator made. A path it names that leads out of the data dir, through .., a link, a link
+# that does not resolve, or a directory the restore refills from the archive, is refused before anything is
+# copied, unless the environment or ./.env sets that key. A default, which the operator's own link may lead
+# elsewhere, is still followed.
+AT="$WORK/at"
+mkdir -p "$AT/src/data/sessions/session-1" "$AT/src/data/baileys/b1" "$AT/src/data/media" "$AT/src/data/plugins/p1" \
+ "$AT/work/data/sessions/old" "$AT/home/.ssh" "$AT/victim" "$AT/stage"
+make_fixture "$AT/src/data/main.sqlite" "at-main"
+make_fixture "$AT/src/data/openwa.sqlite" "at-data"
+printf 'at-session\n' >"$AT/src/data/sessions/session-1/marker"
+printf 'at-baileys\n' >"$AT/src/data/baileys/b1/creds.json"
+printf 'at-media\n' >"$AT/src/data/media/a.jpg"
+printf '{}\n' >"$AT/src/data/plugins/p1/manifest.json"
+printf 'at-key\n' >"$AT/src/data/.api-key"
+(
+ cd "$AT/src"
+ BACKUP_DIR="$AT/out" "$BACKUP" >/dev/null 2>&1
+)
+ARCHIVE_AT="$(ls "$AT"/out/openwa-backup-*.tar.gz)"
+printf 'at-keep\n' >"$AT/work/data/keep.txt"
+printf 'at-precious\n' >"$AT/victim/precious.txt"
+# at_archive <name> <.env.generated text> [<name in sessions/> <link target>]: the backup.sh archive above with that
+# .env.generated, and a link planted in its sessions/ when asked.
+at_archive() {
+ rm -rf "${AT:?}/stage" && mkdir -p "$AT/stage"
+ tar -xzf "$ARCHIVE_AT" -C "$AT/stage"
+ printf '%b' "$2" >"$AT/stage/.env.generated"
+ if [ -n "${3:-}" ]; then
+ ln -s "$4" "$AT/stage/sessions/$3"
+ fi
+ tar -czf "$AT/$1.tar.gz" -C "$AT/stage" .
+}
+# at_refused <label> <message> <archive> [VAR=value ...]: refused like refused_restore, and before the data-dir
+# safety snapshot, the first copy a restore makes.
+at_refused() {
+ local label="$1" message="$2" archive="$3"
+ shift 3
+ refused_restore "$AT" "$label" "$message" "$AT/work" "$archive" OPENWA_DATA_DIR="$AT/work/data" "$@"
+ if printf '%s' "$OUT" | grep -q 'Snapshotting' || [ -n "$(find "$AT" -name '*.pre-restore-*' -print -quit)" ]; then
+ fail "($label) the restore took a snapshot before it refused: $OUT"
+ fi
+}
+AT_OUT="from the archive's .env.generated is not inside the data directory"
+for at_key in MAIN_DATABASE_NAME DATABASE_NAME SESSION_DATA_PATH BAILEYS_AUTH_DIR STORAGE_LOCAL_PATH PLUGINS_DIR \
+ PLUGIN_STATE_DIR BOOTSTRAP_KEY_FILE; do
+ at_archive outside "$at_key=$AT/victim/x\n"
+ at_refused "at: $at_key outside" "$at_key $AT_OUT" "$AT/outside.tar.gz"
+done
+at_archive key "BOOTSTRAP_KEY_FILE=$AT/home/.ssh/authorized_keys\n"
+at_refused "at: key file in another directory" "BOOTSTRAP_KEY_FILE $AT_OUT" "$AT/key.tar.gz"
+at_archive dotdot 'SESSION_DATA_PATH=./data/../../victim\n'
+at_refused "at: .. out of the data dir" "SESSION_DATA_PATH $AT_OUT" "$AT/dotdot.tar.gz"
+at_archive sibling 'SESSION_DATA_PATH=./data-sessions\n'
+at_refused "at: a sibling named like the data dir" "SESSION_DATA_PATH $AT_OUT" "$AT/sibling.tar.gz"
+ln -s "$AT/victim" "$AT/work/data/lnk"
+at_archive link 'SESSION_DATA_PATH=./data/lnk\n'
+at_refused "at: link out of the data dir" "SESSION_DATA_PATH $AT_OUT" "$AT/link.tar.gz"
+# A copy onto a link that does not resolve creates the file the link names, wherever that is.
+ln -s "$AT/home/.ssh/authorized_keys" "$AT/work/data/dangling"
+at_archive dangling 'BOOTSTRAP_KEY_FILE=./data/dangling\n'
+at_refused "at: link that does not resolve" "BOOTSTRAP_KEY_FILE from the archive's .env.generated goes through a link that does not resolve" \
+ "$AT/dangling.tar.gz"
+rm -f "$AT/work/data/lnk" "$AT/work/data/dangling"
+# The sessions refilled from the archive bring their links along before the key file is written, and before a
+# directory the environment names under them is emptied.
+at_archive nested 'SESSION_DATA_PATH=./data/s\nBOOTSTRAP_KEY_FILE=./data/s/k\n' k "$AT/home/.ssh/authorized_keys"
+at_refused "at: key file inside the refilled sessions" \
+ "BOOTSTRAP_KEY_FILE from the archive's .env.generated puts the BOOTSTRAP_KEY_FILE target inside the SESSION_DATA_PATH directory" \
+ "$AT/nested.tar.gz"
+at_archive around 'SESSION_DATA_PATH=./data/x\n' b "$AT/victim"
+at_refused "at: sessions around the environment's Baileys dir" \
+ "SESSION_DATA_PATH from the archive's .env.generated puts the BAILEYS_AUTH_DIR target inside the SESSION_DATA_PATH directory" \
+ "$AT/around.tar.gz" BAILEYS_AUTH_DIR="$AT/work/data/x/b"
+# (f)'s archive, from backup.sh, keeps its databases in its own data dir, which is not this one.
+at_refused "at: (f)'s databases into another data dir" "MAIN_DATABASE_NAME $AT_OUT" "$ARCHIVE_F"
+if [ -e "$AT/home/.ssh/authorized_keys" ] || [ "$(ls -A "$AT/victim")" != precious.txt ]; then
+ fail "(at) a refused restore wrote outside the data dir"
+fi
+pass "(at) a path from the archive's .env.generated that leads out of the data dir is refused before anything is copied"
+# The same paths, named by the operator, are restored there.
+at_archive elsewhere "SESSION_DATA_PATH=$AT/elsewhere\n"
+(cd "$AT/work" && HOME="$AT/home" OPENWA_DATA_DIR="$AT/env/data" SESSION_DATA_PATH="$AT/elsewhere" \
+ "$RESTORE" "$AT/elsewhere.tar.gz" >/dev/null 2>&1) || fail "(at) restore with SESSION_DATA_PATH in the environment failed"
+if [ "$(cat "$AT/elsewhere/session-1/marker" 2>/dev/null || true)" != "at-session" ]; then
+ fail "(at) the sessions did not land at the SESSION_DATA_PATH the environment names"
+fi
+mkdir -p "$AT/dotenv"
+printf 'SESSION_DATA_PATH=%s\n' "$AT/elsewhere-dotenv" >"$AT/dotenv/.env"
+(cd "$AT/dotenv" && HOME="$AT/home" "$RESTORE" "$AT/elsewhere.tar.gz" >/dev/null 2>&1) ||
+ fail "(at) restore with SESSION_DATA_PATH in ./.env failed"
+if [ "$(cat "$AT/elsewhere-dotenv/session-1/marker" 2>/dev/null || true)" != "at-session" ]; then
+ fail "(at) the sessions did not land at the SESSION_DATA_PATH ./.env names"
+fi
+# With both of its keys set, (f)'s archive restores where they point.
+mkdir -p "$AT/other/data"
+(cd "$AT/other" && HOME="$AT/home" MAIN_DATABASE_NAME="$AT/other/auth.sqlite" DATABASE_NAME="$AT/other/store.sqlite" \
+ "$RESTORE" "$ARCHIVE_F" >/dev/null 2>&1) || fail "(at) restore of (f)'s archive with both keys in the environment failed"
+if [ "$(db_fingerprint "$AT/other/auth.sqlite")" != "foxtrot-live-main" ]; then
+ fail "(at) the database did not land where the environment names it"
+fi
+# A default the dashboard writes, through the operator's link to another disk, is followed as before.
+mkdir -p "$AT/disk/sessions" "$AT/linked/data"
+printf 'at-disk-old\n' >"$AT/disk/sessions/old"
+ln -s "$AT/disk/sessions" "$AT/linked/data/sessions"
+at_archive default 'SESSION_DATA_PATH=./data/sessions\nSTORAGE_LOCAL_PATH=./data/media\n'
+(cd "$AT/linked" && HOME="$AT/home" "$RESTORE" "$AT/default.tar.gz" >/dev/null 2>&1) ||
+ fail "(at) restore of the dashboard's default paths through a linked sessions dir failed"
+if [ ! -L "$AT/linked/data/sessions" ] || [ "$(cat "$AT/disk/sessions/session-1/marker" 2>/dev/null || true)" != "at-session" ]; then
+ fail "(at) the default sessions dir was not refilled through the operator's link"
+fi
+pass "(at) the environment, ./.env and the defaults still decide where the restore writes"
+
echo ""
echo "All smoke tests passed!"