Skip to content

Commit a727b13

Browse files
bnoordhuisMylesBorins
authored andcommitted
crypto: make update(buf, enc) ignore encoding
Make the cipher/decipher/hash/hmac update() methods ignore the input encoding when the input is a buffer. This is the documented behavior but some inputs were rejected, notably when the specified encoding is 'hex' and the buffer has an odd length (because a _string_ with an odd length is never a valid hex string.) The sign/verify update() methods work okay because they use different validation logic. Fixes: #31751 PR-URL: #31766 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 83e9a3e commit a727b13

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

lib/internal/crypto/cipher.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,13 @@ Cipher.prototype.update = function update(data, inputEncoding, outputEncoding) {
151151
inputEncoding = inputEncoding || encoding;
152152
outputEncoding = outputEncoding || encoding;
153153

154-
if (typeof data !== 'string' && !isArrayBufferView(data)) {
154+
if (typeof data === 'string') {
155+
validateEncoding(data, inputEncoding);
156+
} else if (!isArrayBufferView(data)) {
155157
throw new ERR_INVALID_ARG_TYPE(
156158
'data', ['string', 'Buffer', 'TypedArray', 'DataView'], data);
157159
}
158160

159-
validateEncoding(data, inputEncoding);
160-
161161
const ret = this[kHandle].update(data, inputEncoding);
162162

163163
if (outputEncoding && outputEncoding !== 'buffer') {

lib/internal/crypto/hash.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,13 @@ Hash.prototype.update = function update(data, encoding) {
7878
if (state[kFinalized])
7979
throw new ERR_CRYPTO_HASH_FINALIZED();
8080

81-
if (typeof data !== 'string' && !isArrayBufferView(data)) {
82-
throw new ERR_INVALID_ARG_TYPE('data',
83-
['string',
84-
'Buffer',
85-
'TypedArray',
86-
'DataView'],
87-
data);
81+
if (typeof data === 'string') {
82+
validateEncoding(data, encoding);
83+
} else if (!isArrayBufferView(data)) {
84+
throw new ERR_INVALID_ARG_TYPE(
85+
'data', ['string', 'Buffer', 'TypedArray', 'DataView'], data);
8886
}
8987

90-
validateEncoding(data, encoding);
91-
9288
if (!this[kHandle].update(data, encoding))
9389
throw new ERR_CRYPTO_HASH_UPDATE_FAILED();
9490
return this;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
const crypto = require('crypto');
8+
9+
const zeros = Buffer.alloc;
10+
const key = zeros(16);
11+
const iv = zeros(16);
12+
13+
const cipher = () => crypto.createCipheriv('aes-128-cbc', key, iv);
14+
const decipher = () => crypto.createDecipheriv('aes-128-cbc', key, iv);
15+
const hash = () => crypto.createSign('sha256');
16+
const hmac = () => crypto.createHmac('sha256', key);
17+
const sign = () => crypto.createSign('sha256');
18+
const verify = () => crypto.createVerify('sha256');
19+
20+
for (const f of [cipher, decipher, hash, hmac, sign, verify])
21+
for (const n of [15, 16])
22+
f().update(zeros(n), 'hex'); // Should ignore inputEncoding.

0 commit comments

Comments
 (0)