GeezerVison is showing github.com — crypto: unify validation of checkPrime checks · nodejs/node@254a03b
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/random.js +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -52,7 +52,6 @@ const { | 52 | 52 | validateFunction, | 53 | 53 | validateInt32, | 54 | 54 | validateObject, | 55 | | - validateUint32, | 56 | 55 | } = require('internal/validators'); | 57 | 56 |
| 58 | 57 | const { | @@ -563,7 +562,8 @@ function checkPrime(candidate, options = kEmptyObject, callback) { | 563 | 562 | checks = 0, | 564 | 563 | } = options; | 565 | 564 |
| 566 | | - validateUint32(checks, 'options.checks'); | | 565 | + // The checks option is unsigned but must fit into a signed C int for OpenSSL. | | 566 | + validateInt32(checks, 'options.checks', 0); | 567 | 567 |
| 568 | 568 | const job = new CheckPrimeJob(kCryptoJobAsync, candidate, checks); | 569 | 569 | job.ondone = callback; | @@ -591,7 +591,8 @@ function checkPrimeSync(candidate, options = kEmptyObject) { | 591 | 591 | checks = 0, | 592 | 592 | } = options; | 593 | 593 |
| 594 | | - validateUint32(checks, 'options.checks'); | | 594 | + // The checks option is unsigned but must fit into a signed C int for OpenSSL. | | 595 | + validateInt32(checks, 'options.checks', 0); | 595 | 596 |
| 596 | 597 | const job = new CheckPrimeJob(kCryptoJobSync, candidate, checks); | 597 | 598 | const { 0: err, 1: result } = job.run(); | |
Collapse file Copy file name to clipboardExpand all lines: src/crypto/crypto_random.cc +4-11Lines changed: 4 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,6 +15,7 @@ using v8::ArrayBuffer; | 15 | 15 | using v8::BackingStore; | 16 | 16 | using v8::False; | 17 | 17 | using v8::FunctionCallbackInfo; | | 18 | +using v8::Int32; | 18 | 19 | using v8::Just; | 19 | 20 | using v8::Local; | 20 | 21 | using v8::Maybe; | @@ -185,8 +186,6 @@ Maybe<bool> CheckPrimeTraits::AdditionalConfig( | 185 | 186 | const FunctionCallbackInfo<Value>& args, | 186 | 187 | unsigned int offset, | 187 | 188 | CheckPrimeConfig* params) { | 188 | | - Environment* env = Environment::GetCurrent(args); | 189 | | - | 190 | 189 | ArrayBufferOrViewContents<unsigned char> candidate(args[offset]); | 191 | 190 |
| 192 | 191 | params->candidate = | @@ -195,15 +194,9 @@ Maybe<bool> CheckPrimeTraits::AdditionalConfig( | 195 | 194 | candidate.size(), | 196 | 195 | nullptr)); | 197 | 196 |
| 198 | | - CHECK(args[offset + 1]->IsUint32()); // Checks | 199 | | - | 200 | | - const int checks = static_cast<int>(args[offset + 1].As<Uint32>()->Value()); | 201 | | - if (checks < 0) { | 202 | | - THROW_ERR_OUT_OF_RANGE(env, "invalid options.checks"); | 203 | | - return Nothing<bool>(); | 204 | | - } | 205 | | - | 206 | | - params->checks = checks; | | 197 | + CHECK(args[offset + 1]->IsInt32()); // Checks | | 198 | + params->checks = args[offset + 1].As<Int32>()->Value(); | | 199 | + CHECK_GE(params->checks, 0); | 207 | 200 |
| 208 | 201 | return Just(true); | 209 | 202 | } | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-crypto-prime.js +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -240,6 +240,17 @@ for (const checks of ['hello', {}, []]) { | 240 | 240 | }); | 241 | 241 | } | 242 | 242 |
| | 243 | +for (const checks of [-(2 ** 31), -1, 2 ** 31, 2 ** 32 - 1, 2 ** 32, 2 ** 50]) { | | 244 | + assert.throws(() => checkPrime(2n, { checks }, common.mustNotCall()), { | | 245 | + code: 'ERR_OUT_OF_RANGE', | | 246 | + message: /<= 2147483647/ | | 247 | + }); | | 248 | + assert.throws(() => checkPrimeSync(2n, { checks }), { | | 249 | + code: 'ERR_OUT_OF_RANGE', | | 250 | + message: /<= 2147483647/ | | 251 | + }); | | 252 | +} | | 253 | + | 243 | 254 | assert(!checkPrimeSync(Buffer.from([0x1]))); | 244 | 255 | assert(checkPrimeSync(Buffer.from([0x2]))); | 245 | 256 | assert(checkPrimeSync(Buffer.from([0x3]))); | |
|