Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(scripts): honour DATABASE_SSL when backing up PostgreSQL (#1781)

Yudhi Armyndharis525326fb738f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fb87a81c9..ac874fb2d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
+- 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.
- Deliver Baileys API edit and revoke events to webhook and WebSocket consumers.
@@ -58,6 +59,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Retain send idempotency keys after engine-stage failures, including HTTP 409.
- Reject poll votes with no matching options while preserving explicit vote clearing.
+### Upgrade notes (behavior changes)
+
+- 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`.
+
## [0.24.0] - 2026-10-03
### Added
diff --git a/docs/11-operational-runbooks.md b/docs/11-operational-runbooks.md
index 3ac62d2d0..abe8210b4 100644
--- a/docs/11-operational-runbooks.md
+++ b/docs/11-operational-runbooks.md
@@ -692,6 +692,11 @@ OPENWA_DATA_DIR=/srv/openwa/data \
BACKUP_DIR=/backups/openwa \
DATABASE_TYPE=postgres DATABASE_URL=postgres://user@host:5432/openwa PGPASSWORD='<password>' \
./scripts/backup.sh
+
+# With DATABASE_SSL=true, pg_dump makes the app's TLS check: sslmode=verify-full against the CA roots
+# 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.
```
> The data directory is a Docker **named volume** (`openwa-data`) in the production
diff --git a/scripts/backup.sh b/scripts/backup.sh
index 758a3382a..5583a0f26 100755
--- a/scripts/backup.sh
+++ b/scripts/backup.sh
@@ -43,6 +43,11 @@
# other local users can read in the process list, so leave the password out of
# it and supply PGPASSWORD or ~/.pgpass instead; the DATABASE_* path already
# passes DATABASE_PASSWORD through PGPASSWORD.
+# DATABASE_SSL, DATABASE_SSL_REJECT_UNAUTHORIZED
+# pg_dump makes the app's TLS check: sslmode=verify-full against the CA roots
+# 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.
#
# 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
@@ -208,6 +213,27 @@ if [ "$DATABASE_TYPE" = "postgres" ]; then
log "ERROR: DATABASE_TYPE=postgres but pg_dump is not installed"
exit 1
fi
+ # libpq defaults to sslmode=prefer, which falls back to plaintext and accepts any certificate, so
+ # 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
+ export PGSSLMODE=require
+ else
+ if [ -z "${PGSSLROOTCERT:-}" ] && command -v node >/dev/null 2>&1; then
+ PG_ROOT_CERTS="$(mktemp)"
+ trap 'rm -rf "$STAGE" "$PG_ROOT_CERTS"' EXIT
+ node -e 'const tls = require("tls");
+ console.log((tls.getCACertificates ? tls.getCACertificates("default") : tls.rootCertificates).join("\n"))' \
+ >"$PG_ROOT_CERTS"
+ elif [ -z "${PGSSLROOTCERT:-}" ]; then
+ log "node not found: verifying the server against the system CA store (sslrootcert=system," \
+ "libpq 16+); set PGSSLROOTCERT to a CA file if pg_dump cannot use it"
+ fi
+ export PGSSLMODE=verify-full PGSSLROOTCERT="${PGSSLROOTCERT:-${PG_ROOT_CERTS:-system}}"
+ fi
+ fi
DATABASE_URL_RESOLVED="$(openwa_resolve DATABASE_URL '')"
if [ -n "$DATABASE_URL_RESOLVED" ]; then
pg_dump "$DATABASE_URL_RESOLVED" >"$STAGE/database.sql"
diff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.sh
index d759bfd7b..a414aee41 100755
--- a/scripts/smoke-test-backup-restore.sh
+++ b/scripts/smoke-test-backup-restore.sh
@@ -50,6 +50,8 @@
# (af) the online SQLite backup waits out a writer holding the database lock (skipped without sqlite3)
# (ag) quoted values, inline comments and `KEY=""` in ./.env resolve as dotenv reads them, and a line
# the scripts cannot parse ends the lookup at the default instead of reading data/.env.generated
+# (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
#
# Usage: ./scripts/smoke-test-backup-restore.sh
# Requires: bash, tar, node (restore.sh path resolution). sqlite3 is optional (see (c) and (k)).
@@ -59,7 +61,8 @@ set -euo pipefail
# would aim a case at a real install, and restore replaces the state directories wholesale, so every
# case starts from none of them and sets exactly the paths it uses.
unset OPENWA_DATA_DIR BACKUP_DIR DATABASE_TYPE MAIN_DATABASE_NAME DATABASE_NAME SESSION_DATA_PATH \
- BAILEYS_AUTH_DIR STORAGE_LOCAL_PATH PLUGINS_DIR PLUGIN_STATE_DIR OPENWA_RESTORE_SNAPSHOT_DIR BOOTSTRAP_KEY_FILE
+ BAILEYS_AUTH_DIR STORAGE_LOCAL_PATH PLUGINS_DIR PLUGIN_STATE_DIR OPENWA_RESTORE_SNAPSHOT_DIR BOOTSTRAP_KEY_FILE \
+ DATABASE_URL DATABASE_SSL DATABASE_SSL_REJECT_UNAUTHORIZED PGSSLMODE PGSSLROOTCERT
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
BACKUP="$REPO_ROOT/scripts/backup.sh"
@@ -1440,5 +1443,66 @@ if [ "$(grep -c 'do not parse' "$AG/err")" -ne 3 ] || ! grep -q 'sets SESSION_DA
fi
pass "(ag) dotenv's quoted, commented and empty forms resolve like the app, and an unparsed line stops the lookup"
+echo ""
+echo "==> (ah) pg_dump makes the app's TLS check when DATABASE_SSL=true"
+# libpq defaults to sslmode=prefer: it falls back to plaintext and takes any certificate, so a dump of
+# a database the app reaches over verified TLS sent its password to whoever answered. The shim pg_dump
+# records the TLS settings it was started with and whether its root certificate file held any CA.
+AH="$WORK/ah"
+mkdir -p "$AH/data" "$AH/shim"
+make_fixture "$AH/data/main.sqlite" "hotel2-main"
+cat >"$AH/shim/pg_dump" <<'SHIM'
+#!/bin/sh
+certs=none
+[ -f "${PGSSLROOTCERT:-}" ] && certs="$(grep -c 'BEGIN CERTIFICATE' "$PGSSLROOTCERT")"
+echo "mode=${PGSSLMODE:-} root=${PGSSLROOTCERT:-} certs=$certs" >"$AH_LOG"
+echo '-- dump'
+SHIM
+chmod +x "$AH/shim/pg_dump"
+# backup_ah <.env.generated content> [env assignments...]: the TLS settings pg_dump ran with. The
+# backup's output is kept in out.log.
+backup_ah() {
+ printf '%b' "$1" >"$AH/data/.env.generated"
+ shift
+ rm -rf "$AH/out"
+ if ! OUT_AH="$(cd "$AH" && env "$@" AH_LOG="$AH/log" PATH="$AH/shim:$PATH" BACKUP_DIR="$AH/out" \
+ "$BACKUP" 2>&1)"; then
+ fail "(ah) the backup failed: $OUT_AH"
+ fi
+ printf '%s\n' "$OUT_AH" >"$AH/out.log"
+ cat "$AH/log"
+}
+PG_AH='DATABASE_TYPE=postgres\n'
+if [ "$(backup_ah "$PG_AH")" != "mode= root= certs=none" ]; then
+ fail "(ah) pg_dump got TLS settings without DATABASE_SSL: $(cat "$AH/log")"
+fi
+GOT_AH="$(backup_ah "${PG_AH}DATABASE_SSL=true\n")"
+if ! [[ "$GOT_AH" =~ ^mode=verify-full\ root=(/[^ ]+)\ certs=([0-9]+)$ ]]; then
+ fail "(ah) DATABASE_SSL=true did not verify the server against a root certificate file: $GOT_AH"
+fi
+# Node's CA set runs to well over a hundred roots; the file holding it is removed with the staging copy.
+if [ "${BASH_REMATCH[2]}" -lt 100 ] || [ -e "${BASH_REMATCH[1]}" ]; then
+ fail "(ah) the root certificates were not Node's CA set, or were left behind: $GOT_AH"
+fi
+if [ "$(backup_ah "${PG_AH}DATABASE_SSL=true\nDATABASE_SSL_REJECT_UNAUTHORIZED=false\n" \
+ 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
+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
+ fail "(ah) an operator's PGSSLROOTCERT or PGSSLMODE was not honoured: $(cat "$AH/log")"
+fi
+# Without node there is no CA set to write: libpq's system store is used, and the log says what it needs.
+mkdir -p "$AH/nonode"
+populate_shim "$AH/nonode"
+ln -sf "$(command -v tail)" "$AH/nonode/tail"
+ln -sf "$AH/shim/pg_dump" "$AH/nonode/pg_dump"
+if [ "$(PATH="$AH/nonode" backup_ah "${PG_AH}DATABASE_SSL=true\n")" != "mode=verify-full root=system certs=none" ] ||
+ ! grep -q 'node not found.*sslrootcert=system.*libpq 16+' "$AH/out.log"; then
+ fail "(ah) without node the system CA store was not used, or the log gave no hint: $(cat "$AH/out.log")"
+fi
+pass "(ah) pg_dump verifies the server under DATABASE_SSL=true and is unchanged without it"
+
echo ""
echo "All smoke tests passed!"