GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [fix][client] Throw IOException for bad V5 bytes (#27357) · apache/pulsar@b5c428a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • pulsar-client-v5/src
    • main/java/org/apache/pulsar/client/impl/v5
      • CheckpointV5.java
      • MessageIdV5.java
    • test/java/org/apache/pulsar/client/impl/v5
      • CheckpointV5Test.java
      • MessageIdV5Test.java
  • pulsar-client/src
    • main/java/org/apache/pulsar/client/impl
      • MessageIdImpl.java
    • test/java/org/apache/pulsar/client/impl
      • MessageIdSerializationTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎pulsar-client-v5/src/main/java/org/apache/pulsar/client/impl/v5/CheckpointV5.java‎

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 numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.pulsar.client.impl.v5;
2020

2121
import java.io.IOException;
22+
import java.nio.BufferUnderflowException;
2223
import java.nio.ByteBuffer;
2324
import java.util.Collection;
2425
import java.util.HashMap;
@@ -99,23 +100,34 @@ static Checkpoint fromByteArray(byte[] data) throws IOException {
99100
return switch (type) {
100101
case TYPE_EARLIEST -> EARLIEST;
101102
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);
115104
default -> throw new IOException("Unknown checkpoint type: " + type);
116105
};
117106
}
118107

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+
119131
/**
120132
* Sentinel checkpoint for earliest/latest positions. Encoded as a single type byte.
121133
*/
Collapse file

‎pulsar-client-v5/src/main/java/org/apache/pulsar/client/impl/v5/MessageIdV5.java‎

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 numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.pulsar.client.impl.v5;
2020

