|
| 1 | +/* |
| 2 | + * SonarQube JavaScript Plugin |
| 3 | + * Copyright (C) 2011-2025 SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the Sonar Source-Available License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Sonar Source-Available License |
| 15 | + * along with this program; if not, see https://sonarsource.com/license/ssal/ |
| 16 | + */ |
| 17 | +// https://sonarsource.github.io/rspec/#/rspec/S7639/javascript |
| 18 | + |
| 19 | +import type { Rule } from 'eslint'; |
| 20 | +import type estree from 'estree'; |
| 21 | +import { |
| 22 | + generateMeta, |
| 23 | + isIdentifier, |
| 24 | + isMemberWithProperty, |
| 25 | + isRequireModule, |
| 26 | +} from '../helpers/index.js'; |
| 27 | +import * as meta from './generated-meta.js'; |
| 28 | + |
| 29 | +const BLOCKCHAIN_MODULES = ['ethers', 'viem/accounts', 'tronweb']; |
| 30 | +const MNEMONIC_FUNCTIONS = ['fromPhrase', 'mnemonicToAccount', 'fromMnemonic']; |
| 31 | + |
| 32 | +export const rule: Rule.RuleModule = { |
| 33 | + meta: generateMeta(meta, { |
| 34 | + messages: { |
| 35 | + reviewBlockchainSeedPhrase: `Revoke and change this seed phrase, as it is compromised.`, |
| 36 | + }, |
| 37 | + }), |
| 38 | + create(context: Rule.RuleContext) { |
| 39 | + let isBlockchainModuleImported = false; |
| 40 | + const hardcodedVariables = new Map<string, estree.Node>(); |
| 41 | + |
| 42 | + function isHardcodedString(expr: estree.Expression): boolean { |
| 43 | + switch (expr.type) { |
| 44 | + case 'Literal': |
| 45 | + return typeof expr.value === 'string'; |
| 46 | + case 'TemplateLiteral': |
| 47 | + return expr.expressions.length === 0; |
| 48 | + case 'Identifier': |
| 49 | + return hardcodedVariables.has(expr.name); |
| 50 | + default: |
| 51 | + return false; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + function getReportNode(expr: estree.Expression): estree.Node { |
| 56 | + // If it's an identifier that references a hardcoded string, report the original declaration |
| 57 | + if (expr.type === 'Identifier' && hardcodedVariables.has(expr.name)) { |
| 58 | + const nodeName = hardcodedVariables.get(expr.name); |
| 59 | + if (nodeName) { |
| 60 | + return nodeName; |
| 61 | + } |
| 62 | + } |
| 63 | + return expr; |
| 64 | + } |
| 65 | + |
| 66 | + function isMnemonicFunction(callee: estree.Expression | estree.Super): boolean { |
| 67 | + return MNEMONIC_FUNCTIONS.some( |
| 68 | + func => isMemberWithProperty(callee, func) || isIdentifier(callee, func), |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + return { |
| 73 | + Program() { |
| 74 | + isBlockchainModuleImported = false; |
| 75 | + hardcodedVariables.clear(); |
| 76 | + }, |
| 77 | + |
| 78 | + ImportDeclaration(node: estree.ImportDeclaration) { |
| 79 | + if (BLOCKCHAIN_MODULES.includes(node.source.value as string)) { |
| 80 | + isBlockchainModuleImported = true; |
| 81 | + } |
| 82 | + }, |
| 83 | + |
| 84 | + VariableDeclarator(node: estree.VariableDeclarator) { |
| 85 | + if ( |
| 86 | + isBlockchainModuleImported && |
| 87 | + node.id.type === 'Identifier' && |
| 88 | + node.init && |
| 89 | + ((node.init.type === 'Literal' && typeof node.init.value === 'string') || |
| 90 | + (node.init.type === 'TemplateLiteral' && node.init.expressions.length === 0)) |
| 91 | + ) { |
| 92 | + hardcodedVariables.set(node.id.name, node.init); |
| 93 | + } |
| 94 | + }, |
| 95 | + |
| 96 | + CallExpression(node: estree.CallExpression) { |
| 97 | + if (isRequireModule(node, ...BLOCKCHAIN_MODULES)) { |
| 98 | + isBlockchainModuleImported = true; |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + if ( |
| 103 | + isBlockchainModuleImported && |
| 104 | + isMnemonicFunction(node.callee) && |
| 105 | + node.arguments.length > 0 && |
| 106 | + node.arguments[0].type !== 'SpreadElement' && |
| 107 | + isHardcodedString(node.arguments[0]) |
| 108 | + ) { |
| 109 | + context.report({ |
| 110 | + messageId: 'reviewBlockchainSeedPhrase', |
| 111 | + node: getReportNode(node.arguments[0]), |
| 112 | + }); |
| 113 | + } |
| 114 | + }, |
| 115 | + }; |
| 116 | + }, |
| 117 | +}; |
0 commit comments