Skip to content

Send role passwords to the database as SCRAM-SHA-256 verifiers - #311

Merged
nathanjcochran merged 4 commits into
mainfrom
nathan/improve-password-handling
Nov 20, 2026
Merged

nathanjcochran merged 4 commits into
mainfrom
nathan/improve-password-handling

Conversation

@nathanjcochran

Copy link
Copy Markdown
Member

tiger db create role sent the new role's password to the database in plaintext, so it could end up in the server's statement logs. The default path put the plaintext into the CREATE ROLE statement, and the --from tsdbadmin path passed it as a parameter to timescale_functions.create_bare_readonly_role, whose value is logged too.

The command now hashes the password into a SCRAM-SHA-256 verifier on the client, as psql's \password does, and sends only that. Postgres stores a verifier as-is, and create_bare_readonly_role passes it straight to CREATE ROLE, so both paths now keep the plaintext off the server. The stored password (keyring or pgpass) is unchanged. Single quotes in passwords are no longer refused.

Because the password is now hashed as typed, the command refuses passwords that SASLprep, the normalization Postgres and libpq apply before hashing, would change (ligatures, full-width letters, non-ASCII spaces, invisible characters). pgx normalizes some of those differently from libpq, so such a password would fail to log in through one client or the other. Ordinary non-ASCII passwords and all generated passwords are unaffected.

Integration tests now log in as each new role through both pgx (tiger db query) and libpq (tiger db psql), with a password containing quotes, a backslash, and non-ASCII characters, on both paths.

…ger db create role

The default path of tiger db create role pasted the plaintext password into
CREATE ROLE, so it reached the server's statement logs. Hash it into a
SCRAM-SHA-256 verifier on the client instead, which Postgres stores as-is.
This also lifts the restriction on single quotes in passwords.

Postgres and libpq normalize a password with SASLprep before hashing it, while
pgx approximates SASLprep and disagrees on some characters. Refuse passwords
that SASLprep would change, so the verifier hashed from the password as typed
works with every client.
Log in as each new role through both pgx (db query) and libpq (db psql), which
hash the password independently, including with a password containing quotes,
a backslash, and non-ASCII characters.
tiger db create role --from tsdbadmin passed the plaintext password to
timescale_functions.create_bare_readonly_role, which logged it among the
statement's parameters. The function passes the password to CREATE ROLE
unchanged, so send it the verifier instead, as the default path does.
@nathanjcochran nathanjcochran self-assigned this Nov 19, 2026
@nathanjcochran
nathanjcochran marked this pull request as ready for review November 19, 2026 22:57
@nathanjcochran
nathanjcochran requested a review from a team as a code owner November 19, 2026 22:57
// buildSCRAMSHA256Verifier builds the verifier in the form Postgres stores it,
// SCRAM-SHA-256$<iterations>:<salt>$<StoredKey>:<ServerKey> (RFC 5802, RFC
// 7677).
func buildSCRAMSHA256Verifier(password string, salt []byte, iterations int) (string, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bit surprised that there is no go lib to help with this, but fair enough.

@nathanjcochran nathanjcochran Nov 20, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is this: https://github.com/xdg-go/scram. But it doesn't seem very widely used, and I'm a little skeptical of depending on it for something as sensitive as hashing user database passwords. Implementing it ourselves is straightforward with only standard library functions, which I trust a lot more. "A little copying is better than a little dependency".

Additionally, the interface that library provides is kind of obtuse, and doesn't provide a function exactly like the one we have here, so we'd still end up with a decent amount of custom code, anyways 🤷‍♂️.

@nathanjcochran
nathanjcochran merged commit a88ba27 into main Nov 20, 2026
4 checks passed
@nathanjcochran
nathanjcochran deleted the nathan/improve-password-handling branch November 20, 2026 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants