Skip to content

Commit 4a094dc

Browse files
tniessenUlisesGascon
authored andcommitted
src: rename IsAnyByteSource to IsAnyBufferSource
The current name is somewhat confusing. There is an internal ByteSource class, which is entirely unrelated to what IsAnyByteSource() does, even though both exist in the crypto subsystem of the C++ code. ByteSource objects can also be constructed from strings, for example, for which IsAnyByteSource() returns false. Web IDL calls the types for which this function returns true BufferSource. This type is commonly used across Web Crypto, for example. Thus, rename the function to match the Web IDL naming. Because the function also appears to accept BufferSource objects backed by SharedArrayBuffer instances, the exact Web IDL name would be AllowSharedBufferSource, but that seems unnecessarily long, so I decided to stick with "any". PR-URL: #49346 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com>
1 parent 0d23c1d commit 4a094dc

9 files changed

+22
-21
lines changed

src/crypto/crypto_aes.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ bool ValidateAuthTag(
381381
AESCipherConfig* params) {
382382
switch (cipher_mode) {
383383
case kWebCryptoCipherDecrypt: {
384-
if (!IsAnyByteSource(value)) {
384+
if (!IsAnyBufferSource(value)) {
385385
THROW_ERR_CRYPTO_INVALID_TAG_LENGTH(env);
386386
return false;
387387
}
@@ -419,7 +419,7 @@ bool ValidateAdditionalData(
419419
Local<Value> value,
420420
AESCipherConfig* params) {
421421
// Additional Data
422-
if (IsAnyByteSource(value)) {
422+
if (IsAnyBufferSource(value)) {
423423
ArrayBufferOrViewContents<char> additional(value);
424424
if (UNLIKELY(!additional.CheckSizeInt32())) {
425425
THROW_ERR_OUT_OF_RANGE(env, "additionalData is too big");

src/crypto/crypto_ec.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ ECPointPointer ECDH::BufferToPoint(Environment* env,
193193
void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
194194
Environment* env = Environment::GetCurrent(args);
195195

196-
CHECK(IsAnyByteSource(args[0]));
196+
CHECK(IsAnyBufferSource(args[0]));
197197

198198
ECDH* ecdh;
199199
ASSIGN_OR_RETURN_UNWRAP(&ecdh, args.Holder());
@@ -347,7 +347,7 @@ void ECDH::SetPublicKey(const FunctionCallbackInfo<Value>& args) {
347347
ECDH* ecdh;
348348
ASSIGN_OR_RETURN_UNWRAP(&ecdh, args.Holder());
349349

350-
CHECK(IsAnyByteSource(args[0]));
350+
CHECK(IsAnyBufferSource(args[0]));
351351

352352
MarkPopErrorOnReturn mark_pop_error_on_return;
353353

@@ -393,7 +393,7 @@ void ECDH::ConvertKey(const FunctionCallbackInfo<Value>& args) {
393393
Environment* env = Environment::GetCurrent(args);
394394

395395
CHECK_EQ(args.Length(), 3);
396-
CHECK(IsAnyByteSource(args[0]));
396+
CHECK(IsAnyBufferSource(args[0]));
397397

398398
ArrayBufferOrViewContents<char> args0(args[0]);
399399
if (UNLIKELY(!args0.CheckSizeInt32()))

src/crypto/crypto_hkdf.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ Maybe<bool> HKDFTraits::AdditionalConfig(
5151

5252
CHECK(args[offset]->IsString()); // Hash
5353
CHECK(args[offset + 1]->IsObject()); // Key
54-
CHECK(IsAnyByteSource(args[offset + 2])); // Salt
55-
CHECK(IsAnyByteSource(args[offset + 3])); // Info
54+
CHECK(IsAnyBufferSource(args[offset + 2])); // Salt
55+
CHECK(IsAnyBufferSource(args[offset + 3])); // Info
5656
CHECK(args[offset + 4]->IsUint32()); // Length
5757

5858
Utf8Value hash(env->isolate(), args[offset]);

src/crypto/crypto_keys.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ ManagedEVPPKey::GetPrivateKeyEncodingFromJs(
699699
(*offset)++;
700700
}
701701

702-
if (IsAnyByteSource(args[*offset])) {
702+
if (IsAnyBufferSource(args[*offset])) {
703703
CHECK_IMPLIES(context != kKeyContextInput, result.cipher_ != nullptr);
704704
ArrayBufferOrViewContents<char> passphrase(args[*offset]);
705705
if (UNLIKELY(!passphrase.CheckSizeInt32())) {
@@ -730,7 +730,7 @@ ManagedEVPPKey ManagedEVPPKey::GetPrivateKeyFromJs(
730730
const FunctionCallbackInfo<Value>& args,
731731
unsigned int* offset,
732732
bool allow_key_object) {
733-
if (args[*offset]->IsString() || IsAnyByteSource(args[*offset])) {
733+
if (args[*offset]->IsString() || IsAnyBufferSource(args[*offset])) {
734734
Environment* env = Environment::GetCurrent(args);
735735
ByteSource key = ByteSource::FromStringOrBuffer(env, args[(*offset)++]);
736736
NonCopyableMaybe<PrivateKeyEncodingConfig> config =
@@ -756,7 +756,7 @@ ManagedEVPPKey ManagedEVPPKey::GetPrivateKeyFromJs(
756756
ManagedEVPPKey ManagedEVPPKey::GetPublicOrPrivateKeyFromJs(
757757
const FunctionCallbackInfo<Value>& args,
758758
unsigned int* offset) {
759-
if (IsAnyByteSource(args[*offset])) {
759+
if (IsAnyBufferSource(args[*offset])) {
760760
Environment* env = Environment::GetCurrent(args);
761761
ArrayBufferOrViewContents<char> data(args[(*offset)++]);
762762
if (UNLIKELY(!data.CheckSizeInt32())) {

src/crypto/crypto_random.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Maybe<bool> RandomBytesTraits::AdditionalConfig(
3939
const FunctionCallbackInfo<Value>& args,
4040
unsigned int offset,
4141
RandomBytesConfig* params) {
42-
CHECK(IsAnyByteSource(args[offset])); // Buffer to fill
42+
CHECK(IsAnyBufferSource(args[offset])); // Buffer to fill
4343
CHECK(args[offset + 1]->IsUint32()); // Offset
4444
CHECK(args[offset + 2]->IsUint32()); // Size
4545

src/crypto/crypto_rsa.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ Maybe<bool> RSACipherTraits::AdditionalConfig(
321321
return Nothing<bool>();
322322
}
323323

324-
if (IsAnyByteSource(args[offset + 2])) {
324+
if (IsAnyBufferSource(args[offset + 2])) {
325325
ArrayBufferOrViewContents<char> label(args[offset + 2]);
326326
if (UNLIKELY(!label.CheckSizeInt32())) {
327327
THROW_ERR_OUT_OF_RANGE(env, "label is too big");

src/crypto/crypto_timing.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ void TimingSafeEqual(const FunctionCallbackInfo<Value>& args) {
2121
// to V8 inlining certain parts of the wrapper. Therefore, keep them in C++.
2222
// Refs: https://github.com/nodejs/node/issues/34073.
2323
Environment* env = Environment::GetCurrent(args);
24-
if (!IsAnyByteSource(args[0])) {
24+
if (!IsAnyBufferSource(args[0])) {
2525
THROW_ERR_INVALID_ARG_TYPE(
2626
env, "The \"buf1\" argument must be an instance of "
2727
"ArrayBuffer, Buffer, TypedArray, or DataView.");
2828
return;
2929
}
30-
if (!IsAnyByteSource(args[1])) {
30+
if (!IsAnyBufferSource(args[1])) {
3131
THROW_ERR_INVALID_ARG_TYPE(
3232
env, "The \"buf2\" argument must be an instance of "
3333
"ArrayBuffer, Buffer, TypedArray, or DataView.");

src/crypto/crypto_util.cc

+5-5
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,8 @@ ByteSource ByteSource::FromEncodedString(Environment* env,
402402

403403
ByteSource ByteSource::FromStringOrBuffer(Environment* env,
404404
Local<Value> value) {
405-
return IsAnyByteSource(value) ? FromBuffer(value)
406-
: FromString(env, value.As<String>());
405+
return IsAnyBufferSource(value) ? FromBuffer(value)
406+
: FromString(env, value.As<String>());
407407
}
408408

409409
ByteSource ByteSource::FromString(Environment* env, Local<String> str,
@@ -429,9 +429,9 @@ ByteSource ByteSource::FromSecretKeyBytes(
429429
// A key can be passed as a string, buffer or KeyObject with type 'secret'.
430430
// If it is a string, we need to convert it to a buffer. We are not doing that
431431
// in JS to avoid creating an unprotected copy on the heap.
432-
return value->IsString() || IsAnyByteSource(value) ?
433-
ByteSource::FromStringOrBuffer(env, value) :
434-
ByteSource::FromSymmetricKeyObjectHandle(value);
432+
return value->IsString() || IsAnyBufferSource(value)
433+
? ByteSource::FromStringOrBuffer(env, value)
434+
: ByteSource::FromSymmetricKeyObjectHandle(value);
435435
}
436436

437437
ByteSource ByteSource::NullTerminatedCopy(Environment* env,

src/crypto/crypto_util.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,8 @@ void array_push_back(const TypeName* evp_ref,
676676
}
677677
#endif
678678

679-
inline bool IsAnyByteSource(v8::Local<v8::Value> arg) {
679+
// WebIDL AllowSharedBufferSource.
680+
inline bool IsAnyBufferSource(v8::Local<v8::Value> arg) {
680681
return arg->IsArrayBufferView() ||
681682
arg->IsArrayBuffer() ||
682683
arg->IsSharedArrayBuffer();
@@ -694,7 +695,7 @@ class ArrayBufferOrViewContents {
694695
return;
695696
}
696697

697-
CHECK(IsAnyByteSource(buf));
698+
CHECK(IsAnyBufferSource(buf));
698699
if (buf->IsArrayBufferView()) {
699700
auto view = buf.As<v8::ArrayBufferView>();
700701
offset_ = view->ByteOffset();

0 commit comments

Comments
 (0)