From 3b78117bf6ba7e99d7a5cfc1ba54d0477554a7f3 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Mon, 22 May 2023 07:37:31 +0200 Subject: [PATCH] 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](https://github.com/socketio/socket.io-parser/commit/78f9fc2999b15804b02f2c22a2b4007734a26af9) (v4.0.1, socket.io@3.0.0) - updated in [1c220dd](https://github.com/socketio/socket.io-parser/commit/1c220ddbf45ea4b44bc8dbf6f9ae245f672ba1b9) (v4.0.4, socket.io@3.1.0) --- lib/index.ts | 5 ++++- test/parser.js | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/index.ts b/lib/index.ts index 6b77094..4319d2c 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -275,7 +275,10 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> { return typeof payload === "string" || typeof payload === "object"; case PacketType.EVENT: case PacketType.BINARY_EVENT: - return Array.isArray(payload) && payload.length > 0; + return ( + Array.isArray(payload) && + (typeof payload[0] === "string" || typeof payload[0] === "number") + ); case PacketType.ACK: case PacketType.BINARY_ACK: return Array.isArray(payload); diff --git a/test/parser.js b/test/parser.js index 1b99166..c78e675 100644 --- a/test/parser.js +++ b/test/parser.js @@ -118,6 +118,9 @@ describe("socket.io-parser", () => { isInvalidPayload("1/admin,{}"); isInvalidPayload('2/admin,"invalid'); isInvalidPayload("2/admin,{}"); + isInvalidPayload('2[{"toString":"foo"}]'); + isInvalidPayload('2[true,"foo"]'); + isInvalidPayload('2[null,"bar"]'); expect(() => new Decoder().add("999")).to.throwException( /^unknown packet type 9$/