Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(scripts): stop the backup when a DATABASE_SSL line cannot be parsed (#1822)

Yudhi Armyndhariseb90b65ccb9d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 866bb5603..f9cacaf65 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -47,7 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Refuse Baileys animated stickers over 500 frames and run at most two sticker conversions at once.
- Stop pending webhook deliveries from holding over-cap inline media already omitted from their payload.
- Resolve `./data/...` paths from `.env.generated` under `OPENWA_DATA_DIR` in host-side `backup.sh` and `restore.sh` runs.
-- Stop `backup.sh` and `restore.sh` on an env line they cannot parse instead of falling back to the default path.
+- Stop `backup.sh` and `restore.sh` on an env line they cannot parse instead of falling back to a default path or TLS setting.
- Verify the PostgreSQL server certificate in `backup.sh` when `DATABASE_SSL=true`, as the app does.
- Dead-letter undispatched ingress events instead of deleting them when they age out of the dedup window.
- Keep a re-sent ingress delivery redrivable when an earlier dead letter for its id was already redriven.
@@ -103,7 +103,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- A storage import stops at `STORAGE_IMPORT_MAX_TOTAL_BYTES` (default 10 GiB); raise it before importing a larger export.
- Baileys answers `400` for an animated sticker with more than 500 frames.
- `backup.sh` and `restore.sh` exit with status 2 when `./.env` or `.env.generated` sets a key in a form they cannot parse; fix the line or pass the key in the environment.
-- With `DATABASE_SSL=true`, `backup.sh` now fails against a PostgreSQL server whose certificate the app would reject; trust a private CA through `NODE_EXTRA_CA_CERTS` or `PGSSLROOTCERT`.
+- With `DATABASE_SSL=true`, `backup.sh` now fails against a PostgreSQL server whose certificate the app would reject; trust a private CA through `NODE_EXTRA_CA_CERTS` or `PGSSLROOTCERT`, and give `DATABASE_URL` the host name the certificate carries or set `PGSSLMODE`.
## [0.24.0] - 2026-10-03
diff --git a/docs/11-operational-runbooks.md b/docs/11-operational-runbooks.md
index 277fd7dc5..cb61e8bb4 100644
--- a/docs/11-operational-runbooks.md
+++ b/docs/11-operational-runbooks.md
@@ -744,6 +744,8 @@ OPENWA_DATA_DIR=/srv/openwa/data \
# Node trusts (add a private CA through NODE_EXTRA_CA_CERTS, as for the app), or sslmode=require when
# DATABASE_SSL_REJECT_UNAUTHORIZED=false. On a host without node it uses sslrootcert=system, which needs
# libpq 16+ and a system CA store. PGSSLMODE and PGSSLROOTCERT, when set, take precedence.
+# verify-full also matches the host pg_dump connects to, DATABASE_URL's included, against the
+# certificate, so name the host the certificate carries.
```
> The data directory is a Docker **named volume** (`openwa-data`) in the production
diff --git a/scripts/backup.sh b/scripts/backup.sh
index d859f4d7b..7c7662b5a 100755
--- a/scripts/backup.sh
+++ b/scripts/backup.sh
@@ -49,6 +49,8 @@
# Node trusts (NODE_EXTRA_CA_CERTS included), or require when the second is
# false. Without node it uses sslrootcert=system, which needs libpq 16+ and a
# system CA store. PGSSLMODE and PGSSLROOTCERT, when set, take precedence.
+# verify-full also matches the host pg_dump connects to, DATABASE_URL's
+# included, against the certificate, so name the host the certificate carries.
#
# Failure policy: a missing source database is FATAL (no silent empty backup), and the finished
# archive must contain every configured database or it is deleted and the run fails. When the
@@ -222,8 +224,16 @@ if [ "$DATABASE_TYPE" = "postgres" ]; then
# the dump sent the database password past the TLS check the app makes. Apply the app's check:
# verify the server against the CA roots Node trusts (the image has no system CA store), or only
# encrypt when DATABASE_SSL_REJECT_UNAUTHORIZED=false. An operator's PGSSLMODE or PGSSLROOTCERT wins.
- if [ "$(openwa_resolve DATABASE_SSL false)" = true ] && [ -z "${PGSSLMODE:-}" ]; then
- if [ "$(openwa_resolve DATABASE_SSL_REJECT_UNAUTHORIZED true)" = false ]; then
+ # Each key is resolved on its own line, where set -e stops the run on a line the scripts cannot
+ # parse; inside a `[` test that failure would be ignored and pg_dump would connect as if the line
+ # were absent.
+ PG_SSL=false
+ if [ -z "${PGSSLMODE:-}" ]; then
+ PG_SSL="$(openwa_resolve DATABASE_SSL false)"
+ fi
+ if [ "$PG_SSL" = true ]; then
+ PG_SSL_REJECT="$(openwa_resolve DATABASE_SSL_REJECT_UNAUTHORIZED true)"
+ if [ "$PG_SSL_REJECT" = false ]; then
export PGSSLMODE=require
else
if [ -z "${PGSSLROOTCERT:-}" ] && command -v node >/dev/null 2>&1; then
diff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.sh
index f08c18526..684fdcc44 100755
--- a/scripts/smoke-test-backup-restore.sh
+++ b/scripts/smoke-test-backup-restore.sh
@@ -52,7 +52,8 @@
# backup and restore stop before archiving or writing anything instead of using a default the app
# may not read
# (ah) pg_dump makes the app's TLS check when DATABASE_SSL=true (falling back to the system CA store
-# without node), and connects as before when it is not
+# without node), connects as before when it is not, and never starts past a DATABASE_SSL or
+# DATABASE_SSL_REJECT_UNAUTHORIZED line the scripts cannot parse
# (ai) ./data paths in .env.generated follow OPENWA_DATA_DIR in a run on the host, in both scripts,
# while non-path settings there are read as written
#
@@ -1749,6 +1750,27 @@ if [ "$(backup_ah "${PG_AH}DATABASE_SSL=true\nDATABASE_SSL_REJECT_UNAUTHORIZED=f
DATABASE_URL=postgres://openwa@db/openwa)" != "mode=require root= certs=none" ]; then
fail "(ah) DATABASE_SSL_REJECT_UNAUTHORIZED=false did not encrypt without verifying: $(cat "$AH/log")"
fi
+# The app reads a TLS line the scripts cannot parse, so the run stops before pg_dump connects without it.
+for check in 'DATABASE_SSL|DATABASE_SSL="true" # tls on\n' \
+ 'DATABASE_SSL_REJECT_UNAUTHORIZED|DATABASE_SSL=true\nDATABASE_SSL_REJECT_UNAUTHORIZED="false" # x\n'; do
+ key="${check%%|*}"
+ printf '%b' "${PG_AH}${check#*|}" >"$AH/data/.env.generated"
+ rm -rf "$AH/out" "$AH/log"
+ set +e
+ OUT_AH="$(cd "$AH" && AH_LOG="$AH/log" PATH="$AH/shim:$PATH" BACKUP_DIR="$AH/out" "$BACKUP" 2>&1)"
+ rc=$?
+ set -e
+ if [ "$rc" -ne 2 ] || [ -e "$AH/log" ] || ls "$AH"/out/openwa-backup-* >/dev/null 2>&1 ||
+ ! grep -q "sets $key in a form" <<<"$OUT_AH"; then
+ fail "(ah) backup ran pg_dump past an unparsed $key line (rc $rc): $OUT_AH"
+ fi
+done
+# A line the run does not read does not stop it: DATABASE_SSL under PGSSLMODE, the second key when
+# the first is false.
+if [ "$(backup_ah "${PG_AH}DATABASE_SSL=\"true\" # tls on\n" PGSSLMODE=require)" != "mode=require root= certs=none" ] ||
+ [ "$(backup_ah "${PG_AH}DATABASE_SSL=false\nDATABASE_SSL_REJECT_UNAUTHORIZED=\"false\" # x\n")" != "mode= root= certs=none" ]; then
+ fail "(ah) an unparsed TLS line the run does not read stopped the backup: $(cat "$AH/log")"
+fi
if [ "$(backup_ah "${PG_AH}DATABASE_SSL=true\n" PGSSLROOTCERT="$AH/ca.pem")" != \
"mode=verify-full root=$AH/ca.pem certs=none" ] ||
[ "$(backup_ah "${PG_AH}DATABASE_SSL=true\n" PGSSLMODE=verify-ca)" != "mode=verify-ca root= certs=none" ]; then