Skip to content

Commit 1d50980

Browse files
addaleaxMylesBorins
authored andcommitted
crypto: throw proper errors if out enc is UTF-16
Throw `Error`s instead of hard crashing when the `.digest()` output encoding is UTF-16. Fixes: #9817 PR-URL: #12752 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]>
1 parent eefa840 commit 1d50980

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/node_crypto.cc

+8
Original file line numberDiff line numberDiff line change
@@ -3792,6 +3792,10 @@ void Hmac::HmacDigest(const FunctionCallbackInfo<Value>& args) {
37923792
encoding = ParseEncoding(env->isolate(), args[0], BUFFER);
37933793
}
37943794

3795+
if (encoding == UCS2) {
3796+
return env->ThrowError("hmac.digest() does not support UTF-16");
3797+
}
3798+
37953799
unsigned char* md_value = nullptr;
37963800
unsigned int md_len = 0;
37973801

@@ -3915,6 +3919,10 @@ void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) {
39153919
encoding = ParseEncoding(env->isolate(), args[0], BUFFER);
39163920
}
39173921

3922+
if (encoding == UCS2) {
3923+
return env->ThrowError("hash.digest() does not support UTF-16");
3924+
}
3925+
39183926
unsigned char md_value[EVP_MAX_MD_SIZE];
39193927
unsigned int md_len;
39203928

test/parallel/test-crypto-hash.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,12 @@ const h3 = crypto.createHash('sha256');
108108
h3.digest();
109109
assert.throws(function() {
110110
h3.digest();
111-
},
112-
/Digest already called/);
111+
}, /Digest already called/);
113112

114113
assert.throws(function() {
115114
h3.update('foo');
116-
},
117-
/Digest already called/);
115+
}, /Digest already called/);
116+
117+
assert.throws(function() {
118+
crypto.createHash('sha256').digest('ucs2');
119+
}, /^Error: hash\.digest\(\) does not support UTF-16$/);

test/parallel/test-crypto-hmac.js

+4
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,7 @@ for (let i = 0, l = rfc2202_sha1.length; i < l; i++) {
377377
`Test HMAC-SHA1 : Test case ${i + 1} rfc 2202`
378378
);
379379
}
380+
381+
assert.throws(function() {
382+
crypto.createHmac('sha256', 'w00t').digest('ucs2');
383+
}, /^Error: hmac\.digest\(\) does not support UTF-16$/);

0 commit comments

Comments
 (0)