crypto: improve prime size argument validation · nodejs/node@e8697cf · GitHub
Skip to content

Commit e8697cf

Browse files
authored
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 9412441 commit e8697cf

3 files changed

Lines changed: 12 additions & 11 deletions

File tree

lib/internal/crypto/random.js

Lines changed: 3 additions & 2 deletions

src/crypto/crypto_random.cc

Lines changed: 2 additions & 4 deletions
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

Lines changed: 7 additions & 5 deletions

0 commit comments

Comments
 (0)