Skip to content

Commit

Permalink
adding validation check for hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
skoranga committed Nov 10, 2014
1 parent b748fe7 commit d9abaae
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
16 changes: 14 additions & 2 deletions lib/dns-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,27 @@ var net = require('net'),
shell = require('shelljs'),
debug = require('debug')('dns-sync');

//source - http://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address
var ValidHostnameRegex = new RegExp("^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$");

function isValidHostName(hostname) {
return ValidHostnameRegex.test(hostname);
}
/**
* Resolve hostname to IP address,
* returns null in case of error
*/
module.exports = {
resolve: function resolve(hostname) {
var output,
nodeBinary = process.execPath,
scriptPath = path.join(__dirname, "../scripts/dns-lookup-script"),
nodeBinary = process.execPath;

if (!isValidHostName(hostname)) {
console.error('Invalid hostname:', hostname);
return null;
}

var scriptPath = path.join(__dirname, "../scripts/dns-lookup-script"),
response,
cmd = util.format('"%s" "%s" %s', nodeBinary, scriptPath, hostname);

Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dns-sync",
"version": "0.1.0",
"version": "0.1.1",
"description": "dns-sync",
"main": "index.js",
"scripts": {
Expand All @@ -20,11 +20,11 @@
"license": "MIT",
"readmeFilename": "README.md",
"dependencies": {
"debug" : "~0.7",
"shelljs": "~0.2"
"debug" : "^2",
"shelljs": "~0.3"
},
"devDependencies": {
"mocha" : "~1",
"jshint" : "*"
"mocha" : "^1",
"jshint" : "^2"
}
}
6 changes: 6 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ describe('dns sync', function () {
assert.ok(!dnsSync.resolve('www.not-google.first'));
assert.ok(!dnsSync.resolve('www.hello-yahoo.next'));
});

it('should fail to resolve valid dns', function () {
assert.ok(!dnsSync.resolve("$(id > /tmp/foo)'"));
assert.ok(!dnsSync.resolve("cd /tmp; rm -f /tmp/echo; env 'x=() { (a)=>\' bash -c \"echo date\"; cat /tmp/echo"));
assert.ok(!dnsSync.resolve("$(grep -l -z '[^)]=() {' /proc/[1-9]*/environ | cut -d/ -f3)'"));
});
});

0 comments on commit d9abaae

Please sign in to comment.