Skip to content

Commit

Permalink
Add checks on spaces before and after bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
rs committed Mar 25, 2021
1 parent accd535 commit 6a3169c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/netmask.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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'
Expand Down
10 changes: 10 additions & 0 deletions test/badnets.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 6a3169c

Please sign in to comment.