Files
supabase/apps/studio/lib/scram-sha-256.ts
Drew McLellan 5ff6930c2a feat(studio): send role passwords as precomputed SCRAM-SHA-256 verifiers (#52544)
## What kind of change does this PR introduce?

Feature.

## What is the current behavior?

Role passwords are sent as plaintext in the SQL (`create role ...
password '...'` and `alter role ... password '...'`), so they can end up
in Postgres logs.

## What is the new behavior?

ASCII role passwords are hashed in the browser with Web Crypto and sent
as a precomputed `SCRAM-SHA-256$...` verifier. Postgres stores a
verifier as given, so the plaintext no longer appears in the SQL or the
server logs. This applies to every flow that sets a role password,
because they all go through the role create and update mutations.

Non-ASCII passwords are still sent as plaintext. Postgres normalizes
passwords with SASLprep before hashing, and the browser code doesn't
apply it, so a verifier built from the raw text could stop the role from
logging in. For ASCII, SASLprep changes nothing, so the verifier always
matches. The `saslprep` package can't be used here because it reads its
table with `fs.readFileSync` at import time. Covering non-ASCII
passwords is a follow-up, most likely a server-side API.

Studio also falls back to plaintext when `crypto.subtle` isn't
available, which browsers only expose in secure contexts. This keeps
setting a password working on self-hosted Studio served over plain HTTP.

## Known limitations

- The verifier is built with 4096 iterations, the Postgres default. If a
project sets `scram_iterations` higher, these roles keep 4096, because
Postgres stores a supplied verifier as given. Reading the setting first
needs an extra query and a grant for it, so it's left for the follow-up
below.
- Non-ASCII passwords, and Studio over plain HTTP, still send plaintext.
- The follow-up is a server-side path that sets the password without it
going through the SQL, which would honor the server's own settings and
cover these cases.

A verifier isn't secret (it can be attacked offline), but it is a clear
improvement over plaintext.

## Testing

- Unit tests compare the helper with an independent `node:crypto`
implementation, and check that ASCII passwords become verifiers and
non-ASCII passwords pass through unchanged.
- I also checked against a real Postgres: roles given these verifiers
log in with the plaintext password and reject a wrong one.
- The `CreateRolePanel` test now expects a verifier and no plaintext in
the SQL.

Resolves FE-4822.
2026-11-02 15:42:59 +00:00

70 lines
2.5 KiB
TypeScript

const ITERATIONS = 4096
const SALT_BYTES = 16
const encoder = new TextEncoder()
const toBase64 = (bytes: Uint8Array) => btoa(String.fromCharCode(...bytes))
const hmacSha256 = async (key: Uint8Array<ArrayBuffer>, message: string) => {
const cryptoKey = await crypto.subtle.importKey(
'raw',
key,
{ name: 'HMAC', hash: 'SHA-256' },
false,
['sign']
)
return new Uint8Array(await crypto.subtle.sign('HMAC', cryptoKey, encoder.encode(message)))
}
/**
* Builds the `SCRAM-SHA-256$...` secret Postgres stores for a password (see
* `scram_build_secret` in the Postgres source), so that the password itself never needs to be
* sent to the server.
*
* This does not apply SASLprep, so only pass ASCII passwords: Postgres normalizes other
* passwords before hashing them, and a verifier built from the raw text wouldn't match what
* clients send at login. Use `getRolePasswordForSql` for arbitrary input.
*/
export const buildScramSha256Verifier = async (
password: string,
salt: Uint8Array<ArrayBuffer> = crypto.getRandomValues(new Uint8Array(SALT_BYTES)),
iterations = ITERATIONS
) => {
const passwordKey = await crypto.subtle.importKey(
'raw',
encoder.encode(password),
'PBKDF2',
false,
['deriveBits']
)
const saltedPassword = new Uint8Array(
await crypto.subtle.deriveBits(
{ name: 'PBKDF2', hash: 'SHA-256', salt, iterations },
passwordKey,
256
)
)
const clientKey = await hmacSha256(saltedPassword, 'Client Key')
const storedKey = new Uint8Array(await crypto.subtle.digest('SHA-256', clientKey))
const serverKey = await hmacSha256(saltedPassword, 'Server Key')
return `SCRAM-SHA-256$${iterations}:${toBase64(salt)}$${toBase64(storedKey)}:${toBase64(serverKey)}`
}
/**
* Returns the value to put in a role's `password` clause. ASCII passwords are sent as a
* precomputed SCRAM-SHA-256 verifier so the plaintext stays out of the SQL, and with it the
* server logs. Anything else is returned as is for Postgres to normalize and hash, because
* SASLprep is not applied here.
*
* Browsers only expose `crypto.subtle` in secure contexts, so Studio served over plain HTTP
* (for example self-hosted at `http://<ip>:8000`) also falls back to the plaintext password
* rather than failing to set it.
*/
export const getRolePasswordForSql = async (password: string) => {
const isAscii = /^\p{ASCII}*$/u.test(password)
const canHash = globalThis.crypto?.subtle !== undefined
return isAscii && canHash ? buildScramSha256Verifier(password) : password
}