skip to content

With no `authMechanism` set, how does a MongoDB driver choose between SCRAM-SHA-256 and SCRAM-SHA-1, and what must the stored user hold for each?

level: middleimportance: should knowfreq 37%

answer

  1. asked during the first hello
  2. saslSupportedMechs names one user
  3. one credential per mechanism
  4. who digests the password

basics

~20 s

A MongoDB driver asks in its hello with saslSupportedMechs, then uses SCRAM-SHA-256 if the reply lists it and SCRAM-SHA-1 otherwise. Each mechanism needs its own stored credential, and SCRAM-SHA-256 requires the server to digest the password.

solid answer

~40 s

During the connection handshake the driver sends `hello` with `saslSupportedMechs: "<db>.<user>"`. The server replies with the mechanisms that user can use. By the driver authentication specification, the driver must use SCRAM-SHA-256 if it is listed and SCRAM-SHA-1 otherwise, and it never picks anything else by default. An explicit `authMechanism` is always honoured. Each mechanism needs its own credential under the user: salted keys, not the password. `db.createUser` builds both by default, as long as `authenticationMechanisms` enables them. SCRAM-SHA-256 requires `passwordDigestor: "server"`, the default, so the cleartext password reaches the server during `createUser` and needs TLS. A SCRAM-SHA-1 credential is computed from an MD5-based digest, which `passwordDigestor: "client"` allows the client to compute. Moving a SHA-1-only user to SHA-256 needs the password again, because the stored keys cannot be converted.

code

javascript · 8 lines
javascript
db.getSiblingDB("admin").runCommand({
  usersInfo: { forAllDBs: true },
  filter: { mechanisms: "SCRAM-SHA-1" }
})

db.getSiblingDB("orders").updateUser("svc", {
  mechanisms: [ "SCRAM-SHA-256" ]
})

go deeper

for a junior

Know that SCRAM is the default password mechanism and that it comes in SHA-256 and SHA-1 forms, with SHA-256 preferred when the user has a credential for it.

for a middle

Explain the hello negotiation and the selection rule, that each mechanism needs its own stored credential, and why SCRAM-SHA-256 forces the server to digest the password.

for a senior

Read a mechanism error correctly, since MechanismUnavailable means the user lacks that credential while a generic failure hides its cause, and plan a SHA-1 removal around which users need a new password.

for a principal

Weigh retiring SCRAM-SHA-1 fleet-wide: the driver versions it needs, the passwords that must be reset, and the FIPS rule that may force the decision on regulated deployments.

