Permalink
Browse files

crypto: migrate crypto.randomBytes to internal/errors

PR-URL: #16454
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information...
jasnell committed Oct 24, 2017
1 parent 76b8803 commit 0a03e350fb6fac67ac85809af947c8dce1a8e569
Showing with 36 additions and 15 deletions.
  1. +9 −2 lib/internal/crypto/random.js
  2. +1 −6 src/node_crypto.cc
  3. +26 −7 test/parallel/test-crypto-random.js
@@ -3,7 +3,7 @@
const errors = require('internal/errors');
const { isArrayBufferView } = require('internal/util/types');
const {
randomBytes,
randomBytes: _randomBytes,
randomFill: _randomFill
} = process.binding('crypto');
@@ -24,7 +24,7 @@ function assertOffset(offset, length) {
}
}
function assertSize(size, offset, length) {
function assertSize(size, offset = 0, length = Infinity) {
if (typeof size !== 'number' || size !== size) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'size', 'number');
}
@@ -38,6 +38,13 @@ function assertSize(size, offset, length) {
}
}
function randomBytes(size, cb) {
assertSize(size);
if (cb !== undefined && typeof cb !== 'function')
throw new errors.TypeError('ERR_INVALID_CALLBACK');
return _randomBytes(size, cb);
}
function randomFillSync(buf, offset = 0, size) {
if (!isArrayBufferView(buf)) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
View
@@ -5526,13 +5526,8 @@ void RandomBytesProcessSync(Environment* env,
void RandomBytes(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
if (!args[0]->IsNumber() || args[0].As<v8::Number>()->Value() < 0) {
return env->ThrowTypeError("size must be a number >= 0");
}
const int64_t size = args[0]->IntegerValue();
if (size > Buffer::kMaxLength)
return env->ThrowTypeError("size must be a uint32");
CHECK(size <= Buffer::kMaxLength);
Local<Object> obj = env->randombytes_constructor_template()->
NewInstance(env->context()).ToLocalChecked();
@@ -33,12 +33,26 @@ crypto.DEFAULT_ENCODING = 'buffer';
// bump, we register a lot of exit listeners
process.setMaxListeners(256);
const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
[crypto.randomBytes, crypto.pseudoRandomBytes].forEach(function(f) {
[-1, undefined, null, false, true, {}, []].forEach(function(value) {
assert.throws(function() { f(value); }, expectedErrorRegexp);
assert.throws(function() { f(value, common.mustNotCall()); },
expectedErrorRegexp);
common.expectsError(
() => f(value),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: /^The "size" argument must be of type (number|uint32)$/
}
);
common.expectsError(
() => f(value, common.mustNotCall()),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: /^The "size" argument must be of type (number|uint32)$/
}
);
});
[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$/;
// #5126, "FATAL ERROR: v8::Object::SetIndexedPropertiesToExternalArrayData()
// length exceeds max acceptable value"
assert.throws(function() {
crypto.randomBytes((-1 >>> 0) + 1);
}, /^TypeError: size must be a uint32$/);
common.expectsError(
() => crypto.randomBytes((-1 >>> 0) + 1),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "size" argument must be of type uint32'
}
);
[1, true, NaN, null, undefined, {}, []].forEach((i) => {
common.expectsError(

5 comments on commit 0a03e35

@Trott

This comment has been minimized.

Show comment
Hide comment
@Trott

Trott Oct 26, 2017

Member

First line longer than 50 chars.

@jasnell If this is an intentional violation of that rule, perhaps we should consider a PR to change that particular requirement to a guideline rather than a rule.

Member

Trott replied Oct 26, 2017

First line longer than 50 chars.

@jasnell If this is an intentional violation of that rule, perhaps we should consider a PR to change that particular requirement to a guideline rather than a rule.

@jasnell

This comment has been minimized.

Show comment
Hide comment
@jasnell

jasnell Oct 26, 2017

Member

Yeah I've been strongly considering proposing we relax that a bit. I understand why it exists but it's extremely difficult to work with at times

Member

jasnell replied Oct 26, 2017

Yeah I've been strongly considering proposing we relax that a bit. I understand why it exists but it's extremely difficult to work with at times

@Trott

This comment has been minimized.

Show comment
Hide comment
@Trott

Trott Oct 27, 2017

Member

Yeah I've been strongly considering proposing we relax that a bit. I understand why it exists but it's extremely difficult to work with at times

In the spirit of "leaders should be held to at least the same standard as everyone else, if not a higher standard", I think we're obligated to at least open a PR to suggest such a change if we're not going to comply with it fastidiously ourselves.

Member

Trott replied Oct 27, 2017

Yeah I've been strongly considering proposing we relax that a bit. I understand why it exists but it's extremely difficult to work with at times

In the spirit of "leaders should be held to at least the same standard as everyone else, if not a higher standard", I think we're obligated to at least open a PR to suggest such a change if we're not going to comply with it fastidiously ourselves.

@apapirovski

This comment has been minimized.

Show comment
Hide comment
@apapirovski
Member

apapirovski replied Oct 27, 2017

@Trott see #16523 :)

@Trott

This comment has been minimized.

Show comment
Hide comment
@Trott

Trott Oct 27, 2017

Member

Cool, my comment was merely 13 hours too late. :-D

Member

Trott replied Oct 27, 2017

Cool, my comment was merely 13 hours too late. :-D

Please sign in to comment.