diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fe8f3a..88d0412 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,21 @@ +## [3.4.3](https://github.com/socketio/socket.io-parser/compare/3.4.2...3.4.3) (2023-05-22) + + +### Bug Fixes + +* check the format of the event name ([2dc3c92](https://github.com/socketio/socket.io-parser/commit/2dc3c92622dad113b8676be06f23b1ed46b02ced)) + + + +## [3.4.2](https://github.com/socketio/socket.io-parser/compare/3.4.1...3.4.2) (2022-11-09) + + +### Bug Fixes + +* check the format of the index of each attachment ([04d23ce](https://github.com/socketio/socket.io-parser/commit/04d23cecafe1b859fb03e0cbf6ba3b74dff56d14)) + + + ## [3.4.1](https://github.com/socketio/socket.io-parser/compare/3.4.0...3.4.1) (2020-05-13) diff --git a/binary.js b/binary.js index 3e2347d..95a1450 100644 --- a/binary.js +++ b/binary.js @@ -70,8 +70,16 @@ exports.reconstructPacket = function(packet, buffers) { function _reconstructPacket(data, buffers) { if (!data) return data; - if (data && data._placeholder) { - return buffers[data.num]; // appropriate buffer (should be natural order anyway) + if (data && data._placeholder === true) { + var isIndexValid = + typeof data.num === "number" && + data.num >= 0 && + data.num < buffers.length; + if (isIndexValid) { + return buffers[data.num]; // appropriate buffer (should be natural order anyway) + } else { + throw new Error("illegal attachments"); + } } else if (isArray(data)) { for (var i = 0; i < data.length; i++) { data[i] = _reconstructPacket(data[i], buffers); diff --git a/index.js b/index.js index ff613cc..50ff90e 100644 --- a/index.js +++ b/index.js @@ -239,6 +239,9 @@ Emitter(Decoder.prototype); Decoder.prototype.add = function(obj) { var packet; if (typeof obj === 'string') { + if (this.reconstructor) { + throw new Error("got plaintext data when reconstructing a packet"); + } packet = decodeString(obj); if (exports.BINARY_EVENT === packet.type || exports.BINARY_ACK === packet.type) { // binary packet's json this.reconstructor = new BinaryReconstructor(packet); @@ -326,11 +329,10 @@ function decodeString(str) { // look up json data if (str.charAt(++i)) { var payload = tryParse(str.substr(i)); - var isPayloadValid = payload !== false && (p.type === exports.ERROR || isArray(payload)); - if (isPayloadValid) { + if (isPayloadValid(p.type, payload)) { p.data = payload; } else { - return error('invalid payload'); + throw new Error("invalid payload"); } } @@ -346,6 +348,26 @@ function tryParse(str) { } } +function isPayloadValid(type, payload) { + switch (type) { + case 0: // CONNECT + return typeof payload === "object"; + case 1: // DISCONNECT + return payload === undefined; + case 4: // ERROR + return typeof payload === "string" || typeof payload === "object"; + case 2: // EVENT + case 5: // BINARY_EVENT + return ( + isArray(payload) && + (typeof payload[0] === "string" || typeof payload[0] === "number") + ); + case 3: // ACK + case 6: // BINARY_ACK + return isArray(payload); + } +} + /** * Deallocates a parser's resources * diff --git a/package.json b/package.json index ae6be5c..5202c76 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "socket.io-parser", - "version": "3.4.1", + "version": "3.4.3", "description": "socket.io protocol parser", "repository": { "type": "git", diff --git a/test/arraybuffer.js b/test/arraybuffer.js index c8295f1..f2948a5 100644 --- a/test/arraybuffer.js +++ b/test/arraybuffer.js @@ -50,7 +50,7 @@ describe('parser', function() { it('cleans itself up on close', function() { var packet = { type: parser.BINARY_EVENT, - data: [new ArrayBuffer(2), new ArrayBuffer(3)], + data: ["foo", new ArrayBuffer(2), new ArrayBuffer(3)], id: 0, nsp: '/' }; diff --git a/test/buffer.js b/test/buffer.js index 3aba898..f18e68a 100644 --- a/test/buffer.js +++ b/test/buffer.js @@ -1,8 +1,7 @@ var parser = require('../index.js'); var expect = require('expect.js'); var helpers = require('./helpers.js'); -var encode = parser.encode; -var decode = parser.decode; +var Decoder = parser.Decoder; describe('parser', function() { it('encodes a Buffer', function() { @@ -14,6 +13,15 @@ describe('parser', function() { }); }); + it("encodes a nested Buffer", function() { + helpers.test_bin({ + type: parser.BINARY_EVENT, + data: ["a", { b: ["c", Buffer.from("abc", "utf8")] }], + id: 23, + nsp: "/cool", + }); + }); + it('encodes a binary ack with Buffer', function() { helpers.test_bin({ type: parser.BINARY_ACK, @@ -22,4 +30,39 @@ describe('parser', function() { nsp: '/back' }) }); + + it("throws an error when adding an attachment with an invalid 'num' attribute (string)", function() { + var decoder = new Decoder(); + + expect(function() { + decoder.add('51-["hello",{"_placeholder":true,"num":"splice"}]'); + decoder.add(Buffer.from("world")); + }).to.throwException(/^illegal attachments$/); + }); + + it("throws an error when adding an attachment with an invalid 'num' attribute (out-of-bound)", function() { + var decoder = new Decoder(); + + expect(function() { + decoder.add('51-["hello",{"_placeholder":true,"num":1}]'); + decoder.add(Buffer.from("world")); + }).to.throwException(/^illegal attachments$/); + }); + + it("throws an error when adding an attachment without header", function() { + var decoder = new Decoder(); + + expect(function() { + decoder.add(Buffer.from("world")); + }).to.throwException(/^got binary data when not reconstructing a packet$/); + }); + + it("throws an error when decoding a binary event without attachments", function() { + var decoder = new Decoder(); + + expect(function() { + decoder.add('51-["hello",{"_placeholder":true,"num":0}]'); + decoder.add('2["hello"]'); + }).to.throwException(/^got plaintext data when reconstructing a packet$/); + }); }); diff --git a/test/parser.js b/test/parser.js index 652aa7f..7d560bf 100644 --- a/test/parser.js +++ b/test/parser.js @@ -86,12 +86,20 @@ describe('parser', function(){ } }); - it('returns an error packet on parsing error', function(done){ - var decoder = new parser.Decoder(); - decoder.on('decoded', function(packet) { - expect(packet).to.eql({ type: 4, data: 'parser error: invalid payload' }); - done(); - }); - decoder.add('442["some","data"'); + it('returns an error packet on parsing error', function(){ + function isInvalidPayload (str) { + expect(function () { + new parser.Decoder().add(str) + }).to.throwException(/^invalid payload$/); + } + + isInvalidPayload('442["some","data"'); + isInvalidPayload('0/admin,"invalid"'); + isInvalidPayload("1/admin,{}"); + isInvalidPayload('2/admin,"invalid'); + isInvalidPayload("2/admin,{}"); + isInvalidPayload('2[{"toString":"foo"}]'); + isInvalidPayload('2[true,"foo"]'); + isInvalidPayload('2[null,"bar"]'); }); });