Skip to content

Commit 656e57e

Browse files
haramjaduh95
authored andcommitted
crypto: fix unhandled error in Hash._transform
Signed-off-by: haramjeong <04harams77@gmail.com> PR-URL: #63261Fixes: #63258 Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Filip Skokan <panva.ip@gmail.com>
1 parent 3f1c8d7 commit 656e57e

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

‎lib/internal/crypto/hash.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ Hash.prototype.copy = function copy(options) {
125125
};
126126

127127
Hash.prototype._transform=function_transform(chunk,encoding,callback){
128-
this[kHandle].update(chunk,encoding);
128+
if(!this[kHandle].update(chunk,encoding))
129+
returncallback(newERR_CRYPTO_HASH_UPDATE_FAILED());
129130
callback();
130131
};
131132

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
// Flags: --expose-internals
4+
5+
constcommon=require('../common');
6+
if(!common.hasCrypto)common.skip('missing crypto');
7+
8+
constassert=require('assert');
9+
constcrypto=require('crypto');
10+
const{ kHandle }=require('internal/crypto/util');
11+
12+
/**
13+
* This test verifies that the Hash stream properly surfaces an error
14+
* when the underlying native update fails.
15+
*/
16+
17+
consth=crypto.createHash('sha256');
18+
19+
// Simulate native update failure by replacing the internal handle
20+
constfakeHandle={
21+
update: ()=>false,
22+
digest: ()=>Buffer.from('')
23+
};
24+
25+
h[kHandle]=fakeHandle;
26+
27+
h.on('error',common.mustCall((err)=>{
28+
assert.strictEqual(err.code,'ERR_CRYPTO_HASH_UPDATE_FAILED');
29+
}));
30+
31+
h.write('test data');
32+
h.end();

0 commit comments

Comments
 (0)