-
Notifications
You must be signed in to change notification settings - Fork 193
JS-824 Add rule S7639 #5919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
JS-824 Add rule S7639 #5919
Changes from 13 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d06601d
JS-824 Add rule S7639
loris-s-sonarsource 6a6bcc9
rule logic first pass doneé
loris-s-sonarsource 6cb3f93
add meta
loris-s-sonarsource 3cd18d9
typo
loris-s-sonarsource 8f51b7a
typo
loris-s-sonarsource b0264de
Add base use case
loris-s-sonarsource 139ed18
actual JS files
loris-s-sonarsource ca8aef9
fix incorrect template
loris-s-sonarsource a3cd221
fake commit
loris-s-sonarsource 4e012dc
fake commit
loris-s-sonarsource d98fcc7
Apply SQ recos
loris-s-sonarsource 697f8a7
oversight (sorry for the noise guys)
loris-s-sonarsource 6d0e196
change typecast
loris-s-sonarsource 3dcced9
Add its
loris-s-sonarsource 3742378
added guardrail to avoid collecting all strings
loris-s-sonarsource e3b4b0c
tweak oversight
loris-s-sonarsource af5a809
remove newline
loris-s-sonarsource 7c0a354
finally getting out of that rabbit hole
loris-s-sonarsource File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { Wallet } from 'ethers'; | ||
| import { mnemonicToAccount } from 'viem/accounts'; | ||
| import TronWeb from 'tronweb'; | ||
|
|
||
| // Variable used instead of hardcoded string | ||
| const seedPhrase = process.env.SEED_PHRASE; | ||
| const wallet = Wallet.fromPhrase(seedPhrase); | ||
|
|
||
| // Function call result used | ||
| const wallet = Wallet.fromPhrase(getSeedPhrase()); | ||
|
|
||
| // No arguments passed | ||
| const wallet = Wallet.fromPhrase(); | ||
|
|
||
| // Template literal with expression | ||
| const account = mnemonicToAccount(`${getPhrase()}`); | ||
|
|
||
| // Ethers with hardcoded string literal | ||
| const wallet = Wallet.fromPhrase("Round Outgoing Yanni Gripped Buoyant Iodine Victoriously"); // Noncompliant {{Revoke and change this seed phrase, as it is compromised.}} | ||
|
|
||
| // Viem with hardcoded string | ||
| const account = mnemonicToAccount('test test test test test test test test test test test junk'); // Noncompliant {{Revoke and change this seed phrase, as it is compromised.}} | ||
|
|
||
| // TronWeb with hardcoded string | ||
| const wallet = TronWeb.fromMnemonic("candy sugar pudding cream honey rich smooth crumble sweet treat"); // Noncompliant {{Revoke and change this seed phrase, as it is compromised.}} | ||
|
|
||
| // Template literal without expressions | ||
| const wallet = Wallet.fromPhrase(`abandon abandon abandon abandon abandon abandon abandon abandon about`); // Noncompliant {{Revoke and change this seed phrase, as it is compromised.}} | ||
|
|
||
| // CommonJS require | ||
| const ethers = require('ethers'); | ||
| const wallet = ethers.HDNodeWallet.fromPhrase("fake test test test test test test test test test test junk"); // Noncompliant {{Revoke and change this seed phrase, as it is compromised.}} | ||
|
|
||
| // HDNodeWallet with named import and variable assignment | ||
| const mnemonic = 'Pigeons Petted Bushes Effectively Once Krusty Defeated Grapes'; // Noncompliant {{Revoke and change this seed phrase, as it is compromised.}} | ||
| const mnemonicWallet = HDNodeWallet.fromPhrase(mnemonic); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| * SonarQube JavaScript Plugin | ||
| * Copyright (C) 2011-2025 SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| // https://sonarsource.github.io/rspec/#/rspec/S7639/javascript | ||
| import { test } from '../../../tests/tools/testers/comment-based/checker.js'; | ||
| import { rule } from './index.js'; | ||
| import { describe } from 'node:test'; | ||
| import * as meta from './generated-meta.js'; | ||
|
|
||
| describe(`Rule S7639`, () => { | ||
| test(meta, rule, import.meta.dirname); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| /* | ||
| * SonarQube JavaScript Plugin | ||
| * Copyright (C) 2011-2025 SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| export { rule } from './rule.js'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /* | ||
| * SonarQube JavaScript Plugin | ||
| * Copyright (C) 2011-2025 SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| // https://sonarsource.github.io/rspec/#/rspec/S7639/javascript | ||
| export const implementation = 'original'; | ||
| export const eslintId = 'review-blockchain-mnemonic'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* | ||
| * SonarQube JavaScript Plugin | ||
| * Copyright (C) 2011-2025 SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| // https://sonarsource.github.io/rspec/#/rspec/S7639/javascript | ||
|
|
||
| import type { Rule } from 'eslint'; | ||
| import type estree from 'estree'; | ||
| import { generateMeta, isMemberWithProperty, isRequireModule } from '../helpers/index.js'; | ||
| import * as meta from './generated-meta.js'; | ||
|
|
||
| const BLOCKCHAIN_MODULES = ['ethers', 'viem/accounts', 'tronweb']; | ||
| const MNEMONIC_FUNCTIONS = ['fromPhrase', 'mnemonicToAccount', 'fromMnemonic']; | ||
|
|
||
| export const rule: Rule.RuleModule = { | ||
| meta: generateMeta(meta, { | ||
| messages: { | ||
| reviewBlockchainSeedPhrase: `Revoke and change this seed phrase, as it is compromised.`, | ||
| }, | ||
| }), | ||
| create(context: Rule.RuleContext) { | ||
| let isBlockchainModuleImported = false; | ||
| const hardcodedVariables = new Map<string, estree.Node>(); | ||
|
|
||
| function isHardcodedString(expr: estree.Expression): boolean { | ||
| switch (expr.type) { | ||
| case 'Literal': | ||
| return typeof expr.value === 'string'; | ||
| case 'TemplateLiteral': | ||
| return expr.expressions.length === 0; | ||
| case 'Identifier': | ||
| return hardcodedVariables.has(expr.name); | ||
| default: | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| function getReportNode(expr: estree.Expression): estree.Node { | ||
| // If it's an identifier that references a hardcoded string, report the original declaration | ||
| if (expr.type === 'Identifier' && hardcodedVariables.has(expr.name)) { | ||
| const nodeName = hardcodedVariables.get(expr.name); | ||
| if (nodeName) { | ||
| return nodeName; | ||
| } | ||
| } | ||
| return expr; | ||
| } | ||
|
|
||
| function isMnemonicFunction(callee: estree.Expression | estree.Super): boolean { | ||
| return MNEMONIC_FUNCTIONS.some(func => isMemberWithProperty(callee, func)); | ||
| } | ||
|
|
||
| return { | ||
| Program() { | ||
| isBlockchainModuleImported = false; | ||
| hardcodedVariables.clear(); | ||
| }, | ||
|
|
||
| ImportDeclaration(node: estree.ImportDeclaration) { | ||
| if (BLOCKCHAIN_MODULES.includes(node.source.value as string)) { | ||
| isBlockchainModuleImported = true; | ||
| } | ||
| }, | ||
|
|
||
| VariableDeclarator(node: estree.VariableDeclarator) { | ||
| if ( | ||
| node.id.type === 'Identifier' && | ||
| node.init && | ||
| ((node.init.type === 'Literal' && typeof node.init.value === 'string') || | ||
| (node.init.type === 'TemplateLiteral' && node.init.expressions.length === 0)) | ||
| ) { | ||
| hardcodedVariables.set(node.id.name, node.init); | ||
| } | ||
| }, | ||
|
|
||
| CallExpression(node: estree.CallExpression) { | ||
| if (isRequireModule(node, ...BLOCKCHAIN_MODULES)) { | ||
| isBlockchainModuleImported = true; | ||
| return; | ||
| } | ||
|
|
||
| if ( | ||
| isBlockchainModuleImported && | ||
| isMnemonicFunction(node.callee) && | ||
| node.arguments.length > 0 && | ||
| node.arguments[0].type !== 'SpreadElement' && | ||
| isHardcodedString(node.arguments[0]) | ||
| ) { | ||
| context.report({ | ||
| messageId: 'reviewBlockchainSeedPhrase', | ||
| node: getReportNode(node.arguments[0]), | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.