{"record":{"id":"cb5f83f8d04f1b21","repo":"mongodb/node-mongodb-native","slug":"binary-type-with-subtype-0x02-contains-too-short-b","errorCode":null,"errorMessage":"Binary type with subtype 0x02 contains too short binary size","messagePattern":"Binary type with subtype 0x02 contains too short binary size","errorType":"exception","errorClass":"BSONError","httpStatus":null,"severity":"error","filePath":"src/cmap/wire_protocol/on_demand/document.ts","lineNumber":208,"sourceCode":"        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:\n        return new OnDemandDocument(this.bson, offset);\n      case BSONType.array:","sourceCodeStart":190,"sourceCodeEnd":226,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/cmap/wire_protocol/on_demand/document.ts#L190-L226","documentation":"Thrown when decoding a BSON Binary subtype 0x02 element whose inner length is smaller than totalBinarySize - 4, meaning the embedded length does not account for all declared bytes. Like the sibling checks this is a consistency guard and produces a BSONError, signalling the BSON is malformed.","triggerScenarios":"Fires at src/cmap/wire_protocol/on_demand/document.ts:208 during toJSValue for a subtype-2 binary when subType2BinarySize < totalBinarySize - 4. Reachable whenever the driver parses a response/document containing such a binary element.","commonSituations":"Wire-level corruption, a non-conforming BSON emitter on the server, or a tampered frame. Effectively never from a correctly functioning stable server.","solutions":["Retry the failing read.","Check the network path and disable any traffic-rewriting proxy.","Upgrade server and driver; report the malformed document if reproducible."],"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 short binary size/.test(e.message)) {\n  }\n}","preventionTips":["Retry transient parse failures.","Validate network hardware stability.","Report reproducible malformed BSON to the server team."],"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"}