Skip to content

Commit 0a03e35

Browse files
committed
crypto: migrate crypto.randomBytes to internal/errors
PR-URL: #16454 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 76b8803 commit 0a03e35

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

Diff for: lib/internal/crypto/random.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const errors = require('internal/errors');
44
const { isArrayBufferView } = require('internal/util/types');
55
const {
6-
randomBytes,
6+
randomBytes: _randomBytes,
77
randomFill: _randomFill
88
} = process.binding('crypto');
99

@@ -24,7 +24,7 @@ function assertOffset(offset, length) {
2424
}
2525
}
2626

27-
function assertSize(size, offset, length) {
27+
function assertSize(size, offset = 0, length = Infinity) {
2828
if (typeof size !== 'number' || size !== size) {
2929
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'size', 'number');
3030
}
@@ -38,6 +38,13 @@ function assertSize(size, offset, length) {
3838
}
3939
}
4040

41+
function randomBytes(size, cb) {
42+
assertSize(size);
43+
if (cb !== undefined && typeof cb !== 'function')
44+
throw new errors.TypeError('ERR_INVALID_CALLBACK');
45+
return _randomBytes(size, cb);
46+
}
47+
4148
function randomFillSync(buf, offset = 0, size) {
4249
if (!isArrayBufferView(buf)) {
4350
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',

Diff for: src/node_crypto.cc

+1-6
Original file line numberDiff line numberDiff line change
@@ -5526,13 +5526,8 @@ void RandomBytesProcessSync(Environment* env,
55265526
void RandomBytes(const FunctionCallbackInfo<Value>& args) {
55275527
Environment* env = Environment::GetCurrent(args);
55285528

5529-
if (!args[0]->IsNumber() || args[0].As<v8::Number>()->Value() < 0) {
5530-
return env->ThrowTypeError("size must be a number >= 0");
5531-
}
5532-
55335529
const int64_t size = args[0]->IntegerValue();
5534-
if (size > Buffer::kMaxLength)
5535-
return env->ThrowTypeError("size must be a uint32");
5530+
CHECK(size <= Buffer::kMaxLength);
55365531

55375532
Local<Object> obj = env->randombytes_constructor_template()->
55385533
NewInstance(env->context()).ToLocalChecked();

Diff for: test/parallel/test-crypto-random.js

+26-7
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,26 @@ crypto.DEFAULT_ENCODING = 'buffer';
3333
// bump, we register a lot of exit listeners
3434
process.setMaxListeners(256);
3535

36-
const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
3736
[crypto.randomBytes, crypto.pseudoRandomBytes].forEach(function(f) {
3837
[-1, undefined, null, false, true, {}, []].forEach(function(value) {
39-
assert.throws(function() { f(value); }, expectedErrorRegexp);
40-
assert.throws(function() { f(value, common.mustNotCall()); },
41-
expectedErrorRegexp);
38+
39+
common.expectsError(
40+
() => f(value),
41+
{
42+
code: 'ERR_INVALID_ARG_TYPE',
43+
type: TypeError,
44+
message: /^The "size" argument must be of type (number|uint32)$/
45+
}
46+
);
47+
48+
common.expectsError(
49+
() => f(value, common.mustNotCall()),
50+
{
51+
code: 'ERR_INVALID_ARG_TYPE',
52+
type: TypeError,
53+
message: /^The "size" argument must be of type (number|uint32)$/
54+
}
55+
);
4256
});
4357

4458
[0, 1, 2, 4, 16, 256, 1024, 101.2].forEach(function(len) {
@@ -464,9 +478,14 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
464478

465479
// #5126, "FATAL ERROR: v8::Object::SetIndexedPropertiesToExternalArrayData()
466480
// length exceeds max acceptable value"
467-
assert.throws(function() {
468-
crypto.randomBytes((-1 >>> 0) + 1);
469-
}, /^TypeError: size must be a uint32$/);
481+
common.expectsError(
482+
() => crypto.randomBytes((-1 >>> 0) + 1),
483+
{
484+
code: 'ERR_INVALID_ARG_TYPE',
485+
type: TypeError,
486+
message: 'The "size" argument must be of type uint32'
487+
}
488+
);
470489

471490
[1, true, NaN, null, undefined, {}, []].forEach((i) => {
472491
common.expectsError(

0 commit comments

Comments
 (0)