eslint-plugin-sonarjs
Version:
SonarJS rules for ESLint
80 lines (79 loc) • 3.23 kB
JavaScript
/*
* SonarQube JavaScript Plugin
* Copyright (C) 2011-2025 SonarSource SA
* 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/S2068/javascript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.rule = void 0;
const index_js_1 = require("../helpers/index.js");
const path_1 = __importDefault(require("path"));
const meta_js_1 = require("./meta.js");
const DEFAULT_NAMES = ['password', 'pwd', 'passwd'];
const messages = {
reviewPassword: 'Review this potentially hard-coded password.',
};
exports.rule = {
meta: (0, index_js_1.generateMeta)(meta_js_1.meta, { messages, schema: meta_js_1.schema }),
create(context) {
const dir = path_1.default.dirname(context.physicalFilename);
const parts = dir.split(path_1.default.sep).map(part => part.toLowerCase());
if (parts.includes('l10n')) {
return {};
}
const variableNames = context.options[0]?.passwordWords ?? DEFAULT_NAMES;
const literalRegExp = variableNames.map(name => new RegExp(`${name}=.+`));
return {
VariableDeclarator: (node) => {
const declaration = node;
checkAssignment(context, variableNames, declaration.id, declaration.init);
},
AssignmentExpression: (node) => {
const assignment = node;
checkAssignment(context, variableNames, assignment.left, assignment.right);
},
Property: (node) => {
const property = node;
checkAssignment(context, variableNames, property.key, property.value);
},
Literal: (node) => {
const literal = node;
checkLiteral(context, literalRegExp, literal);
},
};
},
};
function checkAssignment(context, patterns, variable, initializer) {
if (initializer &&
(0, index_js_1.isStringLiteral)(initializer) &&
initializer.value.length > 0 &&
patterns.some(pattern => context.sourceCode.getText(variable).includes(pattern))) {
context.report({
messageId: 'reviewPassword',
node: initializer,
});
}
}
function checkLiteral(context, patterns, literal) {
if ((0, index_js_1.isStringLiteral)(literal) && patterns.some(pattern => pattern.test(literal.value))) {
context.report({
messageId: 'reviewPassword',
node: literal,
});
}
}
;