Skip to content

Commit bb1aea8

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 d2c9c07 commit bb1aea8

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
@@ -124,7 +124,8 @@ Hash.prototype.copy = function copy(options) {
124124
};
125125

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

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)