{"record":{"id":"8ca04947dd62c976","repo":"mongodb/node-mongodb-native","slug":"server-returned-an-invalid-host-host","errorCode":null,"errorMessage":"Server returned an invalid host: \"${host}\"","messagePattern":"Server returned an invalid host: \"(.+?)\"","errorType":"exception","errorClass":"MongoRuntimeError","httpStatus":null,"severity":"error","filePath":"src/cmap/auth/mongodb_aws.ts","lineNumber":95,"sourceCode":"    };\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),\n          'X-MongoDB-GS2-CB-Flag': 'n'\n        },\n        path: '/',\n        body,\n        date: new Date()","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/cmap/auth/mongodb_aws.ts#L77-L113","documentation":"Thrown during MONGODB-AWS SASL exchange when the host returned by the server fails validation: length is 0, length exceeds 255, or it contains '..'. The host is used to build the STS GetCallerIdentity request URL, so an invalid value would enable host injection/path traversal and is rejected.","triggerScenarios":"serverResponse.h is empty, longer than 255 chars, or contains '..'. Fires at mongodb_aws.ts:95. Indicates a malformed server response or a malicious/buggy endpoint.","commonSituations":"A proxy injecting an unexpected host. A non-MongoDB endpoint responding to the saslStart command. Corrupted BSON.","solutions":["Ensure you are connecting to a legitimate MongoDB server endpoint.","Remove intermediary proxies that could rewrite the response host field.","Report to the server vendor if the host field is unexpectedly malformed."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await client.connect();\n} catch (e) {\n  if (e instanceof MongoRuntimeError && /invalid host/.test(e.message)) {\n    // inspect server endpoint / proxy; possible host injection\n  }\n  throw e;\n}","preventionTips":["Connect to known, trusted MongoDB endpoints only.","Remove proxies that could alter the SASL host field.","Treat this error as a potential security incident and investigate."],"tags":["authentication","aws","security","wire-protocol","runtime"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}