{"record":{"id":"fcdab595acb57579","repo":"mongodb/node-mongodb-native","slug":"message-body-and-message-header-must-be-the-same-l","errorCode":null,"errorMessage":"Message body and message header must be the same length","messagePattern":"Message body and message header must be the same length","errorType":"exception","errorClass":"MongoDecompressionError","httpStatus":null,"severity":"error","filePath":"src/cmap/wire_protocol/compression.ts","lineNumber":212,"sourceCode":"    const ResponseType = messageHeader.opCode === OP_MSG ? OpMsgResponse : OpReply;\n    const messageBody = message.subarray(MESSAGE_HEADER_SIZE);\n    return new ResponseType(message, messageHeader, messageBody);\n  }\n\n  const header: MessageHeader = {\n    ...messageHeader,\n    fromCompressed: true,\n    opCode: readInt32LE(message, MESSAGE_HEADER_SIZE),\n    length: readInt32LE(message, MESSAGE_HEADER_SIZE + 4)\n  };\n  const compressorID = message[MESSAGE_HEADER_SIZE + 8];\n  const compressedBuffer = message.slice(MESSAGE_HEADER_SIZE + 9);\n\n  // recalculate based on wrapped opcode\n  const ResponseType = header.opCode === OP_MSG ? OpMsgResponse : OpReply;\n  const messageBody = await decompress(compressorID, compressedBuffer);\n  if (messageBody.length !== header.length) {\n    throw new MongoDecompressionError('Message body and message header must be the same length');\n  }\n  return new ResponseType(message, header, messageBody);\n}\n","sourceCodeStart":194,"sourceCodeEnd":216,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/cmap/wire_protocol/compression.ts#L194-L216","documentation":"Thrown after decompression succeeds when the length of the decompressed body does not equal the length declared in the compressed message header (read at offset headerSize+4). The driver treats this as a decompression failure because the payload cannot be safely interpreted as a complete OP_MSG/OP_REPLY. It is a MongoDecompressionError. Root causes are almost always data corruption during transit or a bug/version mismatch in the compression library (snappy, zlib, or zstd).","triggerScenarios":"Fires at src/cmap/wire_protocol/compression.ts:212 inside decompressResponse, after `await decompress(compressorID, compressedBuffer)`, when `messageBody.length !== header.length`. Requires an OP_COMPRESSED response, a valid recognized compressorID, successful library decompression, but a resulting byte count that disagrees with the header's declared uncompressed length.","commonSituations":"A transparent proxy truncates or re-encodes frames; an outdated or buggy snappy/zstd native binding (especially optional deps that failed to install cleanly and fell back); mismatched compression level between client and server; network-level packet corruption that still decompresses to a wrong-sized buffer.","solutions":["Turn off compression (drop the 'compressors' option) to confirm the link is otherwise healthy.","Reinstall optional compression dependencies (snappy, @mongodb-js/zstd) ensuring no native build errors; check they are the versions the driver expects.","Bypass any proxy/VPN/tunnel between client and server and connect directly to mongod.","Upgrade both the driver and server to matching supported versions.","File a driver bug with the compressed frame bytes if the failure reproduces on a direct unproxied connection."],"exampleFix":"// before\nconst client = new MongoClient(uri, { compressors: ['snappy'] });\n\n// after\nconst client = new MongoClient(uri);","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await db.command({ ping: 1 });\n} catch (error) {\n  if (error instanceof MongoDecompressionError && /same length/.test(error.message)) {\n  }\n}","preventionTips":["Reinstall native compression deps cleanly (no silent build failures).","Disable compression to confirm link integrity when debugging.","Avoid proxies that re-encode TCP payloads."],"tags":["network","compression","wire-protocol","corruption"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}