Skip to content

Commit 87edd60

Browse files
committed
crypto: validate inputEncoding in Cipher/Decipher update
Cipher.update(string, badEncoding, ...) and Decipher.update with the same shape silently produced incorrect output: the binding skipped the unrecognized encoding and fell back to a default, giving the user wrong ciphertext or plaintext with no signal. Sub-cases 1 and 2 from issue #45189 (bad output encoding to update/final) were addressed in PR #45990. This commit completes the fix for sub-case 3 (bad input encoding) per panva's comment deferring it to a follow-up PR for CITGM testing. When `data` is a string and `inputEncoding` is non-null but does not normalize to a known encoding, throw ERR_UNKNOWN_ENCODING. Buffer / TypedArray / DataView data paths are unaffected (the binding ignores `inputEncoding` for non-string data anyway). Fixes: #45189 Refs: #45990 Signed-off-by: Maruthan G <[email protected]>
1 parent 21436f0 commit 87edd60

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

lib/internal/crypto/cipher.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ function _flush(callback) {
152152
function update(data, inputEncoding, outputEncoding) {
153153
if (typeof data === 'string') {
154154
validateEncoding(data, inputEncoding);
155+
if (inputEncoding != null &&
156+
normalizeEncoding(inputEncoding) === undefined) {
157+
throw new ERR_UNKNOWN_ENCODING(inputEncoding);
158+
}
155159
} else if (!isArrayBufferView(data)) {
156160
throw new ERR_INVALID_ARG_TYPE(
157161
'data', ['string', 'Buffer', 'TypedArray', 'DataView'], data);

test/parallel/test-crypto-encoding-validation-error.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,52 @@ const createCipher = () => {
5050
{ message: /^Unknown encoding: bad3$/, code: 'ERR_UNKNOWN_ENCODING' }
5151
);
5252
}
53+
54+
// Regression tests for https://github.com/nodejs/node/issues/45189:
55+
// Unknown input encodings used to be silently accepted by Cipher/Decipher
56+
// `update`, producing incorrect (and silently non-deterministic) output.
57+
// They must now reject with ERR_UNKNOWN_ENCODING.
58+
59+
{
60+
const cipher = createCipher();
61+
62+
assert.throws(
63+
() => cipher.update('test', 'bad', 'hex'),
64+
{ message: /^Unknown encoding: bad$/, code: 'ERR_UNKNOWN_ENCODING' }
65+
);
66+
}
67+
68+
{
69+
const { createDecipheriv } = require('crypto');
70+
const decipher = createDecipheriv(
71+
'aes-256-cbc', randomBytes(32), randomBytes(16));
72+
73+
assert.throws(
74+
() => decipher.update('test', 'bad', 'hex'),
75+
{ message: /^Unknown encoding: bad$/, code: 'ERR_UNKNOWN_ENCODING' }
76+
);
77+
}
78+
79+
// A buffer-like data argument should not trigger encoding validation,
80+
// because the input encoding is ignored when data is not a string.
81+
{
82+
const cipher = createCipher();
83+
// Should not throw.
84+
cipher.update(Buffer.from('test'), 'bad-but-ignored', 'hex');
85+
}
86+
87+
// Valid input encodings must continue to work.
88+
{
89+
const cipher = createCipher();
90+
let result = cipher.update('test', 'utf-8', 'hex');
91+
result += cipher.final('hex');
92+
assert.strictEqual(typeof result, 'string');
93+
}
94+
95+
// Omitting the input encoding (undefined / null) is allowed; the
96+
// underlying binding falls back to its default behavior.
97+
{
98+
const cipher = createCipher();
99+
// Should not throw.
100+
cipher.update(Buffer.from('test'));
101+
}

0 commit comments

Comments
 (0)