-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MINOR: Add more validation during KRPC deserialization
When deserializing KRPC (which is used for RPCs sent to Kafka, Kafka Metadata records, and some other things), check that we have at least N bytes remaining before allocating an array of size N. Remove DataInputStreamReadable since it was hard to make this class aware of how many bytes were remaining. Instead, when reading an individual record in the Raft layer, simply create a ByteBufferAccessor with a ByteBuffer containing just the bytes we're interested in. Add SimpleArraysMessageTest and ByteBufferAccessorTest. Also add some additional tests in RequestResponseTest. Reviewers: Tom Bentley <[email protected]>, Mickael Maison <[email protected]>, Colin McCabe <[email protected]> Co-authored-by: Colin McCabe <[email protected]> Co-authored-by: Manikumar Reddy <[email protected]> Co-authored-by: Mickael Maison <[email protected]>
- Loading branch information
1 parent
967b89f
commit aaceb6b
Showing
16 changed files
with
434 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 0 additions & 139 deletions
139
clients/src/main/java/org/apache/kafka/common/protocol/DataInputStreamReadable.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
clients/src/test/java/org/apache/kafka/common/message/SimpleArraysMessageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.kafka.common.message; | ||
|
||
import org.apache.kafka.common.protocol.ByteBufferAccessor; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class SimpleArraysMessageTest { | ||
@Test | ||
public void testArrayBoundsChecking() { | ||
// SimpleArraysMessageData takes 2 arrays | ||
final ByteBuffer buf = ByteBuffer.wrap(new byte[] { | ||
(byte) 0x7f, // Set size of first array to 126 which is larger than the size of this buffer | ||
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 | ||
}); | ||
final SimpleArraysMessageData out = new SimpleArraysMessageData(); | ||
ByteBufferAccessor accessor = new ByteBufferAccessor(buf); | ||
assertEquals("Tried to allocate a collection of size 126, but there are only 7 bytes remaining.", | ||
assertThrows(RuntimeException.class, () -> out.read(accessor, (short) 2)).getMessage()); | ||
} | ||
|
||
@Test | ||
public void testArrayBoundsCheckingOtherArray() { | ||
// SimpleArraysMessageData takes 2 arrays | ||
final ByteBuffer buf = ByteBuffer.wrap(new byte[] { | ||
(byte) 0x01, // Set size of first array to 0 | ||
(byte) 0x7e, // Set size of second array to 125 which is larger than the size of this buffer | ||
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 | ||
}); | ||
final SimpleArraysMessageData out = new SimpleArraysMessageData(); | ||
ByteBufferAccessor accessor = new ByteBufferAccessor(buf); | ||
assertEquals("Tried to allocate a collection of size 125, but there are only 6 bytes remaining.", | ||
assertThrows(RuntimeException.class, () -> out.read(accessor, (short) 2)).getMessage()); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
clients/src/test/java/org/apache/kafka/common/protocol/ByteBufferAccessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.kafka.common.protocol; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class ByteBufferAccessorTest { | ||
@Test | ||
public void testReadArray() { | ||
ByteBuffer buf = ByteBuffer.allocate(1024); | ||
ByteBufferAccessor accessor = new ByteBufferAccessor(buf); | ||
final byte[] testArray = new byte[] {0x4b, 0x61, 0x46}; | ||
accessor.writeByteArray(testArray); | ||
accessor.writeInt(12345); | ||
accessor.flip(); | ||
final byte[] testArray2 = accessor.readArray(3); | ||
assertArrayEquals(testArray, testArray2); | ||
assertEquals(12345, accessor.readInt()); | ||
assertEquals("Error reading byte array of 3 byte(s): only 0 byte(s) available", | ||
assertThrows(RuntimeException.class, | ||
() -> accessor.readArray(3)).getMessage()); | ||
} | ||
|
||
@Test | ||
public void testReadString() { | ||
ByteBuffer buf = ByteBuffer.allocate(1024); | ||
ByteBufferAccessor accessor = new ByteBufferAccessor(buf); | ||
String testString = "ABC"; | ||
final byte[] testArray = testString.getBytes(StandardCharsets.UTF_8); | ||
accessor.writeByteArray(testArray); | ||
accessor.flip(); | ||
assertEquals("ABC", accessor.readString(3)); | ||
assertEquals("Error reading byte array of 2 byte(s): only 0 byte(s) available", | ||
assertThrows(RuntimeException.class, | ||
() -> accessor.readString(2)).getMessage()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.