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, socket.io@3.0.0) - updated in [1c220dd](1c220dd) (v4.0.4, socket.io@3.1.0)
1 parent 0841bd5 commit 3b78117

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

lib/index.ts

Lines changed: 4 additions & 1 deletion
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

Lines changed: 3 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy