{"id":"33e6a3dd89431baf","repo":"mongodb/node-mongodb-native","slug":"binary-type-with-subtype-0x02-contains-too-long-bi","errorCode":null,"errorMessage":"Binary type with subtype 0x02 contains too long binary size","messagePattern":"Binary type with subtype 0x02 contains too long binary size","errorType":"exception","errorClass":"BSONError","httpStatus":null,"severity":"error","filePath":"src/cmap/wire_protocol/on_demand/document.ts","lineNumber":206,"sourceCode":"        return NumberUtils.getBigInt64LE(this.bson, offset);\n      case BSONType.bool:\n        return Boolean(this.bson[offset]);\n      case BSONType.objectId:\n        return new ObjectId(this.bson.subarray(offset, offset + 12));\n      case BSONType.timestamp:\n        return new Timestamp(NumberUtils.getBigInt64LE(this.bson, offset));\n      case BSONType.string:\n        return ByteUtils.toUTF8(this.bson, offset + 4, offset + length - 1, false);\n      case BSONType.binData: {\n        const totalBinarySize = NumberUtils.getInt32LE(this.bson, offset);\n        const subType = this.bson[offset + 4];\n\n        if (subType === 2) {\n          const subType2BinarySize = NumberUtils.getInt32LE(this.bson, offset + 1 + 4);\n          if (subType2BinarySize < 0)\n            throw new BSONError('Negative binary type element size found for subtype 0x02');\n          if (subType2BinarySize > totalBinarySize - 4)\n            throw new BSONError('Binary type with subtype 0x02 contains too long binary size');\n          if (subType2BinarySize < totalBinarySize - 4)\n            throw new BSONError('Binary type with subtype 0x02 contains too short binary size');\n          return new Binary(\n            this.bson.subarray(offset + 1 + 4 + 4, offset + 1 + 4 + 4 + subType2BinarySize),\n            2\n          );\n        }\n\n        return new Binary(\n          this.bson.subarray(offset + 1 + 4, offset + 1 + 4 + totalBinarySize),\n          subType\n        );\n      }\n      case BSONType.date:\n        // Pretend this is correct.\n        return new Date(Number(NumberUtils.getBigInt64LE(this.bson, offset)));\n\n      case BSONType.object:","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cmap/wire_protocol/on_demand/document.ts#L188-L224","documentation":"Thrown while decoding a BSON binary subtype 0x02 element when the inner subType2BinarySize is larger than totalBinarySize - 4 (the inner payload cannot be larger than the outer binary payload minus its own 4-byte length prefix). This is a structural BSON validity violation indicating corrupt or malformed data. Surfaced as BSONError by the on-demand decoder.","triggerScenarios":"toJSValue() computes subType2BinarySize and totalBinarySize from the element; if subType2BinarySize > totalBinarySize - 4 the declared inner length overflows the available bytes. Happens when a subtype-0x02 binary field's outer and inner length fields are inconsistent, e.g. truncated or hand-mangled data, or a buffer slice computed incorrectly upstream.","commonSituations":"Corrupt binary payloads from another BSON producer, truncated wire messages, or a driver/bson bug in versions with an off-by-one in size computation. Interop with data written by an old/buggy BSON library.","solutions":["Verify the integrity of the binary data at the source (re-read from the database, compare checksums).","Ensure any custom BSON producer writes consistent outer/inner lengths for subtype 0x02.","Upgrade bson/driver to pick up decoder fixes.","If the data is legitimately malformed, sanitize or rewrite it server-side before reading."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"import { BSONError } from 'bson';\ntry {\n  await collection.findOne({ _id });\n} catch (err) {\n  if (err instanceof BSONError && /too long binary size/.test(err.message)) {\n    // subtype 0x02 inner size exceeds outer payload; quarantine document\n  }\n  throw err;\n}","preventionTips":["Ensure subtype-0x02 binary inner length equals totalBinarySize - 4 at write time.","Use the official bson library to encode binary data.","Re-check producers that emit legacy old-binary elements."],"tags":["bson","decoding","binary","corruption"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}