{"record":{"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/dce7939f86fb283e167ad709955abedb7bf23124/src/cmap/auth/mongodb_aws.ts#L72-L108","documentation":"Thrown during MONGODB-AWS SASL exchange when the first 32 bytes of the server's nonce do not equal the 32-byte client nonce the driver sent. Per the cross-driver auth spec, the server nonce must begin with the client nonce; a mismatch signals a protocol violation or potential attack, so the driver aborts.","triggerScenarios":"ByteUtils.equals(serverNonce.subarray(0,32), clientNonce) returns false. Fires at mongodb_aws.ts:90. A server-side response-construction bug or payload corruption is the usual cause.","commonSituations":"Intermediate proxy rewriting the conversation. A misbehaving MongoDB-compatible service. Extremely rare in normal operation against real MongoDB.","solutions":["Connect directly to the MongoDB host, bypassing any proxy/VPN that could alter payloads.","Confirm the target is an authentic MongoDB deployment supporting MONGODB-AWS.","Gather driver version + server hello and file a bug if reproducible against real MongoDB."],"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/.test(e.message)) {\n    // protocol/security violation; do not retry against same endpoint blindly\n  }\n  throw e;\n}","preventionTips":["Ensure a direct, unmodified path to the MongoDB server.","Treat nonce mismatches as security-sensitive; investigate the endpoint.","Do not retry-loop without diagnostics; the cause is server/proxy-side."],"tags":["authentication","aws","wire-protocol","security","runtime"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}