Repository navigation
Validate SignalR Java MessagePack binary lengths before allocation #70260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b7be2c7
dafcd47
6ee762f
10fd514
c907fac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
|
|
||
| import org.msgpack.core.MessageBufferPacker; | ||
| import org.msgpack.core.MessageFormat; | ||
| import org.msgpack.core.MessageInsufficientBufferException; | ||
| import org.msgpack.core.MessagePack; | ||
| import org.msgpack.core.MessagePackException; | ||
| import org.msgpack.core.MessagePacker; | ||
|
|
@@ -85,6 +86,10 @@ public List<HubMessage> parseMessages(ByteBuffer payload, InvocationBinder binde | |
| } catch (IOException ex) { | ||
| throw new RuntimeException("Error reading length header.", ex); | ||
| } | ||
| int payloadStart = payload.position(); | ||
| int payloadLimit = payload.limit(); | ||
| int messageEnd = payloadStart + length; | ||
| payload.limit(messageEnd); | ||
| // Instantiate MessageUnpacker | ||
| try(MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(payload)) { | ||
|
|
||
|
|
@@ -93,16 +98,16 @@ public List<HubMessage> parseMessages(ByteBuffer payload, InvocationBinder binde | |
|
|
||
| switch (messageType) { | ||
| case INVOCATION: | ||
| hubMessages.add(createInvocationMessage(unpacker, binder, itemCount, payload)); | ||
| hubMessages.add(createInvocationMessage(unpacker, binder, itemCount, payload, length)); | ||
| break; | ||
| case STREAM_ITEM: | ||
| hubMessages.add(createStreamItemMessage(unpacker, binder, payload)); | ||
| hubMessages.add(createStreamItemMessage(unpacker, binder, payload, length)); | ||
| break; | ||
| case COMPLETION: | ||
| hubMessages.add(createCompletionMessage(unpacker, binder, payload)); | ||
| hubMessages.add(createCompletionMessage(unpacker, binder, payload, length)); | ||
| break; | ||
| case STREAM_INVOCATION: | ||
| hubMessages.add(createStreamInvocationMessage(unpacker, binder, itemCount, payload)); | ||
| hubMessages.add(createStreamInvocationMessage(unpacker, binder, itemCount, payload, length)); | ||
| break; | ||
| case CANCEL_INVOCATION: | ||
| hubMessages.add(createCancelInvocationMessage(unpacker)); | ||
|
|
@@ -120,8 +125,9 @@ public List<HubMessage> parseMessages(ByteBuffer payload, InvocationBinder binde | |
| int readBytes = (int) unpacker.getTotalReadBytes(); | ||
| if (readBytes != length) { | ||
| // Check what the last message was | ||
| // If it was an invocation binding failure, we have to correct the position of the buffer | ||
| if (hubMessages.get(hubMessages.size() - 1).getMessageType() == HubMessageType.INVOCATION_BINDING_FAILURE) { | ||
| // If it was a binding failure, we have to correct the position of the buffer | ||
| if (hubMessages.get(hubMessages.size() - 1).getMessageType() == HubMessageType.INVOCATION_BINDING_FAILURE | ||
| || hubMessages.get(hubMessages.size() - 1).getMessageType() == HubMessageType.STREAM_BINDING_FAILURE) { | ||
| // Cast to a Buffer to avoid the Java 9+ behavior where ByteBuffer.position(int) overrides Buffer.position(int), | ||
| // Returning a ByteBuffer rather than a Buffer. This causes issues on Android - see https://github.com/dotnet/aspnetcore/pull/26614 | ||
| ((Buffer) payload).position(payload.position() + (length - readBytes)); | ||
|
|
@@ -134,12 +140,32 @@ public List<HubMessage> parseMessages(ByteBuffer payload, InvocationBinder binde | |
| // Returning a ByteBuffer rather than a Buffer. This causes issues on Android - see https://github.com/dotnet/aspnetcore/pull/26614 | ||
| ((Buffer) payload).position(payload.position() + readBytes); | ||
| } catch (MessagePackException | IOException ex) { | ||
| if (ex instanceof MessageInsufficientBufferException && payloadLimit > messageEnd | ||
| && !hasCompleteMessageAt(payload, messageEnd, payloadLimit)) { | ||
| throw new RuntimeException(String.format("MessagePack message was length %d but claimed to be length %d.", length + 1, length)); | ||
| } | ||
| throw new RuntimeException("Error reading MessagePack data.", ex); | ||
| } finally { | ||
| payload.limit(payloadLimit); | ||
| } | ||
| } | ||
| return hubMessages; | ||
| } | ||
|
|
||
| private boolean hasCompleteMessageAt(ByteBuffer payload, int position, int limit) { | ||
| ByteBuffer remaining = payload.duplicate(); | ||
| remaining.clear(); | ||
| remaining.limit(limit); | ||
| remaining.position(position); | ||
|
|
||
| try { | ||
| int length = Utils.readLengthHeader(remaining); | ||
| return remaining.remaining() >= length; | ||
| } catch (IOException | RuntimeException ex) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public ByteBuffer writeMessage(HubMessage hubMessage) { | ||
| HubMessageType messageType = hubMessage.getMessageType(); | ||
|
|
@@ -190,7 +216,7 @@ public ByteBuffer writeMessage(HubMessage hubMessage) { | |
| } | ||
| } | ||
|
|
||
| private HubMessage createInvocationMessage(MessageUnpacker unpacker, InvocationBinder binder, int itemCount, ByteBuffer payload) throws IOException { | ||
| private HubMessage createInvocationMessage(MessageUnpacker unpacker, InvocationBinder binder, int itemCount, ByteBuffer payload, int messageLength) throws IOException { | ||
| Map<String, String> headers = readHeaders(unpacker); | ||
|
|
||
| // invocationId may be nil | ||
|
|
@@ -210,7 +236,7 @@ private HubMessage createInvocationMessage(MessageUnpacker unpacker, InvocationB | |
| Object[] arguments = null; | ||
| try { | ||
| List<Type> types = binder.getParameterTypes(target); | ||
| arguments = bindArguments(unpacker, types, payload); | ||
| arguments = bindArguments(unpacker, types, payload, messageLength); | ||
| } catch (Exception ex) { | ||
| return new InvocationBindingFailureMessage(invocationId, target, ex); | ||
| } | ||
|
|
@@ -224,21 +250,21 @@ private HubMessage createInvocationMessage(MessageUnpacker unpacker, InvocationB | |
| return new InvocationMessage(headers, invocationId, target, arguments, streams); | ||
| } | ||
|
|
||
| private HubMessage createStreamItemMessage(MessageUnpacker unpacker, InvocationBinder binder, ByteBuffer payload) throws IOException { | ||
| private HubMessage createStreamItemMessage(MessageUnpacker unpacker, InvocationBinder binder, ByteBuffer payload, int messageLength) throws IOException { | ||
| Map<String, String> headers = readHeaders(unpacker); | ||
| String invocationId = unpacker.unpackString(); | ||
| Object value; | ||
| try { | ||
| Type itemType = binder.getReturnType(invocationId); | ||
| value = readValue(unpacker, itemType, payload, true); | ||
| value = readValue(unpacker, itemType, payload, true, messageLength); | ||
| } catch (Exception ex) { | ||
| return new StreamBindingFailureMessage(invocationId, ex); | ||
| } | ||
|
|
||
| return new StreamItem(headers, invocationId, value); | ||
| } | ||
|
|
||
| private HubMessage createCompletionMessage(MessageUnpacker unpacker, InvocationBinder binder, ByteBuffer payload) throws IOException { | ||
| private HubMessage createCompletionMessage(MessageUnpacker unpacker, InvocationBinder binder, ByteBuffer payload, int messageLength) throws IOException { | ||
| Map<String, String> headers = readHeaders(unpacker); | ||
| String invocationId = unpacker.unpackString(); | ||
| int resultKind = unpacker.unpackInt(); | ||
|
|
@@ -254,7 +280,7 @@ private HubMessage createCompletionMessage(MessageUnpacker unpacker, InvocationB | |
| break; | ||
| case NON_VOID_RESULT: | ||
| Type itemType = binder.getReturnType(invocationId); | ||
| result = readValue(unpacker, itemType, payload, true); | ||
| result = readValue(unpacker, itemType, payload, true, messageLength); | ||
| break; | ||
| default: | ||
| throw new RuntimeException("Invalid invocation result kind."); | ||
|
|
@@ -263,15 +289,15 @@ private HubMessage createCompletionMessage(MessageUnpacker unpacker, InvocationB | |
| return new CompletionMessage(headers, invocationId, result, error); | ||
| } | ||
|
|
||
| private HubMessage createStreamInvocationMessage(MessageUnpacker unpacker, InvocationBinder binder, int itemCount, ByteBuffer payload) throws IOException { | ||
| private HubMessage createStreamInvocationMessage(MessageUnpacker unpacker, InvocationBinder binder, int itemCount, ByteBuffer payload, int messageLength) throws IOException { | ||
| Map<String, String> headers = readHeaders(unpacker); | ||
| String invocationId = unpacker.unpackString(); | ||
| String target = unpacker.unpackString(); | ||
|
|
||
| Object[] arguments = null; | ||
| try { | ||
| List<Type> types = binder.getParameterTypes(target); | ||
| arguments = bindArguments(unpacker, types, payload); | ||
| arguments = bindArguments(unpacker, types, payload, messageLength); | ||
| } catch (Exception ex) { | ||
| return new InvocationBindingFailureMessage(invocationId, target, ex); | ||
| } | ||
|
|
@@ -508,7 +534,7 @@ private void writeStreamIds(Collection<String> streamIds, MessagePacker packer) | |
| } | ||
| } | ||
|
|
||
| private Object[] bindArguments(MessageUnpacker unpacker, List<Type> paramTypes, ByteBuffer payload) throws IOException { | ||
| private Object[] bindArguments(MessageUnpacker unpacker, List<Type> paramTypes, ByteBuffer payload, int messageLength) throws IOException { | ||
| int argumentCount = unpacker.unpackArrayHeader(); | ||
|
|
||
| if (paramTypes.size() != argumentCount) { | ||
|
|
@@ -518,13 +544,13 @@ private Object[] bindArguments(MessageUnpacker unpacker, List<Type> paramTypes, | |
| Object[] arguments = new Object[argumentCount]; | ||
|
|
||
| for (int i = 0; i < argumentCount; i++) { | ||
| arguments[i] = readValue(unpacker, paramTypes.get(i), payload, true); | ||
| arguments[i] = readValue(unpacker, paramTypes.get(i), payload, true, messageLength); | ||
| } | ||
|
|
||
| return arguments; | ||
| } | ||
|
|
||
| private Object readValue(MessageUnpacker unpacker, Type itemType, ByteBuffer payload, boolean outermostCall) throws IOException { | ||
| private Object readValue(MessageUnpacker unpacker, Type itemType, ByteBuffer payload, boolean outermostCall, int messageLength) throws IOException { | ||
| Class<?> itemClass = Utils.typeToClass(itemType); | ||
| MessageFormat messageFormat = unpacker.getNextFormat(); | ||
| ValueType valueType = messageFormat.getValueType(); | ||
|
|
@@ -575,6 +601,9 @@ private Object readValue(MessageUnpacker unpacker, Type itemType, ByteBuffer pay | |
| break; | ||
| case BINARY: | ||
| length = unpacker.unpackBinaryHeader(); | ||
| if (length > messageLength - unpacker.getTotalReadBytes()) { | ||
| throw new IOException("Binary value length exceeds the remaining message length."); | ||
| } | ||
|
Comment on lines
+604
to
+606
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot Look into this
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bound the unpacker to the current frame and added truncated |
||
| byte[] binaryValue = new byte[length]; | ||
| unpacker.readPayload(binaryValue); | ||
| item = binaryValue; | ||
|
|
@@ -583,7 +612,7 @@ private Object readValue(MessageUnpacker unpacker, Type itemType, ByteBuffer pay | |
| readBytesStart = unpacker.getTotalReadBytes(); | ||
| length = unpacker.unpackArrayHeader(); | ||
| for (int i = 0; i < length; i++) { | ||
| readValue(unpacker, Object.class, payload, false); | ||
| readValue(unpacker, Object.class, payload, false, messageLength); | ||
| } | ||
| if (outermostCall) { | ||
| // Check how many bytes we've read, grab that from the payload, and deserialize with objectMapper | ||
|
|
@@ -603,8 +632,8 @@ private Object readValue(MessageUnpacker unpacker, Type itemType, ByteBuffer pay | |
| readBytesStart = unpacker.getTotalReadBytes(); | ||
| length = unpacker.unpackMapHeader(); | ||
| for (int i = 0; i < length; i++) { | ||
| readValue(unpacker, Object.class, payload, false); | ||
| readValue(unpacker, Object.class, payload, false); | ||
| readValue(unpacker, Object.class, payload, false, messageLength); | ||
| readValue(unpacker, Object.class, payload, false, messageLength); | ||
| } | ||
| if (outermostCall) { | ||
| // Check how many bytes we've read, grab that from the payload, and deserialize with objectMapper | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P2] [UNRESOLVED] Stream-item binary failures can abort before the stream-binding-failure handler
createStreamItemMessagecatches this newIOExceptionand returnsSTREAM_BINDING_FAILURE, butparseMessagesonly corrects a short read forINVOCATION_BINDING_FAILURE; it otherwise throws at the frame-length check. A malformed stream item followed by another frame therefore cannot reachHubConnection's existing log-and-drop path or return the following message.Why unresolved: The frozen repository source establishes the guard/catch/recovery mismatch, but the bundle does not contain the target-version MessagePack implementation or an immutable validation artifact proving the exact
unpackBinaryHeader/getTotalReadBytesbehavior for the encoded trigger.Minimal repro: Call
parseMessageswith{0x09,0x94,0x02,0x80,0xA2,0x69,0x64,0xC4,0x02,0x2A,0x02,0x91,0x06}and a binder returningbyte[]: the first frame is aSTREAM_ITEMwhose bin8 declares two bytes with one byte left, followed byPING.If the unpacker follows the expected edge, parsing throws the outer length-mismatch exception instead of returning
STREAM_BINDING_FAILUREandPING. Consider allowingSTREAM_BINDING_FAILUREthrough the same position correction asINVOCATION_BINDING_FAILUREand covering this sequence in a regression test.