Skip to content

Commit 4ff3292

Browse files
committed
GG-23022: Fixed exception on message parsing failure
This closes apache#455
1 parent 7c970cf commit 4ff3292

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/ClientListenerBufferedParser.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.nio.ByteBuffer;
2626
import java.nio.ByteOrder;
2727

28+
import static org.apache.ignite.internal.processors.odbc.ClientListenerNioListener.CONN_CTX_HANDSHAKE_TIMEOUT_TASK;
29+
2830
/**
2931
* This class implements stream parser based on {@link ClientListenerNioServerBuffer}.
3032
* <p>
@@ -40,6 +42,9 @@ public class ClientListenerBufferedParser implements GridNioParser {
4042
/** Buffer metadata key. */
4143
private static final int BUF_META_KEY = GridNioSessionMetaKey.nextUniqueKey();
4244

45+
/** Length limit of handshake message - 128 MB.*/
46+
private static final int HANDSHAKE_MSG_LEN_LIMIT = 1024 * 1024 * 128;
47+
4348
/** {@inheritDoc} */
4449
@Override public byte[] decode(GridNioSession ses, ByteBuffer buf) throws IOException, IgniteCheckedException {
4550
ClientListenerNioServerBuffer nioBuf = ses.meta(BUF_META_KEY);
@@ -54,7 +59,18 @@ public class ClientListenerBufferedParser implements GridNioParser {
5459
assert old == null;
5560
}
5661

57-
return nioBuf.read(buf);
62+
int msgLenLimit = 0;
63+
64+
// It means, we are still in handshake process
65+
if (ses.meta(CONN_CTX_HANDSHAKE_TIMEOUT_TASK) != null)
66+
msgLenLimit = HANDSHAKE_MSG_LEN_LIMIT;
67+
68+
try {
69+
return nioBuf.read(buf, msgLenLimit);
70+
}
71+
catch (IgniteCheckedException e) {
72+
throw new IgniteCheckedException("Failed to parse message from remote " + ses.remoteAddress(), e);
73+
}
5874
}
5975

6076
/** {@inheritDoc} */

modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/ClientListenerNioServerBuffer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public byte[] data() {
6666
* @return Message bytes or {@code null} if message is not fully read yet.
6767
* @throws IgniteCheckedException If failed to parse message.
6868
*/
69-
@Nullable public byte[] read(ByteBuffer buf) throws IgniteCheckedException {
69+
@Nullable public byte[] read(ByteBuffer buf, int msgLenLimit) throws IgniteCheckedException {
7070
if (cnt < 0) {
7171
for (; cnt < 0 && buf.hasRemaining(); cnt++)
7272
msgSize |= (buf.get() & 0xFF) << (8*(4 + cnt));
@@ -75,7 +75,7 @@ public byte[] data() {
7575
return null;
7676

7777
// If count is 0 then message size should be inited.
78-
if (msgSize <= 0)
78+
if (msgSize <= 0 || (msgLenLimit > 0 && msgSize > msgLenLimit))
7979
throw new IgniteCheckedException("Invalid message size: " + msgSize);
8080

8181
data = new byte[msgSize];

modules/core/src/test/java/org/apache/ignite/client/ConnectionTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ public void testValidInvalidNodeAddressesMix() throws Exception {
6060
testConnection("127.0.0.1:47500", "127.0.0.1:10801", Config.SERVER);
6161
}
6262

63+
/** */
64+
@Test(expected = org.apache.ignite.client.ClientConnectionException.class)
65+
public void testInvalidBigHandshakeMessage() throws Exception {
66+
char[] data = new char[1024 * 1024 * 128];
67+
String userName = new String(data);
68+
69+
testConnectionWithUsername(userName, Config.SERVER);
70+
}
71+
6372
/**
6473
* @param addrs Addresses to connect.
6574
*/
@@ -69,4 +78,15 @@ private void testConnection(String... addrs) throws Exception {
6978
.setAddresses(addrs))) {
7079
}
7180
}
81+
/**
82+
* @param userName User name.
83+
* @param addrs Addresses to connect.
84+
*/
85+
private void testConnectionWithUsername(String userName, String... addrs) throws Exception {
86+
try (LocalIgniteCluster cluster = LocalIgniteCluster.start(1);
87+
IgniteClient client = Ignition.startClient(new ClientConfiguration()
88+
.setAddresses(addrs)
89+
.setUserName(userName))) {
90+
}
91+
}
7292
}

0 commit comments

Comments
 (0)