{"id":"2692174a3104ac8e","repo":"mongodb/node-mongodb-native","slug":"server-nonce-does-not-begin-with-client-nonce","errorCode":null,"errorMessage":"Server nonce does not begin with client nonce","messagePattern":"Server nonce does not begin with client nonce","errorType":"exception","errorClass":"MongoRuntimeError","httpStatus":null,"severity":"error","filePath":"src/cmap/auth/mongodb_aws.ts","lineNumber":90,"sourceCode":"    const saslStartResponse = await connection.command(ns(`${db}.$cmd`), saslStart, undefined);\n\n    const serverResponse = BSON.deserialize(saslStartResponse.payload.buffer, bsonOptions) as {\n      s: Binary;\n      h: string;\n    };\n    const host = serverResponse.h;\n    const serverNonce = serverResponse.s.buffer;\n    if (serverNonce.length !== 64) {\n      // TODO(NODE-3483)\n      throw new MongoRuntimeError(`Invalid server nonce length ${serverNonce.length}, expected 64`);\n    }\n\n    if (!ByteUtils.equals(serverNonce.subarray(0, nonce.byteLength), nonce)) {\n      // throw because the serverNonce's leading 32 bytes must equal the client nonce's 32 bytes\n      // https://github.com/mongodb/specifications/blob/master/source/auth/auth.md#conversation-5\n\n      // TODO(NODE-3483)\n      throw new MongoRuntimeError('Server nonce does not begin with client nonce');\n    }\n\n    if (host.length < 1 || host.length > 255 || host.indexOf('..') !== -1) {\n      // TODO(NODE-3483)\n      throw new MongoRuntimeError(`Server returned an invalid host: \"${host}\"`);\n    }\n\n    const body = 'Action=GetCallerIdentity&Version=2011-06-15';\n    const headers = await aws4Sign(\n      {\n        method: 'POST',\n        host,\n        region: deriveRegion(serverResponse.h),\n        service: 'sts',\n        headers: {\n          'Content-Type': 'application/x-www-form-urlencoded',\n          'Content-Length': body.length,\n          'X-MongoDB-Server-Nonce': ByteUtils.toBase64(serverNonce),","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cmap/auth/mongodb_aws.ts#L72-L108","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the target host is a genuine MongoDB server supporting MONGODB-AWS (MongoDB 4.4+ Enterprise).","Remove any intermediary (proxy, custom LB, debug MITM) that could alter the saslStart response payload.","Retry against a known-good endpoint; if it persists, capture the server version and report it as a server bug.","Confirm the connection is not being routed to a mock/test server returning canned responses."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await client.connect();\n} catch (e) {\n  if (e instanceof MongoRuntimeError && /Server nonce does not begin with client nonce/.test(e.message)) {\n    // Do not retry the same target - likely a non-conformant or hostile server.\n    // Verify endpoint authenticity, then optionally fail over to a known-good host.\n    log.error('Possible MITM or non-conformant server detected', e);\n  }\n  throw e;\n}","preventionTips":["Only connect to genuine MongoDB Enterprise endpoints supporting MONGODB-AWS.","Avoid proxies/LBs that rewrite wire-protocol payloads.","Use TLS/SSL to the server to prevent tampering of the SASL conversation."],"tags":["auth","aws","security","wire-protocol","sasl"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}