## The two mechanisms **SCRAM** (Salted Challenge Response Authentication Mechanism) is MongoDB's default way of checking a password. MongoDB supports two variants that differ in their hash function: - **SCRAM-SHA-256** uses SHA-256. - **SCRAM-SHA-1** uses SHA-1. The server's `authenticationMechanisms` parameter lists what it accepts. Its default at 9.0 is `MONGODB-X509,SCRAM-SHA-1,SCRAM-SHA-256`, set at startup only. ## How a driver negotiates If the application supplies a username but no mechanism, the driver authentication specification requires negotiation. A driver that allows one credential per client does this during the MongoDB handshake, so it costs no extra round trip: 1. The driver sends `hello` with `saslSupportedMechs: "<db>.<username>"`, using the database the login will go to. 2. The server looks the user up and returns the mechanisms that are both enabled and backed by a credential for that user. 3. The driver applies a fixed rule: if `SCRAM-SHA-256` is listed, use it; otherwise use `SCRAM-SHA-1`, even if SCRAM-SHA-1 is not in the list. It must not pick any other mechanism, such as `PLAIN`, as the default. Two details are easy to get wrong: - **There is no retry.** The manual describes the default as trying SCRAM-SHA-256 and falling back to SCRAM-SHA-1 if that fails. The specification and PyMongo show something narrower: the driver selects once from the `hello` reply. It does not attempt SHA-256, watch it fail, and try SHA-1 with the same password. - **An unknown user still gets a list.** In the 9.0.4 source, a request for a user that does not exist returns the server's enabled mechanisms, and logs that it happened. So the driver usually selects SCRAM-SHA-256 and then fails with the generic login error, which means a mechanism in a failed-login log line does not prove that the user holds that credential. An explicit `authMechanism` overrides all of this, and a driver must honour it. If it names a mechanism the user has no credential for, the server returns a specific error, `MechanismUnavailable`, saying it is unable to use that mechanism for a user without those credentials. ## What the stored user holds A user's document has a `credentials` field with up to one entry per mechanism: an iteration count, a salt, a stored key and a server key. The password itself is not stored. So: | | SCRAM-SHA-1 | SCRAM-SHA-256 | |---|---|---| | Password input | MD5 digest of `<user>:mongo:<password>` | The password, prepared with SASLprep | | `passwordDigestor` allowed | `server` (default) or `client` | `server` only | | Default iterations | `scramIterationCount`, 10000 | `scramSHA256IterationCount`, 15000 | | Minimum the server accepts | 5000 | 5000 | If you leave `mechanisms` out of `db.createUser`, the server builds a credential for each SCRAM mechanism that `authenticationMechanisms` enables, which with the defaults means both. Asking for `SCRAM-SHA-256` with `passwordDigestor: "client"` is rejected with `Use of SCRAM-SHA-256 requires undigested passwords`. Because the server digests the password by default, `createUser` sends it in the clear unless the connection uses TLS, even with `passwordPrompt()`. The manual warns about this explicitly. The MD5 in SCRAM-SHA-1 is, in the manual's words, necessary but not used for cryptographic purposes. ## Moving a deployment off SCRAM-SHA-1 - **Find the users.** `usersInfo` with `{ forAllDBs: true }` and `filter: { mechanisms: "SCRAM-SHA-1" }` lists them. - **Users with both credentials.** `db.updateUser("svc", { mechanisms: ["SCRAM-SHA-256"] })` trims the SHA-1 credential. Without a password, the list must be a subset of what the user already has. - **Users with SHA-1 only.** These need a password in the same update, because a SHA-256 key cannot be derived from the stored SHA-1 keys. - **Then disable it.** Remove `SCRAM-SHA-1` from `authenticationMechanisms` and restart in a rolling fashion. FIPS deployments have a harder rule. Starting in 8.3, specifying `SCRAM-SHA-1` together with `--tlsFIPSMode` is an error, and it is left out of the effective default in that mode. A team still on 8.0 does not have that rule. ## Iteration counts Raising an iteration count makes guessing passwords slower and logins slower too. It only affects passwords set afterwards, never existing ones. Drivers must refuse a SCRAM-SHA-256 conversation offering fewer than 4096 iterations, which guards against a downgrade by a man in the middle, and they must cache computed credentials so that opening many connections does not redo the expensive step.

  • What does a MongoDB login do when the connection string says `authMechanism=SCRAM-SHA-256` but the user only has a SCRAM-SHA-1 credential?
    The driver honours the explicit choice and skips the selection rule. The server cannot run that mechanism for the user and returns `MechanismUnavailable`, saying it cannot use SCRAM-SHA-256 for a user without those credentials. Unlike a wrong password, this is a specific message that the client can see.
  • Why can't MongoDB upgrade a user from SCRAM-SHA-1 to SCRAM-SHA-256 without the password?
    The server stores only derived keys for each mechanism, not the password. A SHA-256 key has to be computed from the password itself, so `db.updateUser` needs `pwd` when `mechanisms` adds a credential. Without a password, the update can only keep a subset of the credentials the user already holds.

  • The driver tries SCRAM-SHA-256 first and retries with SCRAM-SHA-1 if the login fails.
  • Passing passwordDigestor client is safer because the password never leaves the application.
  • Raising scramSHA256IterationCount strengthens every existing password straight away.
  • A user holding only SCRAM-SHA-1 can gain SCRAM-SHA-256 through updateUser without supplying a password.
  • MongoDB stores the user's password encrypted so it can check it at login.

Assumes MongoDB 9.0; the unknown-user reply was read at r9.0.4, and SCRAM-SHA-1 being refused in FIPS mode starts at 8.3, so teams on 8.0 do not have that rule.