Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: add cipher update/final methods encoding validation #45990

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/internal/crypto/cipher.js
Expand Up @@ -27,6 +27,7 @@ const {
ERR_CRYPTO_INVALID_STATE,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_UNKNOWN_ENCODING,
}
} = require('internal/errors');

Expand Down Expand Up @@ -91,9 +92,14 @@ const privateDecrypt = rsaFunctionFor(_privateDecrypt, RSA_PKCS1_OAEP_PADDING,
'private');

function getDecoder(decoder, encoding) {
encoding = normalizeEncoding(encoding);
const normalizedEncoding = normalizeEncoding(encoding);
decoder = decoder || new StringDecoder(encoding);
assert(decoder.encoding === encoding, 'Cannot change encoding');
if (decoder.encoding !== normalizedEncoding) {
if (normalizedEncoding === undefined) {
throw new ERR_UNKNOWN_ENCODING(encoding);
}
assert(false, 'Cannot change encoding');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why throw error by assert? it will be some internal error, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can replace the assert with a coded error in a follow up semver-major PRs that contain breaking changes and should be released in the next major version. PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to have a coded error, can I make the change? @panva

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, best wait until this change lands.

}
return decoder;
}

Expand Down
52 changes: 52 additions & 0 deletions test/parallel/test-crypto-encoding-validation-error.js
@@ -0,0 +1,52 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

// This test checks if error is thrown in case of wrong encoding provided into cipher.

const assert = require('assert');
const { createCipheriv, randomBytes } = require('crypto');

const createCipher = () => {
return createCipheriv('aes-256-cbc', randomBytes(32), randomBytes(16));
};

{
const cipher = createCipher();
cipher.update('test', 'utf-8', 'utf-8');

assert.throws(
() => cipher.update('666f6f', 'hex', 'hex'),
{ message: /Cannot change encoding/ }
);
}

{
const cipher = createCipher();
cipher.update('test', 'utf-8', 'utf-8');

assert.throws(
() => cipher.final('hex'),
{ message: /Cannot change encoding/ }
);
}

{
const cipher = createCipher();
cipher.update('test', 'utf-8', 'utf-8');

assert.throws(
() => cipher.final('bad2'),
{ message: /^Unknown encoding: bad2$/, code: 'ERR_UNKNOWN_ENCODING' }
);
}

{
const cipher = createCipher();

assert.throws(
() => cipher.update('test', 'utf-8', 'bad3'),
{ message: /^Unknown encoding: bad3$/, code: 'ERR_UNKNOWN_ENCODING' }
);
}