Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(restore): refuse an unwritable target before writing anything
Yudhi Armyndharis7766701945fe
diff --git a/scripts/lib-env.sh b/scripts/lib-env.shindex 52e6f4e54..0bbae386b 100644--- a/scripts/lib-env.sh+++ b/scripts/lib-env.sh@@ -51,6 +51,18 @@ openwa_env_file_value() {printf '%s' "$value"}+# openwa_writable <path> - whether <path> can be written, or created when it does not exist yet (its+# nearest existing ancestor is then the directory that has to take it).+openwa_writable() {+ local p="$1"+ if [ -e "$p" ]; then+ [ -w "$p" ]+ return+ fi+ while [ ! -e "$p" ]; do p="$(dirname "$p")"; done+ [ -d "$p" ] && [ -w "$p" ]+}+# Layer 3. Set here rather than read from the environment, so it can never arrive from an operator's# shell; restore.sh points it at the archive's copy, which replaces this file during the restore.OPENWA_GENERATED_ENV="${DATA_DIR:-./data}/.env.generated"diff --git a/scripts/restore.sh b/scripts/restore.shindex 8be819df5..680ff03b8 100755--- a/scripts/restore.sh+++ b/scripts/restore.sh@@ -115,6 +115,13 @@ if [ "$RESOLVED_DATA_DIR" = "$RESOLVED_USER_HOME" ]; thenexit 1fi+# refuse_unwritable <path> <label>: stop in the snapshot phase, before the first database is replaced.+refuse_unwritable() {+ log "ERROR: cannot write the $2 target: $1"+ log " nothing has been restored yet; fix its permissions or point the setting at a writable path, then re-run"+ exit 1+}+replace_tree() {source_dir="$1"target_dir="$2"@@ -143,6 +150,13 @@ replace_tree() {;;esacif [ "$PHASE" = snapshot ]; then+ # An existing directory is emptied before it is refilled, which needs every directory in it.+ openwa_writable "$target_dir" || refuse_unwritable "$target_dir" "$label"+ if [ -d "$target_dir" ]; then+ blocked="$(find "$target_dir/" -type d \+ -exec sh -c 'for d do [ -w "$d" ] || { echo "$d"; exit 1; }; done' sh {} + 2>/dev/null)" || true+ [ -z "$blocked" ] || refuse_unwritable "${blocked%%$'\n'*}" "$label"+ fisnapshot_external "$target_dir"returnfi@@ -197,6 +211,14 @@ snapshot_external() {restore_db() {resolved_db="$(resolve_path "$2")"if [ "$PHASE" = snapshot ]; then+ openwa_writable "$2" || refuse_unwritable "$2" "$3"+ for db in "$2" "$resolved_db"; do+ for sfx in -wal -shm -journal; do+ if [ -e "$db$sfx" ] && [ ! -w "$(dirname "$db")" ]; then+ refuse_unwritable "$(dirname "$db")" "$3"+ fi+ done+ donesnapshot_external "$2"# Empty when the target lives in the data dir, whose own snapshot already holds the sidecars.if [ -n "$external_snapshot" ]; then@@ -364,8 +386,9 @@ if [ -d "$STAGE/plugin-packages" ] && [ -d "$STAGE/plugin-state" ] &&fi# Runs twice: PHASE=snapshot checks every target and snapshots the ones outside the data dir, then-# PHASE=apply writes them. A target that cannot be snapshotted or is refused stops the restore-# before the first database is written, instead of halfway through with a mixed install left behind.+# PHASE=apply writes them. A target that is refused, or cannot be snapshotted or written, stops the+# restore before the first database is written, instead of halfway through with a mixed install left+# behind.restore_targets() {if [ -f "$STAGE/main.sqlite" ]; thenrestore_db "$STAGE/main.sqlite" "$MAIN_DB" "auth/audit DB"diff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.shindex 28b394b9a..814c5ba05 100755--- a/scripts/smoke-test-backup-restore.sh+++ b/scripts/smoke-test-backup-restore.sh@@ -29,6 +29,8 @@# (s) restored state lands where the restored data/.env.generated points, below ./.env# (t) ./.env lines with CRLF endings, blanks around = or trailing blanks resolve as dotenv reads them,# and a `KEY: value` line is reported+# (u) a state or database target the restore cannot write stops it before any database is written+# (skipped as root)## Usage: ./scripts/smoke-test-backup-restore.sh# Requires: bash, tar, node (restore.sh path resolution). sqlite3 is optional (see (c) and (k)).@@ -889,5 +891,65 @@ if [ -n "$(find "$T/dst/data" -name "*$(printf '\r')*" 2>/dev/null)" ]; thenfipass "(t) CRLF, spaced and blank-padded ./.env lines resolve like dotenv, and \`KEY: value\` is reported"+echo ""+echo "==> (u) a target the restore cannot write stops it before any database is written"+# The snapshot pass skipped a target that did not exist yet and never asked whether one could be+# written, so the databases were replaced first and the run died on the state directory after them,+# leaving the archive's databases beside the old sessions, media and configuration.+if [ "$(id -u)" -ne 0 ]; then+ U="$WORK/u"+ mkdir -p "$U/src/data/sessions/session-s1" "$U/live" "$U/ro" "$U/busy/sessions/session-s1"+ make_fixture "$U/src/data/main.sqlite" "uniform-archive-main"+ make_fixture "$U/src/data/openwa.sqlite" "uniform-archive-data"+ printf 'uniform-archive\n' >"$U/src/data/sessions/session-s1/marker"+ (+ cd "$U/src"+ BACKUP_DIR="$U/out" "$BACKUP" >/dev/null+ )+ ARCHIVE_U="$(ls "$U"/out/openwa-backup-*.tar.gz)"+ make_fixture "$U/live/main.sqlite" "uniform-live-main"+ printf 'uniform-live\n' >"$U/busy/sessions/session-s1/marker"++ # restore_u <sessions dir> <data-store path>: a forced restore of ARCHIVE_U over $U/live. Output+ # lands in OUT, the exit code in RC.+ restore_u() {+ set +e+ OUT="$(cd "$U" && MAIN_DATABASE_NAME="$U/live/main.sqlite" DATABASE_NAME="$2" OPENWA_DATA_DIR="$U/live" \+ SESSION_DATA_PATH="$1" OPENWA_RESTORE_SNAPSHOT_DIR="$U/snapshots" "$RESTORE" "$ARCHIVE_U" --force 2>&1)"+ RC=$?+ set -e+ }+ # expect_untouched <label>: the refused restore must fail, say why, and leave the main DB alone.+ expect_untouched() {+ if [ "$RC" -eq 0 ]; then+ fail "(u) restore exited 0 with an unwritable $1 target"+ fi+ if ! printf '%s' "$OUT" | grep -q 'cannot write'; then+ fail "(u) the refusal for an unwritable $1 target does not say so: $OUT"+ fi+ if [ "$(db_fingerprint "$U/live/main.sqlite")" != "uniform-live-main" ]; then+ fail "(u) the main DB was overwritten before the unwritable $1 target stopped the restore"+ fi+ }++ chmod a-w "$U/ro"+ restore_u "$U/ro/sessions" "$U/live/openwa.sqlite"+ expect_untouched "missing sessions"+ restore_u "$U/busy/sessions" "$U/ro/openwa.sqlite"+ expect_untouched "data store"+ chmod u+w "$U/ro"+ # An existing directory is emptied before it is refilled, which needs every directory in it.+ chmod a-w "$U/busy/sessions/session-s1"+ restore_u "$U/busy/sessions" "$U/live/openwa.sqlite"+ chmod u+w "$U/busy/sessions/session-s1"+ expect_untouched "non-empty sessions"+ if [ "$(cat "$U/busy/sessions/session-s1/marker")" != "uniform-live" ]; then+ fail "(u) the refused restore changed the live sessions"+ fi+ pass "(u) an unwritable state or database target stops the restore before anything is written"+else+ echo "SKIP: (u) running as root, which ignores the permission bits this case relies on"+fi+echo ""echo "All smoke tests passed!"