mongodb/node-mongodb-native · error · MongoRuntimeError

Server nonce does not begin with client nonce

Error message

Server nonce does not begin with client nonce

What it means

Thrown during the MONGODB-AWS SASL conversation when the server's returned nonce (the 's' field) does not begin with the 32-byte client nonce the driver sent in saslStart (src/cmap/auth/mongodb_aws.ts:85). Per the MongoDB auth spec, the leading 32 bytes of the server nonce must equal the client nonce as a replay/tampering guard. Surfaced as a MongoRuntimeError.

Source

Thrown at src/cmap/auth/mongodb_aws.ts:90

    const saslStartResponse = await connection.command(ns(`${db}.$cmd`), saslStart, undefined);

    const serverResponse = BSON.deserialize(saslStartResponse.payload.buffer, bsonOptions) as {
      s: Binary;
      h: string;
    };
    const host = serverResponse.h;
    const serverNonce = serverResponse.s.buffer;
    if (serverNonce.length !== 64) {
      // TODO(NODE-3483)
      throw new MongoRuntimeError(`Invalid server nonce length ${serverNonce.length}, expected 64`);
    }

    if (!ByteUtils.equals(serverNonce.subarray(0, nonce.byteLength), nonce)) {
      // throw because the serverNonce's leading 32 bytes must equal the client nonce's 32 bytes
      // https://github.com/mongodb/specifications/blob/master/source/auth/auth.md#conversation-5

      // TODO(NODE-3483)
      throw new MongoRuntimeError('Server nonce does not begin with client nonce');
    }

    if (host.length < 1 || host.length > 255 || host.indexOf('..') !== -1) {
      // TODO(NODE-3483)
      throw new MongoRuntimeError(`Server returned an invalid host: "${host}"`);
    }

    const body = 'Action=GetCallerIdentity&Version=2011-06-15';
    const headers = await aws4Sign(
      {
        method: 'POST',
        host,
        region: deriveRegion(serverResponse.h),
        service: 'sts',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'Content-Length': body.length,
          'X-MongoDB-Server-Nonce': ByteUtils.toBase64(serverNonce),

View on GitHub (pinned to 3366c21a63)

Solutions

  1. Verify the target host is a genuine MongoDB server supporting MONGODB-AWS (MongoDB 4.4+ Enterprise).
  2. Remove any intermediary (proxy, custom LB, debug MITM) that could alter the saslStart response payload.
  3. Retry against a known-good endpoint; if it persists, capture the server version and report it as a server bug.
  4. Confirm the connection is not being routed to a mock/test server returning canned responses.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await client.connect();
} catch (e) {
  if (e instanceof MongoRuntimeError && /Server nonce does not begin with client nonce/.test(e.message)) {
    // Do not retry the same target - likely a non-conformant or hostile server.
    // Verify endpoint authenticity, then optionally fail over to a known-good host.
    log.error('Possible MITM or non-conformant server detected', e);
  }
  throw e;
}

Prevention

When it happens

Trigger: The server response to saslStart contains an 's' field whose first 32 bytes do not match the client-generated random nonce. This is a protocol-level integrity check; it triggers on a misbehaving server, a man-in-the-middle altering the handshake, or a server implementation that does not conform to the MONGODB-AWS conversation spec.

Common situations: Pointing the driver at a non-MongoDB or non-conformant service that answers saslStart with an arbitrary payload, a proxy/load-balancer rewriting the response, or a corrupted/intercepted connection. Extremely rare against a genuine MongoDB server.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04). Data as JSON: /data/errors/2692174a3104ac8e.json. Report an issue: GitHub.