Skip to content
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ The `ipaddr.IPv4` and `ipaddr.IPv6` objects have some methods defined, too. All
`ipaddr.IPvX.isValid(string)` can be used to check if the string is a valid address for particular protocol, and `ipaddr.IPvX.parse(string)` is the error-throwing parser.

`ipaddr.IPvX.isValid(string)` uses the same format for parsing as the POSIX `inet_ntoa` function, which accepts unusual formats like `0xc0.168.1.1` or `0x10000000`. The function `ipaddr.IPv4.isValidFourPartDecimal(string)` validates the IPv4 address and also ensures that it is written in four-part decimal format.
`ipaddr.IPv4.isValidCIDRFourPartDecimal(string)` validates an IPv4 address in CIDR notation and also ensures that its address portion is written in four-part decimal format.

[IPv6 ranges]: https://github.com/whitequark/ipaddr.js/blob/master/lib/ipaddr.js#L530
[IPv4 ranges]: https://github.com/whitequark/ipaddr.js/blob/master/lib/ipaddr.js#L182
Expand Down
11 changes: 11 additions & 0 deletions lib/ipaddr.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@
}

return new this(octets);
} catch (e) {

Check warning on line 329 in lib/ipaddr.js

View workflow job for this annotation

GitHub Actions / lint

'e' is defined but never used
throw new Error('ipaddr: the address does not have IPv4 CIDR format');
}
};
Expand All @@ -341,7 +341,7 @@
try {
new this(this.parser(string));
return true;
} catch (e) {

Check warning on line 344 in lib/ipaddr.js

View workflow job for this annotation

GitHub Actions / lint

'e' is defined but never used
return false;
}
};
Expand All @@ -351,7 +351,7 @@
try {
this.parseCIDR(string);
return true;
} catch (e) {

Check warning on line 354 in lib/ipaddr.js

View workflow job for this annotation

GitHub Actions / lint

'e' is defined but never used
return false;
}
};
Expand All @@ -365,6 +365,17 @@
}
};

// Checks if a given string is a full four-part IPv4 Address with CIDR prefix.
ipaddr.IPv4.isValidCIDRFourPartDecimal = function (string) {
const match = string.match(/^(.+)\/(\d+)$/);

if (!ipaddr.IPv4.isValidCIDR(string) || !match) {
return false;
}

return ipaddr.IPv4.isValidFourPartDecimal(match[1]);
};

// A utility function to return network address given the IPv4 interface and prefix length in CIDR notation
ipaddr.IPv4.networkAddressFromCIDR = function (string) {
let cidr, i, ipInterfaceOctets, octets, subnetMaskOctets;
Expand All @@ -382,7 +393,7 @@
}

return new this(octets);
} catch (e) {

Check warning on line 396 in lib/ipaddr.js

View workflow job for this annotation

GitHub Actions / lint

'e' is defined but never used
throw new Error('ipaddr: the address does not have IPv4 CIDR format');
}
};
Expand Down Expand Up @@ -814,7 +825,7 @@
const addr = this.parser(string);
new this(addr.parts, addr.zoneId);
return true;
} catch (e) {

Check warning on line 828 in lib/ipaddr.js

View workflow job for this annotation

GitHub Actions / lint

'e' is defined but never used
return false;
}
};
Expand All @@ -830,7 +841,7 @@
try {
this.parseCIDR(string);
return true;
} catch (e) {

Check warning on line 844 in lib/ipaddr.js

View workflow job for this annotation

GitHub Actions / lint

'e' is defined but never used
return false;
}
};
Expand Down Expand Up @@ -995,10 +1006,10 @@
ipaddr.parseCIDR = function (string) {
try {
return ipaddr.IPv6.parseCIDR(string);
} catch (e) {

Check warning on line 1009 in lib/ipaddr.js

View workflow job for this annotation

GitHub Actions / lint

'e' is defined but never used
try {
return ipaddr.IPv4.parseCIDR(string);
} catch (e2) {

Check warning on line 1012 in lib/ipaddr.js

View workflow job for this annotation

GitHub Actions / lint

'e2' is defined but never used
throw new Error('ipaddr: the address has neither IPv6 nor IPv4 CIDR format');
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/ipaddr.js.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ declare module "ipaddr.js" {
static isValid(addr: string): boolean;
static isValidCIDR(addr: string): boolean;
static isValidFourPartDecimal(addr: string): boolean;
static isValidCIDRFourPartDecimal(addr: string): boolean;
static networkAddressFromCIDR(addr: string): IPv4;
static parse(addr: string): IPv4;
static parseCIDR(addr: string): [IPv4, number];
Expand Down
8 changes: 8 additions & 0 deletions test/ipaddr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ describe('ipaddr', () => {
assert.equal(ipaddr.IPv4.isValidFourPartDecimal('0xc0.168.1.1'), false);
})

it('checks the conventional IPv4 CIDR format', () => {
assert.equal(ipaddr.IPv4.isValidCIDRFourPartDecimal('192.168.1.1/24'), true);
assert.equal(ipaddr.IPv4.isValidCIDRFourPartDecimal('0.0.0.0/0'), true);
assert.equal(ipaddr.IPv4.isValidCIDRFourPartDecimal('0xc0.168.1.1/24'), false);
assert.equal(ipaddr.IPv4.isValidCIDRFourPartDecimal('192.168.1.1/33'), false);
assert.equal(ipaddr.IPv4.isValidCIDRFourPartDecimal('192.168.1/24'), false);
})

it('refuses to construct IPv4 address with trailing and leading zeros', () => {
assert.equal(ipaddr.IPv4.isValidFourPartDecimal('000000192.168.100.2'), false);
assert.equal(ipaddr.IPv4.isValidFourPartDecimal('192.0000168.100.2'), false);
Expand Down