From 3c6a14247b4807f6a296f58e51685987c269a38d Mon Sep 17 00:00:00 2001 From: ComputerGenie Date: Wed, 25 Sep 2024 08:56:41 -0500 Subject: [PATCH] update cliListener.js Despite being inside a try/catch, data needs pre-validation of JSON --- libs/cliListener.js | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/libs/cliListener.js b/libs/cliListener.js index 2241d77d..0bbeb993 100644 --- a/libs/cliListener.js +++ b/libs/cliListener.js @@ -9,6 +9,14 @@ var listener = module.exports = function listener(server, port){ _this.emit('log', text); }; + function isJson(str) { + try { + JSON.parse(str); + } catch (e) { + return false; + } + return true; + } this.start = function(){ net.createServer(function(c) { @@ -16,12 +24,17 @@ var listener = module.exports = function listener(server, port){ var data = ''; try { c.on('data', function (d) { - data += d; - if (data.slice(-1) === '\n') { - var message = JSON.parse(data); - _this.emit('command', message.command, message.params, message.options, function(message){ - c.end(message); - }); + if (isJson(d.toString())) { + data += d; + if (data.slice(-1) === '\n') { + var message = JSON.parse(data); + _this.emit('command', message.command, message.params, message.options, function(message){ + c.end(message); + }); + } + } else { + c.end('You must send JSON, not: '+d.toString()); + return; } }); c.on('end', function () {