Fix not reading to end of argument JSON on binding error (#2319)

This commit is contained in:
James Newton-King 2018-05-19 19:50:43 +12:00 committed by GitHub
parent 879646aba3
commit e29296220d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View File

@ -209,6 +209,7 @@ namespace Microsoft.AspNetCore.SignalR.Protocol
case ArgumentsPropertyName:
JsonUtils.CheckRead(reader);
int initialDepth = reader.Depth;
if (reader.TokenType != JsonToken.StartArray)
{
throw new InvalidDataException($"Expected '{ArgumentsPropertyName}' to be of type {JTokenType.Array}.");
@ -231,6 +232,14 @@ namespace Microsoft.AspNetCore.SignalR.Protocol
catch (Exception ex)
{
argumentBindingException = ExceptionDispatchInfo.Capture(ex);
// Could be at any point in argument array JSON when an error is thrown
// Read until the end of the argument JSON array
while (reader.Depth == initialDepth && reader.TokenType == JsonToken.StartArray ||
reader.Depth > initialDepth)
{
JsonUtils.CheckRead(reader);
}
}
}
break;

View File

@ -294,6 +294,18 @@ namespace Microsoft.AspNetCore.SignalR.Common.Tests.Internal.Protocol
Assert.Equal(DateTimeKind.Utc, dt.Kind);
}
[Fact]
public void ReadToEndOfArgumentArrayOnError()
{
var binder = new TestBinder(new[] { typeof(string) });
var protocol = new JsonHubProtocol();
var data = new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(Frame("{'type':1,'invocationId':'42','target':'foo','arguments':[[],{'target':'foo2'}]}")));
protocol.TryParseMessage(ref data, binder, out var message);
var bindingFailure = Assert.IsType<InvocationBindingFailureMessage>(message);
Assert.Equal("foo", bindingFailure.Target);
}
private static string Frame(string input)
{
var data = Encoding.UTF8.GetBytes(input);