Skip to content
Permalink
Browse files Browse the repository at this point in the history
allowing only valid rrtypes
  • Loading branch information
skoranga committed May 20, 2020
1 parent 93b8034 commit cb10a5a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -4,3 +4,5 @@ node_js:
- "6"
- "8"
- "10"
- "12"
- "14"
22 changes: 17 additions & 5 deletions CHANGELOG.md
@@ -1,6 +1,18 @@
####0.2.0
- Updating dependencies
CHANGELOG
==========

####0.1.3
- Added support for resolving AAAA and NS records
- Upgrading to latest shelljs dependency
0.2.1
-----

- Allowing only valid rrtypes as per https://nodejs.org/api/dns.html#dns_dns_resolve_hostname_rrtype_callback.

0.2.0
-----

- Updating dependencies

0.1.3
-----

- Added support for resolving AAAA and NS records
- Upgrading to latest shelljs dependency
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -5,7 +5,9 @@ node-dns-sync

Sync/Blocking DNS resolve. Main usecase is in node server startup.

### How to Use
How to Use
-------

```javascript
var dnsSync = require('dns-sync');
Expand Down
18 changes: 18 additions & 0 deletions lib/dns-sync.js
Expand Up @@ -8,6 +8,20 @@ var util = require('util'),
//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])$");

// https://nodejs.org/api/dns.html#dns_dns_resolve_hostname_rrtype_callback
var RRecordTypes = [
'A',
'AAAA',
'NS',
'NAPTR',
'CNAME',
'SOA',
'SRV',
'PTR',
'MX',
'TXT',
'ANY'];

function isValidHostName(hostname) {
return ValidHostnameRegex.test(hostname);
}
Expand All @@ -26,6 +40,10 @@ module.exports = {
console.error('Invalid hostname:', hostname);
return null;
}
if (typeof type !== 'undefined' && RRecordTypes.indexOf(type) === -1) {
console.error('Invalid rrtype:', type);
return null;
}

var scriptPath = path.join(__dirname, "../scripts/dns-lookup-script"),
response,
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "dns-sync",
"version": "0.2.0",
"version": "0.2.1",
"description": "dns-sync",
"main": "index.js",
"scripts": {
Expand Down
9 changes: 9 additions & 0 deletions test/test.js
@@ -1,6 +1,7 @@
'use strict';

var assert = require('assert'),
fs = require('fs'),
dnsSync = require('../index');

describe('dns sync', function () {
Expand Down Expand Up @@ -38,4 +39,12 @@ describe('dns sync', function () {
assert.ok(ns.length >= 1);
assert.ok(ns[0].match(/^ns[0-9]+\.google\.com$/));
});

it('should fail to resolve if invalid record is asked', function () {
var rs1 = dnsSync.resolve('www.google.com', 'Test');
var rs2 = dnsSync.resolve('www.google.com', ' && touch test.txt');
assert.ok(!rs1);
assert.ok(!rs2);
assert.ok(!fs.existsSync('test.txt'));
});
});

0 comments on commit cb10a5a

Please sign in to comment.