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 <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 83e9a3e commit a727b13

3 files changed

Lines changed: 30 additions & 12 deletions

File tree

‎lib/internal/crypto/cipher.js‎

Lines changed: 3 additions & 3 deletions
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(typeofdata!=='string'&&!isArrayBufferView(data)){
154+
if(typeofdata==='string'){
155+
validateEncoding(data,inputEncoding);
156+
}elseif(!isArrayBufferView(data)){
155157
thrownewERR_INVALID_ARG_TYPE(
156158
'data',['string','Buffer','TypedArray','DataView'],data);
157159
}
158160

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

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

‎lib/internal/crypto/hash.js‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,13 @@ Hash.prototype.update = function update(data, encoding) {
7878
if(state[kFinalized])
7979
thrownewERR_CRYPTO_HASH_FINALIZED();
8080

81-
if(typeofdata!=='string'&&!isArrayBufferView(data)){
82-
thrownewERR_INVALID_ARG_TYPE('data',
83-
['string',
84-
'Buffer',
85-
'TypedArray',
86-
'DataView'],
87-
data);
81+
if(typeofdata==='string'){
82+
validateEncoding(data,encoding);
83+
}elseif(!isArrayBufferView(data)){
84+
thrownewERR_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
thrownewERR_CRYPTO_HASH_UPDATE_FAILED();
9490
returnthis;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
4+
if(!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
constcrypto=require('crypto');
8+
9+
constzeros=Buffer.alloc;
10+
constkey=zeros(16);
11+
constiv=zeros(16);
12+
13+
constcipher=()=>crypto.createCipheriv('aes-128-cbc',key,iv);
14+
constdecipher=()=>crypto.createDecipheriv('aes-128-cbc',key,iv);
15+
consthash=()=>crypto.createSign('sha256');
16+
consthmac=()=>crypto.createHmac('sha256',key);
17+
constsign=()=>crypto.createSign('sha256');
18+
constverify=()=>crypto.createVerify('sha256');
19+
20+
for(constfof[cipher,decipher,hash,hmac,sign,verify])
21+
for(constnof[15,16])
22+
f().update(zeros(n),'hex');// Should ignore inputEncoding.

0 commit comments

Comments
 (0)