Skip to content

Commit e8ffe9d

Browse files
fix: ensure the payload format is valid
This commit should prevent some NPE issues encountered after the parsing of the packet. Related: - #642 - #609 - #505
1 parent 4885e7d commit e8ffe9d

File tree

4 files changed

+50
-34
lines changed

4 files changed

+50
-34
lines changed

Diff for: src/main/java/io/socket/client/Manager.java

+8-20
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,14 @@ private void onopen() {
326326
@Override
327327
public void call(Object... objects) {
328328
Object data = objects[0];
329-
if (data instanceof String) {
330-
Manager.this.ondata((String)data);
331-
} else if (data instanceof byte[]) {
332-
Manager.this.ondata((byte[])data);
329+
try {
330+
if (data instanceof String) {
331+
Manager.this.decoder.add((String) data);
332+
} else if (data instanceof byte[]) {
333+
Manager.this.decoder.add((byte[]) data);
334+
}
335+
} catch (DecodingException e) {
336+
logger.fine("error while decoding the packet: " + e.getMessage());
333337
}
334338
}
335339
}));
@@ -353,22 +357,6 @@ public void call (Packet packet) {
353357
});
354358
}
355359

356-
private void ondata(String data) {
357-
try {
358-
this.decoder.add(data);
359-
} catch (DecodingException e) {
360-
this.onerror(e);
361-
}
362-
}
363-
364-
private void ondata(byte[] data) {
365-
try {
366-
this.decoder.add(data);
367-
} catch (DecodingException e) {
368-
this.onerror(e);
369-
}
370-
}
371-
372360
private void ondecoded(Packet packet) {
373361
this.emit(EVENT_PACKET, packet);
374362
}

Diff for: src/main/java/io/socket/parser/IOParser.java

+25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package io.socket.parser;
22

33
import io.socket.hasbinary.HasBinary;
4+
import org.json.JSONArray;
45
import org.json.JSONException;
6+
import org.json.JSONObject;
57
import org.json.JSONTokener;
68

79
import java.util.ArrayList;
@@ -183,6 +185,9 @@ private static Packet decodeString(String str) {
183185
logger.log(Level.WARNING, "An error occured while retrieving data from JSONTokener", e);
184186
throw new DecodingException("invalid payload");
185187
}
188+
if (!isPayloadValid(p.type, p.data)) {
189+
throw new DecodingException("invalid payload");
190+
}
186191
}
187192

