Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public String getString() {
if (strlen < 0) {
return symbolTable.getName(-strlen);
}
checkStrLen(strlen, strBegin, end);

Charset charset;
if (strtype == BC_STR_ASCII) {
Expand Down Expand Up @@ -604,6 +605,7 @@ public Object readAny() {
}
case BC_STR_UTF8: {
int strlen = readLength();
checkStrLen(strlen, offset, end);

if (STRING_CREATOR_JDK11 != null && !JDKUtils.BIG_ENDIAN) {
if (valueBytes == null) {
Expand Down Expand Up @@ -634,12 +636,14 @@ public Object readAny() {
}
case BC_STR_UTF16: {
int strlen = readLength();
checkStrLen(strlen, offset, end);
String str = new String(bytes, offset, strlen, StandardCharsets.UTF_16);
offset += strlen;
return str;
}
case BC_STR_UTF16LE: {
int strlen = readLength();
checkStrLen(strlen, offset, end);

String str;
if (STRING_CREATOR_JDK11 != null && !JDKUtils.BIG_ENDIAN) {
Expand All @@ -655,6 +659,7 @@ public Object readAny() {
}
case BC_STR_UTF16BE: {
int strlen = readLength();
checkStrLen(strlen, offset, end);

String str;
if (STRING_CREATOR_JDK11 != null && JDKUtils.BIG_ENDIAN) {
Expand All @@ -673,6 +678,7 @@ public Object readAny() {
GB18030 = Charset.forName("GB18030");
}
int strlen = readLength();
checkStrLen(strlen, offset, end);
String str = new String(bytes, offset, strlen, GB18030);
offset += strlen;
return str;
Expand Down Expand Up @@ -959,6 +965,7 @@ public Object readAny() {
if (strlen < 0) {
return symbolTable.getName(-strlen);
}
checkStrLen(strlen, offset, end);

if (STRING_CREATOR_JDK8 != null) {
String str = latin1StringJDK8(bytes, offset, strlen);
Expand Down Expand Up @@ -2638,6 +2645,7 @@ public String readFieldName() {
if (strtype == BC_STR_ASCII) {
strlen = readLength();
strBegin = offset;
checkStrLen(strlen, offset, end);
} else {
final int offset = this.offset;
strlen = strtype - BC_STR_ASCII_FIX_MIN;
Comment thread
wenshao marked this conversation as resolved.
Resolved
Expand Down Expand Up @@ -2785,6 +2793,7 @@ public String readFieldName() {
} else if (strtype == BC_STR_UTF8) {
strlen = readLength();
strBegin = offset;
checkStrLen(strlen, offset, end);

if (STRING_CREATOR_JDK11 != null && !JDKUtils.BIG_ENDIAN) {
if (valueBytes == null) {
Expand Down Expand Up @@ -2812,10 +2821,12 @@ public String readFieldName() {
} else if (strtype == BC_STR_UTF16) {
strlen = readLength();
strBegin = offset;
checkStrLen(strlen, offset, end);
charset = StandardCharsets.UTF_16;
} else if (strtype == BC_STR_UTF16LE) {
strlen = readLength();
strBegin = offset;
checkStrLen(strlen, offset, end);

if (STRING_CREATOR_JDK11 != null && !JDKUtils.BIG_ENDIAN) {
byte[] chars = new byte[strlen];
Expand All @@ -2828,6 +2839,7 @@ public String readFieldName() {
} else if (strtype == BC_STR_UTF16BE) {
strlen = readLength();
strBegin = offset;
checkStrLen(strlen, offset, end);

if (STRING_CREATOR_JDK11 != null && JDKUtils.BIG_ENDIAN) {
byte[] chars = new byte[strlen];
Expand All @@ -2839,6 +2851,7 @@ public String readFieldName() {
charset = StandardCharsets.UTF_16BE;
} else if (strtype == BC_STR_GB18030) {
strlen = readLength();
checkStrLen(strlen, offset, end);

if (GB18030 == null) {
GB18030 = Charset.forName("GB18030");
Expand Down Expand Up @@ -2911,6 +2924,7 @@ public String readString() {

String str = null;
if (strlen >= 0) {
checkStrLen(strlen, offset, end);
if (STRING_CREATOR_JDK11 != null) {
byte[] chars = new byte[strlen];
System.arraycopy(bytes, offset, chars, 0, strlen);
Expand Down Expand Up @@ -2977,6 +2991,7 @@ private String readString(Charset charset) {
if (strlen < 0) {
return symbolTable.getName(-strlen);
}
checkStrLen(strlen, offset, end);

char[] chars = null;
if (JVM_VERSION == 8 && strtype == BC_STR_UTF8 && strlen < 8192) {
Expand Down Expand Up @@ -3012,6 +3027,7 @@ private String readString(Charset charset) {
private void readGB18030() {
strlen = readLength();
strBegin = offset;
checkStrLen(strlen, offset, end);

if (GB18030 == null) {
GB18030 = Charset.forName("GB18030");
Expand All @@ -3021,6 +3037,7 @@ private void readGB18030() {
private String readUTF16BE() {
strlen = readLength();
strBegin = offset;
checkStrLen(strlen, offset, end);

if (STRING_CREATOR_JDK11 != null && JDKUtils.BIG_ENDIAN) {
byte[] chars = new byte[strlen];
Expand Down Expand Up @@ -3054,6 +3071,7 @@ private String readUTF16LE() {
strlen = readLength();
}
strBegin = offset;
checkStrLen(strlen, offset, end);

if (strlen == 0) {
return "";
Expand Down Expand Up @@ -3089,6 +3107,7 @@ private String readStringUTF8() {
strlen = readLength();
}
strBegin = offset;
checkStrLen(strlen, offset, end);

if (STRING_CREATOR_JDK11 != null && !JDKUtils.BIG_ENDIAN) {
if (valueBytes == null) {
Expand Down Expand Up @@ -6256,6 +6275,14 @@ static void checkBigintLen(int len, int offset, int end) {
}
}

static void checkStrLen(int strlen, int offset, int end) {
Comment thread
wenshao marked this conversation as resolved.
Resolved
// negative length denotes a symbol reference and is handled by the caller; only reject a
// positive byte length that would read past the frame end into unrelated buffer bytes
if (strlen > end - offset) {
Comment thread
wenshao marked this conversation as resolved.
Resolved
throw new JSONException("string length out of range: " + strlen + ", available: " + (end - offset));
Comment thread
wenshao marked this conversation as resolved.
Resolved
}
}

static JSONException outOfBoundsCheckFromToIndex(int offset, int end) {
return new JSONException("offset overflow, offset " + offset + ", end " + end);
}
Expand Down
119 changes: 119 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/JSONBStringLengthTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.alibaba.fastjson2;

import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import static com.alibaba.fastjson2.JSONB.Constants.*;
import static org.junit.jupiter.api.Assertions.*;

/**
* A JSONB string frame carries its length on the wire. When the length exceeds the bytes that
* remain in the frame, JSONReaderJSONB used to copy that many bytes straight out of the backing
* array, so with the slice API (offset/length shorter than the array) the extra bytes came from
* whatever followed the frame - disclosing neighboring data. These tests pin the bound.
*/
public class JSONBStringLengthTest {
Comment thread
wenshao marked this conversation as resolved.
Resolved
// type + one-byte length + two content bytes belong to the frame; everything after is "neighbor"
private static byte[] frame(byte strType) {
byte[] buf = new byte[64];
buf[0] = strType;
buf[1] = 40; // declared length, far past the 2 content bytes left in the frame
Arrays.fill(buf, 2, buf.length, (byte) 'S'); // stand-in for adjacent buffer data
return buf;
}

private static final int FRAME_LEN = 4;

@Test
public void asciiOverLength() {
assertThrows(JSONException.class,
() -> JSONB.parseObject(frame(BC_STR_ASCII), 0, FRAME_LEN, String.class));
}

@Test
public void utf8OverLength() {
assertThrows(JSONException.class,
() -> JSONB.parseObject(frame(BC_STR_UTF8), 0, FRAME_LEN, String.class));
}

@Test
public void utf16OverLength() {
assertThrows(JSONException.class,
() -> JSONB.parseObject(frame(BC_STR_UTF16), 0, FRAME_LEN, String.class));
}

@Test
public void utf16LEOverLength() {
assertThrows(JSONException.class,
() -> JSONB.parseObject(frame(BC_STR_UTF16LE), 0, FRAME_LEN, String.class));
}

@Test
public void utf16BEOverLength() {
assertThrows(JSONException.class,
() -> JSONB.parseObject(frame(BC_STR_UTF16BE), 0, FRAME_LEN, String.class));
}

@Test
public void gb18030OverLength() {
assertThrows(JSONException.class,
() -> JSONB.parseObject(frame(BC_STR_GB18030), 0, FRAME_LEN, String.class));
}

@Test
public void fieldNameOverLength() {
// BC_OBJECT, then an over-length ASCII field name, then a neighbor region
byte[] buf = new byte[64];
buf[0] = BC_OBJECT;
buf[1] = BC_STR_ASCII;
buf[2] = 40;
Arrays.fill(buf, 3, buf.length, (byte) 'S');
assertThrows(JSONException.class, () -> JSONB.parseObject(buf, 0, 5, JSONObject.class));
Comment thread
wenshao marked this conversation as resolved.
Resolved
}

@Test
public void getStringOverLength() {
// readValueHashCode records the wire length; getString must not copy past the frame end
byte[] buf = new byte[64];
buf[0] = BC_STR_UTF16;
buf[1] = 40;
Arrays.fill(buf, 2, buf.length, (byte) 'S');
JSONReader reader = JSONReader.ofJSONB(buf, 0, FRAME_LEN);
reader.readValueHashCode();
assertThrows(JSONException.class, reader::getString);
}

@Test
public void exactLengthSliceParses() {
// a frame whose declared length exactly fits still parses through the slice API
byte[] buf = new byte[64];
buf[0] = BC_STR_ASCII;
buf[1] = 5;
byte[] hello = "hello".getBytes(StandardCharsets.US_ASCII);
System.arraycopy(hello, 0, buf, 2, hello.length);
assertEquals("hello", JSONB.parseObject(buf, 0, 7, String.class));
}

@Test
public void validStringsRoundTrip() {
String[] strings = {
"",
"hello",
"中文字符串",
"发展特色富民产业,扎扎实实把乡村振兴战略实施好"
};
for (String s : strings) {
assertEquals(s, JSONB.parseObject(JSONB.toBytes(s), String.class));
assertEquals(s, JSONB.parseObject(JSONB.toBytes(s, StandardCharsets.UTF_16), String.class));
}

StringBuilder buf = new StringBuilder();
for (int i = 0; i < 100_000; i++) {
buf.append((char) ('a' + (i % 26)));
}
String large = buf.toString();
assertEquals(large, JSONB.parseObject(JSONB.toBytes(large), String.class));
}
}
Loading