Skip to content

Commit 7e77bbe

Browse files
authored
Change JsonReader and JsonSerializer default max depth to 128 (#2462)
1 parent 42139ea commit 7e77bbe

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7995,5 +7995,46 @@ public void NullableDoubleEmptyValue()
79957995
() => JsonConvert.DeserializeObject<EmptyJsonValueTestClass>("{ A: \"\", B: 1, C: 123, D: 1.23, E: , F: null }"),
79967996
"Unexpected character encountered while parsing value: ,. Path 'E', line 1, position 36.");
79977997
}
7998+
7999+
[Test]
8000+
public void SetMaxDepth_DepthExceeded()
8001+
{
8002+
JsonTextReader reader = new JsonTextReader(new StringReader("[[['text']]]"));
8003+
Assert.AreEqual(128, reader.MaxDepth);
8004+
8005+
JsonSerializerSettings settings = new JsonSerializerSettings();
8006+
Assert.AreEqual(128, settings.MaxDepth);
8007+
Assert.AreEqual(false, settings._maxDepthSet);
8008+
8009+
// Default should be the same
8010+
Assert.AreEqual(reader.MaxDepth, settings.MaxDepth);
8011+
8012+
settings.MaxDepth = 2;
8013+
Assert.AreEqual(2, settings.MaxDepth);
8014+
Assert.AreEqual(true, settings._maxDepthSet);
8015+
8016+
JsonSerializer serializer = JsonSerializer.Create(settings);
8017+
Assert.AreEqual(2, serializer.MaxDepth);
8018+
8019+
ExceptionAssert.Throws<JsonReaderException>(
8020+
() => serializer.Deserialize(reader),
8021+
"The reader's MaxDepth of 2 has been exceeded. Path '[0][0]', line 1, position 3.");
8022+
}
8023+
8024+
[Test]
8025+
public void SetMaxDepth_DepthNotExceeded()
8026+
{
8027+
JsonTextReader reader = new JsonTextReader(new StringReader("['text']"));
8028+
JsonSerializerSettings settings = new JsonSerializerSettings();
8029+
8030+
settings.MaxDepth = 2;
8031+
8032+
JsonSerializer serializer = JsonSerializer.Create(settings);
8033+
Assert.AreEqual(2, serializer.MaxDepth);
8034+
8035+
serializer.Deserialize(reader);
8036+
8037+
Assert.AreEqual(128, reader.MaxDepth);
8038+
}
79988039
}
79998040
}

Src/Newtonsoft.Json/JsonReader.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ public string? DateFormatString
227227

228228
/// <summary>
229229
/// Gets or sets the maximum depth allowed when reading JSON. Reading past this depth will throw a <see cref="JsonReaderException"/>.
230+
/// A null value means there is no maximum.
231+
/// The default value is <c>128</c>.
230232
/// </summary>
231233
public int? MaxDepth
232234
{
@@ -327,6 +329,7 @@ protected JsonReader()
327329
_dateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind;
328330
_dateParseHandling = DateParseHandling.DateTime;
329331
_floatParseHandling = FloatParseHandling.Double;
332+
_maxDepth = 128;
330333

331334
CloseInput = true;
332335
}

Src/Newtonsoft.Json/JsonSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ public virtual CultureInfo Culture
514514
/// <summary>
515515
/// Gets or sets the maximum depth allowed when reading JSON. Reading past this depth will throw a <see cref="JsonReaderException"/>.
516516
/// A null value means there is no maximum.
517-
/// The default value is <c>null</c>.
517+
/// The default value is <c>128</c>.
518518
/// </summary>
519519
public virtual int? MaxDepth
520520
{

Src/Newtonsoft.Json/JsonSerializerSettings.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public class JsonSerializerSettings
6161
internal static readonly CultureInfo DefaultCulture;
6262
internal const bool DefaultCheckAdditionalContent = false;
6363
internal const string DefaultDateFormatString = @"yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK";
64+
internal const int DefaultMaxDepth = 128;
6465

6566
internal Formatting? _formatting;
6667
internal DateFormatHandling? _dateFormatHandling;
@@ -325,11 +326,11 @@ public string DateFormatString
325326
/// <summary>
326327
/// Gets or sets the maximum depth allowed when reading JSON. Reading past this depth will throw a <see cref="JsonReaderException"/>.
327328
/// A null value means there is no maximum.
328-
/// The default value is <c>null</c>.
329+
/// The default value is <c>128</c>.
329330
/// </summary>
330331
public int? MaxDepth
331332
{
332-
get => _maxDepth;
333+
get => _maxDepthSet ? _maxDepth : DefaultMaxDepth;
333334
set
334335
{
335336
if (value <= 0)

0 commit comments

Comments
 (0)