Change JsonReader and JsonSerializer default max depth to 128 (#2462)
This commit is contained in:
@@ -7995,5 +7995,46 @@ This is just junk, though.";
|
|||||||
() => JsonConvert.DeserializeObject<EmptyJsonValueTestClass>("{ A: \"\", B: 1, C: 123, D: 1.23, E: , F: null }"),
|
() => 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.");
|
"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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -227,6 +227,8 @@ namespace Newtonsoft.Json
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the maximum depth allowed when reading JSON. Reading past this depth will throw a <see cref="JsonReaderException"/>.
|
/// 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>
|
/// </summary>
|
||||||
public int? MaxDepth
|
public int? MaxDepth
|
||||||
{
|
{
|
||||||
@@ -327,6 +329,7 @@ namespace Newtonsoft.Json
|
|||||||
_dateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind;
|
_dateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind;
|
||||||
_dateParseHandling = DateParseHandling.DateTime;
|
_dateParseHandling = DateParseHandling.DateTime;
|
||||||
_floatParseHandling = FloatParseHandling.Double;
|
_floatParseHandling = FloatParseHandling.Double;
|
||||||
|
_maxDepth = 128;
|
||||||
|
|
||||||
CloseInput = true;
|
CloseInput = true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -514,7 +514,7 @@ namespace Newtonsoft.Json
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the maximum depth allowed when reading JSON. Reading past this depth will throw a <see cref="JsonReaderException"/>.
|
/// 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.
|
/// A null value means there is no maximum.
|
||||||
/// The default value is <c>null</c>.
|
/// The default value is <c>128</c>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual int? MaxDepth
|
public virtual int? MaxDepth
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ namespace Newtonsoft.Json
|
|||||||
internal static readonly CultureInfo DefaultCulture;
|
internal static readonly CultureInfo DefaultCulture;
|
||||||
internal const bool DefaultCheckAdditionalContent = false;
|
internal const bool DefaultCheckAdditionalContent = false;
|
||||||
internal const string DefaultDateFormatString = @"yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK";
|
internal const string DefaultDateFormatString = @"yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK";
|
||||||
|
internal const int DefaultMaxDepth = 128;
|
||||||
|
|
||||||
internal Formatting? _formatting;
|
internal Formatting? _formatting;
|
||||||
internal DateFormatHandling? _dateFormatHandling;
|
internal DateFormatHandling? _dateFormatHandling;
|
||||||
@@ -325,11 +326,11 @@ namespace Newtonsoft.Json
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the maximum depth allowed when reading JSON. Reading past this depth will throw a <see cref="JsonReaderException"/>.
|
/// 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.
|
/// A null value means there is no maximum.
|
||||||
/// The default value is <c>null</c>.
|
/// The default value is <c>128</c>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int? MaxDepth
|
public int? MaxDepth
|
||||||
{
|
{
|
||||||
get => _maxDepth;
|
get => _maxDepthSet ? _maxDepth : DefaultMaxDepth;
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (value <= 0)
|
if (value <= 0)
|
||||||
|
|||||||
Reference in New Issue
Block a user