From 1d509801e9715908c75931f28d8762f47dc4dfc0 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 30 Apr 2017 00:48:46 +0200 Subject: [PATCH] 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: https://github.com/nodejs/node/issues/9817 PR-URL: https://github.com/nodejs/node/pull/12752 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Refael Ackermann Reviewed-By: Gibson Fahnestock --- src/node_crypto.cc | 8 ++++++++ test/parallel/test-crypto-hash.js | 10 ++++++---- test/parallel/test-crypto-hmac.js | 4 ++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 6e5db66313f61c..c2e5b5120e1c77 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -3792,6 +3792,10 @@ void Hmac::HmacDigest(const FunctionCallbackInfo& args) { encoding = ParseEncoding(env->isolate(), args[0], BUFFER); } + if (encoding == UCS2) { + return env->ThrowError("hmac.digest() does not support UTF-16"); + } + unsigned char* md_value = nullptr; unsigned int md_len = 0; @@ -3915,6 +3919,10 @@ void Hash::HashDigest(const FunctionCallbackInfo& args) { encoding = ParseEncoding(env->isolate(), args[0], BUFFER); } + if (encoding == UCS2) { + return env->ThrowError("hash.digest() does not support UTF-16"); + } + unsigned char md_value[EVP_MAX_MD_SIZE]; unsigned int md_len; diff --git a/test/parallel/test-crypto-hash.js b/test/parallel/test-crypto-hash.js index 0d55d4236a656e..28f0f2f30349ba 100644 --- a/test/parallel/test-crypto-hash.js +++ b/test/parallel/test-crypto-hash.js @@ -108,10 +108,12 @@ const h3 = crypto.createHash('sha256'); h3.digest(); assert.throws(function() { h3.digest(); -}, - /Digest already called/); +}, /Digest already called/); assert.throws(function() { h3.update('foo'); -}, - /Digest already called/); +}, /Digest already called/); + +assert.throws(function() { + crypto.createHash('sha256').digest('ucs2'); +}, /^Error: hash\.digest\(\) does not support UTF-16$/); diff --git a/test/parallel/test-crypto-hmac.js b/test/parallel/test-crypto-hmac.js index a3372bcb7242b9..94c5cefa058d02 100644 --- a/test/parallel/test-crypto-hmac.js +++ b/test/parallel/test-crypto-hmac.js @@ -377,3 +377,7 @@ for (let i = 0, l = rfc2202_sha1.length; i < l; i++) { `Test HMAC-SHA1 : Test case ${i + 1} rfc 2202` ); } + +assert.throws(function() { + crypto.createHmac('sha256', 'w00t').digest('ucs2'); +}, /^Error: hmac\.digest\(\) does not support UTF-16$/);