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: fix wrong error message #33482

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/internal/crypto/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,10 @@ function prepareAsymmetricKey(key, ctx) {
// Either PEM or DER using PKCS#1 or SPKI.
if (!isStringOrBuffer(data)) {
throw new ERR_INVALID_ARG_TYPE(
'key',
'key.key',
['string', 'Buffer', 'TypedArray', 'DataView',
...(ctx !== kCreatePrivate ? ['KeyObject'] : [])],
key);
data);
}

const isPublic =
Expand Down
30 changes: 22 additions & 8 deletions test/parallel/test-crypto-sign-verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,18 @@ assert.throws(
});

[1, {}, [], Infinity].forEach((input) => {
let prop = '"key" argument';
let value = input;
if (typeof input === 'object') {
prop = '"key.key" property';
value = undefined;
}
const errObj = {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "key" argument must be of type string or an instance of ' +
'Buffer, TypedArray, DataView, or KeyObject.' +
common.invalidArgTypeHelper(input)
message: `The ${prop} must be of type string or ` +
'an instance of Buffer, TypedArray, DataView, or KeyObject.' +
common.invalidArgTypeHelper(value)
};

assert.throws(() => sign.sign(input), errObj);
Expand Down Expand Up @@ -478,25 +484,33 @@ assert.throws(
[1, {}, [], true, Infinity].forEach((input) => {
const data = Buffer.alloc(1);
const sig = Buffer.alloc(1);
const received = common.invalidArgTypeHelper(input);
const errObj = {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "data" argument must be an instance of Buffer, ' +
`TypedArray, or DataView.${received}`
'TypedArray, or DataView.' +
common.invalidArgTypeHelper(input)
};

assert.throws(() => crypto.sign(null, input, 'asdf'), errObj);
assert.throws(() => crypto.verify(null, input, 'asdf', sig), errObj);

errObj.message = 'The "key" argument must be of type string or an instance ' +
`of Buffer, TypedArray, DataView, or KeyObject.${received}`;
let prop = '"key" argument';
let value = input;
if (typeof input === 'object') {
prop = '"key.key" property';
value = undefined;
}
errObj.message = `The ${prop} must be of type string or ` +
'an instance of Buffer, TypedArray, DataView, or KeyObject.' +
common.invalidArgTypeHelper(value);

assert.throws(() => crypto.sign(null, data, input), errObj);
assert.throws(() => crypto.verify(null, data, input, sig), errObj);

errObj.message = 'The "signature" argument must be an instance of ' +
`Buffer, TypedArray, or DataView.${received}`;
'Buffer, TypedArray, or DataView.' +
common.invalidArgTypeHelper(input);
assert.throws(() => crypto.verify(null, data, 'test', input), errObj);
});

Expand Down