Skip to content

Commit f249704

Browse files
committed
fix: escape postgres password in shell, not via psql -v binding
The previous wrapper passed the password through `psql -v pass=...` and `:'pass'` substitution. psql 18's -c parser doesn't expand the variable in that position — postgres logged `syntax error at or near ":"` on every start and the ALTER never ran. Switch to a straightforward shell-side single-quote-doubling escape and interpolate directly into the SQL. Same security bound (the only character that can escape a standard-conforming SQL string literal is `'`, doubled to `''`), and it actually works.
1 parent d0ad9e5 commit f249704

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

services/postgis/scripts/sync-password.sh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,17 @@ until pg_isready -U "$PG_USER" -d postgres -h /var/run/postgresql -q 2>/dev/null
4242
sleep 1
4343
done
4444

45-
# Resync the superuser password from env. Pass it via psql's `-v`/`:'…'`
46-
# binding so libpq quotes it — embedded quotes / backslashes can't break out
47-
# of the SQL string. Suppressed `log_statement` for this one connection so
48-
# the password doesn't land in postgres logs.
45+
# Resync the superuser password from env. Embedded single quotes (the only
46+
# character that can break out of a SQL string literal in standard-conforming
47+
# mode) are doubled before interpolation. `log_statement=none` keeps the
48+
# ALTER off the postgres logs so the password doesn't land there in
49+
# plaintext.
4950
if [ -n "${POSTGRES_PASSWORD:-}" ]; then
51+
ESCAPED_PASSWORD=${POSTGRES_PASSWORD//\'/\'\'}
5052
PGOPTIONS="-c log_statement=none" \
5153
psql -U "$PG_USER" -d postgres -h /var/run/postgresql \
5254
-v ON_ERROR_STOP=1 --no-psqlrc -q \
53-
-v "pass=${POSTGRES_PASSWORD}" \
54-
-c "ALTER USER \"${PG_USER}\" WITH PASSWORD :'pass';" >/dev/null
55+
-c "ALTER USER \"${PG_USER}\" WITH PASSWORD '${ESCAPED_PASSWORD}';" >/dev/null
5556
echo "[openmapx-postgis] superuser password synced from env"
5657
fi
5758

0 commit comments

Comments
 (0)