|
| 1 | +package org.msgpack.core |
| 2 | + |
| 3 | +import wvlet.airspec.AirSpec |
| 4 | + |
| 5 | +import java.nio.ByteBuffer |
| 6 | + |
| 7 | +class PayloadSizeLimitTest extends AirSpec: |
| 8 | + |
| 9 | + test("detects malicious EXT32 file with huge declared size but tiny actual data") { |
| 10 | + // Craft a malicious EXT32 header: |
| 11 | + // 0xC9 = EXT32 format |
| 12 | + // 4 bytes = declared length (100MB, which exceeds 64MB threshold) |
| 13 | + // 1 byte = extension type |
| 14 | + // followed by only 1 byte of actual data (not 100MB) |
| 15 | + val declaredLength = 100 * 1024 * 1024 // 100 MB |
| 16 | + val buffer = ByteBuffer.allocate(7) // header(1) + length(4) + type(1) + data(1) |
| 17 | + buffer.put(0xC9.toByte) // EXT32 format code |
| 18 | + buffer.putInt(declaredLength) // declared length (big-endian) |
| 19 | + buffer.put(0x01.toByte) // extension type |
| 20 | + buffer.put(0x41.toByte) // only 1 byte of actual data |
| 21 | + val maliciousData = buffer.array() |
| 22 | + |
| 23 | + val unpacker = MessagePack.newDefaultUnpacker(maliciousData) |
| 24 | + |
| 25 | + // Should throw MessageSizeException because the declared size exceeds actual data |
| 26 | + intercept[MessageSizeException] { |
| 27 | + unpacker.unpackValue() |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + test("detects malicious BIN32 file with huge declared size but tiny actual data") { |
| 32 | + // Craft a malicious BIN32 header: |
| 33 | + // 0xC6 = BIN32 format |
| 34 | + // 4 bytes = declared length (100MB, which exceeds 64MB threshold) |
| 35 | + // followed by only 1 byte of actual data (not 100MB) |
| 36 | + val declaredLength = 100 * 1024 * 1024 // 100 MB |
| 37 | + val buffer = ByteBuffer.allocate(6) // header(1) + length(4) + data(1) |
| 38 | + buffer.put(0xC6.toByte) // BIN32 format code |
| 39 | + buffer.putInt(declaredLength) // declared length (big-endian) |
| 40 | + buffer.put(0x41.toByte) // only 1 byte of actual data |
| 41 | + val maliciousData = buffer.array() |
| 42 | + |
| 43 | + val unpacker = MessagePack.newDefaultUnpacker(maliciousData) |
| 44 | + |
| 45 | + // Should throw MessageSizeException because the declared size exceeds actual data |
| 46 | + intercept[MessageSizeException] { |
| 47 | + unpacker.unpackValue() |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + test("legitimate extension data works correctly") { |
| 52 | + val packer = MessagePack.newDefaultBufferPacker() |
| 53 | + val testData = Array.fill[Byte](1000)(0x42) |
| 54 | + packer.packExtensionTypeHeader(0x01.toByte, testData.length) |
| 55 | + packer.writePayload(testData) |
| 56 | + val msgpack = packer.toByteArray |
| 57 | + |
| 58 | + val unpacker = MessagePack.newDefaultUnpacker(msgpack) |
| 59 | + val value = unpacker.unpackValue() |
| 60 | + |
| 61 | + assert(value.isExtensionValue) |
| 62 | + assert(value.asExtensionValue().getData.length == 1000) |
| 63 | + } |
| 64 | + |
| 65 | + test("legitimate binary data works correctly") { |
| 66 | + val packer = MessagePack.newDefaultBufferPacker() |
| 67 | + val testData = Array.fill[Byte](1000)(0x42) |
| 68 | + packer.packBinaryHeader(testData.length) |
| 69 | + packer.writePayload(testData) |
| 70 | + val msgpack = packer.toByteArray |
| 71 | + |
| 72 | + val unpacker = MessagePack.newDefaultUnpacker(msgpack) |
| 73 | + val value = unpacker.unpackValue() |
| 74 | + |
| 75 | + assert(value.isBinaryValue) |
| 76 | + assert(value.asBinaryValue().asByteArray().length == 1000) |
| 77 | + } |
| 78 | + |
| 79 | + test("readPayload directly with malicious size throws exception") { |
| 80 | + // Test readPayload(int) directly with a malicious input |
| 81 | + val declaredLength = 100 * 1024 * 1024 // 100 MB |
| 82 | + val buffer = ByteBuffer.allocate(6) // header(1) + length(4) + data(1) |
| 83 | + buffer.put(0xC6.toByte) // BIN32 format code |
| 84 | + buffer.putInt(declaredLength) // declared length (big-endian) |
| 85 | + buffer.put(0x41.toByte) // only 1 byte of actual data |
| 86 | + val maliciousData = buffer.array() |
| 87 | + |
| 88 | + val unpacker = MessagePack.newDefaultUnpacker(maliciousData) |
| 89 | + |
| 90 | + // First, unpack the binary header to get the declared length |
| 91 | + val len = unpacker.unpackBinaryHeader() |
| 92 | + assert(len == declaredLength) |
| 93 | + |
| 94 | + // Then try to read the payload - should throw exception |
| 95 | + intercept[MessageSizeException] { |
| 96 | + unpacker.readPayload(len) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + test("small payloads under threshold work with upfront allocation") { |
| 101 | + // Payloads under 64MB should use the efficient upfront allocation path |
| 102 | + val packer = MessagePack.newDefaultBufferPacker() |
| 103 | + val testData = Array.fill[Byte](10000)(0x42) // 10KB, well under threshold |
| 104 | + packer.packBinaryHeader(testData.length) |
| 105 | + packer.writePayload(testData) |
| 106 | + val msgpack = packer.toByteArray |
| 107 | + |
| 108 | + val unpacker = MessagePack.newDefaultUnpacker(msgpack) |
| 109 | + val len = unpacker.unpackBinaryHeader() |
| 110 | + val payload = unpacker.readPayload(len) |
| 111 | + |
| 112 | + assert(payload.length == 10000) |
| 113 | + assert(payload.forall(_ == 0x42.toByte)) |
| 114 | + } |
| 115 | + |
| 116 | +end PayloadSizeLimitTest |
0 commit comments