From cbda4e9fc510ce46d98c9e32e731e737d967acb5 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Fri, 24 Nov 2023 17:33:24 +0100 Subject: [PATCH] assert,crypto: make KeyObject and CryptoKey testable for equality PR-URL: https://github.com/nodejs/node/pull/50897 Reviewed-By: Antoine du Hamel --- lib/internal/util/comparisons.js | 18 ++++++++++ test/parallel/test-assert-deep.js | 59 ++++++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/lib/internal/util/comparisons.js b/lib/internal/util/comparisons.js index d26d887636c565..f9354d01ce5730 100644 --- a/lib/internal/util/comparisons.js +++ b/lib/internal/util/comparisons.js @@ -44,6 +44,8 @@ const { isSymbolObject, isFloat32Array, isFloat64Array, + isKeyObject, + isCryptoKey, } = types; const { constants: { @@ -61,6 +63,8 @@ const kIsArray = 1; const kIsSet = 2; const kIsMap = 3; +let kKeyObject; + // Check if they have the same source and flags function areSimilarRegExps(a, b) { return a.source === b.source && @@ -251,6 +255,20 @@ function innerDeepEqual(val1, val2, strict, memos) { isNativeError(val2) || val2 instanceof Error) { return false; + } else if (isKeyObject(val1)) { + if (!isKeyObject(val2) || !val1.equals(val2)) { + return false; + } + } else if (isCryptoKey(val1)) { + kKeyObject ??= require('internal/crypto/util').kKeyObject; + if (!isCryptoKey(val2) || + val1.extractable !== val2.extractable || + !innerDeepEqual(val1.algorithm, val2.algorithm, strict, memos) || + !innerDeepEqual(val1.usages, val2.usages, strict, memos) || + !innerDeepEqual(val1[kKeyObject], val2[kKeyObject], strict, memos) + ) { + return false; + } } return keyCheck(val1, val2, strict, memos, kNoIterator); } diff --git a/test/parallel/test-assert-deep.js b/test/parallel/test-assert-deep.js index f7489993aa60ba..f147be1b41f2a8 100644 --- a/test/parallel/test-assert-deep.js +++ b/test/parallel/test-assert-deep.js @@ -1,6 +1,6 @@ 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const util = require('util'); const { AssertionError } = assert; @@ -1220,3 +1220,60 @@ assert.throws( assertNotDeepOrStrict(a, b); } + +// eslint-disable-next-line node-core/crypto-check +if (common.hasCrypto) { + const crypto = require('crypto'); + const { subtle } = globalThis.crypto; + + { + const a = crypto.createSecretKey(Buffer.alloc(1, 0)); + const b = crypto.createSecretKey(Buffer.alloc(1, 1)); + + assertNotDeepOrStrict(a, b); + } + + { + const a = crypto.createSecretKey(Buffer.alloc(0)); + const b = crypto.createSecretKey(Buffer.alloc(0)); + + assertDeepAndStrictEqual(a, b); + } + + (async () => { + { + const a = await subtle.importKey('raw', Buffer.alloc(1, 0), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']); + const b = await subtle.importKey('raw', Buffer.alloc(1, 1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']); + + assertNotDeepOrStrict(a, b); + } + + { + const a = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']); + const b = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']); + + assertNotDeepOrStrict(a, b); + } + + { + const a = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']); + const b = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-384' }, true, ['sign']); + + assertNotDeepOrStrict(a, b); + } + + { + const a = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']); + const b = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['verify']); + + assertNotDeepOrStrict(a, b); + } + + { + const a = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']); + const b = await subtle.importKey('raw', Buffer.alloc(1), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']); + + assertDeepAndStrictEqual(a, b); + } + })().then(common.mustCall()); +}