{"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/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cmap/wire_protocol/on_demand/document.ts#L190-L226","documentation":"Thrown while decoding a BSON binary subtype 0x02 element when the inner subType2BinarySize is smaller than totalBinarySize - 4 (the inner payload should exactly fill the outer payload minus the 4-byte prefix). Per BSON subtype-0x02 semantics the inner length must equal totalBinarySize - 4; a smaller value is a validity violation. Surfaced as BSONError by the on-demand decoder.","triggerScenarios":"toJSValue() finds subType2BinarySize < totalBinarySize - 4, i.e. leftover bytes in the outer binary payload that the inner length does not account for. Triggered when a subtype-0x02 binary field is malformed (inner length too small for the outer payload). Same shape of corruption as 131 but in the opposite direction.","commonSituations":"Malformed binary written by a non-compliant BSON producer, buffer truncation/padding, or an upstream slicing bug. Interop issues with legacy BSON libraries that padded or mis-sized old-binary fields.","solutions":["Inspect and correct the source binary data so inner length equals totalBinarySize - 4 for subtype 0x02.","Re-read the document from the server to rule out in-flight corruption.","Upgrade bson/driver for decoder robustness.","If producing this BSON yourself, use the official bson library's Binary type to guarantee correct sizing."],"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 short binary size/.test(err.message)) {\n    // subtype 0x02 inner size smaller than outer payload; quarantine document\n  }\n  throw err;\n}","preventionTips":["Write subtype-0x02 binary with inner length exactly equal to totalBinarySize - 4.","Prefer the official bson Binary type to avoid manual sizing.","Re-validate data from legacy BSON producers."],"tags":["bson","decoding","binary","corruption"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}