Skip to content

Commit 2dc3c92

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]) Backported from 3b78117
1 parent 4b3c191 commit 2dc3c92

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

index.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,10 @@ function decodeString(str) {
329329
// look up json data
330330
if (str.charAt(++i)) {
331331
var payload = tryParse(str.substr(i));
332-
var isPayloadValid = payload !== false && (p.type === exports.ERROR || isArray(payload));
333-
if (isPayloadValid) {
332+
if (isPayloadValid(p.type, payload)) {
334333
p.data = payload;
335334
} else {
336-
return error('invalid payload');
335+
throw new Error("invalid payload");
337336
}
338337
}
339338

@@ -349,6 +348,26 @@ function tryParse(str) {
349348
}
350349
}
351350

351+
function isPayloadValid(type, payload) {
352+
switch (type) {
353+
case 0: // CONNECT
354+
return typeof payload === "object";
355+
case 1: // DISCONNECT
356+
return payload === undefined;
357+
case 4: // ERROR
358+
return typeof payload === "string" || typeof payload === "object";
359+
case 2: // EVENT
360+
case 5: // BINARY_EVENT
361+
return (
362+
isArray(payload) &&
363+
(typeof payload[0] === "string" || typeof payload[0] === "number")
364+
);
365+
case 3: // ACK
366+
case 6: // BINARY_ACK
367+
return isArray(payload);
368+
}
369+
}
370+
352371
/**
353372
* Deallocates a parser's resources
354373
*

test/arraybuffer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('parser', function() {
5050
it('cleans itself up on close', function() {
5151
var packet = {
5252
type: parser.BINARY_EVENT,
53-
data: [new ArrayBuffer(2), new ArrayBuffer(3)],
53+
data: ["foo", new ArrayBuffer(2), new ArrayBuffer(3)],
5454
id: 0,
5555
nsp: '/'
5656
};

test/parser.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,20 @@ describe('parser', function(){
8686
}
8787
});
8888

89-
it('returns an error packet on parsing error', function(done){
90-
var decoder = new parser.Decoder();
91-
decoder.on('decoded', function(packet) {
92-
expect(packet).to.eql({ type: 4, data: 'parser error: invalid payload' });
93-
done();
94-
});
95-
decoder.add('442["some","data"');
89+
it('returns an error packet on parsing error', function(){
90+
function isInvalidPayload (str) {
91+
expect(function () {
92+
new parser.Decoder().add(str)
93+
}).to.throwException(/^invalid payload$/);
94+
}
95+
96+
isInvalidPayload('442["some","data"');
97+
isInvalidPayload('0/admin,"invalid"');
98+
isInvalidPayload("1/admin,{}");
99+
isInvalidPayload('2/admin,"invalid');
100+
isInvalidPayload("2/admin,{}");
101+
isInvalidPayload('2[{"toString":"foo"}]');
102+
isInvalidPayload('2[true,"foo"]');
103+
isInvalidPayload('2[null,"bar"]');
96104
});
97105
});

0 commit comments

Comments
 (0)