From c49559455da8121021e0d7ebddc654fe1bde2ae9 Mon Sep 17 00:00:00 2001 From: Jonathan Lipps Date: Mon, 28 Jul 2014 11:33:40 -0700 Subject: [PATCH] add validation for port values to server (fix #3227) --- lib/server/helpers.js | 30 +++++++++++++++++++++++++++++- lib/server/main.js | 2 +- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/server/helpers.js b/lib/server/helpers.js index a686acf8087..34d9bff3305 100644 --- a/lib/server/helpers.js +++ b/lib/server/helpers.js @@ -47,7 +47,7 @@ module.exports.catchAllHandler = function (e, req, res, next) { next(e); }; -module.exports.checkArgs = function (args) { +module.exports.checkArgs = function (parser, args) { var exclusives = [ ['noReset', 'fullReset'] , ['ipa', 'safari'] @@ -69,6 +69,34 @@ module.exports.checkArgs = function (args) { process.exit(1); } }); + + var checkValidPort = function (port) { + if (port > 0 && port < 65536) return true; + console.error("Port must be greater than 0 and less than 65536"); + return false; + }; + + var validations = { + port: checkValidPort + , callbackPort: checkValidPort + , bootstrapPort: checkValidPort + , selendroidPort: checkValidPort + , chromedriverPort: checkValidPort + , robotPort: checkValidPort + , backendRetries: function (r) { return r >= 0; } + }; + + var nonDefaultArgs = getNonDefaultArgs(parser, args); + + _.each(validations, function (validator, arg) { + if (_.has(nonDefaultArgs, arg)) { + if (!validator(args[arg])) { + console.error("Invalid argument for param " + arg + ": " + args[arg]); + process.exit(1); + } + } + }); + }; module.exports.noColorLogger = function (tokens, req, res) { diff --git a/lib/server/main.js b/lib/server/main.js index 75ac36756a2..87604ee6a6e 100644 --- a/lib/server/main.js +++ b/lib/server/main.js @@ -81,7 +81,7 @@ var main = function (args, readyCb, doneCb) { process.exit(0); } - checkArgs(args); + checkArgs(parser, args); if (typeof doneCb === "undefined") { doneCb = function () {}; }