Skip to content

Commit ee00660

Browse files
fix: check the format of the event name (#125)
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 Backported from 3b78117
1 parent cd11e38 commit ee00660

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

index.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,26 @@ Decoder.prototype.add = function(obj) {
268268
}
269269
};
270270

271+
function isPayloadValid(type, payload) {
272+
switch (type) {
273+
case 0: // CONNECT
274+
return typeof payload === "object";
275+
case 1: // DISCONNECT
276+
return payload === undefined;
277+
case 4: // ERROR
278+
return typeof payload === "string" || typeof payload === "object";
279+
case 2: // EVENT
280+
case 5: // BINARY_EVENT
281+
return (
282+
isArray(payload) &&
283+
(typeof payload[0] === "string" || typeof payload[0] === "number")
284+
);
285+
case 3: // ACK
286+
case 6: // BINARY_ACK
287+
return isArray(payload);
288+
}
289+
}
290+
271291
/**
272292
* Decode a packet String (JSON data)
273293
*
@@ -329,11 +349,10 @@ function decodeString(str) {
329349
// look up json data
330350
if (str.charAt(++i)) {
331351
var payload = tryParse(str.substr(i));
332-
var isPayloadValid = payload !== false && (p.type === exports.ERROR || isArray(payload));
333-
if (isPayloadValid) {
352+
if (isPayloadValid(p.type, payload)) {
334353
p.data = payload;
335354
} else {
336-
return error('invalid payload');
355+
throw new Error("invalid payload");
337356
}
338357
}
339358

test/arraybuffer.js

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

Lines changed: 15 additions & 7 deletions
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)
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