Repository navigation
Send role passwords to the database as SCRAM-SHA-256 verifiers - #311
Merged
Merged
Conversation
…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.
Askir
approved these changes
Nov 20, 2026
| // 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) { |
Contributor
There was a problem hiding this comment.
bit surprised that there is no go lib to help with this, but fair enough.
Member
Author
There was a problem hiding this comment.
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 🤷♂️.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
tiger db create rolesent 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 theCREATE ROLEstatement, and the--from tsdbadminpath passed it as a parameter totimescale_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
\passworddoes, and sends only that. Postgres stores a verifier as-is, andcreate_bare_readonly_rolepasses it straight toCREATE 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.