GeezerVison is showing github.com — [fix][client] Throw IOException for bad V5 bytes (#27357) · apache/pulsar@b5c428a
File treeExpand file treeCollapse file tree Open diff view settings main/java/org/apache/pulsar/client/impl/v5 test/java/org/apache/pulsar/client/impl/v5
main/java/org/apache/pulsar/client/impl test/java/org/apache/pulsar/client/impl
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: pulsar-client-v5/src/main/java/org/apache/pulsar/client/impl/v5/CheckpointV5.java +25-13Lines changed: 25 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 19 | 19 | package org.apache.pulsar.client.impl.v5; | 20 | 20 |
| 21 | 21 | import java.io.IOException; | | 22 | +import java.nio.BufferUnderflowException; | 22 | 23 | import java.nio.ByteBuffer; | 23 | 24 | import java.util.Collection; | 24 | 25 | import java.util.HashMap; | @@ -99,23 +100,34 @@ static Checkpoint fromByteArray(byte[] data) throws IOException { | 99 | 100 | return switch (type) { | 100 | 101 | case TYPE_EARLIEST -> EARLIEST; | 101 | 102 | case TYPE_LATEST -> LATEST; | 102 | | - case TYPE_REGULAR -> { | 103 | | - int numEntries = buf.getInt(); | 104 | | - Map<Long, org.apache.pulsar.client.api.MessageId> positions = new HashMap<>(); | 105 | | - for (int i = 0; i < numEntries; i++) { | 106 | | - long segmentId = buf.getLong(); | 107 | | - int msgIdLen = buf.getInt(); | 108 | | - byte[] msgIdBytes = new byte[msgIdLen]; | 109 | | - buf.get(msgIdBytes); | 110 | | - positions.put(segmentId, | 111 | | - org.apache.pulsar.client.api.MessageId.fromByteArray(msgIdBytes)); | 112 | | - } | 113 | | - yield new CheckpointV5(positions); | 114 | | - } | | 103 | + case TYPE_REGULAR -> readRegular(buf); | 115 | 104 | default -> throw new IOException("Unknown checkpoint type: " + type); | 116 | 105 | }; | 117 | 106 | } | 118 | 107 |
| | 108 | + private static CheckpointV5 readRegular(ByteBuffer buf) throws IOException { | | 109 | + try { | | 110 | + int numEntries = buf.getInt(); | | 111 | + if (numEntries < 0) { | | 112 | + throw new IOException("Invalid checkpoint data: bad entry count"); | | 113 | + } | | 114 | + Map<Long, MessageId> positions = new HashMap<>(); | | 115 | + for (int i = 0; i < numEntries; i++) { | | 116 | + long segmentId = buf.getLong(); | | 117 | + int msgIdLen = buf.getInt(); | | 118 | + if (msgIdLen < 0 || msgIdLen > buf.remaining()) { | | 119 | + throw new IOException("Invalid checkpoint data: bad message id length"); | | 120 | + } | | 121 | + byte[] msgIdBytes = new byte[msgIdLen]; | | 122 | + buf.get(msgIdBytes); | | 123 | + positions.put(segmentId, MessageId.fromByteArray(msgIdBytes)); | | 124 | + } | | 125 | + return new CheckpointV5(positions); | | 126 | + } catch (BufferUnderflowException e) { | | 127 | + throw new IOException("Invalid checkpoint data: truncated", e); | | 128 | + } | | 129 | + } | | 130 | + | 119 | 131 | /** | 120 | 132 | * Sentinel checkpoint for earliest/latest positions. Encoded as a single type byte. | 121 | 133 | */ | |
Collapse file Copy file name to clipboardExpand all lines: pulsar-client-v5/src/main/java/org/apache/pulsar/client/impl/v5/MessageIdV5.java +23-3Lines changed: 23 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 19 | 19 | package org.apache.pulsar.client.impl.v5; | 20 | 20 |
| 21 | 21 | import java.io.IOException; | | 22 | +import java.nio.BufferUnderflowException; | 22 | 23 | import java.nio.ByteBuffer; | 23 | 24 | import java.nio.charset.StandardCharsets; | 24 | 25 | import java.util.HashMap; | @@ -266,7 +267,14 @@ static MessageIdV5 fromByteArray(byte[] data) throws IOException { | 266 | 267 | if (data == null || data.length < 12) { | 267 | 268 | throw new IOException("Invalid MessageIdV5 data: too short"); | 268 | 269 | } | 269 | | - ByteBuffer buf = ByteBuffer.wrap(data); | | 270 | + try { | | 271 | + return read(ByteBuffer.wrap(data)); | | 272 | + } catch (BufferUnderflowException e) { | | 273 | + throw new IOException("Invalid MessageIdV5 data: truncated", e); | | 274 | + } | | 275 | + } | | 276 | + | | 277 | + private static MessageIdV5 read(ByteBuffer buf) throws IOException { | 270 | 278 | long segmentId = buf.getLong(); | 271 | 279 | int v4Length = buf.getInt(); | 272 | 280 | if (v4Length < 0 || v4Length > buf.remaining()) { | @@ -286,6 +294,9 @@ static MessageIdV5 fromByteArray(byte[] data) throws IOException { | 286 | 294 | String parentTopic = null; | 287 | 295 | if (buf.hasRemaining()) { | 288 | 296 | int parentLen = buf.getInt(); | | 297 | + if (parentLen < -1) { | | 298 | + throw new IOException("Invalid MessageIdV5 data: bad parent-topic length"); | | 299 | + } | 289 | 300 | if (parentLen >= 0) { | 290 | 301 | if (parentLen > buf.remaining()) { | 291 | 302 | throw new IOException("Invalid MessageIdV5 data: bad parent-topic length"); | @@ -300,10 +311,16 @@ static MessageIdV5 fromByteArray(byte[] data) throws IOException { | 300 | 311 | Map<String, Map<Long, MessageId>> multiTopic = null; | 301 | 312 | if (buf.hasRemaining()) { | 302 | 313 | int topicCount = buf.getInt(); | | 314 | + if (topicCount < -1) { | | 315 | + throw new IOException("Invalid MessageIdV5 data: bad topic count"); | | 316 | + } | 303 | 317 | if (topicCount >= 0) { | 304 | | - multiTopic = new HashMap<>(topicCount); | | 318 | + multiTopic = new HashMap<>(); | 305 | 319 | for (int i = 0; i < topicCount; i++) { | 306 | 320 | int topicLen = buf.getInt(); | | 321 | + if (topicLen < 0 || topicLen > buf.remaining()) { | | 322 | + throw new IOException("Invalid MessageIdV5 data: bad topic length"); | | 323 | + } | 307 | 324 | byte[] topicBytes = new byte[topicLen]; | 308 | 325 | buf.get(topicBytes); | 309 | 326 | String topic = new String(topicBytes, StandardCharsets.UTF_8); | @@ -318,7 +335,10 @@ static MessageIdV5 fromByteArray(byte[] data) throws IOException { | 318 | 335 |
| 319 | 336 | private static Map<Long, MessageId> readSegmentVector(ByteBuffer buf) throws IOException { | 320 | 337 | int count = buf.getInt(); | 321 | | - Map<Long, MessageId> out = new HashMap<>(count); | | 338 | + if (count < 0) { | | 339 | + throw new IOException("Invalid MessageIdV5 data: bad segment count"); | | 340 | + } | | 341 | + Map<Long, MessageId> out = new HashMap<>(); | 322 | 342 | for (int i = 0; i < count; i++) { | 323 | 343 | long segId = buf.getLong(); | 324 | 344 | int idLen = buf.getInt(); | |
Collapse file Copy file name to clipboardExpand all lines: pulsar-client-v5/src/test/java/org/apache/pulsar/client/impl/v5/CheckpointV5Test.java +41Lines changed: 41 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | import static org.testng.Assert.assertSame; | 23 | 23 | import static org.testng.Assert.assertThrows; | 24 | 24 | import static org.testng.Assert.assertTrue; | | 25 | +import io.netty.buffer.ByteBuf; | | 26 | +import io.netty.buffer.ByteBufUtil; | | 27 | +import io.netty.buffer.Unpooled; | | 28 | +import java.io.IOException; | | 29 | +import java.nio.ByteBuffer; | | 30 | +import java.util.Arrays; | 25 | 31 | import java.util.Map; | 26 | 32 | import org.apache.pulsar.client.api.MessageId; | 27 | 33 | import org.apache.pulsar.client.api.v5.Checkpoint; | 28 | 34 | import org.apache.pulsar.client.impl.MessageIdImpl; | | 35 | +import org.apache.pulsar.common.api.proto.MessageIdData; | | 36 | +import org.testng.annotations.DataProvider; | 29 | 37 | import org.testng.annotations.Test; | 30 | 38 |
| 31 | 39 | public class CheckpointV5Test { | @@ -112,4 +120,37 @@ public void testFromByteArrayRejectsUnknownType() { | 112 | 120 | assertThrows(java.io.IOException.class, | 113 | 121 | () -> CheckpointV5.fromByteArray(new byte[]{127, 0, 0, 0, 0, 0, 0, 0, 0})); | 114 | 122 | } | | 123 | + | | 124 | + @Test | | 125 | + public void testFromByteArrayRejectsTruncatedData() { | | 126 | + byte[] bytes = new CheckpointV5(Map.of(1L, v4(10, 20))).toByteArray(); | | 127 | + for (int length = 1; length < bytes.length; length++) { | | 128 | + byte[] truncated = Arrays.copyOf(bytes, length); | | 129 | + assertThrows(IOException.class, () -> CheckpointV5.fromByteArray(truncated)); | | 130 | + } | | 131 | + } | | 132 | + | | 133 | + @DataProvider | | 134 | + public Object[][] badEntryCountAndLength() { | | 135 | + return new Object[][]{{-1, 0}, {1, -1}, {1, Integer.MAX_VALUE}}; | | 136 | + } | | 137 | + | | 138 | + @Test(dataProvider = "badEntryCountAndLength") | | 139 | + public void testFromByteArrayRejectsBadLengths(int entryCount, int messageIdLength) { | | 140 | + ByteBuffer buf = ByteBuffer.allocate(Byte.BYTES + Integer.BYTES + Long.BYTES + Integer.BYTES); | | 141 | + buf.put((byte) 0).putInt(entryCount).putLong(1L).putInt(messageIdLength); | | 142 | + assertThrows(IOException.class, () -> CheckpointV5.fromByteArray(buf.array())); | | 143 | + } | | 144 | + | | 145 | + @Test | | 146 | + public void testFromByteArrayRejectsNegativeBatchSizeInMessageId() { | | 147 | + // The v4 encoder itself never writes a negative batch size | | 148 | + MessageIdData idData = new MessageIdData().setLedgerId(1).setEntryId(2).setBatchIndex(0).setBatchSize(-1); | | 149 | + ByteBuf idBuf = Unpooled.buffer(idData.getSerializedSize()); | | 150 | + idData.writeTo(idBuf); | | 151 | + byte[] idBytes = ByteBufUtil.getBytes(idBuf); | | 152 | + ByteBuffer buf = ByteBuffer.allocate(Byte.BYTES + Integer.BYTES + Long.BYTES + Integer.BYTES + idBytes.length); | | 153 | + buf.put((byte) 0).putInt(1).putLong(1L).putInt(idBytes.length).put(idBytes); | | 154 | + assertThrows(IOException.class, () -> CheckpointV5.fromByteArray(buf.array())); | | 155 | + } | 115 | 156 | } |
Collapse file Copy file name to clipboardExpand all lines: pulsar-client-v5/src/test/java/org/apache/pulsar/client/impl/v5/MessageIdV5Test.java +93Lines changed: 93 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | import static org.testng.Assert.assertEquals; | 22 | 22 | import static org.testng.Assert.assertFalse; | 23 | 23 | import static org.testng.Assert.assertNotNull; | | 24 | +import static org.testng.Assert.assertNull; | 24 | 25 | import static org.testng.Assert.assertThrows; | 25 | 26 | import static org.testng.Assert.assertTrue; | | 27 | +import static org.testng.Assert.fail; | | 28 | +import io.netty.buffer.ByteBuf; | | 29 | +import io.netty.buffer.ByteBufUtil; | | 30 | +import io.netty.buffer.Unpooled; | | 31 | +import java.io.IOException; | | 32 | +import java.nio.ByteBuffer; | | 33 | +import java.nio.charset.StandardCharsets; | | 34 | +import java.util.Arrays; | 26 | 35 | import java.util.HashMap; | 27 | 36 | import java.util.Map; | 28 | 37 | import org.apache.pulsar.client.api.MessageId; | 29 | 38 | import org.apache.pulsar.client.impl.MessageIdImpl; | | 39 | +import org.apache.pulsar.common.api.proto.MessageIdData; | | 40 | +import org.testng.annotations.DataProvider; | 30 | 41 | import org.testng.annotations.Test; | 31 | 42 |
| 32 | 43 | public class MessageIdV5Test { | @@ -241,6 +252,88 @@ public void testFromByteArrayRejectsGarbage() { | 241 | 252 | () -> MessageIdV5.fromByteArray(buf.array())); | 242 | 253 | } | 243 | 254 |
| | 255 | + @DataProvider | | 256 | + public Object[][] malformedSections() { | | 257 | + return new Object[][]{ | | 258 | + {"negative position count", idFollowedBy(-1)}, | | 259 | + {"negative cross-topic count", idFollowedBy(0, -1, -2)}, | | 260 | + {"negative cross-topic name length", idFollowedBy(0, -1, 1, -1)}, | | 261 | + {"cross-topic name longer than the data", idFollowedBy(0, -1, 1, 100)}, | | 262 | + {"negative cross-topic position count", idFollowedBy(0, -1, 1, 0, -1)}, | | 263 | + {"truncated parent-topic length", withoutLastBytes(idFollowedBy(0, -1), 2)}, | | 264 | + {"parent-topic length below the null sentinel", idFollowedBy(0, -2, -1)}, | | 265 | + {"parent-topic length of Integer.MIN_VALUE", idFollowedBy(0, Integer.MIN_VALUE, -1)}, | | 266 | + {"negative batch size in the v4 id", idOf(v4BytesWithBatchSize(-1))} | | 267 | + }; | | 268 | + } | | 269 | + | | 270 | + @Test(dataProvider = "malformedSections") | | 271 | + public void testFromByteArrayRejectsMalformedSections(String description, byte[] data) { | | 272 | + assertThrows(IOException.class, () -> MessageIdV5.fromByteArray(data)); | | 273 | + } | | 274 | + | | 275 | + @Test | | 276 | + public void testFromByteArrayReadsOlderEncodingsAndRejectsOtherPrefixes() throws Exception { | | 277 | + MessageId v4Id = v4(10, 20, 0); | | 278 | + Map<Long, MessageId> positions = Map.of(0L, v4(1, 2, 0)); | | 279 | + String parentTopic = "topic://t"; | | 280 | + byte[] bytes = new MessageIdV5(v4Id, 1L, positions, parentTopic, | | 281 | + Map.of(parentTopic, Map.of(0L, v4(1, 1, 0)))).toByteArray(); | | 282 | + // An older encoding ends after the v4 id, after the position vector or after the parent topic | | 283 | + int afterV4Id = Long.BYTES + Integer.BYTES + v4Id.toByteArray().length; | | 284 | + int afterPositions = afterV4Id + Integer.BYTES | | 285 | + + Long.BYTES + Integer.BYTES + positions.get(0L).toByteArray().length; | | 286 | + int afterParentTopic = afterPositions + Integer.BYTES + parentTopic.getBytes(StandardCharsets.UTF_8).length; | | 287 | + | | 288 | + for (int length = 0; length < bytes.length; length++) { | | 289 | + byte[] prefix = Arrays.copyOf(bytes, length); | | 290 | + if (length != afterV4Id && length != afterPositions && length != afterParentTopic) { | | 291 | + // A prefix that ends anywhere else is malformed | | 292 | + try { | | 293 | + MessageIdV5.fromByteArray(prefix); | | 294 | + fail("A prefix of " + length + " bytes was read as an id"); | | 295 | + } catch (IOException expected) { | | 296 | + // reported as malformed, as the API documents | | 297 | + } | | 298 | + continue; | | 299 | + } | | 300 | + MessageIdV5 decoded = MessageIdV5.fromByteArray(prefix); | | 301 | + assertEquals(decoded.segmentId(), 1L); | | 302 | + assertEquals(decoded.v4MessageId(), v4Id); | | 303 | + assertEquals(decoded.positionVector(), length >= afterPositions ? positions : Map.of()); | | 304 | + assertEquals(decoded.parentTopic(), length >= afterParentTopic ? parentTopic : null); | | 305 | + assertNull(decoded.multiTopicVector()); | | 306 | + } | | 307 | + } | | 308 | + | | 309 | + private static byte[] withoutLastBytes(byte[] data, int count) { | | 310 | + return Arrays.copyOf(data, data.length - count); | | 311 | + } | | 312 | + | | 313 | + /** A serialised id of segment 5 whose sections after the v4 id are the given ints. */ | | 314 | + private static byte[] idFollowedBy(int... ints) { | | 315 | + return idOf(v4(1, 2, 0).toByteArray(), ints); | | 316 | + } | | 317 | + | | 318 | + /** A serialised v4 id with the given batch size; the v4 encoder itself never writes a negative one. */ | | 319 | + private static byte[] v4BytesWithBatchSize(int batchSize) { | | 320 | + MessageIdData idData = new MessageIdData() | | 321 | + .setLedgerId(1).setEntryId(2).setBatchIndex(0).setBatchSize(batchSize); | | 322 | + ByteBuf buf = Unpooled.buffer(idData.getSerializedSize()); | | 323 | + idData.writeTo(buf); | | 324 | + return ByteBufUtil.getBytes(buf); | | 325 | + } | | 326 | + | | 327 | + /** A serialised id of segment 5 made of the given v4 id bytes followed by the given ints. */ | | 328 | + private static byte[] idOf(byte[] v4Bytes, int... ints) { | | 329 | + ByteBuffer buf = ByteBuffer.allocate(Long.BYTES + Integer.BYTES + v4Bytes.length + Integer.BYTES * ints.length); | | 330 | + buf.putLong(5L).putInt(v4Bytes.length).put(v4Bytes); | | 331 | + for (int value : ints) { | | 332 | + buf.putInt(value); | | 333 | + } | | 334 | + return buf.array(); | | 335 | + } | | 336 | + | 244 | 337 | // --- sentinels --- | 245 | 338 |
| 246 | 339 | @Test | |
Collapse file Copy file name to clipboardExpand all lines: pulsar-client/src/main/java/org/apache/pulsar/client/impl/MessageIdImpl.java +11-3Lines changed: 11 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | import io.netty.buffer.Unpooled; | 23 | 23 | import io.netty.util.concurrent.FastThreadLocal; | 24 | 24 | import java.io.IOException; | | 25 | +import java.util.BitSet; | 25 | 26 | import java.util.Objects; | 26 | 27 | import org.apache.pulsar.client.api.MessageId; | 27 | 28 | import org.apache.pulsar.client.api.MessageIdAdv; | @@ -106,7 +107,7 @@ public static MessageId fromByteArray(byte[] data) throws IOException { | 106 | 107 | if (idData.hasBatchIndex()) { | 107 | 108 | if (idData.hasBatchSize()) { | 108 | 109 | messageId = new BatchMessageIdImpl(idData.getLedgerId(), idData.getEntryId(), idData.getPartition(), | 109 | | - idData.getBatchIndex(), idData.getBatchSize(), BatchMessageIdImpl.newAckSet(idData.getBatchSize())); | | 110 | + idData.getBatchIndex(), idData.getBatchSize(), ackSetFor(idData)); | 110 | 111 | } else { | 111 | 112 | messageId = new BatchMessageIdImpl(idData.getLedgerId(), idData.getEntryId(), idData.getPartition(), | 112 | 113 | idData.getBatchIndex()); | @@ -141,8 +142,7 @@ public static MessageId fromByteArrayWithTopic(byte[] data, TopicName topicName) | 141 | 142 | if (idData.hasBatchIndex()) { | 142 | 143 | if (idData.hasBatchSize()) { | 143 | 144 | messageId = new BatchMessageIdImpl(idData.getLedgerId(), idData.getEntryId(), idData.getPartition(), | 144 | | - idData.getBatchIndex(), idData.getBatchSize(), | 145 | | - BatchMessageIdImpl.newAckSet(idData.getBatchSize())); | | 145 | + idData.getBatchIndex(), idData.getBatchSize(), ackSetFor(idData)); | 146 | 146 | } else { | 147 | 147 | messageId = new BatchMessageIdImpl(idData.getLedgerId(), idData.getEntryId(), idData.getPartition(), | 148 | 148 | idData.getBatchIndex(), 0, null); | @@ -158,6 +158,14 @@ public static MessageId fromByteArrayWithTopic(byte[] data, TopicName topicName) | 158 | 158 | return messageId; | 159 | 159 | } | 160 | 160 |
| | 161 | + private static BitSet ackSetFor(MessageIdData idData) throws IOException { | | 162 | + final int batchSize = idData.getBatchSize(); | | 163 | + if (batchSize < 0) { | | 164 | + throw new IOException("Invalid message id data: negative batch size " + batchSize); | | 165 | + } | | 166 | + return BatchMessageIdImpl.newAckSet(batchSize); | | 167 | + } | | 168 | + | 161 | 169 | protected MessageIdData writeMessageIdData(MessageIdData msgId, int batchIndex, int batchSize) { | 162 | 170 | if (msgId == null) { | 163 | 171 | msgId = LOCAL_MESSAGE_ID.get() | |
Collapse file Copy file name to clipboardExpand all lines: pulsar-client/src/test/java/org/apache/pulsar/client/impl/MessageIdSerializationTest.java +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 19 | 19 | package org.apache.pulsar.client.impl; | 20 | 20 |
| 21 | 21 | import static org.testng.Assert.assertEquals; | | 22 | +import static org.testng.Assert.assertThrows; | | 23 | +import io.netty.buffer.ByteBuf; | | 24 | +import io.netty.buffer.ByteBufUtil; | | 25 | +import io.netty.buffer.Unpooled; | 22 | 26 | import java.io.IOException; | 23 | 27 | import org.apache.pulsar.client.api.MessageId; | | 28 | +import org.apache.pulsar.common.api.proto.MessageIdData; | 24 | 29 | import org.testng.annotations.Test; | 25 | 30 |
| 26 | 31 | public class MessageIdSerializationTest { | @@ -58,4 +63,18 @@ public void testProtobufSerializationNull() throws Exception { | 58 | 63 | void testProtobufSerializationEmpty() throws Exception { | 59 | 64 | MessageId.fromByteArray(new byte[0]); | 60 | 65 | } | | 66 | + | | 67 | + @Test | | 68 | + public void testNegativeBatchSizeIsRejected() { | | 69 | + byte[] serialized = serialize( | | 70 | + new MessageIdData().setLedgerId(1).setEntryId(2).setBatchIndex(0).setBatchSize(-1)); | | 71 | + assertThrows(IOException.class, () -> MessageId.fromByteArray(serialized)); | | 72 | + assertThrows(IOException.class, () -> MessageId.fromByteArrayWithTopic(serialized, "my-topic")); | | 73 | + } | | 74 | + | | 75 | + private static byte[] serialize(MessageIdData idData) { | | 76 | + ByteBuf buf = Unpooled.buffer(idData.getSerializedSize()); | | 77 | + idData.writeTo(buf); | | 78 | + return ByteBufUtil.getBytes(buf); | | 79 | + } | 61 | 80 | } |
|