Every connection authenticating with SCRAM-SHA-256 ran PBKDF2 over the
password again, then the HMACs for ClientKey, StoredKey and ServerKey.
On Workers that is WebCrypto deriveBits, 17-25% of samples on connect,
select 1, end under the local workerd profiler, and a Worker pays it on
every request because its connection cannot outlive the request. On
Node.js it is pbkdf2Sync, which blocks the event loop per connection.
RFC 5802 section 5.1 allows a client to cache ClientKey and ServerKey for
later authentication. A module-level cache, shared by every client in the
isolate or process, now holds ClientKey, StoredKey and ServerKey for five
minutes from derivation, at most 16 entries, least recently used evicted.
Expired entries are swept on each SCRAM login; there is no timer, so an
expired entry with no later login stays in memory unused.
Entries are keyed by a SHA-256 of the salt, iteration count and password.
The key never leaves the map, so it can only leak in a heap dump, which
also holds the derived keys (password-equivalent for servers with that
salt, now living up to five minutes past sql.end()) and usually the
plaintext password; an HMAC under a per-process secret would add nothing
there, since the dump holds the secret too.
The password is still read through options.pass on every connection, so
a rotating pass function keeps working, and a changed password, salt or
count derives again. Both nonces, the proof over the auth message and the
check of the server signature stay per connection. Concurrent connections
share one pending derivation; a failed one is dropped from the cache.
The new test counts pbkdf2Sync calls across clients: one derivation for
two clients, a rotating pass function asked each time, a wrong password
deriving again and rejected with 28P01, a password reset (new salt)
deriving again, and a login past the TTL deriving again.
Closes #168
A module-level cache shared by every client in the isolate or process holds ClientKey, StoredKey and ServerKey for five minutes from derivation (16 entries, least recently used evicted), as RFC 5802 section 5.1 allows. Entries are keyed by a SHA-256 of salt, iteration count and password; the key never leaves the map. CodeQL flags that hash as a password hash with insufficient effort; it is a lookup key, not stored or sent, and an HMAC under a per-process secret did not clear the alert and adds nothing against a heap dump, which holds the secret too. Expired entries are swept on each SCRAM login; there is no timer. options.pass is still read on every connection; both nonces, the client proof and the server-signature check stay per connection. PBKDF2 is deterministic, so an entry is exact for its key: a changed password or salt is a different key and derives again.
Exposure: the derived keys are password-equivalent for servers holding that salt and now live up to five minutes past sql.end(). A heap dump holds the keys and usually the plaintext password.
The new test counts pbkdf2Sync calls across clients: one derivation for two clients, a rotating pass function asked each time, a wrong password deriving again and rejected with 28P01, a password reset (new salt) deriving again, and a login past the TTL deriving again.
Closes #168
Measurements (CPU per request in ms, connect,
select 1, end; release → this branch)Local workerd profile, 200 rounds, SCRAM against PostgreSQL 18 (https://github.com/steadybrew/postgresjs/actions/runs/39428238915):
deriveBitsleaves the top functions.Node.js 24, same run:
Edge
cpujob, 150 rounds, at d3f4dac (https://github.com/steadybrew/postgresjs/actions/runs/39429524141),cpuTimeMs:Gate: costs no more than the release (p = 1.0 and 0.99). Open: the edge p99 of 29 ms in two groups also appeared on PR #169's run; at about 125 requests p99 is the top one or two, and the job does not print per-request values to tell whether they are first requests in a new isolate.
Edge A/B against main, 150 rounds, Supabase pooler, fetch_types false (https://github.com/steadybrew/postgresjs/actions/runs/39437587531), one warm isolate for every request: median 4 → 3, p95 6 → 5, p99 7 → 6, max 7 → 8; costs less, p = 6.6e-19. An A/A run of main against itself in the same job (https://github.com/steadybrew/postgresjs/actions/runs/39437577733) gave medians 7 and 8, p = 0.10, with a 26 ms request on one side, so the 29 ms p99 above is platform noise on a warm isolate, not this change.