mongodb/node-mongodb-native · error · BSONError

Binary type with subtype 0x02 contains too short binary size

Error message

Binary type with subtype 0x02 contains too short binary size

What it means

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.

Solutions

  1. Retry the failing read.
  2. Check the network path and disable any traffic-rewriting proxy.
  3. Upgrade server and driver; report the malformed document if reproducible.
Defensive patterns

Strategy: retry

Try / catch

try {
  await coll.findOne({_id:1});
} catch (e) {
  if (e instanceof BSONError && /too short binary size/.test(e.message)) {
  }
}

Prevention

When it happens

Trigger: 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.

Common situations: Wire-level corruption, a non-conforming BSON emitter on the server, or a tampered frame. Effectively never from a correctly functioning stable server.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11). Data as JSON: /api/errors/cb5f83f8d04f1b21. Report an issue: GitHub.

Appendix: source

Thrown at src/cmap/wire_protocol/on_demand/document.ts:208

        return Boolean(this.bson[offset]);
      case BSONType.objectId:
        return new ObjectId(this.bson.subarray(offset, offset + 12));
      case BSONType.timestamp:
        return new Timestamp(NumberUtils.getBigInt64LE(this.bson, offset));
      case BSONType.string:
        return ByteUtils.toUTF8(this.bson, offset + 4, offset + length - 1, false);
      case BSONType.binData: {
        const totalBinarySize = NumberUtils.getInt32LE(this.bson, offset);
        const subType = this.bson[offset + 4];

        if (subType === 2) {
          const subType2BinarySize = NumberUtils.getInt32LE(this.bson, offset + 1 + 4);
          if (subType2BinarySize < 0)
            throw new BSONError('Negative binary type element size found for subtype 0x02');
          if (subType2BinarySize > totalBinarySize - 4)
            throw new BSONError('Binary type with subtype 0x02 contains too long binary size');
          if (subType2BinarySize < totalBinarySize - 4)
            throw new BSONError('Binary type with subtype 0x02 contains too short binary size');
          return new Binary(
            this.bson.subarray(offset + 1 + 4 + 4, offset + 1 + 4 + 4 + subType2BinarySize),
            2
          );
        }

        return new Binary(
          this.bson.subarray(offset + 1 + 4, offset + 1 + 4 + totalBinarySize),
          subType
        );
      }
      case BSONType.date:
        // Pretend this is correct.
        return new Date(Number(NumberUtils.getBigInt64LE(this.bson, offset)));

      case BSONType.object:
        return new OnDemandDocument(this.bson, offset);
      case BSONType.array:

View on GitHub (pinned to dce7939f86)