{"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/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cmap/wire_protocol/compression.ts#L194-L216","documentation":"Thrown by decompressResponse() after decompressing an OP_COMPRESSED message when the decompressed body length does not equal the original (uncompressed) length declared in the compressed message header. The mismatch means the decompression produced the wrong number of bytes, indicating data corruption, a truncated/modified message, or a compressor library bug. Surfaced as MongoDecompressionError.","triggerScenarios":"After decompress(compressorID, compressedBuffer) returns messageBody, the check `messageBody.length !== header.length` fails. The header.length was read from the OP_COMPRESSED wrapper (the original message size). Occurs on any compressed response where bytes were lost/corrupted in transit or the compressor produced incorrect output.","commonSituations":"Network corruption not caught at the TCP layer (rare), a proxy/load balancer truncating or rewriting compressed payloads, a faulty zstd/zlib/snappy native build producing wrong-length output, or memory corruption in the buffer pipeline.","solutions":["Disable compression to rule out a native compressor library bug.","Eliminate any intermediary (proxy, LB, MITM) that could alter compressed message bytes.","Reinstall/rebuild native compression dependencies (`@mongodb-js/zstd`, snappy) if using them.","If it persists on a clean network path with current libraries, report a driver bug with the corrupted message bytes."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"let lastErr;\nfor (let attempt = 0; attempt < 3; attempt++) {\n  try {\n    return await collection.find({}).toArray();\n  } catch (err) {\n    lastErr = err;\n    if (err instanceof MongoDecompressionError && /same length/.test(err.message)) {\n      // likely transient corruption; retry with backoff\n      await new Promise(r => setTimeout(r, 100 * 2 ** attempt));\n      continue;\n    }\n    throw err;\n  }\n}\nthrow lastErr;","preventionTips":["Disable compression to rule out a native compressor library bug if it recurs.","Remove proxies/LBs that may alter compressed message bytes.","Rebuild native compression dependencies on the target platform."],"tags":["compression","wire-protocol","corruption","network"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}