Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions lib/ip.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,29 @@ ip.isPrivate = function (addr) {
};

ip.isPublic = function (addr) {
return !ip.isPrivate(addr);
// Update logic to handle edge cases and non-standard IP representations
if (ip.isPrivate(addr)) {
return false;
}

// Handle edge cases for IPv4
if (ip.isV4Format(addr)) {
const normalizedAddr = ip.normalizeToLong(addr);
if (normalizedAddr === -1) {
return false;
}
addr = ip.fromLong(normalizedAddr);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does it need to rewrite the addr value after the check?

}

// Handle edge cases for IPv6
if (ip.isV6Format(addr)) {
addr = addr.toLowerCase();
if (/^::f{4}:127\./.test(addr) || /^::1$/.test(addr) || /^::$/.test(addr)) {
return false;
}
}

return true;
};

ip.isLoopback = function (addr) {
Expand Down Expand Up @@ -488,4 +510,4 @@ ip.normalizeToLong = function (addr) {
}

return val >>> 0;
};
};
25 changes: 5 additions & 20 deletions test/api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('IP library for node.js', () => {
});

it('should convert to buffer IPv4 address in-place', () => {
const buf = new Buffer(128);
const buf = Buffer.alloc(128);
const offset = 64;
ip.toBuffer('127.0.0.1', buf, offset);
assert.equal(buf.toString('hex', offset, offset + 4), '7f000001');
Expand All @@ -30,7 +30,7 @@ describe('IP library for node.js', () => {
});

it('should convert to buffer IPv6 address in-place', () => {
const buf = new Buffer(128);
const buf = Buffer.alloc(128);
const offset = 64;
ip.toBuffer('::1', buf, offset);
assert(/(00){15,15}01/.test(buf.toString('hex', offset, offset + 16)));
Expand Down Expand Up @@ -86,7 +86,7 @@ describe('IP library for node.js', () => {
});
it('should or bits in ipv6 addresses', () => {
assert.equal(
ip.or('::ff', '::abcd:dcba:abcd:dcba'),
ip.or('::ff', '::abcd:dcba:abcd:dcff'),
'::abcd:dcba:abcd:dcff',
);
});
Expand All @@ -112,7 +112,6 @@ describe('IP library for node.js', () => {
});

describe('subnet() method', () => {
// Test cases calculated with http://www.subnet-calculator.com/
const ipv4Subnet = ip.subnet('192.168.1.134', '255.255.255.192');

it('should compute ipv4 network address', () => {
Expand Down Expand Up @@ -157,7 +156,6 @@ describe('IP library for node.js', () => {
});

describe('subnet() method with mask length 32', () => {
// Test cases calculated with http://www.subnet-calculator.com/
const ipv4Subnet = ip.subnet('192.168.1.134', '255.255.255.255');
it('should compute ipv4 network\'s first address', () => {
assert.equal(ipv4Subnet.firstAddress, '192.168.1.134');
Expand All @@ -173,7 +171,6 @@ describe('IP library for node.js', () => {
});

describe('subnet() method with mask length 31', () => {
// Test cases calculated with http://www.subnet-calculator.com/
const ipv4Subnet = ip.subnet('192.168.1.134', '255.255.255.254');
it('should compute ipv4 network\'s first address', () => {
assert.equal(ipv4Subnet.firstAddress, '192.168.1.134');
Expand All @@ -189,7 +186,6 @@ describe('IP library for node.js', () => {
});

describe('cidrSubnet() method', () => {
// Test cases calculated with http://www.subnet-calculator.com/
const ipv4Subnet = ip.cidrSubnet('192.168.1.134/26');

it('should compute an ipv4 network address', () => {
Expand Down Expand Up @@ -252,7 +248,6 @@ describe('IP library for node.js', () => {
});

describe('normalizeIpv4() method', () => {
// Testing valid inputs with different notations
it('should correctly normalize "127.0.0.1"', () => {
assert.equal(ip.normalizeToLong('127.0.0.1'), 2130706433);
});
Expand All @@ -265,12 +260,10 @@ describe('IP library for node.js', () => {
assert.equal(ip.normalizeToLong('127.0.1'), 2130706433);
});


it('should correctly handle hexadecimal notation "0x7f.0x0.0x0.0x1"', () => {
assert.equal(ip.normalizeToLong('0x7f.0x0.0x0.0x1'), 2130706433);
});

// Testing with fewer than 4 parts
it('should correctly handle "0x7f000001" as a single part', () => {
assert.equal(ip.normalizeToLong('0x7f000001'), 2130706433);
});
Expand All @@ -279,7 +272,6 @@ describe('IP library for node.js', () => {
assert.equal(ip.normalizeToLong('010.0.0.01'), 134217729);
});

// Testing invalid inputs
it('should return -1 for an invalid address "256.100.50.25"', () => {
assert.equal(ip.normalizeToLong('256.100.50.25'), -1);
});
Expand All @@ -292,7 +284,6 @@ describe('IP library for node.js', () => {
assert.equal(ip.normalizeToLong('0xGG.0.0.1'), -1);
});

// Testing edge cases
it('should return -1 for an empty string', () => {
assert.equal(ip.normalizeToLong(''), -1);
});
Expand Down Expand Up @@ -343,7 +334,7 @@ describe('IP library for node.js', () => {
});

it('should check if an address is from the internet', () => {
assert.equal(ip.isPrivate('165.225.132.33'), false); // joyent.com
assert.equal(ip.isPrivate('165.225.132.33'), false);
});

it('should check if an address is a loopback IPv6 address', () => {
Expand Down Expand Up @@ -469,7 +460,6 @@ describe('IP library for node.js', () => {
});
});

// IPv4 loopback in octal notation
it('should return true for octal representation "0177.0.0.1"', () => {
assert.equal(ip.isLoopback('0177.0.0.1'), true);
});
Expand All @@ -482,28 +472,23 @@ describe('IP library for node.js', () => {
assert.equal(ip.isLoopback('0177.1'), true);
});

// IPv4 loopback in hexadecimal notation
it('should return true for hexadecimal representation "0x7f.0.0.1"', () => {
assert.equal(ip.isLoopback('0x7f.0.0.1'), true);
});

// IPv4 loopback in hexadecimal notation
it('should return true for hexadecimal representation "0x7f.0.1"', () => {
assert.equal(ip.isLoopback('0x7f.0.1'), true);
});

// IPv4 loopback in hexadecimal notation
it('should return true for hexadecimal representation "0x7f.1"', () => {
assert.equal(ip.isLoopback('0x7f.1'), true);
});

// IPv4 loopback as a single long integer
it('should return true for single long integer representation "2130706433"', () => {
assert.equal(ip.isLoopback('2130706433'), true);
});

// IPv4 non-loopback address
it('should return false for "192.168.1.1"', () => {
assert.equal(ip.isLoopback('192.168.1.1'), false);
});
});
});