Skip to content

Commit d9abaae

Browse files
committed
adding validation check for hostname
1 parent b748fe7 commit d9abaae

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

lib/dns-sync.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,27 @@ var net = require('net'),
66
shell = require('shelljs'),
77
debug = require('debug')('dns-sync');
88

9+
//source - http://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address
10+
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])$");
11+
12+
function isValidHostName(hostname) {
13+
return ValidHostnameRegex.test(hostname);
14+
}
915
/**
1016
* Resolve hostname to IP address,
1117
* returns null in case of error
1218
*/
1319
module.exports = {
1420
resolve: function resolve(hostname) {
1521
var output,
16-
nodeBinary = process.execPath,
17-
scriptPath = path.join(__dirname, "../scripts/dns-lookup-script"),
22+
nodeBinary = process.execPath;
23+
24+
if (!isValidHostName(hostname)) {
25+
console.error('Invalid hostname:', hostname);
26+
return null;
27+
}
28+
29+
var scriptPath = path.join(__dirname, "../scripts/dns-lookup-script"),
1830
response,
1931
cmd = util.format('"%s" "%s" %s', nodeBinary, scriptPath, hostname);
2032

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dns-sync",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "dns-sync",
55
"main": "index.js",
66
"scripts": {
@@ -20,11 +20,11 @@
2020
"license": "MIT",
2121
"readmeFilename": "README.md",
2222
"dependencies": {
23-
"debug" : "~0.7",
24-
"shelljs": "~0.2"
23+
"debug" : "^2",
24+
"shelljs": "~0.3"
2525
},
2626
"devDependencies": {
27-
"mocha" : "~1",
28-
"jshint" : "*"
27+
"mocha" : "^1",
28+
"jshint" : "^2"
2929
}
3030
}

test/test.js

+6
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ describe('dns sync', function () {
1616
assert.ok(!dnsSync.resolve('www.not-google.first'));
1717
assert.ok(!dnsSync.resolve('www.hello-yahoo.next'));
1818
});
19+
20+
it('should fail to resolve valid dns', function () {
21+
assert.ok(!dnsSync.resolve("$(id > /tmp/foo)'"));
22+
assert.ok(!dnsSync.resolve("cd /tmp; rm -f /tmp/echo; env 'x=() { (a)=>\' bash -c \"echo date\"; cat /tmp/echo"));
23+
assert.ok(!dnsSync.resolve("$(grep -l -z '[^)]=() {' /proc/[1-9]*/environ | cut -d/ -f3)'"));
24+
});
1925
});

0 commit comments

Comments
 (0)