188193
if (logger.isLoggable(Level.FINE)) {
@@ -191,6 +196,26 @@ private static Packet decodeString(String str) {
191196
return p;
192197
}
193198

199+
private static boolean isPayloadValid(int type, Object payload) {
200+
switch (type) {
201+
case Parser.CONNECT:
202+
case Parser.CONNECT_ERROR:
203+
return payload instanceof JSONObject;
204+
case Parser.DISCONNECT:
205+
return payload == null;
206+
case Parser.EVENT:
207+
case Parser.BINARY_EVENT:
208+
return payload instanceof JSONArray
209+
&& ((JSONArray) payload).length() > 0
210+
&& !((JSONArray) payload).isNull(0);
211+
case Parser.ACK:
212+
case Parser.BINARY_ACK:
213+
return payload instanceof JSONArray;
214+
default:
215+
return false;
216+
}
217+
}
218+
194219
@Override
195220
public void destroy() {
196221
if (this.reconstructor != null) {

Diff for: src/test/java/io/socket/parser/ByteArrayTest.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package io.socket.parser;
22

3-
import io.socket.emitter.Emitter;
43
import org.json.JSONArray;
54
import org.json.JSONException;
6-
import org.json.JSONObject;
75
import org.junit.Test;
86
import org.junit.runner.RunWith;
97
import org.junit.runners.JUnit4;
108

119
import java.nio.charset.Charset;
10+
import java.nio.charset.StandardCharsets;
1211

12+
import static java.util.Arrays.asList;
1313
import static org.hamcrest.CoreMatchers.is;
1414
import static org.junit.Assert.assertThat;
1515

@@ -19,30 +19,30 @@ public class ByteArrayTest {
1919
private static Parser.Encoder encoder = new IOParser.Encoder();
2020

2121
@Test
22-
public void encodeByteArray() {
23-
Packet<byte[]> packet = new Packet<>(Parser.BINARY_EVENT);
24-
packet.data = "abc".getBytes(Charset.forName("UTF-8"));
22+
public void encodeByteArray() throws JSONException {
23+
Packet<JSONArray> packet = new Packet<>(Parser.BINARY_EVENT);
24+
packet.data = new JSONArray(asList("abc", "abc".getBytes(StandardCharsets.UTF_8)));
2525
packet.id = 23;
2626
packet.nsp = "/cool";
2727
Helpers.testBin(packet);
2828
}
2929

3030
@Test
3131
public void encodeByteArray2() {
32-
Packet<byte[]> packet = new Packet<>(Parser.BINARY_EVENT);
33-
packet.data = new byte[2];
32+
Packet<JSONArray> packet = new Packet<>(Parser.BINARY_EVENT);
33+
packet.data = new JSONArray(asList("2", new byte[] { 0, 1 }));
3434
packet.id = 0;
3535
packet.nsp = "/";
3636
Helpers.testBin(packet);
3737
}
3838

3939
@Test
4040
public void encodeByteArrayDeepInJson() throws JSONException {
41-
JSONObject data = new JSONObject("{a: \"hi\", b: {}, c: {a: \"bye\", b: {}}}");
42-
data.getJSONObject("b").put("why", new byte[3]);
43-
data.getJSONObject("c").getJSONObject("b").put("a", new byte[6]);
41+
JSONArray data = new JSONArray("[{a: \"hi\", b: {}, c: {a: \"bye\", b: {}}}]");
42+
data.getJSONObject(0).getJSONObject("b").put("why", new byte[3]);
43+
data.getJSONObject(0).getJSONObject("c").getJSONObject("b").put("a", new byte[6]);
4444

45-
Packet<JSONObject> packet = new Packet<>(Parser.BINARY_EVENT);
45+
Packet<JSONArray> packet = new Packet<>(Parser.BINARY_EVENT);
4646
packet.data = data;
4747
packet.id = 999;
4848
packet.nsp = "/deep";
@@ -51,10 +51,10 @@ public void encodeByteArrayDeepInJson() throws JSONException {
5151

5252
@Test
5353
public void encodeDeepBinaryJSONWithNullValue() throws JSONException {
54-
JSONObject data = new JSONObject("{a: \"b\", c: 4, e: {g: null}, h: null}");
55-
data.put("h", new byte[9]);
54+
JSONArray data = new JSONArray("[{a: \"b\", c: 4, e: {g: null}, h: null}]");
55+
data.getJSONObject(0).put("h", new byte[9]);
5656

57-
Packet<JSONObject> packet = new Packet<>(Parser.BINARY_EVENT);
57+
Packet<JSONArray> packet = new Packet<>(Parser.BINARY_EVENT);
5858
packet.data = data;
5959
packet.nsp = "/";
6060
packet.id = 600;

Diff for: src/test/java/io/socket/parser/ParserTest.java

+3
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,8 @@ public void decodeInError() throws JSONException {
6363
Helpers.testDecodeError(Parser.EVENT + "2sd");
6464
// event with invalid json data
6565
Helpers.testDecodeError(Parser.EVENT + "2[\"a\",1,{asdf}]");
66+
Helpers.testDecodeError(Parser.EVENT + "2{}");
67+
Helpers.testDecodeError(Parser.EVENT + "2[]");
68+
Helpers.testDecodeError(Parser.EVENT + "2[null]");
6669
}
6770
}

0 commit comments

Comments
 (0)