Skip to content

Commit

Permalink
add more checks for keylen
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid committed Feb 24, 2016
1 parent 14dbae5 commit f64f48d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
4 changes: 2 additions & 2 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function pbkdf2 (password, salt, iterations, keylen, digest, callback) {
})
}

function checkParameters(iterations, keylen) {
function checkParameters (iterations, keylen) {
if (typeof iterations !== 'number') {
throw new TypeError('Iterations not a number')
}
Expand All @@ -34,7 +34,7 @@ function checkParameters(iterations, keylen) {
throw new TypeError('Key length not a number')
}

if (keylen < 0 || keylen > MAX_ALLOC) {
if (keylen < 0 || keylen > MAX_ALLOC || isNaN(keylen)) {
throw new TypeError('Bad key length')
}
}
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function asyncPBKDF2 (password, salt, iterations, keylen, digest, callback) {
throw new TypeError('Key length not a number')
}

if (keylen < 0 || keylen > MAX_ALLOC) {
if (keylen < 0 || keylen > MAX_ALLOC || isNaN(keylen)) {
throw new TypeError('Bad key length')
}

Expand Down
7 changes: 0 additions & 7 deletions test/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,6 @@
"iterations": 1,
"dkLen": -1,
"exception": "Bad key length"
},
{
"key": "password",
"salt": "salt",
"iterations": 1,
"dkLen": 4073741824,
"exception": "Bad key length"
}
]
}
14 changes: 14 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ var compatNode = require('../')
var compatBrowser = require('../browser')
var fixtures = require('./fixtures')

fixtures.invalid.push({
"key": "password",
"salt": "salt",
"iterations": 1,
"dkLen": NaN,
"exception": "Bad key length"
}, {
"key": "password",
"salt": "salt",
"iterations": 1,
"dkLen": Infinity,
"exception": "Bad key length"
})

// SHA-1 vectors generated by Node.js
// SHA-256/SHA-512 test vectors from:
// https://stackoverflow.com/questions/5130513/pbkdf2-hmac-sha2-test-vectors
Expand Down

0 comments on commit f64f48d

Please sign in to comment.