Skip to content

Commit 931b4bc

Browse files
committed
crypto: fix unhandled error in Hash._transform
Signed-off-by: haramjeong <04harams77@gmail.com>
1 parent a37c61d commit 931b4bc

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

124124
Hash.prototype._transform = function _transform(chunk, encoding, callback) {
125-
this[kHandle].update(chunk, encoding);
125+
if (!this[kHandle].update(chunk, encoding))
126+
return callback(new ERR_CRYPTO_HASH_UPDATE_FAILED());
126127
callback();
127128
};
128129

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+
const common = require('../common');
6+
if (!common.hasCrypto) common.skip('missing crypto');
7+
8+
const assert = require('assert');
9+
const crypto = 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+
const h = crypto.createHash('sha256');
18+
19+
// Simulate native update failure by replacing the internal handle
20+
const fakeHandle = {
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)