| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c868e36 commit 5cefd02
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -31,7 +31,6 @@ const { | |||
| 31 | 31 | SecretKeyObject, | |
| 32 | 32 | parsePublicKeyEncoding, | |
| 33 | 33 | parsePrivateKeyEncoding, | |
| 34 | - isJwk | ||
| 35 | 34 | } = require('internal/crypto/keys'); | |
| 36 | 35 | ||
| 37 | 36 | const { | |
@@ -66,6 +65,10 @@ const { isArrayBufferView } = require('internal/util/types'); | |||
| 66 | 65 | ||
| 67 | 66 | const { getOptionValue } = require('internal/options'); | |
| 68 | 67 | ||
| 68 | + function isJwk(obj) { | ||
| 69 | + return obj != null && obj.kty !== undefined; | ||
| 70 | + } | ||
| 71 | + | ||
| 69 | 72 | function wrapKey(key, ctor) { | |
| 70 | 73 | if (typeof key === 'string' || | |
| 71 | 74 | isArrayBufferView(key) || | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -525,14 +525,18 @@ function prepareAsymmetricKey(key, ctx) { | |||
| 525 | 525 | return { format: kKeyFormatPEM, data: getArrayBufferOrView(key, 'key') }; | |
| 526 | 526 | } else if (typeof key === 'object') { | |
| 527 | 527 | const { key: data, encoding, format } = key; | |
| 528 | + | ||
| 528 | 529 | // The 'key' property can be a KeyObject as well to allow specifying | |
| 529 | 530 | // additional options such as padding along with the key. | |
| 530 | 531 | if (isKeyObject(data)) | |
| 531 | 532 | return { data: getKeyObjectHandle(data, ctx) }; | |
| 532 | 533 | else if (isCryptoKey(data)) | |
| 533 | 534 | return { data: getKeyObjectHandle(data[kKeyObject], ctx) }; | |
| 534 | - else if (isJwk(data) && format === 'jwk') | ||
| 535 | + else if (format === 'jwk') { | ||
| 536 | + validateObject(data, 'key.key'); | ||
| 535 | 537 | return { data: getKeyObjectHandleFromJwk(data, ctx), format: 'jwk' }; | |
| 538 | + } | ||
| 539 | + | ||
| 536 | 540 | // Either PEM or DER using PKCS#1 or SPKI. | |
| 537 | 541 | if (!isStringOrBuffer(data)) { | |
| 538 | 542 | throw new ERR_INVALID_ARG_TYPE( | |
@@ -720,10 +724,6 @@ function isCryptoKey(obj) { | |||
| 720 | 724 | return obj != null && obj[kKeyObject] !== undefined; | |
| 721 | 725 | } | |
| 722 | 726 | ||
| 723 | - function isJwk(obj) { | ||
| 724 | - return obj != null && obj.kty !== undefined; | ||
| 725 | - } | ||
| 726 | - | ||
| 727 | 727 | module.exports = { | |
| 728 | 728 | // Public API. | |
| 729 | 729 | createSecretKey, | |
@@ -745,5 +745,4 @@ module.exports = { | |||
| 745 | 745 | PrivateKeyObject, | |
| 746 | 746 | isKeyObject, | |
| 747 | 747 | isCryptoKey, | |
| 748 | - isJwk, | ||
| 749 | 748 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -868,3 +868,15 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem', | |||
| 868 | 868 | assert(!first.equals(third)); | |
| 869 | 869 | assert(!third.equals(first)); | |
| 870 | 870 | } | |
| 871 | + | ||
| 872 | + { | ||
| 873 | + // This should not cause a crash: https://github.com/nodejs/node/issues/44471 | ||
| 874 | + for (const key of ['', 'foo', null, undefined, true, Boolean]) { | ||
| 875 | + assert.throws(() => { | ||
| 876 | + createPublicKey({ key, format: 'jwk' }); | ||
| 877 | + }, { code: 'ERR_INVALID_ARG_TYPE', message: /The "key\.key" property must be of type object/ }); | ||
| 878 | + assert.throws(() => { | ||
| 879 | + createPrivateKey({ key, format: 'jwk' }); | ||
| 880 | + }, { code: 'ERR_INVALID_ARG_TYPE', message: /The "key\.key" property must be of type object/ }); | ||
| 881 | + } | ||
| 882 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -756,3 +756,21 @@ assert.throws( | |||
| 756 | 756 | message: /digest too big for rsa key/ | |
| 757 | 757 | }); | |
| 758 | 758 | } | |
| 759 | + | ||
| 760 | + { | ||
| 761 | + // This should not cause a crash: https://github.com/nodejs/node/issues/44471 | ||
| 762 | + for (const key of ['', 'foo', null, undefined, true, Boolean]) { | ||
| 763 | + assert.throws(() => { | ||
| 764 | + crypto.verify('sha256', 'foo', { key, format: 'jwk' }, Buffer.alloc(0)); | ||
| 765 | + }, { code: 'ERR_INVALID_ARG_TYPE', message: /The "key\.key" property must be of type object/ }); | ||
| 766 | + assert.throws(() => { | ||
| 767 | + crypto.createVerify('sha256').verify({ key, format: 'jwk' }, Buffer.alloc(0)); | ||
| 768 | + }, { code: 'ERR_INVALID_ARG_TYPE', message: /The "key\.key" property must be of type object/ }); | ||
| 769 | + assert.throws(() => { | ||
| 770 | + crypto.sign('sha256', 'foo', { key, format: 'jwk' }); | ||
| 771 | + }, { code: 'ERR_INVALID_ARG_TYPE', message: /The "key\.key" property must be of type object/ }); | ||
| 772 | + assert.throws(() => { | ||
| 773 | + crypto.createSign('sha256').sign({ key, format: 'jwk' }); | ||
| 774 | + }, { code: 'ERR_INVALID_ARG_TYPE', message: /The "key\.key" property must be of type object/ }); | ||
| 775 | + } | ||
| 776 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments