Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f3570f2

Browse filesBrowse files
bnoordhuistargos
authored andcommitted
lib: replace checkUint() with validateInt32()
PR-URL: #20816 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 02adb2d commit f3570f2
Copy full SHA for f3570f2

File tree

Expand file treeCollapse file tree

5 files changed

+36
-31
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+36
-31
lines changed
Open diff view settings
Collapse file

‎lib/internal/crypto/pbkdf2.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/pbkdf2.js
+4-6Lines changed: 4 additions & 6 deletions
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
}
Collapse file

‎lib/internal/crypto/scrypt.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/scrypt.js
+11-8Lines changed: 11 additions & 8 deletions
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;
Collapse file

‎lib/internal/crypto/util.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/util.js
-15Lines changed: 0 additions & 15 deletions
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,
Collapse file

‎lib/internal/validators.js‎

Copy file name to clipboardExpand all lines: lib/internal/validators.js
+6Lines changed: 6 additions & 0 deletions
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 = {
Collapse file

‎test/parallel/test-crypto-pbkdf2.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-crypto-pbkdf2.js
+15-2Lines changed: 15 additions & 2 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.