2121
import java.io.IOException;
22+
import java.nio.BufferUnderflowException;
2223
import java.nio.ByteBuffer;
2324
import java.nio.charset.StandardCharsets;
2425
import java.util.HashMap;
@@ -266,7 +267,14 @@ static MessageIdV5 fromByteArray(byte[] data) throws IOException {
266267
if (data == null || data.length < 12) {
267268
throw new IOException("Invalid MessageIdV5 data: too short");
268269
}
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 {
270278
long segmentId = buf.getLong();
271279
int v4Length = buf.getInt();
272280
if (v4Length < 0 || v4Length > buf.remaining()) {
@@ -286,6 +294,9 @@ static MessageIdV5 fromByteArray(byte[] data) throws IOException {
286294
String parentTopic = null;
287295
if (buf.hasRemaining()) {
288296
int parentLen = buf.getInt();
297+
if (parentLen < -1) {
298+
throw new IOException("Invalid MessageIdV5 data: bad parent-topic length");
299+
}
289300
if (parentLen >= 0) {
290301
if (parentLen > buf.remaining()) {
291302
throw new IOException("Invalid MessageIdV5 data: bad parent-topic length");
@@ -300,10 +311,16 @@ static MessageIdV5 fromByteArray(byte[] data) throws IOException {
300311
Map<String, Map<Long, MessageId>> multiTopic = null;
301312
if (buf.hasRemaining()) {
302313
int topicCount = buf.getInt();
314+
if (topicCount < -1) {
315+
throw new IOException("Invalid MessageIdV5 data: bad topic count");
316+
}
303317
if (topicCount >= 0) {
304-
multiTopic = new HashMap<>(topicCount);
318+
multiTopic = new HashMap<>();
305319
for (int i = 0; i < topicCount; i++) {
306320
int topicLen = buf.getInt();
321+
if (topicLen < 0 || topicLen > buf.remaining()) {
322+
throw new IOException("Invalid MessageIdV5 data: bad topic length");
323+
}
307324
byte[] topicBytes = new byte[topicLen];
308325
buf.get(topicBytes);
309326
String topic = new String(topicBytes, StandardCharsets.UTF_8);
@@ -318,7 +335,10 @@ static MessageIdV5 fromByteArray(byte[] data) throws IOException {
318335

319336
private static Map<Long, MessageId> readSegmentVector(ByteBuffer buf) throws IOException {
320337
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<>();
322342
for (int i = 0; i < count; i++) {
323343
long segId = buf.getLong();
324344
int idLen = buf.getInt();
Collapse file

‎pulsar-client-v5/src/test/java/org/apache/pulsar/client/impl/v5/CheckpointV5Test.java‎

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 numberDiff line numberDiff line change
@@ -22,10 +22,18 @@
2222
import static org.testng.Assert.assertSame;
2323
import static org.testng.Assert.assertThrows;
2424
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;
2531
import java.util.Map;
2632
import org.apache.pulsar.client.api.MessageId;
2733
import org.apache.pulsar.client.api.v5.Checkpoint;
2834
import org.apache.pulsar.client.impl.MessageIdImpl;
35+
import org.apache.pulsar.common.api.proto.MessageIdData;
36+
import org.testng.annotations.DataProvider;
2937
import org.testng.annotations.Test;
3038

3139
public class CheckpointV5Test {
@@ -112,4 +120,37 @@ public void testFromByteArrayRejectsUnknownType() {
112120
assertThrows(java.io.IOException.class,
113121
() -> CheckpointV5.fromByteArray(new byte[]{127, 0, 0, 0, 0, 0, 0, 0, 0}));
114122
}
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+
}
115156
}
Collapse file

‎pulsar-client-v5/src/test/java/org/apache/pulsar/client/impl/v5/MessageIdV5Test.java‎

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 numberDiff line numberDiff line change
@@ -21,12 +21,23 @@
2121
import static org.testng.Assert.assertEquals;
2222
import static org.testng.Assert.assertFalse;
2323
import static org.testng.Assert.assertNotNull;
24+
import static org.testng.Assert.assertNull;
2425
import static org.testng.Assert.assertThrows;
2526
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;
2635
import java.util.HashMap;
2736
import java.util.Map;
2837
import org.apache.pulsar.client.api.MessageId;
2938
import org.apache.pulsar.client.impl.MessageIdImpl;
39+
import org.apache.pulsar.common.api.proto.MessageIdData;
40+
import org.testng.annotations.DataProvider;
3041
import org.testng.annotations.Test;
3142

3243
public class MessageIdV5Test {
@@ -241,6 +252,88 @@ public void testFromByteArrayRejectsGarbage() {
241252
() -> MessageIdV5.fromByteArray(buf.array()));
242253
}
243254

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+
244337
// --- sentinels ---
245338

246339
@Test
Collapse file

‎pulsar-client/src/main/java/org/apache/pulsar/client/impl/MessageIdImpl.java‎

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 numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.netty.buffer.Unpooled;
2323
import io.netty.util.concurrent.FastThreadLocal;
2424
import java.io.IOException;
25+
import java.util.BitSet;
2526
import java.util.Objects;
2627
import org.apache.pulsar.client.api.MessageId;
2728
import org.apache.pulsar.client.api.MessageIdAdv;
@@ -106,7 +107,7 @@ public static MessageId fromByteArray(byte[] data) throws IOException {
106107
if (idData.hasBatchIndex()) {
107108
if (idData.hasBatchSize()) {
108109
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));
110111
} else {
111112
messageId = new BatchMessageIdImpl(idData.getLedgerId(), idData.getEntryId(), idData.getPartition(),
112113
idData.getBatchIndex());
@@ -141,8 +142,7 @@ public static MessageId fromByteArrayWithTopic(byte[] data, TopicName topicName)
141142
if (idData.hasBatchIndex()) {
142143
if (idData.hasBatchSize()) {
143144
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));
146146
} else {
147147
messageId = new BatchMessageIdImpl(idData.getLedgerId(), idData.getEntryId(), idData.getPartition(),
148148
idData.getBatchIndex(), 0, null);
@@ -158,6 +158,14 @@ public static MessageId fromByteArrayWithTopic(byte[] data, TopicName topicName)
158158
return messageId;
159159
}
160160

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+
161169
protected MessageIdData writeMessageIdData(MessageIdData msgId, int batchIndex, int batchSize) {
162170
if (msgId == null) {
163171
msgId = LOCAL_MESSAGE_ID.get()
Collapse file

‎pulsar-client/src/test/java/org/apache/pulsar/client/impl/MessageIdSerializationTest.java‎

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 numberDiff line numberDiff line change
@@ -19,8 +19,13 @@
1919
package org.apache.pulsar.client.impl;
2020

2121
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;
2226
import java.io.IOException;
2327
import org.apache.pulsar.client.api.MessageId;
28+
import org.apache.pulsar.common.api.proto.MessageIdData;
2429
import org.testng.annotations.Test;
2530

2631
public class MessageIdSerializationTest {
@@ -58,4 +63,18 @@ public void testProtobufSerializationNull() throws Exception {
5863
void testProtobufSerializationEmpty() throws Exception {
5964
MessageId.fromByteArray(new byte[0]);
6065
}
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+
}
6180
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.