Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ public void encode(Channel channel, ChannelBuffer buffer, Object msg) throws IOE
@Override
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
int readable = buffer.readableBytes();
if (readable > 0
&& isServerSide(channel)
&& (readable < 2
|| buffer.getByte(buffer.readerIndex()) != MAGIC_HIGH
|| buffer.getByte(buffer.readerIndex() + 1) != MAGIC_LOW)) {
checkPayload(channel, readable);
}
byte[] header = new byte[Math.min(readable, HEADER_LENGTH)];
buffer.readBytes(header);
return decode(channel, buffer, readable, header);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.dubbo.remoting.exchange.codec.ExchangeCodec;
import org.apache.dubbo.remoting.exchange.support.DefaultFuture;
import org.apache.dubbo.remoting.telnet.codec.TelnetCodec;
import org.apache.dubbo.remoting.transport.ExceedPayloadLimitException;
import org.apache.dubbo.rpc.model.FrameworkModel;

import java.io.ByteArrayOutputStream;
Expand All @@ -41,6 +42,7 @@
import java.util.Map;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -213,6 +215,19 @@ void test_Decode_Check_Payload() throws IOException {
}
}

@Test
void testDecodeRejectsCompleteNonMagicInputBeforeCopy() {
Assumptions.assumeTrue(codec instanceof ExchangeCodec);
int payloadLimit = 16;
byte[] oversizedInput = new byte[payloadLimit + 1];
ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(oversizedInput);
Channel channel = getServerSideChannel(url.addParameter(Constants.PAYLOAD_KEY, payloadLimit));

Assertions.assertThrows(ExceedPayloadLimitException.class, () -> codec.decode(channel, buffer));
Assertions.assertEquals(
0, buffer.readerIndex(), "payload validation should reject non-magic input before reading it");
}

@Test
void test_Decode_Header_Need_Readmore() throws IOException {
byte[] header = new byte[] {MAGIC_HIGH, MAGIC_LOW, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Expand Down
Loading