Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(scripts): refuse an env file this user cannot read

Yudhi Armyndharis458cf2c3b768
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 850003204..259e31cc4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -65,6 +65,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Set `PGSSLMODE` in the `psql` import command `restore.sh` prints when `DATABASE_SSL=true`.
- Use the `OPENWA_DATA_DIR` volume's `media/`, even before it exists, for a leftover `STORAGE_LOCAL_PATH=./uploads` in host-side `backup.sh` and `restore.sh` runs, instead of the working directory's `./uploads`.
- Read the `./data` database defaults and `./data/...` paths in `./.env` under `OPENWA_DATA_DIR` in host-side `backup.sh` and `restore.sh` runs, instead of the working directory's `./data`.
+- Stop `backup.sh` and `restore.sh` with an error when `./.env` or `.env.generated` exists but the running user cannot read it, instead of using the default data paths.
- Probe the configured `PORT` in the Docker image healthcheck instead of a fixed 2785.
- Cap the total bytes one storage import extracts at `STORAGE_IMPORT_MAX_TOTAL_BYTES`.
- Close the Baileys session proxy's fetch dispatcher when the session disconnects, logs out or is destroyed.
diff --git a/scripts/lib-env.sh b/scripts/lib-env.sh
index 672910bee..7fef236e6 100644
--- a/scripts/lib-env.sh
+++ b/scripts/lib-env.sh
@@ -206,6 +206,12 @@ openwa_env_file_value() {
# A line of well-formed UTF-8, without NUL.
local utf8=$'([\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xec\xee\xef][\x80-\xbf]{2}|\xed[\x80-\x9f][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf]{2}|[\xf1-\xf3][\x80-\xbf]{3}|\xf4[\x80-\x8f][\x80-\xbf]{2})*'
[ -f "$file" ] || return 1
+ # A file this user cannot open would read as one that does not set the key, and the lookup would fall
+ # through to a default the app does not use. Root passes the test whatever the mode.
+ if [ ! -r "$file" ]; then
+ echo "[config] ERROR: $file exists but cannot be read by this user; run as a user that can read it or pass $key in the environment." >&2
+ return 2
+ fi
# Without these the key's line would read as absent and the lookup would fall through to a default.
for tool in tr tail sed grep; do
command -v "$tool" >/dev/null 2>&1 && continue
diff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.sh
index bc767f576..b6c837979 100755
--- a/scripts/smoke-test-backup-restore.sh
+++ b/scripts/smoke-test-backup-restore.sh
@@ -62,6 +62,8 @@
# a ./data path or absolute media path from the environment are read as written, an
# environment ./uploads is exempt from the empty-volume rule, and a skipped non-empty ./uploads
# is named in a warning
+# (aj) a ./.env this user cannot read stops backup and restore, where it used to read as a file that
+# sets nothing and send both to the default paths (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)).
@@ -2157,5 +2159,44 @@ if [ "$(cat "$AI/pg/pg_dump-args" 2>/dev/null || true)" != "data/s3cret|-h|data|
fi
pass "(ai) a host run backs up and restores the volume's databases, media, sessions and plugins, not the working directory's"
+echo ""
+echo "==> (aj) a ./.env this user cannot read stops backup and restore"
+# A file that exists but cannot be opened (written by sudo or deploy tooling, say) read as a layer that
+# sets nothing. Backup then archived the stale database at the default path and exited 0, and restore
+# wrote to the default paths while the app opened the ones the file names. Root ignores the mode bits.
+if [ "$(id -u)" -ne 0 ]; then
+ AJ="$WORK/aj"
+ mkdir -p "$AJ/src/data" "$AJ/dst"
+ make_fixture "$AJ/src/data/main.sqlite" "aj-main"
+ make_fixture "$AJ/src/data/openwa.sqlite" "aj-data"
+ (cd "$AJ/src" && BACKUP_DIR="$AJ/out" "$BACKUP" >/dev/null 2>&1) || fail "(aj) the setup backup failed"
+ ARCHIVE_AJ="$(ls "$AJ"/out/openwa-backup-*.tar.gz)"
+ for AJ_DIR in src dst; do
+ printf 'DATABASE_NAME=./data/active.sqlite\n' >"$AJ/$AJ_DIR/.env"
+ chmod 000 "$AJ/$AJ_DIR/.env"
+ done
+ set +e
+ OUT_AJ_BACKUP="$(cd "$AJ/src" && BACKUP_DIR="$AJ/out-unread" "$BACKUP" 2>&1)"
+ RC_AJ_BACKUP=$?
+ OUT_AJ_RESTORE="$(cd "$AJ/dst" && "$RESTORE" "$ARCHIVE_AJ" 2>&1)"
+ RC_AJ_RESTORE=$?
+ set -e
+ if [ "$RC_AJ_BACKUP" -eq 0 ] || ! printf '%s' "$OUT_AJ_BACKUP" | grep -q 'cannot be read'; then
+ fail "(aj) backup went on past an unreadable ./.env (rc $RC_AJ_BACKUP): $OUT_AJ_BACKUP"
+ fi
+ if ls "$AJ"/out-unread/openwa-backup-*.tar.gz >/dev/null 2>&1; then
+ fail "(aj) backup archived the default path past an unreadable ./.env"
+ fi
+ if [ "$RC_AJ_RESTORE" -eq 0 ] || ! printf '%s' "$OUT_AJ_RESTORE" | grep -q 'cannot be read'; then
+ fail "(aj) restore went on past an unreadable ./.env (rc $RC_AJ_RESTORE): $OUT_AJ_RESTORE"
+ fi
+ if [ -e "$AJ/dst/data" ]; then
+ fail "(aj) restore wrote into the default data dir past an unreadable ./.env"
+ fi
+ pass "(aj) an unreadable ./.env stops backup and restore before any state moves"
+else
+ echo "SKIP: (aj) running as root, which ignores the permission bits this case relies on"
+fi
+
echo ""
echo "All smoke tests passed!"