Skip to content

Commit 5827917

Browse files
committed
Fix "class notations" parsing
1 parent d1bd7d3 commit 5827917

3 files changed

Lines changed: 39 additions & 14 deletions

File tree

lib/netmask.coffee

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,29 @@ ip2long = (ip) ->
1919
b.push(n)
2020
if ip.length != 0
2121
throw new Error('Invalid IP')
22-
while b.length < 4
23-
b.unshift(0)
24-
return (b[0] << 24 | b[1] << 16 | b[2] << 8 | b[3]) >>> 0
22+
switch b.length
23+
when 1
24+
# Long input notation
25+
if b[0] > 0xFFFFFFFF
26+
throw new Error('Invalid IP')
27+
return b[0] >>> 0
28+
when 2
29+
# Class A notation
30+
if b[0] > 0xFF or b[1] > 0xFFFFFF
31+
throw new Error('Invalid IP')
32+
return (b[0] << 24 | b[1]) >>> 0
33+
when 3
34+
# Class B notation
35+
if b[0] > 0xFF or b[1] > 0xFF or b[2] > 0xFFFF
36+
throw new Error('Invalid IP')
37+
return (b[0] << 24 | b[1] << 16 | b[2]) >>> 0
38+
when 4
39+
# Dotted quad notation
40+
if b[0] > 0xFF or b[1] > 0xFF or b[2] > 0xFF or b[3] > 0xFF
41+
throw new Error('Invalid IP')
42+
return (b[0] << 24 | b[1] << 16 | b[2] << 8 | b[3]) >>> 0
43+
else
44+
throw new Error('Invalid IP')
2545

2646
chr = (b) ->
2747
return b.charCodeAt(0)
@@ -46,18 +66,18 @@ atob = (s) ->
4666
start = i
4767
while s.length > 0
4868
if '0' <= s[i] and s[i] <= dmax
49-
n = n*base + (chr(s[i])-chr0)
69+
n = (n*base + (chr(s[i])-chr0)) >>> 0
5070
else if base == 16
5171
if 'a' <= s[i] and s[i] <= 'f'
52-
n = n*base + (10+chr(s[i])-chra)
72+
n = (n*base + (10+chr(s[i])-chra)) >>> 0
5373
else if 'A' <= s[i] and s[i] <= 'F'
54-
n = n*base + (10+chr(s[i])-chrA)
74+
n = (n*base + (10+chr(s[i])-chrA)) >>> 0
5575
else
5676
break
5777
else
5878
break
59-
if n > 0xFF
60-
throw new Error('byte overflow')
79+
if n > 0xFFFFFFFF
80+
throw new Error('too large')
6181
i++
6282
if i == start
6383
throw new Error('empty octet')

test/badnets.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ vows.describe('IPs with bytes greater than 255')
2626
'209.500.70.33/19': shouldFailWithError 'Invalid net'
2727
'140.999.82': shouldFailWithError 'Invalid net'
2828
'899.174': shouldFailWithError 'Invalid net'
29-
'900': shouldFailWithError 'Invalid net'
30-
'209.157.300/19': shouldFailWithError 'Invalid net'
29+
'209.157.65536/19': shouldFailWithError 'Invalid net'
3130
'209.300.64.0.10': shouldFailWithError 'Invalid net'
3231
'garbage': shouldFailWithError 'Invalid net'
3332
.export(module)
33+
3434

3535
vows.describe('Invalid IP format')
3636
.addBatch

test/netmasks.coffee

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,19 @@ fixtures =
1010
['209.157.68.22', '255.255.224.0', '209.157.64.0', '255.255.224.0', 19]
1111
['209.157.70.33/19', null, '209.157.64.0', '255.255.224.0', 19]
1212
['209.157.70.33', null, '209.157.70.33', '255.255.255.255', 32]
13-
['140.174.82', null, '0.140.174.82', '255.255.255.255', 32]
14-
['140.174', null, '0.0.140.174', '255.255.255.255', 32]
13+
['140.174.82', null, '140.174.0.82', '255.255.255.255', 32]
14+
['140.174', null, '140.0.0.174', '255.255.255.255', 32]
1515
['10', null, '0.0.0.10', '255.255.255.255', 32]
1616
['10/8', null, '0.0.0.0', '255.0.0.0', 8]
17-
['209.157.64/19', null, '0.209.128.0', '255.255.224.0', 19]
17+
['209.157.64/19', null, '209.157.0.0', '255.255.224.0', 19]
1818
['216.140.48.16/32', null, '216.140.48.16', '255.255.255.255', 32]
19-
['209.157/17', null, '0.0.128.0', '255.255.128.0', 17]
19+
['209.157/17', null, '209.0.0.0', '255.255.128.0', 17]
2020
['0.0.0.0/0', null, '0.0.0.0', '0.0.0.0', 0]
21+
['0xffffffff', null, '255.255.255.255', '255.255.255.255', 32]
22+
['1.1', null, '1.0.0.1', '255.255.255.255', 32]
23+
['1.0xffffff', null, '1.255.255.255', '255.255.255.255', 32]
24+
['1.2.3', null, '1.2.0.3', '255.255.255.255', 32]
25+
['1.2.0xffff', null, '1.2.255.255', '255.255.255.255', 32]
2126
]
2227

2328
contexts = []

0 commit comments

Comments
 (0)