diff --git a/.travis.yml b/.travis.yml index f776680..58b3e94 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,8 @@ node_js: - "6" - "7" - "8" + - "9" + - "10" install: - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update && brew install nmap; npm install; node examples/discover.js; npm test; fi diff --git a/lib/classes/validation.js b/lib/classes/validation.js index 6fc6ffb..55043dd 100644 --- a/lib/classes/validation.js +++ b/lib/classes/validation.js @@ -48,7 +48,6 @@ class validation { * Regex for matching port ranges * @ref http://stackoverflow.com/a/21075138/901697 */ - //ports: /^(?:(?:^|[-,])(?:[1-9][0-9]{0,3}|[1-5][0-9]{4}|6(?:[0-4][0-9]{3}|5(?:[0-4][0-9]{2}|5(?:[0-2][0-9]|3[0-5])))))+$/, ports: /\d{1,6}(?:-\d+)?/g, /** @@ -99,13 +98,14 @@ class validation { */ init(opts, cb) { const scope = this; - const errors = []; + let errors = []; + let result; scope.exists(opts.nmap, (result) => { if (!result) { // Try full path vs. process.env.PATH - fs.access(opts.nmap, fs.constants.F_OK|fs.constants.X_OK, e => { + fs.access(opts.nmap, fs.constants.F_OK | fs.constants.X_OK, e => { if (e) return cb(scope.messages.path); }); @@ -113,10 +113,10 @@ class validation { }); if (opts.blocksize > 128) - return cb(scope.messages.block); + return cb(scope.messages.block); - if ((!opts.range) || (!/array|object/.test(typeof(opts.range))) || - (opts.range.length === 0)) + if ((!opts.range) || (!/array|object/.test(typeof (opts.range))) || + (opts.range.length === 0)) return cb(scope.messages.range); if (opts.range.length >= 1) { @@ -131,7 +131,8 @@ class validation { errors.push(new Error(scope.messages.port)); if (opts.ports) { - if (!scope.patterns.ports.test(opts.ports)) + result = opts.ports.match(scope.patterns.ports).join(","); + if (result == "") return cb(scope.messages.port); } @@ -151,13 +152,14 @@ class validation { verify(host, cb) { if (this.test(this.patterns.hostname, host) || - this.test(this.patterns.IPv4, host) || - this.test(this.patterns.IPv6, host) || - this.test(this.patterns.IPv4CIDR, host) || - this.test(this.patterns.IPv6CIDR, host) || - this.test(this.patterns.IPv4Range, host)) { + this.test(this.patterns.IPv4, host) || + this.test(this.patterns.IPv6, host) || + this.test(this.patterns.IPv4CIDR, host) || + this.test(this.patterns.IPv6CIDR, host) || + this.test(this.patterns.IPv4Range, host)) { return cb(null, true); - } else { + } + else { return cb(`Supplied host (${host}) did not pass validation. ${this.messages.range}`); } @@ -178,6 +180,20 @@ class validation { } + /** + * @function match + * Match specified regex test on string and return array of results + * + * @param {Object} regex - Regex test case + * @param {String} str - String to perform test on + * + * @returns {String} + */ + match(regex, str) { + return regex.match(str); + } + + /** * @function exists * Binary file tests diff --git a/test/errors.js b/test/errors.js deleted file mode 100644 index 69ac90e..0000000 --- a/test/errors.js +++ /dev/null @@ -1,105 +0,0 @@ -/*! - * libnmap - * Copyright(c) 2013-2018 Jason Gerfen - * License: MIT - */ - -'use strict' - -const nmap = require('../'); -const timeout = 1024 * 1024; -const chai = require('chai'); -const should = chai.should(); -const expect = chai.expect; -let opts; - - -describe('nmap', function() { - - context('error handling', function() { - - this.timeout(timeout); - - it('missing nmap binary', function(done) { - opts = { - nmap: 'ping-pong' - }; - nmap.scan(opts, function(err, report) { - should.exist(err); - should.not.exist(report); - done(); - }); - }); - - it('invalid host range', function(done) { - opts = { - range: ['10.0.5.256'] - }; - nmap.scan(opts, function(err, report) { - should.exist(err); - should.not.exist(report); - done(); - }); - }); - - it('invalid ip range', function(done) { - opts = { - range: ['10.0.2.5-256'] - }; - nmap.scan(opts, function(err, report) { - should.exist(err); - should.not.exist(report); - done(); - }); - }); - - it('invalid CIDR range', function(done) { - opts = { - range: ['256.128.0/17'] - }; - nmap.scan(opts, function(err, report) { - should.exist(err); - should.not.exist(report); - done(); - }); - }); - - it('invalid port range', function(done) { - opts = { - ports: '1,5-40,1024,-90' - }; - nmap.scan(opts, function(err, report) { - should.exist(err); - should.not.exist(report); - done(); - }); - }); - - it('invalid flag options', function(done) { - opts = { - range: ['localhost'], - flags: [ - '-sV', // Open port to determine service (i.e. FTP, SSH etc) - '-O', // OS finger printing (requires elevated privileges) - '-sC', // Enables the nmap scripts (all) against each host (requires elevated privileges) - ] - }; - nmap.scan(opts, function(err, report) { - should.exist(err); - should.not.exist(report); - done(); - }); - }); - - it('unspecified range', function(done) { - opts = { - range: [] - }; - nmap.scan(opts, function(err, report) { - should.exist(err); - should.not.exist(report); - done(); - }); - }); - }); -});