Skip to content

Commit b298291

Browse files
tniessenjuanarbol
authored andcommitted
crypto: improve prime size argument validation
The current validation in JavaScript is insufficient and also produces an incorrect error message, restricting the size parameter to 32-bit values, whereas the C++ backend restricts the size parameter to the positive range of an int. This change tightens the validation in JavaScript and adapts the error message accordingly, making the validation in C++ superfluous. Refs: #42207 PR-URL: #42234 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent b96cfde commit b298291

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

lib/internal/crypto/random.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const {
4343
validateNumber,
4444
validateBoolean,
4545
validateCallback,
46+
validateInt32,
4647
validateObject,
4748
validateUint32,
4849
} = require('internal/validators');
@@ -460,7 +461,7 @@ function createRandomPrimeJob(type, size, options) {
460461
}
461462

462463
function generatePrime(size, options, callback) {
463-
validateUint32(size, 'size', true);
464+
validateInt32(size, 'size', 1);
464465
if (typeof options === 'function') {
465466
callback = options;
466467
options = {};
@@ -482,7 +483,7 @@ function generatePrime(size, options, callback) {
482483
}
483484

484485
function generatePrimeSync(size, options = {}) {
485-
validateUint32(size, 'size', true);
486+
validateInt32(size, 'size', 1);
486487

487488
const job = createRandomPrimeJob(kCryptoJobSync, size, options);
488489
const { 0: err, 1: prime } = job.run();

src/crypto/crypto_random.cc

+2-4
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,9 @@ Maybe<bool> RandomPrimeTraits::AdditionalConfig(
122122
}
123123
}
124124

125+
// The JS interface already ensures that the (positive) size fits into an int.
125126
int bits = static_cast<int>(size);
126-
if (bits < 0) {
127-
THROW_ERR_OUT_OF_RANGE(env, "invalid size");
128-
return Nothing<bool>();
129-
}
127+
CHECK_GT(bits, 0);
130128

131129
if (params->add) {
132130
if (BN_num_bits(params->add.get()) > bits) {

test/parallel/test-crypto-prime.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ const pCheckPrime = promisify(checkPrime);
4141
});
4242
});
4343

44-
[-1, 0, 2 ** 31, 2 ** 31 + 1, 2 ** 32 - 1, 2 ** 32].forEach((i) => {
45-
assert.throws(() => generatePrime(i, common.mustNotCall()), {
46-
code: 'ERR_OUT_OF_RANGE'
44+
[-1, 0, 2 ** 31, 2 ** 31 + 1, 2 ** 32 - 1, 2 ** 32].forEach((size) => {
45+
assert.throws(() => generatePrime(size, common.mustNotCall()), {
46+
code: 'ERR_OUT_OF_RANGE',
47+
message: />= 1 && <= 2147483647/
4748
});
48-
assert.throws(() => generatePrimeSync(i), {
49-
code: 'ERR_OUT_OF_RANGE'
49+
assert.throws(() => generatePrimeSync(size), {
50+
code: 'ERR_OUT_OF_RANGE',
51+
message: />= 1 && <= 2147483647/
5052
});
5153
});
5254

0 commit comments

Comments
 (0)