Skip to content

Commit e292962

Browse files
authored
Fix not reading to end of argument JSON on binding error (#2319)
1 parent 879646a commit e292962

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/Microsoft.AspNetCore.SignalR.Protocols.Json/Protocol/JsonHubProtocol.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ private HubMessage ParseMessage(Utf8BufferTextReader textReader, IInvocationBind
209209
case ArgumentsPropertyName:
210210
JsonUtils.CheckRead(reader);
211211

212+
int initialDepth = reader.Depth;
212213
if (reader.TokenType != JsonToken.StartArray)
213214
{
214215
throw new InvalidDataException($"Expected '{ArgumentsPropertyName}' to be of type {JTokenType.Array}.");
@@ -231,6 +232,14 @@ private HubMessage ParseMessage(Utf8BufferTextReader textReader, IInvocationBind
231232
catch (Exception ex)
232233
{
233234
argumentBindingException = ExceptionDispatchInfo.Capture(ex);
235+
236+
// Could be at any point in argument array JSON when an error is thrown
237+
// Read until the end of the argument JSON array
238+
while (reader.Depth == initialDepth && reader.TokenType == JsonToken.StartArray ||
239+
reader.Depth > initialDepth)
240+
{
241+
JsonUtils.CheckRead(reader);
242+
}
234243
}
235244
}
236245
break;

test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/JsonHubProtocolTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,18 @@ public void DateTimeReturnValuePreservesUtcKind(string input)
294294
Assert.Equal(DateTimeKind.Utc, dt.Kind);
295295
}
296296

297+
[Fact]
298+
public void ReadToEndOfArgumentArrayOnError()
299+
{
300+
var binder = new TestBinder(new[] { typeof(string) });
301+
var protocol = new JsonHubProtocol();
302+
var data = new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(Frame("{'type':1,'invocationId':'42','target':'foo','arguments':[[],{'target':'foo2'}]}")));
303+
protocol.TryParseMessage(ref data, binder, out var message);
304+
var bindingFailure = Assert.IsType<InvocationBindingFailureMessage>(message);
305+
306+
Assert.Equal("foo", bindingFailure.Target);
307+
}
308+
297309
private static string Frame(string input)
298310
{
299311
var data = Encoding.UTF8.GetBytes(input);

0 commit comments

Comments
 (0)