diff --git a/lib/netmask.coffee b/lib/netmask.coffee index f2ecbf7..44c6263 100644 --- a/lib/netmask.coffee +++ b/lib/netmask.coffee @@ -16,6 +16,8 @@ ip2long = (ip) -> else # make sure 0 prefixed bytes are parsed as octal byte = parseInt(byte, 8) + else if byte and (byte[0] == ' ' or byte[byte.length-1] == ' ') + throw new Error('Invalid IP') else byte = parseInt(byte, 10) if isNaN(byte) then throw new Error("Invalid byte: #{byte}") @@ -25,7 +27,6 @@ ip2long = (ip) -> b.unshift(0) return (b[0] << 24 | b[1] << 16 | b[2] << 8 | b[3]) >>> 0 - class Netmask constructor: (net, mask) -> throw new Error("Missing `net' parameter") unless typeof net is 'string' diff --git a/test/badnets.coffee b/test/badnets.coffee index a3d3568..175ddf2 100644 --- a/test/badnets.coffee +++ b/test/badnets.coffee @@ -32,6 +32,16 @@ vows.describe('IPs with bytes greater than 255') 'garbage': shouldFailWithError 'Invalid net' .export(module) +vows.describe('Invalid IP format') + .addBatch + ' 1.2.3.4': shouldFailWithError 'Invalid net' + '1. 2.3.4': shouldFailWithError 'Invalid net' + '1.2. 3.4': shouldFailWithError 'Invalid net' + '1.2.3. 4': shouldFailWithError 'Invalid net' + '1.2.3.4 ': shouldFailWithError 'Invalid net' + '1 .2.3.4': shouldFailWithError 'Invalid net' + .export(module) + vows.describe('Ranges that are a power-of-two big, but are not legal blocks') .addBatch '218.0.0.0/221.255.255.255': shouldFailWithError 'Invalid mask'