Commit 3d257a9d authored by Jon Skeet's avatar Jon Skeet

Add recursion limit handling to JSON parsing.

Fixes issue #932.
parent b6a32e90
...@@ -284,7 +284,7 @@ namespace Google.Protobuf ...@@ -284,7 +284,7 @@ namespace Google.Protobuf
Assert.Throws<InvalidProtocolBufferException>(() => input.ReadBytes()); Assert.Throws<InvalidProtocolBufferException>(() => input.ReadBytes());
} }
private static TestRecursiveMessage MakeRecursiveMessage(int depth) internal static TestRecursiveMessage MakeRecursiveMessage(int depth)
{ {
if (depth == 0) if (depth == 0)
{ {
...@@ -296,7 +296,7 @@ namespace Google.Protobuf ...@@ -296,7 +296,7 @@ namespace Google.Protobuf
} }
} }
private static void AssertMessageDepth(TestRecursiveMessage message, int depth) internal static void AssertMessageDepth(TestRecursiveMessage message, int depth)
{ {
if (depth == 0) if (depth == 0)
{ {
......
...@@ -723,5 +723,23 @@ namespace Google.Protobuf ...@@ -723,5 +723,23 @@ namespace Google.Protobuf
string json = "{} 10"; string json = "{} 10";
Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json)); Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
} }
/// <summary>
/// JSON equivalent to <see cref="CodedInputStreamTest.MaliciousRecursion"/>
/// </summary>
[Test]
public void MaliciousRecursion()
{
string data64 = CodedInputStreamTest.MakeRecursiveMessage(64).ToString();
string data65 = CodedInputStreamTest.MakeRecursiveMessage(65).ToString();
var parser64 = new JsonParser(new JsonParser.Settings(64));
CodedInputStreamTest.AssertMessageDepth(parser64.Parse<TestRecursiveMessage>(data64), 64);
Assert.Throws<InvalidProtocolBufferException>(() => parser64.Parse<TestRecursiveMessage>(data65));
var parser63 = new JsonParser(new JsonParser.Settings(63));
Assert.Throws<InvalidProtocolBufferException>(() => parser63.Parse<TestRecursiveMessage>(data64));
}
} }
} }
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment