Skip to content

Commit

Permalink
fix: check the format of the event name
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
darrachequesne committed May 22, 2023
1 parent 0841bd5 commit 3b78117
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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$/
Expand Down

2 comments on commit 3b78117

@nicoscra
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update on test parser

@masiceiu
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

Please sign in to comment.