Skip to content

perf: derive SCRAM keys once per password, salt and iteration count - #170

Merged
okomarov merged 1 commit into
mainfrom
perf/scram-key-cache
Oct 20, 2026
Merged

okomarov merged 1 commit into
mainfrom
perf/scram-key-cache

Conversation

@okomarov

@okomarov okomarov commented Oct 20, 2026 •

Copy link
Copy Markdown

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): deriveBits leaves the top functions.

Request Median p95 p99 Less than release, p
fetch_types off 2.45 → 1.77 2.99 → 2.18 3.40 → 2.45 9.8e-59
fetch_types on 3.40 → 2.72 4.35 → 3.40 7.07 → 5.71 1.6e-44

Node.js 24, same run:

Request Median p95 p99 Less than release, p
fetch_types off 1.61 → 0.94 2.48 → 3.09 5.48 → 5.30 2.5e-45
fetch_types on 2.04 → 1.38 3.81 → 3.40 12.35 → 8.72 8.0e-36

Edge cpu job, 150 rounds, at d3f4dac (https://github.com/steadybrew/postgresjs/actions/runs/39429524141), cpuTimeMs:

Target Median p95 p99
Supabase pooler, fetch_types off 8 → 7 12 → 11 15 → 29
Supabase pooler, fetch_types on 12 → 11 17 → 17 26 → 19
Neon over TLS 10 → 8 13 → 13 16 → 29

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.

@okomarov
okomarov force-pushed the perf/scram-key-cache branch 2 times, most recently from d3f4dac to 77dd5f3 Compare October 20, 2026 14:33
@okomarov

Copy link
Copy Markdown
Author

CodeQL alert 23 (js/insufficient-password-hash), dismissed as a false positive

CodeQL reports that scramKeys() in src/connection.js hashes the password with SHA-256, which is "insufficient computational effort" for a password hash. The rule assumes the hash is used to store or verify the password, where a fast hash lets anyone holding the stored value test guesses cheaply. That is not what this hash does.

What the hash is

SHA-256(salt + ',' + iterations + ',' + password) is only the key of an in-memory Map that caches the SCRAM-SHA-256 keys (ClientKey, StoredKey, ServerKey) derived by PBKDF2. It is never written to disk, logged, sent to the server, or returned from the module. PBKDF2 itself is unchanged: same algorithm, and the iteration count the server sends. On a cache hit the SCRAM exchange on the wire is byte-for-byte what it was before.

Why the password must be part of the key

PostgreSQL sends the same salt to anyone who tries to log in as a role, whatever password they try. A cache keyed by salt and iteration count alone would let a wrong password reuse the keys derived from the right one and log in. The key has to include the password. The choices are:

  1. The plaintext password as the key. Strictly worse than a hash.
  2. A slow hash (PBKDF2 with its own salt) as the key. Costs as much as the derivation the cache exists to skip.
  3. A fast hash (this change).
  4. An HMAC under a random per-process secret. Tried in an earlier revision of this PR. CodeQL flagged it the same way (createHmac over the password). Against the one way the key can leak, a heap dump, it adds nothing, because the dump holds the secret too. Dropped for SHA-256 as simpler with the same risk.

What an attacker gains from the hash

Only someone who can read the process's memory (a heap dump, a core file, a memory-disclosure bug) ever sees this key. Such a reader also gets:

  • the cached ClientKey and StoredKey, which already authenticate as that role to any server holding that salt, without the password;
  • in practice, the plaintext password itself, held in options.pass, process.env or a Workers env binding for the life of the client, process or isolate.

The fast hash matters only to an attacker who has memory access but not the plaintext, and who wants the password itself to reuse elsewhere (other services, other servers with a different salt). For that attacker, guessing against SHA-256 costs one hash per guess instead of a full PBKDF2 (4096 HMAC rounds on Supabase and Neon), about 4096× cheaper. In practice:

  • a dictionary or leaked password falls in seconds either way;
  • a short random password (8 lowercase letters and digits) takes minutes against SHA-256 instead of about two weeks against PBKDF2-4096 on one GPU;
  • a generated password of 16–20+ random characters, the default for Supabase, Neon and RDS, is out of reach either way.

What this change does add

The derived keys, password-equivalent for servers holding that salt, now outlive sql.end(). They stay at most five minutes from derivation, in at most 16 entries, least recently used evicted. Expired entries are swept on the next SCRAM login; with no later login an expired entry stays in memory, unused, until the process or isolate ends. Before this change the same keys lived only for the length of one authentication.

When to revisit

Reopen this if the cache key or the cached keys are ever logged, persisted, exported from the module, shared across processes, or exposed through diagnostics or tracing. Then the hash would be a stored value and the rule would apply.

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
@okomarov
okomarov force-pushed the perf/scram-key-cache branch from 77dd5f3 to 693e248 Compare October 20, 2026 14:38
@okomarov
okomarov merged commit 693e248 into main Oct 20, 2026
20 checks passed
@okomarov
okomarov deleted the perf/scram-key-cache branch October 20, 2026 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FIXED in 4.6.0] SCRAM authentication derives the salted password again on every connection

1 participant