Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
net: More accurate IP address validation and IPv6 dotted notation.
Browse files Browse the repository at this point in the history
* Added isIP method to make use of inet_pton to cares_wrap.cc
* Modified net.isIP() to make use of new C++ isIP method.
* Added new tests to test-net-isip.js.

This is a back-port of commit fb6377e from the master branch.
  • Loading branch information
snoj authored and bnoordhuis committed Nov 28, 2012
1 parent 8316145 commit c9f2531
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
23 changes: 2 additions & 21 deletions lib/net.js
Expand Up @@ -24,6 +24,7 @@ var Stream = require('stream');
var timers = require('timers');
var util = require('util');
var assert = require('assert');
var cares = process.binding('cares_wrap');
var cluster;

function noop() {}
Expand Down Expand Up @@ -1091,27 +1092,7 @@ Server.prototype._setupSlave = function(socketList) {

// TODO: isIP should be moved to the DNS code. Putting it here now because
// this is what the legacy system did.
// NOTE: This does not accept IPv6 with an IPv4 dotted address at the end,
// and it does not detect more than one double : in a string.
exports.isIP = function(input) {
if (!input) {
return 0;
} else if (/^(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)$/.test(input)) {
var parts = input.split('.');
for (var i = 0; i < parts.length; i++) {
var part = parseInt(parts[i]);
if (part < 0 || 255 < part) {
return 0;
}
}
return 4;
} else if (/^::|^::1|^([a-fA-F0-9]{1,4}::?){1,7}([a-fA-F0-9]{1,4})$/.test(
input)) {
return 6;
} else {
return 0;
}
};
exports.isIP = cares.isIP;


exports.isIPv4 = function(input) {
Expand Down
19 changes: 19 additions & 0 deletions src/cares_wrap.cc
Expand Up @@ -685,6 +685,24 @@ void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) {
}


static Handle<Value> IsIP(const Arguments& args) {
HandleScope scope;

String::AsciiValue ip(args[0]);
char address_buffer[sizeof(struct in6_addr)];

if (uv_inet_pton(AF_INET, *ip, &address_buffer) == 1) {
return scope.Close(v8::Integer::New(4));
}

if (uv_inet_pton(AF_INET6, *ip, &address_buffer) == 1) {
return scope.Close(v8::Integer::New(6));
}

return scope.Close(v8::Integer::New(0));
}


static Handle<Value> GetAddrInfo(const Arguments& args) {
HandleScope scope;

Expand Down Expand Up @@ -750,6 +768,7 @@ static void Initialize(Handle<Object> target) {
NODE_SET_METHOD(target, "getHostByName", QueryWithFamily<GetHostByNameWrap>);

NODE_SET_METHOD(target, "getaddrinfo", GetAddrInfo);
NODE_SET_METHOD(target, "isIP", IsIP);

target->Set(String::NewSymbol("AF_INET"), Integer::New(AF_INET));
target->Set(String::NewSymbol("AF_INET6"), Integer::New(AF_INET6));
Expand Down
15 changes: 14 additions & 1 deletion test/simple/test-net-isip.js
Expand Up @@ -31,7 +31,20 @@ assert.equal(net.isIP('0000:0000:0000:0000:0000:0000:0000:0000::0000'), 0);
assert.equal(net.isIP('1050:0:0:0:5:600:300c:326b'), 6);
assert.equal(net.isIP('2001:252:0:1::2008:6'), 6);
assert.equal(net.isIP('2001:dead:beef:1::2008:6'), 6);
assert.equal(net.isIP('2001::'), 6);
assert.equal(net.isIP('2001:dead::'), 6);
assert.equal(net.isIP('2001:dead:beef::'), 6);
assert.equal(net.isIP('2001:dead:beef:1::'), 6);
assert.equal(net.isIP('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'), 6);
assert.equal(net.isIP(':2001:252:0:1::2008:6:'), 0);
assert.equal(net.isIP(':2001:252:0:1::2008:6'), 0);
assert.equal(net.isIP('2001:252:0:1::2008:6:'), 0);
assert.equal(net.isIP('2001:252::1::2008:6'), 0);
assert.equal(net.isIP('::2001:252:1:2008:6'), 6);
assert.equal(net.isIP('::2001:252:1:1.1.1.1'), 6);
assert.equal(net.isIP('::2001:252:1:255.255.255.255'), 6);
assert.equal(net.isIP('::2001:252:1:255.255.255.255.76'), 0);
assert.equal(net.isIP('::anything'), 0);
assert.equal(net.isIP('::1'), 6);
assert.equal(net.isIP('::'), 6);
assert.equal(net.isIP('0000:0000:0000:0000:0000:0000:12345:0000'), 0);
Expand All @@ -45,4 +58,4 @@ assert.equal(net.isIPv4('2001:252:0:1::2008:6'), false);

assert.equal(net.isIPv6('127.0.0.1'), false);
assert.equal(net.isIPv6('example.com'), false);
assert.equal(net.isIPv6('2001:252:0:1::2008:6'), true);
assert.equal(net.isIPv6('2001:252:0:1::2008:6'), true);

0 comments on commit c9f2531

Please sign in to comment.