Skip to content

Commit 3b78117

Browse files
fix: check the format of the event name
A packet like '2[{"toString":"foo"}]' was decoded as: { type: EVENT, data: [ { "toString": "foo" } ] } Which would then throw an error when passed to the EventEmitter class: > TypeError: Cannot convert object to primitive value > at Socket.emit (node:events:507:25) > at .../node_modules/socket.io/lib/socket.js:531:14 History of the isPayloadValid() method: - added in [78f9fc2](78f9fc2) (v4.0.1, [email protected]) - updated in [1c220dd](1c220dd) (v4.0.4, [email protected])
1 parent 0841bd5 commit 3b78117

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

lib/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,10 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> {
275275
return typeof payload === "string" || typeof payload === "object";
276276
case PacketType.EVENT:
277277
case PacketType.BINARY_EVENT:
278-
return Array.isArray(payload) && payload.length > 0;
278+
return (
279+
Array.isArray(payload) &&
280+
(typeof payload[0] === "string" || typeof payload[0] === "number")
281+
);
279282
case PacketType.ACK:
280283
case PacketType.BINARY_ACK:
281284
return Array.isArray(payload);

test/parser.js

+3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ describe("socket.io-parser", () => {
118118
isInvalidPayload("1/admin,{}");
119119
isInvalidPayload('2/admin,"invalid');
120120
isInvalidPayload("2/admin,{}");
121+
isInvalidPayload('2[{"toString":"foo"}]');
122+
isInvalidPayload('2[true,"foo"]');
123+
isInvalidPayload('2[null,"bar"]');
121124

122125
expect(() => new Decoder().add("999")).to.throwException(
123126
/^unknown packet type 9$/

0 commit comments

Comments
 (0)