Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: handle exceptions in hmac/hash.digest #12164

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ Hash.prototype.update = function update(data, encoding) {

Hash.prototype.digest = function digest(outputEncoding) {
outputEncoding = outputEncoding || exports.DEFAULT_ENCODING;
return this._handle.digest(outputEncoding);
// Explicit conversion for backward compatibility
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Femto-nit: can you punctuate the comment?

return this._handle.digest(String(outputEncoding));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use `${outputEncoding}`; otherwise symbols (which were not allowed) will be converted to string also.

};


Expand Down
2 changes: 2 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,8 @@ enum encoding ParseEncoding(const char* encoding,
enum encoding ParseEncoding(Isolate* isolate,
Local<Value> encoding_v,
enum encoding default_encoding) {
CHECK(!encoding_v.IsEmpty());

if (!encoding_v->IsString())
return default_encoding;

Expand Down
18 changes: 5 additions & 13 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3797,9 +3797,7 @@ void Hmac::HmacDigest(const FunctionCallbackInfo<Value>& args) {

enum encoding encoding = BUFFER;
if (args.Length() >= 1) {
encoding = ParseEncoding(env->isolate(),
args[0]->ToString(env->isolate()),
BUFFER);
encoding = ParseEncoding(env->isolate(), args[0], BUFFER);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe leave a CHECK(args[i]->IsString()); line before the ParseEncoding calls?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember correctly, most functions just ignore invalid encoding values and use the default value instead. Currently, we are forcing the conversion to a string, but just because it is a string doesn't make it a valid encoding value. Adding a CHECK shouldn't do any harm, until we decide to finally drop support for toString() for consistency with other APIs. However, if we do not add a chack and the parameter is not a string (which won't happen now, but might as soon as we decide to drop support for toString()), there won't be any problems, the value will be ignored (ParseEncoding will return instantly).

The above only applies to hmac and hash. For sign and verify, the encoding parameter seems to always be null, so the CHECK would fail, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tniessen Still, if we drop toString() support it’s easy to just drop the checks again. (Likewise, it would be good if you included the test you said you had prepared in this PR – it’s easy to change things later, but for now it’s best to test the current behaviour.)

The above only applies to hmac and hash. For sign and verify, the encoding parameter seems to always be null, so the CHECK would fail, right?

It looks that way, yes… maybe you could fix that up and drop the unused code bits? That also simplifies SignFinal(), right now the StringBytes::Encode() encode call there seems like a completely unnecessary extra copy operation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@addaleax You are right, I will add the CHECKs and add the regression test.

maybe you could fix that up and drop the unused code bits?

Is it okay to do that in a separate PR or will that create too much organizational overhead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it okay to do that in a separate PR or will that create too much organizational overhead?

That’s your call… you can also maintain multiple commits in a single PR, which may or may not be easier for you. :) I don’t think there are other open pull requests for this code that would generate merge conflicts, and everything we talked about (except maybe dropping toString() support) semver-patch.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will create a separate PR after this gets merged to avoid conflicts and keep it simple, thank you for your support :)

}

unsigned char* md_value = nullptr;
Expand Down Expand Up @@ -3921,9 +3919,7 @@ void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) {

enum encoding encoding = BUFFER;
if (args.Length() >= 1) {
encoding = ParseEncoding(env->isolate(),
args[0]->ToString(env->isolate()),
BUFFER);
encoding = ParseEncoding(env->isolate(), args[0], BUFFER);
}

unsigned char md_value[EVP_MAX_MD_SIZE];
Expand Down Expand Up @@ -4201,10 +4197,8 @@ void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) {

unsigned int len = args.Length();
enum encoding encoding = BUFFER;
if (len >= 2 && args[1]->IsString()) {
encoding = ParseEncoding(env->isolate(),
args[1]->ToString(env->isolate()),
BUFFER);
if (len >= 2) {
encoding = ParseEncoding(env->isolate(), args[1], BUFFER);
}

node::Utf8Value passphrase(env->isolate(), args[2]);
Expand Down Expand Up @@ -4452,9 +4446,7 @@ void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) {

enum encoding encoding = UTF8;
if (args.Length() >= 3) {
encoding = ParseEncoding(env->isolate(),
args[2]->ToString(env->isolate()),
UTF8);
encoding = ParseEncoding(env->isolate(), args[2], UTF8);
}

ssize_t hlen = StringBytes::Size(env->isolate(), args[1], encoding);
Expand Down