Skip to content

Commit

Permalink
Fix improper parsing of octal bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
rs committed Mar 18, 2021
1 parent b95d113 commit 4678fd8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
8 changes: 7 additions & 1 deletion lib/netmask.coffee
Expand Up @@ -9,8 +9,14 @@ ip2long = (ip) ->
b = (ip + '').split('.'); b = (ip + '').split('.');
if b.length is 0 or b.length > 4 then throw new Error('Invalid IP') if b.length is 0 or b.length > 4 then throw new Error('Invalid IP')
for byte, i in b for byte, i in b
if isNaN parseInt(byte, 10) then throw new Error("Invalid byte: #{byte}") if byte and byte[0] == '0'
# make sure 0 prefixed bytes are parsed as octal
byte = parseInt(byte, 8)
else
byte = parseInt(byte, 10)
if isNaN(byte) then throw new Error("Invalid byte: #{byte}")
if byte < 0 or byte > 255 then throw new Error("Invalid byte: #{byte}") if byte < 0 or byte > 255 then throw new Error("Invalid byte: #{byte}")
b[i] = byte
return ((b[0] or 0) << 24 | (b[1] or 0) << 16 | (b[2] or 0) << 8 | (b[3] or 0)) >>> 0 return ((b[0] or 0) << 24 | (b[1] or 0) << 16 | (b[2] or 0) << 8 | (b[3] or 0)) >>> 0




Expand Down
23 changes: 14 additions & 9 deletions package.json
Expand Up @@ -6,24 +6,29 @@
"homepage": "https://github.com/rs/node-netmask", "homepage": "https://github.com/rs/node-netmask",
"bugs": "https://github.com/rs/node-netmask/issues", "bugs": "https://github.com/rs/node-netmask/issues",
"license": "MIT", "license": "MIT",
"repository": "repository": {
{
"type": "git", "type": "git",
"url": "git://github.com/rs/node-netmask.git" "url": "git://github.com/rs/node-netmask.git"
}, },
"keywords": ["net", "mask", "ip", "network", "cidr", "netmask", "subnet", "ipcalc"], "keywords": [
"net",
"mask",
"ip",
"network",
"cidr",
"netmask",
"subnet",
"ipcalc"
],
"main": "./lib/netmask", "main": "./lib/netmask",
"scripts": "scripts": {
{
"prepublish": "coffee -c lib/*.coffee", "prepublish": "coffee -c lib/*.coffee",
"test": "vows --spec test/*" "test": "vows --spec test/*"
}, },
"engines": "engines": {
{
"node": ">= 0.4.0" "node": ">= 0.4.0"
}, },
"devDependencies": "devDependencies": {
{
"coffee-script": ">=1.2.0", "coffee-script": ">=1.2.0",
"vows": "*" "vows": "*"
} }
Expand Down
8 changes: 8 additions & 0 deletions test/netmasks.coffee
Expand Up @@ -53,6 +53,14 @@ vows.describe('Netmask contains IP')
'block 192.168.0.0/24': 'block 192.168.0.0/24':
topic: -> new Netmask('192.168.0.0/24') topic: -> new Netmask('192.168.0.0/24')
'does not contain block 192.168': (block) -> assert.ok not block.contains('192.168') 'does not contain block 192.168': (block) -> assert.ok not block.contains('192.168')
'block 31.0.0.0/8':
topic: -> new Netmask('31.0.0.0/8')
'contains IP 31.5.5.5': (block) -> assert.ok block.contains('31.5.5.5')
'does not contain IP 031.5.5.5 (25.5.5.5)': (block) -> assert.ok not block.contains('031.5.5.5')
'block 127.0.0.0/8':
topic: -> new Netmask('127.0.0.0/8')
'contains IP 127.0.0.2': (block) -> assert.ok block.contains('127.0.0.2')
'contains IP 0177.0.0.2 (127.0.0.2)': (block) -> assert.ok block.contains('0177.0.0.2')
.export(module) .export(module)


vows.describe('Netmask forEach') vows.describe('Netmask forEach')
Expand Down

0 comments on commit 4678fd8

Please sign in to comment.