From 9adddc552bbeaabb7a43ee053a2a4fc084dd00a6 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Mon, 4 May 2026 11:52:37 +0200 Subject: [PATCH] tools: prevent lib code from reading KeyObject and CryptoKey accessors Add ESLint rules that reject public KeyObject and CryptoKey accessor reads after internal brand checks. Internal code must use the private key helpers so it reads native-backed slots instead of user-replaceable properties. Add a separate rule that rejects instanceof checks against KeyObject and CryptoKey constructors, including the global CryptoKey constructor. Signed-off-by: Filip Skokan PR-URL: https://github.com/nodejs/node/pull/63111 Reviewed-By: James M Snell Reviewed-By: Yagiz Nizipli --- lib/eslint.config_partial.mjs | 3 + ...st-eslint-no-cryptokey-public-accessors.js | 88 ++++++ ...slint-no-keyobject-cryptokey-instanceof.js | 78 +++++ ...st-eslint-no-keyobject-public-accessors.js | 85 ++++++ .../no-cryptokey-public-accessors.js | 277 +++++++++++++++++ .../no-keyobject-cryptokey-instanceof.js | 122 ++++++++ .../no-keyobject-public-accessors.js | 284 ++++++++++++++++++ 7 files changed, 937 insertions(+) create mode 100644 test/parallel/test-eslint-no-cryptokey-public-accessors.js create mode 100644 test/parallel/test-eslint-no-keyobject-cryptokey-instanceof.js create mode 100644 test/parallel/test-eslint-no-keyobject-public-accessors.js create mode 100644 tools/eslint-rules/no-cryptokey-public-accessors.js create mode 100644 tools/eslint-rules/no-keyobject-cryptokey-instanceof.js create mode 100644 tools/eslint-rules/no-keyobject-public-accessors.js diff --git a/lib/eslint.config_partial.mjs b/lib/eslint.config_partial.mjs index 3c9bf0bc4e1..d4c3fd68831 100644 --- a/lib/eslint.config_partial.mjs +++ b/lib/eslint.config_partial.mjs @@ -424,6 +424,9 @@ export default [ 'node-core/lowercase-name-for-primitive': 'error', 'node-core/non-ascii-character': 'error', 'node-core/no-array-destructuring': 'error', + 'node-core/no-cryptokey-public-accessors': 'error', + 'node-core/no-keyobject-cryptokey-instanceof': 'error', + 'node-core/no-keyobject-public-accessors': 'error', 'node-core/prefer-primordials': [ 'error', { name: 'AggregateError' }, diff --git a/test/parallel/test-eslint-no-cryptokey-public-accessors.js b/test/parallel/test-eslint-no-cryptokey-public-accessors.js new file mode 100644 index 00000000000..1b0cd7f930f --- /dev/null +++ b/test/parallel/test-eslint-no-cryptokey-public-accessors.js @@ -0,0 +1,88 @@ +'use strict'; + +const common = require('../common'); +common.skipIfEslintMissing(); + +const RuleTester = require('../../tools/eslint/node_modules/eslint').RuleTester; +const rule = require('../../tools/eslint-rules/no-cryptokey-public-accessors'); + +new RuleTester().run('no-cryptokey-public-accessors', rule, { + valid: [ + 'foo.algorithm;', + ` + const { isCryptoKey, getCryptoKeyAlgorithm } = + require('internal/crypto/keys'); + if (isCryptoKey(key)) { + getCryptoKeyAlgorithm(key); + } + `, + ` + const { CryptoKey } = require('internal/crypto/keys'); + class Key extends CryptoKey { + get type() { return 'secret'; } + } + `, + ` + key = webidl.converters.KeyFormat(key); + key.algorithm; + `, + ], + invalid: [ + { + code: ` + const { isCryptoKey } = require('internal/crypto/keys'); + if (isCryptoKey(key)) { + key.type; + } + `, + errors: [{ messageId: 'noPublicAccessor' }], + }, + { + code: ` + const { isCryptoKey: check } = require('internal/crypto/keys'); + if (check(key) && key.extractable) {} + `, + errors: [{ messageId: 'noPublicAccessor' }], + }, + { + code: ` + const { isCryptoKey } = require('internal/crypto/keys'); + if (!isCryptoKey(key)) { + throw new TypeError(); + } + key.algorithm.name; + `, + errors: [{ messageId: 'noPublicAccessor' }], + }, + { + code: ` + const keys = require('internal/crypto/keys'); + if (!keys.isCryptoKey(key)) throw new TypeError(); + key['usages']; + `, + errors: [{ messageId: 'noPublicAccessor' }], + }, + { + code: ` + key = webidl.converters.CryptoKey(key); + key.algorithm; + `, + errors: [{ messageId: 'noPublicAccessor' }], + }, + { + code: ` + const key = webidl.converters.CryptoKey(value); + key.usages; + `, + errors: [{ messageId: 'noPublicAccessor' }], + }, + { + code: ` + class CryptoKey { + inspect() { return this.algorithm; } + } + `, + errors: [{ messageId: 'noPublicAccessor' }], + }, + ], +}); diff --git a/test/parallel/test-eslint-no-keyobject-cryptokey-instanceof.js b/test/parallel/test-eslint-no-keyobject-cryptokey-instanceof.js new file mode 100644 index 00000000000..29b70c9ec58 --- /dev/null +++ b/test/parallel/test-eslint-no-keyobject-cryptokey-instanceof.js @@ -0,0 +1,78 @@ +'use strict'; + +const common = require('../common'); +common.skipIfEslintMissing(); + +const RuleTester = require('../../tools/eslint/node_modules/eslint').RuleTester; +const rule = require('../../tools/eslint-rules/no-keyobject-cryptokey-instanceof'); + +new RuleTester().run('no-keyobject-cryptokey-instanceof', rule, { + valid: [ + 'key instanceof Buffer;', + 'key instanceof KeyObject;', + ` + const { isKeyObject } = require('internal/crypto/keys'); + isKeyObject(key); + `, + ` + const { isCryptoKey } = require('internal/crypto/keys'); + isCryptoKey(key); + `, + ], + invalid: [ + { + code: ` + const { KeyObject } = require('internal/crypto/keys'); + key instanceof KeyObject; + `, + errors: [{ messageId: 'noKeyObjectInstanceof' }], + }, + { + code: ` + const { KeyObject: KO } = require('internal/crypto/keys'); + key instanceof KO; + `, + errors: [{ messageId: 'noKeyObjectInstanceof' }], + }, + { + code: ` + const keys = require('internal/crypto/keys'); + key instanceof keys.KeyObject; + `, + errors: [{ messageId: 'noKeyObjectInstanceof' }], + }, + { + code: ` + key instanceof CryptoKey; + `, + errors: [{ messageId: 'noCryptoKeyInstanceof' }], + }, + { + code: ` + const { CryptoKey } = require('internal/crypto/keys'); + key instanceof CryptoKey; + `, + errors: [{ messageId: 'noCryptoKeyInstanceof' }], + }, + { + code: ` + const { CryptoKey: CK } = require('internal/crypto/webcrypto'); + key instanceof CK; + `, + errors: [{ messageId: 'noCryptoKeyInstanceof' }], + }, + { + code: ` + const webcrypto = require('internal/crypto/webcrypto'); + key instanceof webcrypto.CryptoKey; + `, + errors: [{ messageId: 'noCryptoKeyInstanceof' }], + }, + { + code: ` + key instanceof globalThis.CryptoKey; + `, + errors: [{ messageId: 'noCryptoKeyInstanceof' }], + }, + ], +}); diff --git a/test/parallel/test-eslint-no-keyobject-public-accessors.js b/test/parallel/test-eslint-no-keyobject-public-accessors.js new file mode 100644 index 00000000000..2420ae48763 --- /dev/null +++ b/test/parallel/test-eslint-no-keyobject-public-accessors.js @@ -0,0 +1,85 @@ +'use strict'; + +const common = require('../common'); +common.skipIfEslintMissing(); + +const RuleTester = require('../../tools/eslint/node_modules/eslint').RuleTester; +const rule = require('../../tools/eslint-rules/no-keyobject-public-accessors'); + +new RuleTester().run('no-keyobject-public-accessors', rule, { + valid: [ + 'foo.type;', + ` + const { isKeyObject, getKeyObjectType } = + require('internal/crypto/keys'); + if (isKeyObject(key)) { + getKeyObjectType(key); + } + `, + ` + const { isKeyObject } = require('internal/crypto/keys'); + if (format === 'raw-public') { + key.asymmetricKeyType; + } + `, + ` + const { KeyObject } = require('internal/crypto/keys'); + class Key extends KeyObject { + get type() { return 'secret'; } + } + `, + ], + invalid: [ + { + code: ` + const { isKeyObject } = require('internal/crypto/keys'); + if (isKeyObject(key)) { + key.type; + } + `, + errors: [{ messageId: 'noPublicAccessor' }], + }, + { + code: ` + const { isKeyObject: check } = require('internal/crypto/keys'); + if (check(key) && key.symmetricKeySize === 32) {} + `, + errors: [{ messageId: 'noPublicAccessor' }], + }, + { + code: ` + const { isKeyObject } = require('internal/crypto/keys'); + if (!isKeyObject(otherKeyObject)) { + throw new TypeError(); + } + otherKeyObject.asymmetricKeyType; + `, + errors: [{ messageId: 'noPublicAccessor' }], + }, + { + code: ` + const keys = require('internal/crypto/keys'); + if (!keys.isKeyObject(otherKeyObject)) throw new TypeError(); + otherKeyObject.asymmetricKeyDetails; + `, + errors: [{ messageId: 'noPublicAccessor' }], + }, + { + code: ` + class SecretKeyObject extends KeyObject { + export() { return this.symmetricKeySize; } + } + `, + errors: [{ messageId: 'noPublicAccessor' }], + }, + { + code: ` + const { isKeyObject } = require('internal/crypto/keys'); + if (isKeyObject(key)) { + key.equals(otherKey); + } + `, + errors: [{ messageId: 'noPublicAccessor' }], + }, + ], +}); diff --git a/tools/eslint-rules/no-cryptokey-public-accessors.js b/tools/eslint-rules/no-cryptokey-public-accessors.js new file mode 100644 index 00000000000..cec9198d740 --- /dev/null +++ b/tools/eslint-rules/no-cryptokey-public-accessors.js @@ -0,0 +1,277 @@ +/** + * @file Prevent internal code from using public CryptoKey accessors. + */ +'use strict'; + +const { isRequireCall, isString } = require('./rules-utils.js'); + +const CRYPTO_KEYS_MODULE = 'internal/crypto/keys'; +const WEBCRYPTO_MODULE = 'internal/crypto/webcrypto'; + +const accessors = new Map([ + ['type', 'getCryptoKeyType(key)'], + ['extractable', 'getCryptoKeyExtractable(key)'], + ['algorithm', 'getCryptoKeyAlgorithm(key)'], + ['usages', 'getCryptoKeyUsages(key)'], +]); + +const cryptoKeyClassNames = new Set([ + 'CryptoKey', + 'InternalCryptoKey', +]); + +function isCryptoKeyModuleRequire(node) { + return node?.type === 'CallExpression' && + isRequireCall(node) && + isString(node.arguments[0]) && + (node.arguments[0].value === CRYPTO_KEYS_MODULE || + node.arguments[0].value === WEBCRYPTO_MODULE); +} + +function getPropertyName(node) { + if (!node) return undefined; + if (node.computed) { + return node.property.type === 'Literal' ? node.property.value : undefined; + } + return node.property.name; +} + +function getIdentifierArgument(node) { + const arg = node.arguments[0]; + return arg?.type === 'Identifier' ? arg.name : undefined; +} + +function isNodeWithin(node, ancestor) { + return node.range[0] >= ancestor.range[0] && + node.range[1] <= ancestor.range[1]; +} + +function exits(statement) { + if (!statement) return false; + switch (statement.type) { + case 'BlockStatement': + return statement.body.length > 0 && exits(statement.body.at(-1)); + case 'ReturnStatement': + case 'ThrowStatement': + return true; + default: + return false; + } +} + +function findStatementInBlock(node) { + let current = node; + while (current?.parent) { + if ((current.parent.type === 'BlockStatement' || + current.parent.type === 'Program') && + current.parent.body.includes(current)) { + return { block: current.parent, statement: current }; + } + current = current.parent; + } +} + +function isWebIDLCryptoKeyConverter(node) { + if (node?.type !== 'CallExpression') return false; + if (node.callee.type !== 'MemberExpression') return false; + if (getPropertyName(node.callee) !== 'CryptoKey') return false; + + const converter = node.callee.object; + return converter?.type === 'MemberExpression' && + getPropertyName(converter) === 'converters'; +} + +module.exports = { + meta: { + messages: { + noPublicAccessor: 'Use `{{replacement}}` instead of the public CryptoKey `{{property}}` accessor.', + }, + schema: [], + }, + + create(context) { + const isCryptoKeyNames = new Set(); + const namespaceNames = new Set(); + const knownCryptoKeyNames = new Set(); + const knownCryptoKeyClassNames = new Set(cryptoKeyClassNames); + + function isIsCryptoKeyCall(node) { + if (node?.type !== 'CallExpression') return false; + + if (node.callee.type === 'Identifier') { + return isCryptoKeyNames.has(node.callee.name); + } + + if (node.callee.type === 'MemberExpression' && + !node.callee.computed && + node.callee.object.type === 'Identifier' && + namespaceNames.has(node.callee.object.name)) { + return node.callee.property.name === 'isCryptoKey'; + } + + return false; + } + + function getConsequentCryptoKeys(test) { + const names = new Set(); + if (isIsCryptoKeyCall(test)) { + names.add(getIdentifierArgument(test)); + } else if (test?.type === 'LogicalExpression' && test.operator === '&&') { + for (const name of getConsequentCryptoKeys(test.left)) { + names.add(name); + } + for (const name of getConsequentCryptoKeys(test.right)) { + names.add(name); + } + } + names.delete(undefined); + return names; + } + + function getAlternateCryptoKeys(test) { + const names = new Set(); + if (test?.type === 'UnaryExpression' && + test.operator === '!' && + isIsCryptoKeyCall(test.argument)) { + names.add(getIdentifierArgument(test.argument)); + } + names.delete(undefined); + return names; + } + + function isCryptoKeyFactory(node) { + if (node?.type === 'NewExpression' && + node.callee.type === 'Identifier') { + return knownCryptoKeyClassNames.has(node.callee.name); + } + + return isWebIDLCryptoKeyConverter(node); + } + + function isInCryptoKeyBranch(name, node) { + for (let current = node.parent; current; current = current.parent) { + if (current.type !== 'IfStatement') continue; + if (isNodeWithin(node, current.consequent) && + getConsequentCryptoKeys(current.test).has(name)) { + return true; + } + if (current.alternate && + isNodeWithin(node, current.alternate) && + getAlternateCryptoKeys(current.test).has(name)) { + return true; + } + } + return false; + } + + function followsExitingCryptoKeyGuard(name, node) { + const location = findStatementInBlock(node); + if (!location) return false; + const index = location.block.body.indexOf(location.statement); + for (let i = 0; i < index; i++) { + const statement = location.block.body[i]; + if (statement.type === 'IfStatement' && + exits(statement.consequent) && + getAlternateCryptoKeys(statement.test).has(name)) { + return true; + } + } + return false; + } + + function followsLogicalCryptoKeyCheck(name, node) { + for (let current = node; current.parent; current = current.parent) { + const parent = current.parent; + if (parent.type !== 'LogicalExpression' || parent.operator !== '&&') { + continue; + } + if (parent.right === current && + getConsequentCryptoKeys(parent.left).has(name)) { + return true; + } + } + return false; + } + + function isInsideCryptoKeyClass(node) { + for (let current = node.parent; current; current = current.parent) { + if (current.type !== 'ClassDeclaration' && + current.type !== 'ClassExpression') { + continue; + } + + const className = current.id?.name; + const superName = current.superClass?.type === 'Identifier' ? + current.superClass.name : undefined; + return knownCryptoKeyClassNames.has(className) || + knownCryptoKeyClassNames.has(superName); + } + return false; + } + + function isKnownCryptoKey(node) { + if (node.type === 'ThisExpression') { + return isInsideCryptoKeyClass(node); + } + + if (node.type !== 'Identifier') return false; + return knownCryptoKeyNames.has(node.name) || + isInCryptoKeyBranch(node.name, node) || + followsLogicalCryptoKeyCheck(node.name, node) || + followsExitingCryptoKeyGuard(node.name, node); + } + + return { + VariableDeclarator(node) { + if (isCryptoKeyModuleRequire(node.init)) { + if (node.id.type === 'Identifier') { + namespaceNames.add(node.id.name); + return; + } + + if (node.id.type !== 'ObjectPattern') return; + + for (const property of node.id.properties) { + if (property.type !== 'Property') continue; + const keyName = property.key.name ?? property.key.value; + if (property.value.type !== 'Identifier') continue; + const localName = property.value.name; + if (keyName === 'isCryptoKey') { + isCryptoKeyNames.add(localName); + } else if (cryptoKeyClassNames.has(keyName)) { + knownCryptoKeyClassNames.add(localName); + } + } + return; + } + + if (node.id.type === 'Identifier' && isCryptoKeyFactory(node.init)) { + knownCryptoKeyNames.add(node.id.name); + } + }, + + AssignmentExpression(node) { + if (node.left.type === 'Identifier' && isCryptoKeyFactory(node.right)) { + knownCryptoKeyNames.add(node.left.name); + } + }, + + MemberExpression(node) { + const property = getPropertyName(node); + const replacement = accessors.get(property); + if (replacement === undefined) return; + if (!isKnownCryptoKey(node.object)) return; + + context.report({ + node: node.property, + messageId: 'noPublicAccessor', + data: { + property, + replacement, + }, + }); + }, + + }; + }, +}; diff --git a/tools/eslint-rules/no-keyobject-cryptokey-instanceof.js b/tools/eslint-rules/no-keyobject-cryptokey-instanceof.js new file mode 100644 index 00000000000..19e13437247 --- /dev/null +++ b/tools/eslint-rules/no-keyobject-cryptokey-instanceof.js @@ -0,0 +1,122 @@ +/** + * @file Prevent internal code from brand-checking keys with instanceof. + */ +'use strict'; + +const { isRequireCall, isString } = require('./rules-utils.js'); + +const CRYPTO_KEYS_MODULE = 'internal/crypto/keys'; +const WEBCRYPTO_MODULE = 'internal/crypto/webcrypto'; + +const keyObjectClassNames = new Set([ + 'KeyObject', + 'SecretKeyObject', + 'AsymmetricKeyObject', + 'PublicKeyObject', + 'PrivateKeyObject', +]); + +const cryptoKeyClassNames = new Set([ + 'CryptoKey', + 'InternalCryptoKey', +]); + +function isKeyModuleRequire(node) { + return node?.type === 'CallExpression' && + isRequireCall(node) && + isString(node.arguments[0]) && + (node.arguments[0].value === CRYPTO_KEYS_MODULE || + node.arguments[0].value === WEBCRYPTO_MODULE); +} + +function getPropertyName(node) { + if (!node) return undefined; + if (node.computed) { + return node.property.type === 'Literal' ? node.property.value : undefined; + } + return node.property.name; +} + +module.exports = { + meta: { + messages: { + noKeyObjectInstanceof: 'Use `isKeyObject(value)` instead of `value instanceof KeyObject`.', + noCryptoKeyInstanceof: 'Use `isCryptoKey(value)` instead of `value instanceof CryptoKey`.', + }, + schema: [], + }, + + create(context) { + const namespaceNames = new Set(); + const keyObjectConstructorNames = new Set(); + const cryptoKeyConstructorNames = new Set(['CryptoKey']); + + function registerRequire(node) { + if (!isKeyModuleRequire(node.init)) return; + + if (node.id.type === 'Identifier') { + namespaceNames.add(node.id.name); + return; + } + + if (node.id.type !== 'ObjectPattern') return; + + for (const property of node.id.properties) { + if (property.type !== 'Property') continue; + const keyName = property.key.name ?? property.key.value; + if (property.value.type !== 'Identifier') continue; + const localName = property.value.name; + if (keyObjectClassNames.has(keyName)) { + keyObjectConstructorNames.add(localName); + } else if (cryptoKeyClassNames.has(keyName)) { + cryptoKeyConstructorNames.add(localName); + } + } + } + + function constructorKind(node) { + if (node.type === 'Identifier') { + if (keyObjectConstructorNames.has(node.name)) return 'KeyObject'; + if (cryptoKeyConstructorNames.has(node.name)) return 'CryptoKey'; + return undefined; + } + + if (node.type !== 'MemberExpression') return undefined; + + const property = getPropertyName(node); + if (node.object.type === 'Identifier') { + if (namespaceNames.has(node.object.name)) { + if (keyObjectClassNames.has(property)) return 'KeyObject'; + if (cryptoKeyClassNames.has(property)) return 'CryptoKey'; + } + if (node.object.name === 'globalThis' && + cryptoKeyClassNames.has(property)) { + return 'CryptoKey'; + } + } + + return undefined; + } + + return { + VariableDeclarator: registerRequire, + + BinaryExpression(node) { + if (node.operator !== 'instanceof') return; + + const kind = constructorKind(node.right); + if (kind === 'KeyObject') { + context.report({ + node, + messageId: 'noKeyObjectInstanceof', + }); + } else if (kind === 'CryptoKey') { + context.report({ + node, + messageId: 'noCryptoKeyInstanceof', + }); + } + }, + }; + }, +}; diff --git a/tools/eslint-rules/no-keyobject-public-accessors.js b/tools/eslint-rules/no-keyobject-public-accessors.js new file mode 100644 index 00000000000..64b93f4a70e --- /dev/null +++ b/tools/eslint-rules/no-keyobject-public-accessors.js @@ -0,0 +1,284 @@ +/** + * @file Prevent internal code from using public KeyObject accessors. + */ +'use strict'; + +const { isRequireCall, isString } = require('./rules-utils.js'); + +const KEYOBJECT_MODULE = 'internal/crypto/keys'; + +const accessors = new Map([ + ['type', 'getKeyObjectType(key)'], + ['symmetricKeySize', 'getKeyObjectSymmetricKeySize(key)'], + ['asymmetricKeyType', 'getKeyObjectAsymmetricKeyType(key)'], + ['asymmetricKeyDetails', 'getKeyObjectAsymmetricKeyDetails(key)'], + ['equals', 'getKeyObjectType(key) and getKeyObjectHandle(key)'], +]); + +const keyObjectClassNames = new Set([ + 'KeyObject', + 'SecretKeyObject', + 'AsymmetricKeyObject', + 'PublicKeyObject', + 'PrivateKeyObject', +]); + +const keyObjectFactoryNames = new Set([ + 'createSecretKey', + 'createPublicKey', + 'createPrivateKey', +]); + +function isInternalCryptoKeysRequire(node) { + return node?.type === 'CallExpression' && + isRequireCall(node) && + isString(node.arguments[0]) && + node.arguments[0].value === KEYOBJECT_MODULE; +} + +function getPropertyName(node) { + if (!node) return undefined; + if (node.computed) { + return node.property.type === 'Literal' ? node.property.value : undefined; + } + return node.property.name; +} + +function getIdentifierArgument(node) { + const arg = node.arguments[0]; + return arg?.type === 'Identifier' ? arg.name : undefined; +} + +function isNodeWithin(node, ancestor) { + return node.range[0] >= ancestor.range[0] && + node.range[1] <= ancestor.range[1]; +} + +function exits(statement) { + if (!statement) return false; + switch (statement.type) { + case 'BlockStatement': + return statement.body.length > 0 && exits(statement.body.at(-1)); + case 'ReturnStatement': + case 'ThrowStatement': + return true; + default: + return false; + } +} + +function findStatementInBlock(node) { + let current = node; + while (current?.parent) { + if ((current.parent.type === 'BlockStatement' || + current.parent.type === 'Program') && + current.parent.body.includes(current)) { + return { block: current.parent, statement: current }; + } + current = current.parent; + } +} + +module.exports = { + meta: { + messages: { + noPublicAccessor: 'Use `{{replacement}}` instead of the public KeyObject `{{property}}` accessor.', + }, + schema: [], + }, + + create(context) { + const isKeyObjectNames = new Set(); + const namespaceNames = new Set(); + const knownKeyObjectNames = new Set(); + const knownKeyObjectClassNames = new Set(keyObjectClassNames); + + function isIsKeyObjectCall(node) { + if (node?.type !== 'CallExpression') return false; + + if (node.callee.type === 'Identifier') { + return isKeyObjectNames.has(node.callee.name); + } + + if (node.callee.type === 'MemberExpression' && + !node.callee.computed && + node.callee.object.type === 'Identifier' && + namespaceNames.has(node.callee.object.name)) { + return node.callee.property.name === 'isKeyObject'; + } + + return false; + } + + function getConsequentKeyObjects(test) { + const names = new Set(); + if (isIsKeyObjectCall(test)) { + names.add(getIdentifierArgument(test)); + } else if (test?.type === 'LogicalExpression' && test.operator === '&&') { + for (const name of getConsequentKeyObjects(test.left)) { + names.add(name); + } + for (const name of getConsequentKeyObjects(test.right)) { + names.add(name); + } + } + names.delete(undefined); + return names; + } + + function getAlternateKeyObjects(test) { + const names = new Set(); + if (test?.type === 'UnaryExpression' && + test.operator === '!' && + isIsKeyObjectCall(test.argument)) { + names.add(getIdentifierArgument(test.argument)); + } + names.delete(undefined); + return names; + } + + function isKeyObjectFactory(node) { + if (node?.type === 'NewExpression' && + node.callee.type === 'Identifier') { + return knownKeyObjectClassNames.has(node.callee.name); + } + + if (node?.type !== 'CallExpression') return false; + + if (node.callee.type === 'Identifier') { + return keyObjectFactoryNames.has(node.callee.name); + } + + if (node.callee.type !== 'MemberExpression') return false; + const object = node.callee.object; + const property = getPropertyName(node.callee); + if (object.type === 'Identifier' && + knownKeyObjectClassNames.has(object.name)) { + return property === 'from'; + } + return object.type === 'Identifier' && + namespaceNames.has(object.name) && + keyObjectFactoryNames.has(property); + } + + function isInKeyObjectBranch(name, node) { + for (let current = node.parent; current; current = current.parent) { + if (current.type !== 'IfStatement') continue; + if (isNodeWithin(node, current.consequent) && + getConsequentKeyObjects(current.test).has(name)) { + return true; + } + if (current.alternate && + isNodeWithin(node, current.alternate) && + getAlternateKeyObjects(current.test).has(name)) { + return true; + } + } + return false; + } + + function followsExitingKeyObjectGuard(name, node) { + const location = findStatementInBlock(node); + if (!location) return false; + const index = location.block.body.indexOf(location.statement); + for (let i = 0; i < index; i++) { + const statement = location.block.body[i]; + if (statement.type === 'IfStatement' && + exits(statement.consequent) && + getAlternateKeyObjects(statement.test).has(name)) { + return true; + } + } + return false; + } + + function followsLogicalKeyObjectCheck(name, node) { + for (let current = node; current.parent; current = current.parent) { + const parent = current.parent; + if (parent.type !== 'LogicalExpression' || parent.operator !== '&&') { + continue; + } + if (parent.right === current && + getConsequentKeyObjects(parent.left).has(name)) { + return true; + } + } + return false; + } + + function isInsideKeyObjectClass(node) { + for (let current = node.parent; current; current = current.parent) { + if (current.type !== 'ClassDeclaration' && + current.type !== 'ClassExpression') { + continue; + } + + const className = current.id?.name; + const superName = current.superClass?.type === 'Identifier' ? + current.superClass.name : undefined; + return knownKeyObjectClassNames.has(className) || + knownKeyObjectClassNames.has(superName); + } + return false; + } + + function isKnownKeyObject(node) { + if (node.type === 'ThisExpression') { + return isInsideKeyObjectClass(node); + } + + if (node.type !== 'Identifier') return false; + return knownKeyObjectNames.has(node.name) || + isInKeyObjectBranch(node.name, node) || + followsLogicalKeyObjectCheck(node.name, node) || + followsExitingKeyObjectGuard(node.name, node); + } + + return { + VariableDeclarator(node) { + if (isInternalCryptoKeysRequire(node.init)) { + if (node.id.type === 'Identifier') { + namespaceNames.add(node.id.name); + return; + } + + if (node.id.type !== 'ObjectPattern') return; + + for (const property of node.id.properties) { + if (property.type !== 'Property') continue; + const keyName = property.key.name ?? property.key.value; + if (property.value.type !== 'Identifier') continue; + const localName = property.value.name; + if (keyName === 'isKeyObject') { + isKeyObjectNames.add(localName); + } else if (keyObjectClassNames.has(keyName)) { + knownKeyObjectClassNames.add(localName); + } + } + return; + } + + if (node.id.type === 'Identifier' && isKeyObjectFactory(node.init)) { + knownKeyObjectNames.add(node.id.name); + } + }, + + MemberExpression(node) { + const property = getPropertyName(node); + const replacement = accessors.get(property); + if (replacement === undefined) return; + if (!isKnownKeyObject(node.object)) return; + + context.report({ + node: node.property, + messageId: 'noPublicAccessor', + data: { + property, + replacement, + }, + }); + }, + + }; + }, +};