Skip to content

Commit d0a328e

Browse files
authored
Fix MaxDepth not being used with ISerializable deserialization (#2736)
1 parent aae9284 commit d0a328e

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#region License
2+
// Copyright (c) 2007 James Newton-King
3+
//
4+
// Permission is hereby granted, free of charge, to any person
5+
// obtaining a copy of this software and associated documentation
6+
// files (the "Software"), to deal in the Software without
7+
// restriction, including without limitation the rights to use,
8+
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the
10+
// Software is furnished to do so, subject to the following
11+
// conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be
14+
// included in all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
#endregion
25+
26+
#if !NET20
27+
using System;
28+
using System.Collections.Generic;
29+
using System.IO;
30+
using System.Linq;
31+
using System.Runtime.Serialization;
32+
using System.Text;
33+
using System.Threading;
34+
using Newtonsoft.Json.Linq;
35+
using Newtonsoft.Json.Tests.Documentation.Samples.Serializer;
36+
#if DNXCORE50
37+
using System.Reflection;
38+
using Xunit;
39+
using Test = Xunit.FactAttribute;
40+
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
41+
#else
42+
using NUnit.Framework;
43+
#endif
44+
45+
namespace Newtonsoft.Json.Tests.Issues
46+
{
47+
[TestFixture]
48+
public class Issue2735 : TestFixtureBase
49+
{
50+
[Test]
51+
public void Test()
52+
{
53+
int maxDepth = 512;
54+
55+
var currentFoo = new Foo(null);
56+
57+
for (var i = 0; i < 100; i++)
58+
{
59+
currentFoo = new Foo(currentFoo);
60+
}
61+
62+
var fooBar = new FooBar();
63+
fooBar.AddFoo("main", currentFoo);
64+
65+
var json = JsonConvert.SerializeObject(fooBar, SerializeSettings(maxDepth));
66+
67+
JsonConvert.DeserializeObject<FooBar>(json, DeserializeSettings(maxDepth));
68+
}
69+
70+
[Test]
71+
public void Test_Failure()
72+
{
73+
int maxDepth = 512;
74+
75+
var currentFoo = new Foo(null);
76+
77+
for (var i = 0; i < 600; i++)
78+
{
79+
currentFoo = new Foo(currentFoo);
80+
}
81+
82+
var fooBar = new FooBar();
83+
fooBar.AddFoo("main", currentFoo);
84+
85+
var json = JsonConvert.SerializeObject(fooBar, SerializeSettings(maxDepth));
86+
87+
var ex = ExceptionAssert.Throws<JsonReaderException>(() => JsonConvert.DeserializeObject<FooBar>(json, DeserializeSettings(maxDepth)));
88+
Assert.IsTrue(ex.Message.StartsWith("The reader's MaxDepth of 512 has been exceeded."));
89+
}
90+
91+
[Test]
92+
public void Test_Failure2()
93+
{
94+
int maxDepth = 10;
95+
96+
var currentFoo = new Foo(null);
97+
98+
for (var i = 0; i < 20; i++)
99+
{
100+
currentFoo = new Foo(currentFoo);
101+
}
102+
103+
var fooBar = new FooBar();
104+
fooBar.AddFoo("main", currentFoo);
105+
106+
var json = JsonConvert.SerializeObject(fooBar, SerializeSettings(maxDepth));
107+
108+
var ex = ExceptionAssert.Throws<JsonReaderException>(() => JsonConvert.DeserializeObject<FooBar>(json, DeserializeSettings(maxDepth)));
109+
Assert.IsTrue(ex.Message.StartsWith("The reader's MaxDepth of 10 has been exceeded."));
110+
}
111+
112+
[Serializable]
113+
public class FooBar : ISerializable
114+
{
115+
private Dictionary<string, Foo> _myData = new Dictionary<string, Foo>();
116+
117+
public IList<Foo> FooList => _myData.Values.ToList();
118+
119+
public FooBar()
120+
{
121+
}
122+
123+
public FooBar(SerializationInfo info, StreamingContext context)
124+
{
125+
_myData = (Dictionary<string, Foo>)info.GetValue(nameof(_myData), typeof(Dictionary<string, Foo>));
126+
}
127+
128+
public void GetObjectData(SerializationInfo info, StreamingContext context)
129+
{
130+
info.AddValue(nameof(_myData), _myData);
131+
}
132+
133+
public void AddFoo(string name, Foo myFoo)
134+
{
135+
_myData[name] = myFoo;
136+
}
137+
}
138+
139+
public class Foo
140+
{
141+
public Guid Id { get; }
142+
public Foo MyFoo { get; set; }
143+
144+
public Foo(Foo myFoo)
145+
{
146+
MyFoo = myFoo;
147+
Id = Guid.NewGuid();
148+
}
149+
}
150+
151+
private JsonSerializerSettings DeserializeSettings(int maxDepth) => new JsonSerializerSettings()
152+
{
153+
TypeNameHandling = TypeNameHandling.None,
154+
MaxDepth = maxDepth
155+
};
156+
157+
private JsonSerializerSettings SerializeSettings(int maxDepth) => new JsonSerializerSettings()
158+
{
159+
TypeNameHandling = TypeNameHandling.All,
160+
MaxDepth = maxDepth
161+
};
162+
}
163+
}
164+
#endif

Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1804,6 +1804,7 @@ private object CreateISerializable(JsonReader reader, JsonISerializableContract
18041804
JsonConverter? itemConverter = GetConverter(itemContract, null, contract, member);
18051805

18061806
JsonReader tokenReader = token.CreateReader();
1807+
tokenReader.MaxDepth = Serializer.MaxDepth;
18071808
tokenReader.ReadAndAssert(); // Move to first token
18081809

18091810
object? result;

0 commit comments

Comments
 (0)