GeezerVison is showing github.com — lib: replace checkUint() with validateInt32() · nodejs/node@f3570f2
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/pbkdf2.js +4-6Lines changed: 4 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | const { AsyncWrap, Providers } = process.binding('async_wrap'); | 4 | 4 | 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'); | 6 | 7 | const { | 7 | 8 | ERR_CRYPTO_INVALID_DIGEST, | 8 | 9 | ERR_CRYPTO_PBKDF2_ERROR, | @@ -11,7 +12,6 @@ const { | 11 | 12 | } = require('internal/errors').codes; | 12 | 13 | const { | 13 | 14 | checkIsArrayBufferView, | 14 | | - checkIsUint, | 15 | 15 | getDefaultEncoding, | 16 | 16 | } = require('internal/crypto/util'); | 17 | 17 |
| @@ -59,10 +59,8 @@ function check(password, salt, iterations, keylen, digest, callback) { | 59 | 59 |
| 60 | 60 | password = checkIsArrayBufferView('password', password); | 61 | 61 | 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); | 66 | 64 |
| 67 | 65 | return { password, salt, iterations, keylen, digest }; | 68 | 66 | } | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/scrypt.js +11-8Lines changed: 11 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | const { AsyncWrap, Providers } = process.binding('async_wrap'); | 4 | 4 | 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'); | 6 | 7 | const { | 7 | 8 | ERR_CRYPTO_SCRYPT_INVALID_PARAMETER, | 8 | 9 | ERR_CRYPTO_SCRYPT_NOT_SUPPORTED, | 9 | 10 | ERR_INVALID_CALLBACK, | 10 | 11 | } = require('internal/errors').codes; | 11 | 12 | const { | 12 | 13 | checkIsArrayBufferView, | 13 | | - checkIsUint, | 14 | 14 | getDefaultEncoding, | 15 | 15 | } = require('internal/crypto/util'); | 16 | 16 |
| @@ -75,16 +75,19 @@ function check(password, salt, keylen, options, callback) { | 75 | 75 | throw new ERR_CRYPTO_SCRYPT_NOT_SUPPORTED(); | 76 | 76 |
| 77 | 77 | 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); | 80 | 80 |
| 81 | 81 | let { N, r, p, maxmem } = defaults; | 82 | 82 | 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); | 86 | 89 | if (options.hasOwnProperty('maxmem')) | 87 | | - maxmem = checkIsUint('maxmem', options.maxmem); | | 90 | + maxmem = validateInt32(options.maxmem, 'maxmem', 0, INT_MAX); | 88 | 91 | if (N === 0) N = defaults.N; | 89 | 92 | if (r === 0) r = defaults.r; | 90 | 93 | if (p === 0) p = defaults.p; | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/util.js -15Lines changed: 0 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,7 +16,6 @@ const { | 16 | 16 | ERR_CRYPTO_ENGINE_UNKNOWN, | 17 | 17 | ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH, | 18 | 18 | ERR_INVALID_ARG_TYPE, | 19 | | - ERR_OUT_OF_RANGE, | 20 | 19 | } = require('internal/errors').codes; | 21 | 20 | const { Buffer } = require('buffer'); | 22 | 21 | const { | @@ -26,9 +25,6 @@ const { | 26 | 25 | const { | 27 | 26 | isArrayBufferView | 28 | 27 | } = require('internal/util/types'); | 29 | | -const { | 30 | | - INT_MAX | 31 | | -} = process.binding('constants').crypto; | 32 | 28 |
| 33 | 29 | var defaultEncoding = 'buffer'; | 34 | 30 |
| @@ -99,19 +95,8 @@ function checkIsArrayBufferView(name, buffer) { | 99 | 95 | return buffer; | 100 | 96 | } | 101 | 97 |
| 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 | | - | 112 | 98 | module.exports = { | 113 | 99 | checkIsArrayBufferView, | 114 | | - checkIsUint, | 115 | 100 | getCiphers, | 116 | 101 | getCurves, | 117 | 102 | getDefaultEncoding, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/validators.js +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -71,6 +71,8 @@ function validateInteger(value, name) { | 71 | 71 | Error.captureStackTrace(err, validateInteger); | 72 | 72 | throw err; | 73 | 73 | } | | 74 | + | | 75 | + return value; | 74 | 76 | } | 75 | 77 |
| 76 | 78 | function validateInt32(value, name, min = -2147483648, max = 2147483647) { | @@ -91,6 +93,8 @@ function validateInt32(value, name, min = -2147483648, max = 2147483647) { | 91 | 93 | Error.captureStackTrace(err, validateInt32); | 92 | 94 | throw err; | 93 | 95 | } | | 96 | + | | 97 | + return value; | 94 | 98 | } | 95 | 99 |
| 96 | 100 | function validateUint32(value, name, positive) { | @@ -112,6 +116,8 @@ function validateUint32(value, name, positive) { | 112 | 116 | Error.captureStackTrace(err, validateUint32); | 113 | 117 | throw err; | 114 | 118 | } | | 119 | + | | 120 | + return value; | 115 | 121 | } | 116 | 122 |
| 117 | 123 | module.exports = { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-crypto-pbkdf2.js +15-2Lines changed: 15 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -71,7 +71,7 @@ assert.throws( | 71 | 71 | code: 'ERR_OUT_OF_RANGE', | 72 | 72 | name: 'RangeError [ERR_OUT_OF_RANGE]', | 73 | 73 | 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' | 75 | 75 | } | 76 | 76 | ); | 77 | 77 |
| @@ -87,7 +87,20 @@ assert.throws( | 87 | 87 | }); | 88 | 88 | }); | 89 | 89 |
| 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) => { | 91 | 104 | assert.throws( | 92 | 105 | () => { | 93 | 106 | crypto.pbkdf2('password', 'salt', 1, input, 'sha256', | |
|