Skip to content

Commit d9db473

Browse files
committedMay 31, 2023
fix: ensure reserved events cannot be used as event names
1 parent 6a5a004 commit d9db473

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed
 

‎lib/index.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ import debugModule from "debug"; // debug()
55

66
const debug = debugModule("socket.io-parser"); // debug()
77

8+
/**
9+
* These strings must not be used as event names, as they have a special meaning.
10+
*/
11+
const RESERVED_EVENTS = [
12+
"connect", // used on the client side
13+
"connect_error", // used on the client side
14+
"disconnect", // used on both sides
15+
"disconnecting", // used on the server side
16+
"newListener", // used by the Node.js EventEmitter
17+
"removeListener", // used by the Node.js EventEmitter
18+
];
19+
820
/**
921
* Protocol version.
1022
*
@@ -277,7 +289,9 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> {
277289
case PacketType.BINARY_EVENT:
278290
return (
279291
Array.isArray(payload) &&
280-
(typeof payload[0] === "string" || typeof payload[0] === "number")
292+
(typeof payload[0] === "number" ||
293+
(typeof payload[0] === "string" &&
294+
RESERVED_EVENTS.indexOf(payload[0]) === -1))
281295
);
282296
case PacketType.ACK:
283297
case PacketType.BINARY_ACK:

‎test/parser.js

+2
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ describe("socket.io-parser", () => {
121121
isInvalidPayload('2[{"toString":"foo"}]');
122122
isInvalidPayload('2[true,"foo"]');
123123
isInvalidPayload('2[null,"bar"]');
124+
isInvalidPayload('2["connect"]');
125+
isInvalidPayload('2["disconnect","123"]');
124126

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

0 commit comments

Comments
 (0)
Please sign in to comment.