Skip to content

Commit f3570f2

Browse files
bnoordhuistargos
authored andcommitted
lib: replace checkUint() with validateInt32()
PR-URL: #20816 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 02adb2d commit f3570f2

File tree

5 files changed

+36
-31
lines changed

5 files changed

+36
-31
lines changed

lib/internal/crypto/pbkdf2.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
const { AsyncWrap, Providers } = process.binding('async_wrap');
44
const { Buffer } = require('buffer');
5-
const { pbkdf2: _pbkdf2 } = process.binding('crypto');
5+
const { INT_MAX, pbkdf2: _pbkdf2 } = process.binding('crypto');
6+
const { validateInt32 } = require('internal/validators');
67
const {
78
ERR_CRYPTO_INVALID_DIGEST,
89
ERR_CRYPTO_PBKDF2_ERROR,
@@ -11,7 +12,6 @@ const {
1112
} = require('internal/errors').codes;
1213
const {
1314
checkIsArrayBufferView,
14-
checkIsUint,
1515
getDefaultEncoding,
1616
} = require('internal/crypto/util');
1717

@@ -59,10 +59,8 @@ function check(password, salt, iterations, keylen, digest, callback) {
5959

6060
password = checkIsArrayBufferView('password', password);
6161
salt = checkIsArrayBufferView('salt', salt);
62-
// FIXME(bnoordhuis) The error message is in fact wrong since |iterations|
63-
// cannot be > INT_MAX. Adjust in the next major release.
64-
iterations = checkIsUint('iterations', iterations, 'a non-negative number');
65-
keylen = checkIsUint('keylen', keylen);
62+
iterations = validateInt32(iterations, 'iterations', 0, INT_MAX);
63+
keylen = validateInt32(keylen, 'keylen', 0, INT_MAX);
6664

6765
return { password, salt, iterations, keylen, digest };
6866
}

lib/internal/crypto/scrypt.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
const { AsyncWrap, Providers } = process.binding('async_wrap');
44
const { Buffer } = require('buffer');
5-
const { scrypt: _scrypt } = process.binding('crypto');
5+
const { INT_MAX, scrypt: _scrypt } = process.binding('crypto');
6+
const { validateInt32 } = require('internal/validators');
67
const {
78
ERR_CRYPTO_SCRYPT_INVALID_PARAMETER,
89
ERR_CRYPTO_SCRYPT_NOT_SUPPORTED,
910
ERR_INVALID_CALLBACK,
1011
} = require('internal/errors').codes;
1112
const {
1213
checkIsArrayBufferView,
13-
checkIsUint,
1414
getDefaultEncoding,
1515
} = require('internal/crypto/util');
1616

@@ -75,16 +75,19 @@ function check(password, salt, keylen, options, callback) {
7575
throw new ERR_CRYPTO_SCRYPT_NOT_SUPPORTED();
7676

7777
password = checkIsArrayBufferView('password', password);
78-
salt = checkIsArrayBufferView('salt', salt);
79-
keylen = checkIsUint('keylen', keylen);
78+
salt = checkIsArrayBufferView(salt, 'salt');
79+
keylen = validateInt32(keylen, 'keylen', 0, INT_MAX);
8080

8181
let { N, r, p, maxmem } = defaults;
8282
if (options && options !== defaults) {
83-
if (options.hasOwnProperty('N')) N = checkIsUint('N', options.N);
84-
if (options.hasOwnProperty('r')) r = checkIsUint('r', options.r);
85-
if (options.hasOwnProperty('p')) p = checkIsUint('p', options.p);
83+
if (options.hasOwnProperty('N'))
84+
N = validateInt32(options.N, 'N', 0, INT_MAX);
85+
if (options.hasOwnProperty('r'))
86+
r = validateInt32(options.r, 'r', 0, INT_MAX);
87+
if (options.hasOwnProperty('p'))
88+
p = validateInt32(options.p, 'p', 0, INT_MAX);
8689
if (options.hasOwnProperty('maxmem'))
87-
maxmem = checkIsUint('maxmem', options.maxmem);
90+
maxmem = validateInt32(options.maxmem, 'maxmem', 0, INT_MAX);
8891
if (N === 0) N = defaults.N;
8992
if (r === 0) r = defaults.r;
9093
if (p === 0) p = defaults.p;

lib/internal/crypto/util.js

-15
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ const {
1616
ERR_CRYPTO_ENGINE_UNKNOWN,
1717
ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH,
1818
ERR_INVALID_ARG_TYPE,
19-
ERR_OUT_OF_RANGE,
2019
} = require('internal/errors').codes;
2120
const { Buffer } = require('buffer');
2221
const {
@@ -26,9 +25,6 @@ const {
2625
const {
2726
isArrayBufferView
2827
} = require('internal/util/types');
29-
const {
30-
INT_MAX
31-
} = process.binding('constants').crypto;
3228

3329
var defaultEncoding = 'buffer';
3430

@@ -99,19 +95,8 @@ function checkIsArrayBufferView(name, buffer) {
9995
return buffer;
10096
}
10197

102-
function checkIsUint(name, value, errmsg = `>= 0 && <= ${INT_MAX}`) {
103-
if (typeof value !== 'number')
104-
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
105-
106-
if (value < 0 || !Number.isInteger(value) || value > INT_MAX)
107-
throw new ERR_OUT_OF_RANGE(name, errmsg, value);
108-
109-
return value;
110-
}
111-
11298
module.exports = {
11399
checkIsArrayBufferView,
114-
checkIsUint,
115100
getCiphers,
116101
getCurves,
117102
getDefaultEncoding,

lib/internal/validators.js

+6
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ function validateInteger(value, name) {
7171
Error.captureStackTrace(err, validateInteger);
7272
throw err;
7373
}
74+
75+
return value;
7476
}
7577

7678
function validateInt32(value, name, min = -2147483648, max = 2147483647) {
@@ -91,6 +93,8 @@ function validateInt32(value, name, min = -2147483648, max = 2147483647) {
9193
Error.captureStackTrace(err, validateInt32);
9294
throw err;
9395
}
96+
97+
return value;
9498
}
9599

96100
function validateUint32(value, name, positive) {
@@ -112,6 +116,8 @@ function validateUint32(value, name, positive) {
112116
Error.captureStackTrace(err, validateUint32);
113117
throw err;
114118
}
119+
120+
return value;
115121
}
116122

117123
module.exports = {

test/parallel/test-crypto-pbkdf2.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ assert.throws(
7171
code: 'ERR_OUT_OF_RANGE',
7272
name: 'RangeError [ERR_OUT_OF_RANGE]',
7373
message: 'The value of "iterations" is out of range. ' +
74-
'It must be a non-negative number. Received -1'
74+
'It must be >= 0 && <= 2147483647. Received -1'
7575
}
7676
);
7777

@@ -87,7 +87,20 @@ assert.throws(
8787
});
8888
});
8989

90-
[Infinity, -Infinity, NaN, -1, 4073741824, INT_MAX + 1].forEach((input) => {
90+
[Infinity, -Infinity, NaN].forEach((input) => {
91+
assert.throws(
92+
() => {
93+
crypto.pbkdf2('password', 'salt', 1, input, 'sha256',
94+
common.mustNotCall());
95+
}, {
96+
code: 'ERR_OUT_OF_RANGE',
97+
name: 'RangeError [ERR_OUT_OF_RANGE]',
98+
message: 'The value of "keylen" is out of range. It ' +
99+
`must be an integer. Received ${input}`
100+
});
101+
});
102+
103+
[-1, 4073741824, INT_MAX + 1].forEach((input) => {
91104
assert.throws(
92105
() => {
93106
crypto.pbkdf2('password', 'salt', 1, input, 'sha256',

0 commit comments

Comments
 (0)