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
6 changes: 6 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ public Map<String, Object> readObject() {
len = valueType - BC_ARRAY_FIX_MIN;
}

checkLength(len, offset, end, "array length");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] The array-length guard is spelled two ways in this PR: the checkArrayLen wrapper (body exactly checkLength(len, offset, end, "array length") plus the 1-byte-per-element invariant comment) is used only at the two startArray() sites, while the three inline sites this PR adds (here, ~932 in readAny(), ~1105 in readArray()) bypass the wrapper and call checkLength directly with the identical label. startArray() measures 136 bytes at target 8 either way — the dual spelling buys no bytecode-size benefit. — Failure scenario: a future change to the array-length policy or message (e.g. an absolute cap like readLength()'s 256M limit) must find two spellings to stay consistent, and the invariant rationale comment is trapped in a wrapper three of the five array sites never route through — the same drift this PR exists to eliminate for checkBigintLen.

Suggested change
checkLength(len, offset, end, "array length");
checkArrayLen(len, offset, end);

(same change at the other two inline sites, ~932 and ~1105 — or delete the wrapper and keep five direct calls)

— qwen3.8-max via Qwen Code /review (v0.21.10)


if (len == 0) {
if ((features & Feature.UseNativeObject.mask) != 0) {
value = new ArrayList<>();
Expand Down Expand Up @@ -927,6 +929,8 @@ public Object readAny() {
? readLength()
: type - BC_ARRAY_FIX_MIN;

checkLength(len, offset, end, "array length");

if (len == 0) {
if ((context.features & Feature.UseNativeObject.mask) != 0) {
return new ArrayList<>();
Expand Down Expand Up @@ -1098,6 +1102,8 @@ public List readArray() {
? readLength()
: valueType - BC_ARRAY_FIX_MIN;

checkLength(len, offset, end, "array length");

if (len == 0) {
if ((context.features & Feature.UseNativeObject.mask) != 0) {
value = new ArrayList<>();
Expand Down
79 changes: 79 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/issues/Issue7669.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.junit.jupiter.api.Test;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -189,4 +191,81 @@ public void testArrayDeclaredLengthNotAtBufferEnd() {
assertThrows(JSONException.class,
() -> JSONB.parseObject(NESTED_ARRAY_BAD_LEN, int[][].class));
}

// --- R2-1: the same declared-length pre-allocation OOM class, but through the
// untyped/nested BC_ARRAY decode paths that never call startArray():
// readAny() (default branch), readObject() (map-value branch), readArray()
// (element branch). They read the length via readLength() and pre-allocate
// new JSONArray(len)/new ArrayList(len) with no remaining-buffer check.
// This change folds the same checkLength guard into those three inline sites.

// 0xA4 = BC_ARRAY, 0x48 = BC_INT32, 0x0FFFFFFF = 268,435,455 elements
static final byte[] UNTYPED_ARRAY_MAX_LEN = {
(byte) 0xA4,
(byte) 0x48,
(byte) 0x0F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
};

/**
* Corrupt the first BC_ARRAY (0xA4) + BC_INT32 (0x48) length field found in
* the payload to a huge value, so the declared length vastly exceeds the
* remaining buffer. Used to exercise the untyped/nested readLength() sites.
*/
private static void corruptFirstArrayLength(byte[] bytes) {
for (int i = 0; i + 5 < bytes.length; i++) {
if ((bytes[i] & 0xFF) == 0xA4) { // BC_ARRAY
// Rewrite the array's leading byte as BC_INT32 (0x48) so the inline
// readLength() site treats it as a 4-byte length-type, then overwrite
// those 4 bytes with 0x0FFFFFFF (268,435,455) — far larger than the
// remaining buffer — to exercise the checkLength guard placed at the
// inline readLength() decode site (readObject() map-value branch).
// The corrupt target is the inner array value, not the (BC_OBJECT)
// root, because the root is never encoded as BC_ARRAY.
bytes[i + 1] = (byte) 0x48; // BC_INT32
bytes[i + 2] = (byte) 0x0F;
bytes[i + 3] = (byte) 0xFF;
bytes[i + 4] = (byte) 0xFF;
bytes[i + 5] = (byte) 0xFF;
return;
}
}
throw new IllegalStateException("BC_ARRAY not found in payload");
}

@Test
public void testUntypedArrayDeclaredLengthOOM_readAny() {
// JSONB.parse with no target type dispatches to readAny(); its BC_ARRAY
// branch reads the length via readLength() and pre-allocates
// new JSONArray(len) — now guarded by checkLength.
assertThrows(JSONException.class, () -> JSONB.parse(UNTYPED_ARRAY_MAX_LEN));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] The type-only assertThrows(JSONException.class, ...) does not distinguish the new checkLength rejection from a later generic failure. Empirically reproduced: with the inline guards stripped, new JSONArray(0x0FFFFFFF) (~1 GB) still pre-allocates and a downstream JSONException (readAny overflow : 6/6 at JSONReaderJSONB.java:550) still satisfies this assertion — the test passes with the fix removed. — Failure scenario: on any CI heap large enough to absorb the ~1 GB attacker-controlled pre-allocation, a regression of this guard passes the suite silently (or surfaces as a flaky OOM on small-heap forks) — the fix rots while the tests claim to pin it.

Fix — pin the guard's own message, which no downstream failure produces (same change for the _readObject test; needs import static org.junit.jupiter.api.Assertions.assertTrue;):

JSONException ex = assertThrows(JSONException.class, () -> JSONB.parse(UNTYPED_ARRAY_MAX_LEN));
assertTrue(ex.getMessage().contains("array length out of range"));

— qwen3.8-max via Qwen Code /review (v0.21.10)

}

@Test
public void testUntypedArrayDeclaredLengthOOM_readObject() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] This test never exercises the readObject() map-value BC_ARRAY guard (JSONReaderJSONB.java:407). JSONB.parseObject(bytes, Object.class) dispatches through readAny() (the Object.class shortcut), so the corrupted array is rejected by the readAny guard at line 932 — the same site the _readAny test already covers. A bytecode-verified deletion probe confirmed it: removing the line-407 guard leaves this test green, so the readObject() guard ships with zero regression coverage. — Failure scenario: a future refactor that drops the checkLength at line 407 revives the declared-length pre-allocation OOM (~1 GB new JSONArray from a 6-byte payload) on readObject()'s map-value path (reachable via the untyped JSONB.parseObject(bytes) overload) while this suite stays green.

Fix — route through readObject() via the untyped overload:

assertThrows(JSONException.class, () -> JSONB.parseObject(bytes));

(keep the Object.class variant too if desired, and update the "readObject() takes the map-value BC_ARRAY branch" comment, which describes the opposite of what happens)

— qwen3.8-max via Qwen Code /review (v0.21.10)

// A map whose value is an int[] (32 elems -> BC_ARRAY + BC_INT32 length).
// Corrupt the declared length, then parse as Object.class so readObject()
// takes the map-value BC_ARRAY branch (readLength).
Map<String, Object> m = new HashMap<>();
m.put("k", new int[32]);
byte[] bytes = JSONB.toBytes(m);
corruptFirstArrayLength(bytes);
assertThrows(JSONException.class, () -> JSONB.parseObject(bytes, Object.class));
}

@Test
public void testUntypedArrayDeclaredLengthOOM_readArray() {
// Top-level JSONB.parseArray dispatches to readArray(); a nested element
// that is itself a BC_ARRAY must have its declared length (read via the
// inline readLength() at the element branch, line ~1099) rejected by
// checkLength.
// 0xA4 = BC_ARRAY (outer, 1 element), 0x01 = outer declared length,
// 0xA4 = BC_ARRAY (inner element), 0x48 = BC_INT32 length-type,
// 0x0FFFFFFF = declares 268,435,455 inner elements (exceeds buffer).
byte[] bytes = {
(byte) 0xA4, 0x01,
(byte) 0xA4, (byte) 0x48,
(byte) 0x0F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
};
assertThrows(JSONException.class, () -> JSONB.parseArray(bytes));
}
}
Loading