Skip to content

Commit

Permalink
Add support for IPv4 trailing and leading zeros
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfrades authored and whitequark committed Apr 6, 2018
1 parent 2906429 commit d591d47
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ipaddr.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/ipaddr.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@
};

ipaddr.IPv4.isValidFourPartDecimal = function(string) {
if (ipaddr.IPv4.isValid(string) && string.match(/^\d+(\.\d+){3}$/)) {
if (ipaddr.IPv4.isValid(string) && string.match(/^[1-9]\d*(\.[1-9]\d*){3}$/)) {
return true;
} else {
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/ipaddr.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ ipaddr.IPv4.isValid = (string) ->
return false

ipaddr.IPv4.isValidFourPartDecimal = (string) ->
if ipaddr.IPv4.isValid(string) and string.match(/^\d+(\.\d+){3}$/)
if ipaddr.IPv4.isValid(string) and string.match(/^[1-9]\d*(\.[1-9]\d*){3}$/)
return true
else
return false
Expand Down
7 changes: 7 additions & 0 deletions test/ipaddr.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ module.exports =
test.equal(ipaddr.IPv4.isValidFourPartDecimal('0xc0.168.1.1'), false)
test.done()

'refuses to construct IPv4 address with trailing and leading zeros': (test) ->
test.equal(ipaddr.IPv4.isValidFourPartDecimal('000000192.168.100.2'), false)
test.equal(ipaddr.IPv4.isValidFourPartDecimal('192.0000168.100.2'), false)
test.equal(ipaddr.IPv4.isValidFourPartDecimal('192.168.100.00000002'), false)
test.equal(ipaddr.IPv4.isValidFourPartDecimal('192.168.100.20000000'), false)
test.done()

'can construct IPv6 from 16bit parts': (test) ->
test.doesNotThrow ->
new ipaddr.IPv6([0x2001, 0xdb8, 0xf53a, 0, 0, 0, 0, 1])
Expand Down

0 comments on commit d591d47

Please sign in to comment.