Commit 6fa17e75 authored by Jon Skeet's avatar Jon Skeet

Reimplement JSON recursion by detecting the depth in the tokenizer.

Added a TODO around a possible change to the tokenizer API, changing PushBack(token) into just Rewind() or something similar.
parent 3d257a9d
...@@ -81,6 +81,63 @@ namespace Google.Protobuf ...@@ -81,6 +81,63 @@ namespace Google.Protobuf
AssertTokens("'\ud800\\udc00'", JsonToken.Value(expected)); AssertTokens("'\ud800\\udc00'", JsonToken.Value(expected));
} }
[Test]
public void ObjectDepth()
{
string json = "{ \"foo\": { \"x\": 1, \"y\": [ 0 ] } }";
var tokenizer = new JsonTokenizer(new StringReader(json));
// If we had more tests like this, I'd introduce a helper method... but for one test, it's not worth it.
Assert.AreEqual(0, tokenizer.ObjectDepth);
Assert.AreEqual(JsonToken.StartObject, tokenizer.Next());
Assert.AreEqual(1, tokenizer.ObjectDepth);
Assert.AreEqual(JsonToken.Name("foo"), tokenizer.Next());
Assert.AreEqual(1, tokenizer.ObjectDepth);
Assert.AreEqual(JsonToken.StartObject, tokenizer.Next());
Assert.AreEqual(2, tokenizer.ObjectDepth);
Assert.AreEqual(JsonToken.Name("x"), tokenizer.Next());
Assert.AreEqual(2, tokenizer.ObjectDepth);
Assert.AreEqual(JsonToken.Value(1), tokenizer.Next());
Assert.AreEqual(2, tokenizer.ObjectDepth);
Assert.AreEqual(JsonToken.Name("y"), tokenizer.Next());
Assert.AreEqual(2, tokenizer.ObjectDepth);
Assert.AreEqual(JsonToken.StartArray, tokenizer.Next());
Assert.AreEqual(2, tokenizer.ObjectDepth); // Depth hasn't changed in array
Assert.AreEqual(JsonToken.Value(0), tokenizer.Next());
Assert.AreEqual(2, tokenizer.ObjectDepth);
Assert.AreEqual(JsonToken.EndArray, tokenizer.Next());
Assert.AreEqual(2, tokenizer.ObjectDepth);
Assert.AreEqual(JsonToken.EndObject, tokenizer.Next());
Assert.AreEqual(1, tokenizer.ObjectDepth);
Assert.AreEqual(JsonToken.EndObject, tokenizer.Next());
Assert.AreEqual(0, tokenizer.ObjectDepth);
Assert.AreEqual(JsonToken.EndDocument, tokenizer.Next());
Assert.AreEqual(0, tokenizer.ObjectDepth);
}
[Test]
public void ObjectDepth_WithPushBack()
{
string json = "{}";
var tokenizer = new JsonTokenizer(new StringReader(json));
Assert.AreEqual(0, tokenizer.ObjectDepth);
var token = tokenizer.Next();
Assert.AreEqual(1, tokenizer.ObjectDepth);
// When we push back a "start object", we should effectively be back to the previous depth.
tokenizer.PushBack(token);
Assert.AreEqual(0, tokenizer.ObjectDepth);
// Read the same token again, and get back to depth 1
token = tokenizer.Next();
Assert.AreEqual(1, tokenizer.ObjectDepth);
// Now the same in reverse, with EndObject
token = tokenizer.Next();
Assert.AreEqual(0, tokenizer.ObjectDepth);
tokenizer.PushBack(token);
Assert.AreEqual(1, tokenizer.ObjectDepth);
tokenizer.Next();
Assert.AreEqual(0, tokenizer.ObjectDepth);
}
[Test] [Test]
[TestCase("embedded tab\t")] [TestCase("embedded tab\t")]
[TestCase("embedded CR\r")] [TestCase("embedded CR\r")]
......
...@@ -95,6 +95,13 @@ namespace Google.Protobuf ...@@ -95,6 +95,13 @@ namespace Google.Protobuf
"Use CodedInputStream.SetRecursionLimit() to increase the depth limit."); "Use CodedInputStream.SetRecursionLimit() to increase the depth limit.");
} }
internal static InvalidProtocolBufferException JsonRecursionLimitExceeded()
{
return new InvalidProtocolBufferException(
"Protocol message had too many levels of nesting. May be malicious. " +
"Use JsonParser.Settings to increase the depth limit.");
}
internal static InvalidProtocolBufferException SizeLimitExceeded() internal static InvalidProtocolBufferException SizeLimitExceeded()
{ {
return new InvalidProtocolBufferException( return new InvalidProtocolBufferException(
......
This diff is collapsed.
...@@ -58,6 +58,13 @@ namespace Google.Protobuf ...@@ -58,6 +58,13 @@ namespace Google.Protobuf
private readonly PushBackReader reader; private readonly PushBackReader reader;
private JsonToken bufferedToken; private JsonToken bufferedToken;
private State state; private State state;
private int objectDepth = 0;
/// <summary>
/// Returns the depth of the stack, purely in objects (not collections).
/// Informally, this is the number of remaining unclosed '{' characters we have.
/// </summary>
internal int ObjectDepth { get { return objectDepth; } }
internal JsonTokenizer(TextReader reader) internal JsonTokenizer(TextReader reader)
{ {
...@@ -66,6 +73,8 @@ namespace Google.Protobuf ...@@ -66,6 +73,8 @@ namespace Google.Protobuf
containerStack.Push(ContainerType.Document); containerStack.Push(ContainerType.Document);
} }
// TODO: Why do we allow a different token to be pushed back? It might be better to always remember the previous
// token returned, and allow a parameterless Rewind() method (which could only be called once, just like the current PushBack).
internal void PushBack(JsonToken token) internal void PushBack(JsonToken token)
{ {
if (bufferedToken != null) if (bufferedToken != null)
...@@ -73,6 +82,14 @@ namespace Google.Protobuf ...@@ -73,6 +82,14 @@ namespace Google.Protobuf
throw new InvalidOperationException("Can't push back twice"); throw new InvalidOperationException("Can't push back twice");
} }
bufferedToken = token; bufferedToken = token;
if (token.Type == JsonToken.TokenType.StartObject)
{
objectDepth--;
}
else if (token.Type == JsonToken.TokenType.EndObject)
{
objectDepth++;
}
} }
/// <summary> /// <summary>
...@@ -94,6 +111,14 @@ namespace Google.Protobuf ...@@ -94,6 +111,14 @@ namespace Google.Protobuf
{ {
var ret = bufferedToken; var ret = bufferedToken;
bufferedToken = null; bufferedToken = null;
if (ret.Type == JsonToken.TokenType.StartObject)
{
objectDepth++;
}
else if (ret.Type == JsonToken.TokenType.EndObject)
{
objectDepth--;
}
return ret; return ret;
} }
if (state == State.ReaderExhausted) if (state == State.ReaderExhausted)
...@@ -141,10 +166,12 @@ namespace Google.Protobuf ...@@ -141,10 +166,12 @@ namespace Google.Protobuf
ValidateState(ValueStates, "Invalid state to read an open brace: "); ValidateState(ValueStates, "Invalid state to read an open brace: ");
state = State.ObjectStart; state = State.ObjectStart;
containerStack.Push(ContainerType.Object); containerStack.Push(ContainerType.Object);
objectDepth++;
return JsonToken.StartObject; return JsonToken.StartObject;
case '}': case '}':
ValidateState(State.ObjectAfterProperty | State.ObjectStart, "Invalid state to read a close brace: "); ValidateState(State.ObjectAfterProperty | State.ObjectStart, "Invalid state to read a close brace: ");
PopContainer(); PopContainer();
objectDepth--;
return JsonToken.EndObject; return JsonToken.EndObject;
case '[': case '[':
ValidateState(ValueStates, "Invalid state to read an open square bracket: "); ValidateState(ValueStates, "Invalid state to read an open square bracket: ");
......
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