Repository navigation
Description
Every connection authenticating with SCRAM-SHA-256 derives SaltedPassword = PBKDF2(password, salt, iterations) again, even when the password, salt and iteration count are the same as the last connection's.
SASLContinue in src/connection.js runs the derivation on every connection, then computes ClientKey, StoredKey and the server signature. On Cloudflare Workers that is WebCrypto importKey + deriveBits (cf/polyfills.js), 4096 iterations on Supabase; on Node.js it is crypto.pbkdf2Sync, which blocks the event loop for each new connection. A Worker cannot keep a connection past its request, so it pays this on every request: deriveBits is 17-25% of samples on connect, select 1, end under the local workerd profiler, the largest single item.
Allowed by the spec
RFC 5802 section 5.1, under the i attribute: "a client implementation MAY cache ClientKey&ServerKey (or just SaltedPassword) for later reauthentication ... as it is likely that the server is going to advertise the same salt value upon reauthentication."
Proposal
- One cache per isolate or process, shared by every client in it, holding
ClientKeyandServerKeyrather than the password. - Keyed by a digest of password, salt and iteration count, so a changed password, salt or count derives again. Bounded size.
- The password is read through
options.passon every connection, so a function returning rotating credentials keeps working. - Unchanged per connection: both nonces, the client proof over the auth message, and verification of the server signature. A stale entry cannot log in, because the server rejects a wrong proof.
- SCRAM-SHA-256-PLUS (Support SCRAM-SHA-256-PLUS channel binding #115) is unaffected.
The cached keys are password-equivalent for servers holding that salt. The password is already in memory for the client's life; what changes is that the keys outlive sql.end() until the isolate or process ends.
Applies to src/ as well, so Node.js and Deno connections gain too. Whether a Worker runs SCRAM against Hyperdrive is unverified and is checked before anything is claimed for it.
Tests
- A second connection with the same credentials does not derive again.
- A changed password or salt derives again.
- A wrong cached key cannot authenticate.
- A rotating
passfunction.
Measure
Edge cpu job at 150 rounds against npm latest, judged by median and p95 with the rank test, plus a Node.js connect benchmark.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsShipped
Activity
perf: derive SCRAM keys once per password, salt and iteration count
perf: derive SCRAM keys once per password, salt and iteration count
perf: derive SCRAM keys once per password, salt and iteration count
okomarov commentedon Oct 20, 2026
Landed on main in 693e248 (PR #170); ships in the next release.
Every client in a process or Worker isolate now reuses the ClientKey, StoredKey and ServerKey that PBKDF2 derives, for five minutes from derivation, at most 16 entries, keyed by a SHA-256 of salt, iteration count and password. Expired entries are swept on the next SCRAM login.
options.passis still read on every connection; both nonces, the client proof and the check of the server signature stay per connection. A wrong password, a new salt after a password change, and a login past the TTL all derive again (tested by countingpbkdf2Synccalls against the CI cluster's SCRAM user). An HMAC under a per-process secret was tried for the key and dropped: CodeQL flagged it the same way, and it adds nothing against a heap dump. CodeQL alert 23 is dismissed as a false positive; the reasoning is on PR #170.Measured CPU per request (connect,
select 1, end):deriveBitsleaves the top functions.Hyperdrive is not measured: whether the Worker runs SCRAM against Hyperdrive is unverified.
chore: release 4.6.0-rc1
chore: release 4.6.0
okomarov commentedon Oct 21, 2026
Shipped in 4.6.0 (
npm install @steadybrew/[email protected]), unchanged from 4.6.0-rc1. The tests that cover it passed in the tag run.[-]SCRAM authentication derives the salted password again on every connection[/-][+][FIXED in 4.6.0] SCRAM authentication derives the salted password again on every connection[/+]