Skip to content

Commit

Permalink
Change JsonReader and JsonSerializer default max depth to 128 (#2462)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Jan 31, 2021
1 parent 42139ea commit 7e77bbe
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
41 changes: 41 additions & 0 deletions Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7995,5 +7995,46 @@ public void NullableDoubleEmptyValue()
() => JsonConvert.DeserializeObject<EmptyJsonValueTestClass>("{ A: \"\", B: 1, C: 123, D: 1.23, E: , F: null }"),
"Unexpected character encountered while parsing value: ,. Path 'E', line 1, position 36.");
}

[Test]
public void SetMaxDepth_DepthExceeded()
{
JsonTextReader reader = new JsonTextReader(new StringReader("[[['text']]]"));
Assert.AreEqual(128, reader.MaxDepth);

JsonSerializerSettings settings = new JsonSerializerSettings();
Assert.AreEqual(128, settings.MaxDepth);
Assert.AreEqual(false, settings._maxDepthSet);

// Default should be the same
Assert.AreEqual(reader.MaxDepth, settings.MaxDepth);

settings.MaxDepth = 2;
Assert.AreEqual(2, settings.MaxDepth);
Assert.AreEqual(true, settings._maxDepthSet);

JsonSerializer serializer = JsonSerializer.Create(settings);
Assert.AreEqual(2, serializer.MaxDepth);

ExceptionAssert.Throws<JsonReaderException>(
() => serializer.Deserialize(reader),
"The reader's MaxDepth of 2 has been exceeded. Path '[0][0]', line 1, position 3.");
}

[Test]
public void SetMaxDepth_DepthNotExceeded()
{
JsonTextReader reader = new JsonTextReader(new StringReader("['text']"));
JsonSerializerSettings settings = new JsonSerializerSettings();

settings.MaxDepth = 2;

JsonSerializer serializer = JsonSerializer.Create(settings);
Assert.AreEqual(2, serializer.MaxDepth);

serializer.Deserialize(reader);

Assert.AreEqual(128, reader.MaxDepth);
}
}
}
3 changes: 3 additions & 0 deletions Src/Newtonsoft.Json/JsonReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ public string? DateFormatString

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

CloseInput = true;
}
Expand Down
2 changes: 1 addition & 1 deletion Src/Newtonsoft.Json/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ public virtual CultureInfo Culture
/// <summary>
/// Gets or sets the maximum depth allowed when reading JSON. Reading past this depth will throw a <see cref="JsonReaderException"/>.
/// A null value means there is no maximum.
/// The default value is <c>null</c>.
/// The default value is <c>128</c>.
/// </summary>
public virtual int? MaxDepth
{
Expand Down
5 changes: 3 additions & 2 deletions Src/Newtonsoft.Json/JsonSerializerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class JsonSerializerSettings
internal static readonly CultureInfo DefaultCulture;
internal const bool DefaultCheckAdditionalContent = false;
internal const string DefaultDateFormatString = @"yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK";
internal const int DefaultMaxDepth = 128;

internal Formatting? _formatting;
internal DateFormatHandling? _dateFormatHandling;
Expand Down Expand Up @@ -325,11 +326,11 @@ public string DateFormatString
/// <summary>
/// Gets or sets the maximum depth allowed when reading JSON. Reading past this depth will throw a <see cref="JsonReaderException"/>.
/// A null value means there is no maximum.
/// The default value is <c>null</c>.
/// The default value is <c>128</c>.
/// </summary>
public int? MaxDepth
{
get => _maxDepth;
get => _maxDepthSet ? _maxDepth : DefaultMaxDepth;
set
{
if (value <= 0)
Expand Down

0 comments on commit 7e77bbe

Please sign in to comment.