Skip to content

Commit b2f629d

Browse files
joerg1985diemol
andauthored
[java] Fail with JsonException when JsonOutput.MAX_DEPTH is reached (#12056)
Fail with JsonException when JsonOutput.MAX_DEPTH is reached Co-authored-by: Diego Molina <[email protected]>
1 parent 1bfcb4a commit b2f629d

2 files changed

Lines changed: 40 additions & 9 deletions

File tree

java/src/org/openqa/selenium/json/JsonOutput.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ public class JsonOutput implements Closeable {
174174
builder.put(
175175
Collection.class::isAssignableFrom,
176176
(obj, depth) -> {
177+
if (depth < 1) {
178+
throw new JsonException("Reached the maximum depth of " + MAX_DEPTH + " while writing JSON");
179+
}
177180
beginArray();
178181
((Collection<?>) obj)
179182
.stream()
@@ -185,6 +188,9 @@ public class JsonOutput implements Closeable {
185188
builder.put(
186189
Map.class::isAssignableFrom,
187190
(obj, depth) -> {
191+
if (depth < 1) {
192+
throw new JsonException("Reached the maximum depth of " + MAX_DEPTH + " while writing JSON");
193+
}
188194
beginObject();
189195
((Map<?, ?>) obj)
190196
.forEach(
@@ -199,6 +205,9 @@ public class JsonOutput implements Closeable {
199205
builder.put(
200206
Class::isArray,
201207
(obj, depth) -> {
208+
if (depth < 1) {
209+
throw new JsonException("Reached the maximum depth of " + MAX_DEPTH + " while writing JSON");
210+
}
202211
beginArray();
203212
Stream.of((Object[]) obj)
204213
.filter(o -> (!(o instanceof Optional) || ((Optional<?>) o).isPresent()))
@@ -219,7 +228,12 @@ public class JsonOutput implements Closeable {
219228
});
220229

221230
// Finally, attempt to convert as an object
222-
builder.put(cls -> true, (obj, depth) -> mapObject(obj, depth - 1));
231+
builder.put(cls -> true, (obj, depth) -> {
232+
if (depth < 1) {
233+
throw new JsonException("Reached the maximum depth of " + MAX_DEPTH + " while writing JSON");
234+
}
235+
mapObject(obj, depth - 1);
236+
});
223237

224238
this.converters = Collections.unmodifiableMap(builder);
225239
}
@@ -376,12 +390,7 @@ private JsonOutput convertUsingMethod(String methodName, Object toConvert, int d
376390
}
377391
}
378392

379-
private void mapObject(Object toConvert, int maxDepth) {
380-
if (maxDepth < 1) {
381-
append("null");
382-
return;
383-
}
384-
393+
private void mapObject(Object toConvert, int depthRemaining) {
385394
if (toConvert instanceof Class) {
386395
write(((Class<?>) toConvert).getName());
387396
return;
@@ -405,7 +414,7 @@ private void mapObject(Object toConvert, int maxDepth) {
405414
Object value = pd.getReadMethod().apply(toConvert);
406415
if (!Optional.empty().equals(value)) {
407416
name(pd.getName());
408-
write(value, maxDepth - 1);
417+
write(value, depthRemaining - 1);
409418
}
410419
}
411420
endObject();

java/test/org/openqa/selenium/json/JsonOutputTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static java.util.Arrays.asList;
2323
import static java.util.Collections.emptyList;
2424
import static java.util.Collections.emptyMap;
25+
import static java.util.Collections.singletonList;
2526
import static java.util.concurrent.TimeUnit.MILLISECONDS;
2627
import static org.assertj.core.api.Assertions.assertThat;
2728
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -37,7 +38,6 @@
3738
import com.google.gson.JsonObject;
3839
import com.google.gson.JsonParser;
3940
import com.google.gson.JsonPrimitive;
40-
import java.awt.*;
4141
import java.io.IOException;
4242
import java.io.StringReader;
4343
import java.io.StringWriter;
@@ -62,6 +62,7 @@
6262
import org.openqa.selenium.ImmutableCapabilities;
6363
import org.openqa.selenium.MutableCapabilities;
6464
import org.openqa.selenium.Platform;
65+
import org.openqa.selenium.Point;
6566
import org.openqa.selenium.Proxy;
6667
import org.openqa.selenium.UnhandledAlertException;
6768
import org.openqa.selenium.WebDriverException;
@@ -717,6 +718,27 @@ public String getCheese() {
717718
assertThat(obj.get("cheese").getAsString()).isEqualTo("gouda");
718719
}
719720

721+
@Test
722+
void shouldRespectMaxDepth() {
723+
StringBuilder builder = new StringBuilder();
724+
725+
JsonOutput jsonOutput = new Json().newOutput(builder);
726+
jsonOutput.beginArray();
727+
728+
Object value = emptyList();
729+
730+
for (int i = 0; i < 10; i++) {
731+
jsonOutput.write(value);
732+
733+
value = singletonList(value);
734+
}
735+
736+
Object finalValue = value;
737+
738+
assertThatExceptionOfType(JsonException.class)
739+
.isThrownBy(() -> jsonOutput.write(finalValue));
740+
}
741+
720742
private String convert(Object toConvert) {
721743
try (Writer writer = new StringWriter();
722744
JsonOutput jsonOutput = new Json().newOutput(writer)) {

0 commit comments

Comments
 (0)