Repository navigation
fix: bound jsonb string length to remaining input #7867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
119 changes: 119 additions & 0 deletions
119
core/src/test/java/com/alibaba/fastjson2/JSONBStringLengthTest.java
This file contains hidden or 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,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 { | ||
|
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)); | ||
|
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)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.