From 9b99771dabb51124eda51458bfb24074b70a4dbb Mon Sep 17 00:00:00 2001 From: Alexey Orlenko Date: Mon, 20 Feb 2017 18:55:47 +0200 Subject: [PATCH] server: handle connection errors before handshake This commit fixes a critical issue that allowed to crash the server with an unhandled error event sending an invalid packet that causes parser error before a handshake. Fixes: https://github.com/metarhia/JSTP/issues/76 Backport-of: https://github.com/metarhia/JSTP/pull/78 --- lib/server.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/server.js b/lib/server.js index 4de7e6be..395b5fb7 100644 --- a/lib/server.js +++ b/lib/server.js @@ -98,6 +98,10 @@ Server.prototype._onRawConnection = function(socket) { var connection = new Connection(this.rawServer.createTransport(socket), this); var server = this; + connection.on('error', function(error) { + this.emit('connectionError', error, connection); + }); + connection.setTimeout(HANDSHAKE_TIMEOUT, function() { if (!connection.handshakeDone) { connection.close(); @@ -114,11 +118,6 @@ Server.prototype._onRawConnection = function(socket) { Server.prototype._onClientConnect = function(connection) { this.clients[connection.id] = connection; this._cachedClientsArray.push(connection); - - var server = this; - connection.on('error', function(error) { - server.emit('connectionError', error, connection); - }); }; // Client connection close event handler