Skip to content

Commit 170cbaa

Browse files
authored
Fix for 451: Add negative index checking for token retrieval (#452)
1 parent 973b3dc commit 170cbaa

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORParser.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -3183,7 +3183,6 @@ protected void _skipIncomplete() throws IOException
31833183
_throwInternal();
31843184
}
31853185
final int lowBits = _typeByte & 0x1F;
3186-
31873186
if (lowBits <= 23) {
31883187
if (lowBits > 0) {
31893188
_skipBytes(lowBits);
@@ -3224,8 +3223,8 @@ protected void _skipChunked(int expectedType) throws IOException
32243223
// verify that type matches
32253224
int type = (ch >> 5);
32263225
if (type != expectedType) {
3227-
throw _constructError("Mismatched chunk in chunked content: expected "+expectedType
3228-
+" but encountered "+type);
3226+
throw _constructReadException(
3227+
"Mismatched chunk in chunked content: expected %d but encountered %s", expectedType, type);
32293228
}
32303229

32313230
final int lowBits = ch & 0x1F;
@@ -3251,7 +3250,7 @@ protected void _skipChunked(int expectedType) throws IOException
32513250
break;
32523251
case 31:
32533252
throw _constructReadException(
3254-
"Illegal chunked-length indicator within chunked-length value (type %d)",
3253+
"Invalid chunked-length indicator within chunked-length value (type %d)",
32553254
expectedType);
32563255
default:
32573256
_invalidToken(_typeByte);
@@ -3261,6 +3260,11 @@ protected void _skipChunked(int expectedType) throws IOException
32613260

32623261
protected void _skipBytesL(long llen) throws IOException
32633262
{
3263+
if (llen < 0L) {
3264+
throw _constructReadException(
3265+
"Corrupt content: invalid length indicator (%d) encountered during skipping, current token: %s",
3266+
llen, currentToken());
3267+
}
32643268
while (llen > MAX_INT_L) {
32653269
_skipBytes((int) MAX_INT_L);
32663270
llen -= MAX_INT_L;
@@ -3270,6 +3274,11 @@ protected void _skipBytesL(long llen) throws IOException
32703274

32713275
protected void _skipBytes(int len) throws IOException
32723276
{
3277+
if (len < 0) {
3278+
throw _constructReadException(
3279+
"Corrupt content: invalid length indicator (%d) encountered during skipping, current token: %s",
3280+
len, currentToken());
3281+
}
32733282
while (true) {
32743283
int toAdd = Math.min(len, _inputEnd - _inputPtr);
32753284
_inputPtr += toAdd;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fasterxml.jackson.dataformat.cbor.fuzz;
2+
3+
import com.fasterxml.jackson.core.JsonParser;
4+
import com.fasterxml.jackson.core.JsonToken;
5+
import com.fasterxml.jackson.core.exc.StreamReadException;
6+
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
9+
import com.fasterxml.jackson.dataformat.cbor.CBORTestBase;
10+
11+
public class CBORFuzz451_65617_IOOBETest extends CBORTestBase
12+
{
13+
private final ObjectMapper MAPPER = cborMapper();
14+
15+
public void testInvalidText() throws Exception
16+
{
17+
final byte[] input = readResource("/data/clusterfuzz-cbor-65617.cbor");
18+
try (JsonParser p = MAPPER.createParser(input)) {
19+
try {
20+
assertToken(JsonToken.VALUE_STRING, p.nextToken());
21+
// Important: do not access String, force skipping
22+
p.nextToken();
23+
fail("Should not reach here (invalid input)");
24+
} catch (StreamReadException e) {
25+
verifyException(e, "Invalid length indicator");
26+
}
27+
}
28+
}
29+
}
Binary file not shown.

release-notes/CREDITS-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,5 @@ Arthur Chan (@arthurscchan)
301301
* Contributed #449: (avro) `IndexOutOfBoundsException` in `JacksonAvroParserImpl`
302302
for invalid input
303303
(2.17.0)
304+
* Contributed #451: (cbor) `IndexOutOfBoundsException` in `CBORParser` for invalid input
305+
(2.17.0)

release-notes/VERSION-2.x

+5-3
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ Active maintainers:
2828
(fix contributed by Arthur C)-(
2929
#432: (ion) More methods from `IonReader` could throw an unexpected `AssertionError`
3030
(fix contributed by Arthur C)
31-
#434 (ion) Unexpected `NullPointerException` thrown from `IonParser::getNumberType()`
31+
#434: (ion) Unexpected `NullPointerException` thrown from `IonParser::getNumberType()`
3232
(fix contributed by Arthur C)
33-
#437 (ion) `IonReader.next()` throws NPEs for some invalid content
34-
#449 (avro) `IndexOutOfBoundsException` in `JacksonAvroParserImpl` for invalid input
33+
#437: (ion) `IonReader.next()` throws NPEs for some invalid content
34+
#449: (avro) `IndexOutOfBoundsException` in `JacksonAvroParserImpl` for invalid input
35+
(fix contributed by Arthur C)
36+
#451: (cbor) `IndexOutOfBoundsException` in `CBORParser` for invalid input
3537
(fix contributed by Arthur C)
3638
- (ion) Update `com.amazon.ion:ion-java` to 1.11.0 (from 1.10.5)
3739

0 commit comments

Comments
 (0)