Skip to content

Commit

Permalink
Fix validation of IPv4 four-part decimals containing individual zeroes
Browse files Browse the repository at this point in the history
Fixes #103
  • Loading branch information
rgrove authored and whitequark committed May 11, 2018
1 parent c645e0b commit 845a4f2
Show file tree
Hide file tree
Showing 4 changed files with 5 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(/^[1-9]\d*(\.[1-9]\d*){3}$/)) {
if (ipaddr.IPv4.isValid(string) && string.match(/^(0|[1-9]\d*)(\.(0|[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(/^[1-9]\d*(\.[1-9]\d*){3}$/)
if ipaddr.IPv4.isValid(string) and string.match(/^(0|[1-9]\d*)(\.(0|[1-9]\d*)){3}$/)
return true
else
return false
Expand Down
2 changes: 2 additions & 0 deletions test/ipaddr.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ module.exports =
test.done()

'checks the conventional IPv4 address format': (test) ->
test.equal(ipaddr.IPv4.isValidFourPartDecimal('0.0.0.0'), true)
test.equal(ipaddr.IPv4.isValidFourPartDecimal('127.0.0.1'), true)
test.equal(ipaddr.IPv4.isValidFourPartDecimal('192.168.1.1'), true)
test.equal(ipaddr.IPv4.isValidFourPartDecimal('0xc0.168.1.1'), false)
test.done()
Expand Down

0 comments on commit 845a4f2

Please sign in to comment.