Skip to content

Commit 8f4b376

Browse files
fix(nix,postgres): set role password safely for any value
The password was interpolated into the SQL string as '$DATABASE_PASS', which breaks (and risks SQL injection) for passwords containing single quotes. Read it from the environment with psql's \getenv and quote it with :'password', producing a correctly escaped SQL string literal. The password is no longer placed on the command line either. Requires PostgreSQL >= 14 for \getenv.
1 parent d3ab47a commit 8f4b376

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- fix(webview): show charging finish time in browser local time (#5436 - @Ashok28)
1010
- refactor(nix,postgres): provision database declaratively, connect via socket (#5337 - @JakobLichterfeld)
1111
- fix(nix,maintenance): read RELEASE_COOKIE without sourcing the env file (#5337 - @JakobLichterfeld)
12+
- fix(nix,postgres): set role password safely for any value (#5337 - @JakobLichterfeld)
1213

1314
#### Build, CI, internal
1415

nix/module.nix

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,15 @@ in
277277
# hook there (after the role exists) and scope the secret to that unit.
278278
systemd.services.postgresql-setup = {
279279
serviceConfig.EnvironmentFile = cfg.secretsFile;
280+
# Read the password from the environment with psql's \getenv (so it is
281+
# never placed on the command line or shell-expanded) and quote it with
282+
# :'password', which produces a correctly escaped SQL string literal.
283+
# This is safe for any password, including ones containing single
284+
# quotes. Requires PostgreSQL >= 14 for \getenv.
280285
postStart = ''
281-
psql -tAc "ALTER USER \"${cfg.postgres.user}\" WITH ENCRYPTED PASSWORD '$DATABASE_PASS'"
286+
psql -v ON_ERROR_STOP=1 -d postgres \
287+
-c '\getenv password DATABASE_PASS' \
288+
-c "ALTER USER \"${cfg.postgres.user}\" WITH ENCRYPTED PASSWORD :'password'"
282289
'';
283290
};
284291
})

0 commit comments

Comments
 (0)