Skip to content

Commit c201c26

Browse files
committed
crypto: input typechecking
Handle input typechecking in JavaScript using errors. Minor fixes and updated tests
1 parent d5f07d9 commit c201c26

3 files changed

Lines changed: 13 additions & 10 deletions

File tree

lib/internal/crypto/diffiehellman.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,15 @@ ECDH.prototype.getPublicKey = function getPublicKey(encoding, format) {
193193
};
194194

195195
ECDH.convertKey = function convertKey(key, curve, inEnc, outEnc, format) {
196+
if (typeof key !== 'string' && !isArrayBufferView(key)) {
197+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'key',
198+
['string', 'Buffer', 'TypedArray', 'DataView']);
199+
}
200+
201+
if (typeof curve !== 'string') {
202+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'curve', 'string');
203+
}
204+
196205
const encoding = getDefaultEncoding();
197206
inEnc = inEnc || encoding;
198207
outEnc = outEnc || encoding;

src/node_crypto.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5561,13 +5561,10 @@ void ConvertKey(const FunctionCallbackInfo<Value>& args) {
55615561

55625562
CHECK_EQ(args.Length(), 3);
55635563

5564-
THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "Public key");
5565-
55665564
size_t len = Buffer::Length(args[0]);
55675565
if (len == 0)
55685566
return args.GetReturnValue().SetEmptyString();
55695567

5570-
THROW_AND_RETURN_IF_NOT_STRING(args[1], "ECDH curve name");
55715568
node::Utf8Value curve(env->isolate(), args[1]);
55725569

55735570
int nid = OBJ_sn2nid(*curve);

test/parallel/test-crypto-ecdh-convert-key.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ if (!common.hasCrypto)
44
common.skip('missing crypto');
55

66
const assert = require('assert');
7-
const crypto = require('crypto');
87

9-
const { ECDH, getCurves } = crypto;
8+
const { ECDH, getCurves } = require('crypto');
109

1110
// A valid private key for the secp256k1 curve.
1211
const cafebabeKey = 'cafebabe'.repeat(8);
@@ -21,16 +20,16 @@ const cafebabePubPtUnComp =
2120
common.expectsError(
2221
() => ECDH.convertKey(),
2322
{
23+
code: 'ERR_INVALID_ARG_TYPE',
2424
type: TypeError,
25-
message: 'Public key must be a buffer'
2625
});
2726

2827
// Invalid test: curve argument is undefined.
2928
common.expectsError(
3029
() => ECDH.convertKey(cafebabePubPtComp),
3130
{
31+
code: 'ERR_INVALID_ARG_TYPE',
3232
type: TypeError,
33-
message: 'ECDH curve name must be a string'
3433
});
3534

3635
// Invalid test: curve argument is invalid.
@@ -41,9 +40,7 @@ common.expectsError(
4140
message: 'Invalid ECDH curve name'
4241
});
4342

44-
const availableCurves = new Set(getCurves());
45-
46-
if (availableCurves.has('secp256k1')) {
43+
if (getCurves().includes('secp256k1')) {
4744
// Invalid test: format argument is undefined.
4845
common.expectsError(
4946
() => ECDH.convertKey(cafebabePubPtComp, 'secp256k1', 'hex', 'hex', 10),

0 commit comments

Comments
 (0)