From 3d257a9dc1427acf163603cea95fb015e839bd2b Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Wed, 4 Nov 2015 09:28:28 +0000 Subject: Add recursion limit handling to JSON parsing. Fixes issue #932. --- .../src/Google.Protobuf.Test/CodedInputStreamTest.cs | 4 ++-- csharp/src/Google.Protobuf.Test/JsonParserTest.cs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) (limited to 'csharp/src/Google.Protobuf.Test') diff --git a/csharp/src/Google.Protobuf.Test/CodedInputStreamTest.cs b/csharp/src/Google.Protobuf.Test/CodedInputStreamTest.cs index 54c44e47..6ae02112 100644 --- a/csharp/src/Google.Protobuf.Test/CodedInputStreamTest.cs +++ b/csharp/src/Google.Protobuf.Test/CodedInputStreamTest.cs @@ -284,7 +284,7 @@ namespace Google.Protobuf Assert.Throws(() => input.ReadBytes()); } - private static TestRecursiveMessage MakeRecursiveMessage(int depth) + internal static TestRecursiveMessage MakeRecursiveMessage(int depth) { if (depth == 0) { @@ -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) { diff --git a/csharp/src/Google.Protobuf.Test/JsonParserTest.cs b/csharp/src/Google.Protobuf.Test/JsonParserTest.cs index b1c7b46c..cb138f53 100644 --- a/csharp/src/Google.Protobuf.Test/JsonParserTest.cs +++ b/csharp/src/Google.Protobuf.Test/JsonParserTest.cs @@ -723,5 +723,23 @@ namespace Google.Protobuf string json = "{} 10"; Assert.Throws(() => TestAllTypes.Parser.ParseJson(json)); } + + /// + /// JSON equivalent to + /// + [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(data64), 64); + Assert.Throws(() => parser64.Parse(data65)); + + var parser63 = new JsonParser(new JsonParser.Settings(63)); + Assert.Throws(() => parser63.Parse(data64)); + + } } } -- cgit v1.2.3