mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
## 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.
72 lines
2.7 KiB
TypeScript
72 lines
2.7 KiB
TypeScript
import { createHash, createHmac, pbkdf2Sync } from 'node:crypto'
|
|
import { afterEach, describe, expect, test, vi } from 'vitest'
|
|
|
|
import { buildScramSha256Verifier, getRolePasswordForSql } from './scram-sha-256'
|
|
|
|
// An independent implementation of how Postgres builds a SCRAM-SHA-256 secret
|
|
const referenceVerifier = (password: string, salt: Buffer, iterations: number) => {
|
|
const saltedPassword = pbkdf2Sync(password, salt, iterations, 32, 'sha256')
|
|
const clientKey = createHmac('sha256', saltedPassword).update('Client Key').digest()
|
|
const storedKey = createHash('sha256').update(clientKey).digest()
|
|
const serverKey = createHmac('sha256', saltedPassword).update('Server Key').digest()
|
|
return `SCRAM-SHA-256$${iterations}:${salt.toString('base64')}$${storedKey.toString('base64')}:${serverKey.toString('base64')}`
|
|
}
|
|
|
|
const VERIFIER_PATTERN =
|
|
/^SCRAM-SHA-256\$4096:[A-Za-z0-9+/]{22}==\$[A-Za-z0-9+/]{43}=:[A-Za-z0-9+/]{43}=$/
|
|
|
|
describe('buildScramSha256Verifier', () => {
|
|
test.each(['pencil', `it's a "secret"`, '100%s%I%%', 'a'.repeat(200)])(
|
|
'matches the reference implementation for %j',
|
|
async (password) => {
|
|
const salt = Buffer.from('W22ZaJ0SNY7soEsUEjb6gQ==', 'base64')
|
|
|
|
expect(await buildScramSha256Verifier(password, new Uint8Array(salt), 4096)).toBe(
|
|
referenceVerifier(password, salt, 4096)
|
|
)
|
|
}
|
|
)
|
|
|
|
test('uses a new random salt each time', async () => {
|
|
const [first, second] = await Promise.all([
|
|
buildScramSha256Verifier('pencil'),
|
|
buildScramSha256Verifier('pencil'),
|
|
])
|
|
|
|
expect(first).toMatch(VERIFIER_PATTERN)
|
|
expect(second).toMatch(VERIFIER_PATTERN)
|
|
expect(first).not.toBe(second)
|
|
})
|
|
})
|
|
|
|
describe('getRolePasswordForSql', () => {
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals()
|
|
})
|
|
|
|
test.each(['pencil', `it's a "secret"`, 'with space & symbols !@#$%^*()'])(
|
|
'sends a verifier instead of the ASCII password %j',
|
|
async (password) => {
|
|
const result = await getRolePasswordForSql(password)
|
|
|
|
expect(result).toMatch(VERIFIER_PATTERN)
|
|
expect(result).not.toContain(password)
|
|
}
|
|
)
|
|
|
|
// Postgres applies SASLprep to these before hashing them, which this doesn't, so a
|
|
// verifier built here could stop the role from logging in
|
|
test.each(['pässword', '密码', 'emoji 🔑'])(
|
|
'leaves the non-ASCII password %j as is',
|
|
async (password) => {
|
|
expect(await getRolePasswordForSql(password)).toBe(password)
|
|
}
|
|
)
|
|
// Browsers don't expose crypto.subtle outside secure contexts, such as self-hosted Studio over HTTP
|
|
test('leaves an ASCII password as is when crypto.subtle is unavailable', async () => {
|
|
vi.stubGlobal('crypto', { getRandomValues: crypto.getRandomValues.bind(crypto) })
|
|
|
|
expect(await getRolePasswordForSql('pencil')).toBe('pencil')
|
|
})
|
|
})
|