Skip to content

Commit b5e0eed

Browse files
nosanphilwebb
authored andcommitted
Protected against JsonValueWriter stack overflow
Add validation for the maximum JSON nesting depth in the JsonValueWriter. This helps prevent a StackOverflowError that can potentially occur due to excessive recursion when dealing with deeply nested JSON structures. See gh-44627 Signed-off-by: Dmytro Nosan <[email protected]>
1 parent b2e65f6 commit b5e0eed

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/JsonValueWriter.java

+23
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@
4747
*/
4848
class JsonValueWriter {
4949

50+
private static final int DEFAULT_MAX_NESTING_DEPTH = 1000;
51+
5052
private final Appendable out;
5153

54+
private final int maxNestingDepth;
55+
5256
private MemberPath path = MemberPath.ROOT;
5357

5458
private final Deque<JsonWriterFiltersAndProcessors> filtersAndProcessors = new ArrayDeque<>();
@@ -60,7 +64,18 @@ class JsonValueWriter {
6064
* @param out the {@link Appendable} used to receive the JSON output
6165
*/
6266
JsonValueWriter(Appendable out) {
67+
this(out, DEFAULT_MAX_NESTING_DEPTH);
68+
}
69+
70+
/**
71+
* Create a new {@link JsonValueWriter} instance.
72+
* @param out the {@link Appendable} used to receive the JSON output
73+
* @param maxNestingDepth the maximum allowed nesting depth for JSON objects and
74+
* arrays
75+
*/
76+
JsonValueWriter(Appendable out, int maxNestingDepth) {
6377
this.out = out;
78+
this.maxNestingDepth = maxNestingDepth;
6479
}
6580

6681
void pushProcessors(JsonWriterFiltersAndProcessors jsonProcessors) {
@@ -145,6 +160,7 @@ private <V> boolean canWriteAsArray(Iterable<?> iterable) {
145160
*/
146161
void start(Series series) {
147162
if (series != null) {
163+
validateNestingDepth();
148164
this.activeSeries.push(new ActiveSeries(series));
149165
append(series.openChar);
150166
}
@@ -272,6 +288,13 @@ private void writeString(Object value) {
272288
}
273289
}
274290

291+
private void validateNestingDepth() {
292+
if (this.activeSeries.size() > this.maxNestingDepth) {
293+
throw new IllegalStateException("JSON nesting depth (%s) exceeds maximum depth of %s (current path: %s)"
294+
.formatted(this.activeSeries.size(), this.maxNestingDepth, this.path));
295+
}
296+
}
297+
275298
private void append(String value) {
276299
try {
277300
this.out.append(value);

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/json/JsonValueWriterTests.java

+31
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.nio.file.Path;
21+
import java.util.ArrayList;
2122
import java.util.LinkedHashMap;
2223
import java.util.LinkedHashSet;
2324
import java.util.List;
@@ -253,6 +254,36 @@ void writeJavaNioPathShouldBeSerializedAsString() {
253254
.isEqualTo(quoted("a\\%1$sb\\%1$sc".formatted(File.separator)));
254255
}
255256

257+
@Test
258+
void illegalStateExceptionShouldBeThrownWhenCollectionExceededNestingDepth() {
259+
JsonValueWriter writer = new JsonValueWriter(new StringBuilder(), 128);
260+
List<Object> list = new ArrayList<>();
261+
list.add(list);
262+
assertThatIllegalStateException().isThrownBy(() -> writer.write(list))
263+
.withMessageStartingWith(
264+
"JSON nesting depth (129) exceeds maximum depth of 128 (current path: [0][0][0][0][0][0][0][0][0][0][0][0]");
265+
}
266+
267+
@Test
268+
void illegalStateExceptionShouldBeThrownWhenMapExceededNestingDepth() {
269+
JsonValueWriter writer = new JsonValueWriter(new StringBuilder(), 128);
270+
Map<String, Object> map = new LinkedHashMap<>();
271+
map.put("foo", Map.of("bar", map));
272+
assertThatIllegalStateException().isThrownBy(() -> writer.write(map))
273+
.withMessageStartingWith(
274+
"JSON nesting depth (129) exceeds maximum depth of 128 (current path: foo.bar.foo.bar.foo.bar.foo");
275+
}
276+
277+
@Test
278+
void illegalStateExceptionShouldBeThrownWhenIterableExceededNestingDepth() {
279+
JsonValueWriter writer = new JsonValueWriter(new StringBuilder(), 128);
280+
List<Object> list = new ArrayList<>();
281+
list.add(list);
282+
assertThatIllegalStateException().isThrownBy(() -> writer.write((Iterable<Object>) list::iterator))
283+
.withMessageStartingWith(
284+
"JSON nesting depth (129) exceeds maximum depth of 128 (current path: [0][0][0][0][0][0][0][0][0][0][0][0]");
285+
}
286+
256287
private <V> String write(V value) {
257288
return doWrite((valueWriter) -> valueWriter.write(value));
258289
}

0 commit comments

Comments
 (0)