Skip to content

Commit

Permalink
crypto: improve error handling in parseKeyEncoding
Browse files Browse the repository at this point in the history
This change only affects KeyObject.export().

PR-URL: #26455
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
  • Loading branch information
tniessen committed Mar 9, 2019
1 parent 1acf3b1 commit 3afa5d7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/internal/crypto/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ function isStringOrBuffer(val) {
}

function parseKeyEncoding(enc, keyType, isPublic, objName) {
if (enc === null || typeof enc !== 'object')
throw new ERR_INVALID_ARG_TYPE('options', 'object', enc);

const isInput = keyType === undefined;

const {
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-crypto-key-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ const privatePem = fixtures.readSync('test_rsa_privkey.pem', 'ascii');
assert.strictEqual(derivedPublicKey.asymmetricKeyType, 'rsa');
assert.strictEqual(derivedPublicKey.symmetricKeySize, undefined);

// Test exporting with an invalid options object, this should throw.
for (const opt of [undefined, null, 'foo', 0, NaN]) {
common.expectsError(() => publicKey.export(opt), {
type: TypeError,
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options" argument must be of type object. Received type ' +
typeof opt
});
}

const publicDER = publicKey.export({
format: 'der',
type: 'pkcs1'
Expand Down

0 comments on commit 3afa5d7

Please sign in to comment.