Skip to content

Commit f64f48d

Browse files
committed
add more checks for keylen
1 parent 14dbae5 commit f64f48d

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

browser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function pbkdf2 (password, salt, iterations, keylen, digest, callback) {
2121
})
2222
}
2323

24-
function checkParameters(iterations, keylen) {
24+
function checkParameters (iterations, keylen) {
2525
if (typeof iterations !== 'number') {
2626
throw new TypeError('Iterations not a number')
2727
}
@@ -34,7 +34,7 @@ function checkParameters(iterations, keylen) {
3434
throw new TypeError('Key length not a number')
3535
}
3636

37-
if (keylen < 0 || keylen > MAX_ALLOC) {
37+
if (keylen < 0 || keylen > MAX_ALLOC || isNaN(keylen)) {
3838
throw new TypeError('Bad key length')
3939
}
4040
}

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function asyncPBKDF2 (password, salt, iterations, keylen, digest, callback) {
1818
throw new TypeError('Key length not a number')
1919
}
2020

21-
if (keylen < 0 || keylen > MAX_ALLOC) {
21+
if (keylen < 0 || keylen > MAX_ALLOC || isNaN(keylen)) {
2222
throw new TypeError('Bad key length')
2323
}
2424

test/fixtures.json

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,6 @@
159159
"iterations": 1,
160160
"dkLen": -1,
161161
"exception": "Bad key length"
162-
},
163-
{
164-
"key": "password",
165-
"salt": "salt",
166-
"iterations": 1,
167-
"dkLen": 4073741824,
168-
"exception": "Bad key length"
169162
}
170163
]
171164
}

test/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@ var compatNode = require('../')
33
var compatBrowser = require('../browser')
44
var fixtures = require('./fixtures')
55

6+
fixtures.invalid.push({
7+
"key": "password",
8+
"salt": "salt",
9+
"iterations": 1,
10+
"dkLen": NaN,
11+
"exception": "Bad key length"
12+
}, {
13+
"key": "password",
14+
"salt": "salt",
15+
"iterations": 1,
16+
"dkLen": Infinity,
17+
"exception": "Bad key length"
18+
})
19+
620
// SHA-1 vectors generated by Node.js
721
// SHA-256/SHA-512 test vectors from:
822
// https://stackoverflow.com/questions/5130513/pbkdf2-hmac-sha2-test-vectors

0 commit comments

Comments
 (0)