Skip to content

Commit 6c2daf0

Browse files
committed
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 9990be2 commit 6c2daf0

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
@@ -3798,6 +3798,10 @@ void Hmac::HmacDigest(const FunctionCallbackInfo<Value>& args) {
37983798
encoding = ParseEncoding(env->isolate(), args[0], BUFFER);
37993799
}
38003800

3801+
if (encoding == UCS2) {
3802+
return env->ThrowError("hmac.digest() does not support UTF-16");
3803+
}
3804+
38013805
unsigned char* md_value = nullptr;
38023806
unsigned int md_len = 0;
38033807

@@ -3921,6 +3925,10 @@ void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) {
39213925
encoding = ParseEncoding(env->isolate(), args[0], BUFFER);
39223926
}
39233927

3928+
if (encoding == UCS2) {
3929+
return env->ThrowError("hash.digest() does not support UTF-16");
3930+
}
3931+
39243932
unsigned char md_value[EVP_MAX_MD_SIZE];
39253933
unsigned int md_len;
39263934

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)