Skip to content

Commit c78d9ba

Browse files
committed
fix: don't allow NaN/Infinity params
1 parent 3f106c1 commit c78d9ba

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Diff for: scrypt.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@
251251
}
252252

253253
function ensureInteger(value, name) {
254-
if (typeof(value) !== "number" || (value % 1)) { throw new Error('invalid ' + name); }
254+
if (!Number.isInteger(value)) { throw new Error('invalid ' + name); }
255255
return value;
256256
}
257257

Diff for: test/test-scrypt.js

+14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ const scrypt = require('../scrypt.js');
66

77
const testVectors = require('./test-vectors.json');
88

9+
for (const invalid of [NaN, Infinity, -Infinity, 0.5, null]) {
10+
for (let i = 0; i < 4; i++) {
11+
const password = Buffer.from('password');
12+
const salt = Buffer.from('salt');
13+
const argname = ['N', 'r', 'p', 'dkLen'][i];
14+
const args = [16, 1, 1, 64];
15+
args[i] = invalid;
16+
it(`Test ${invalid} rejection ${i}`, () => assert.rejects(
17+
() => scrypt.scrypt(password, salt, ...args, () => {}),
18+
{ message: `invalid ${argname}` }
19+
));
20+
}
21+
}
22+
923
for (let i = 0; i < testVectors.length; i++) {
1024
const test = testVectors[i];
1125

0 commit comments

Comments
 (0)