Skip to content

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

Closed

Description

@okomarov

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 ClientKey and ServerKey rather 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.pass on 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 pass function.

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.

Activity

  1. added
    enhancement
    area: connections
    area: performance
    on Oct 20, 2026
  2. moved this to In progress in Roadmapon Oct 20, 2026
  3. moved this from In progress to Next in Roadmapon Oct 20, 2026
  4. moved this from Next to In progress in Roadmapon Oct 20, 2026
  5. added 3 commits that reference this issue on Oct 20, 2026
    d3f4dac
    77dd5f3
    693e248
  6. okomarov commented on Oct 20, 2026

    @okomarov
    Author

    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.pass is 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 counting pbkdf2Sync calls 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):

    • Local workerd profiler, 200 rounds, SCRAM against PostgreSQL 18 (run 39428238915): median 2.45 → 1.77 ms, p95 2.99 → 2.18, p99 3.40 → 2.45 (p = 9.8e-59) with fetch_types off; 3.40 → 2.72 with it on (p = 1.6e-44). deriveBits leaves the top functions.
    • Node.js 24, same run: median 1.61 → 0.94 ms (p = 2.5e-45) with fetch_types off, 2.04 → 1.38 on.
    • Edge, A/B against main, 150 rounds, Supabase pooler (run 39437587531): median 4 → 3 ms, p95 6 → 5, p99 7 → 6; costs less, p = 6.6e-19. The saving is about a quarter of the connection's CPU, so its size in ms follows the machine: same-code medians ranged 4-10 ms across runs that day.

    Hyperdrive is not measured: whether the Worker runs SCRAM against Hyperdrive is unverified.

  7. added this to the 4.6.0 milestone on Oct 20, 2026
  8. moved this from In progress to Shipped in Roadmapon Oct 21, 2026
  9. okomarov commented on Oct 21, 2026

    @okomarov
    Author

    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.

  10. changed the title [-]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[/+] on Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: connectionsarea: performanceenhancement

    Type

    No type

    Projects

    • Status
      Shipped

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions