{"record":{"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/dce7939f86fb283e167ad709955abedb7bf23124/src/cmap/wire_protocol/on_demand/document.ts#L188-L224","documentation":"Thrown when decoding a BSON Binary subtype 0x02 element whose declared inner length exceeds totalBinarySize - 4 (the space left after the inner-length prefix). The element is structurally impossible, so the parser refuses it with a BSONError. This is a malformed-BSON guard inside the on-demand document reader.","triggerScenarios":"Fires at src/cmap/wire_protocol/on_demand/document.ts:206 during toJSValue for a binData/subtype-2 element when subType2BinarySize > totalBinarySize - 4. Encountered while reading any server response or OnDemandDocument containing such a corrupted binary field.","commonSituations":"Truncated or corrupted wire response; a faulty proxy; a server-side serialization bug; BSON produced by a non-conforming writer. Not triggered by normal application data.","solutions":["Retry to rule out transient corruption.","Remove proxies/inspectors on the connection path.","Update server and driver; capture and report the offending payload if it persists."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await coll.findOne({_id:1});\n} catch (e) {\n  if (e instanceof BSONError && /too long binary size/.test(e.message)) {\n  }\n}","preventionTips":["Retry once on BSON consistency errors before escalating.","Eliminate response-mutating intermediaries.","Upgrade server and driver to current patch releases."],"tags":["bson","corruption","wire-protocol","parsing"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}