eslint-plugin-sonarjs
Version:
SonarJS rules for ESLint
132 lines (131 loc) • 5.29 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/S4426/javascript
Object.defineProperty(exports, "__esModule", { value: true });
exports.rule = void 0;
const index_js_1 = require("../helpers/index.js");
const meta_js_1 = require("./meta.js");
const MINIMAL_MODULUS_LENGTH = 2048;
const MINIMAL_DIVISOR_LENGTH = 224;
const WEAK_CURVES = [
'secp112r1',
'secp112r2',
'secp128r1',
'secp128r2',
'secp160k1',
'secp160r1',
'secp160r2',
'secp160r2',
'secp192k1',
'secp192r1',
'prime192v2',
'prime192v3',
'sect113r1',
'sect113r2',
'sect131r1',
'sect131r2',
'sect163k1',
'sect163r1',
'sect163r2',
'sect193r1',
'sect193r2',
'c2tnb191v1',
'c2tnb191v2',
'c2tnb191v3',
];
exports.rule = {
meta: (0, index_js_1.generateMeta)(meta_js_1.meta, {
messages: {
modulusLength: 'Use a modulus length of at least {{minimalLength}} bits for {{algorithm}} cipher algorithm.',
divisorLength: 'Use a divisor length of at least {{minimalLength}} bits for {{algorithm}} cipher algorithm.',
strongerCurve: `{{curve}} doesn't provide enough security. Use a stronger curve.`,
},
}),
create(context) {
function getNumericValue(node) {
const literal = (0, index_js_1.getValueOfExpression)(context, node, 'Literal');
if (literal && typeof literal.value === 'number') {
return literal.value;
}
return undefined;
}
function checkRsaAndDsaOptions(algorithm, options) {
const modulusProperty = (0, index_js_1.getProperty)(options, 'modulusLength', context);
const modulusLength = getNumericValue(modulusProperty?.value);
if (modulusProperty && modulusLength && modulusLength < MINIMAL_MODULUS_LENGTH) {
context.report({
node: modulusProperty,
messageId: 'modulusLength',
data: {
minimalLength: MINIMAL_MODULUS_LENGTH.toString(),
algorithm,
},
});
}
const divisorProperty = (0, index_js_1.getProperty)(options, 'divisorLength', context);
const divisorLength = getNumericValue(divisorProperty?.value);
if (divisorProperty && divisorLength && divisorLength < MINIMAL_DIVISOR_LENGTH) {
context.report({
node: divisorProperty,
messageId: 'divisorLength',
data: {
minimalLength: MINIMAL_DIVISOR_LENGTH.toString(),
algorithm,
},
});
}
}
function checkEcCurve(options) {
const namedCurveProperty = (0, index_js_1.getProperty)(options, 'namedCurve', context);
const namedCurve = (0, index_js_1.getValueOfExpression)(context, namedCurveProperty?.value, 'Literal')?.value?.toString();
if (namedCurveProperty && namedCurve && WEAK_CURVES.includes(namedCurve)) {
context.report({
node: namedCurveProperty,
messageId: 'strongerCurve',
data: {
curve: namedCurve,
},
});
}
}
return {
CallExpression: (node) => {
const callExpression = node;
const { callee } = callExpression;
if (callee.type === 'MemberExpression' &&
(0, index_js_1.isIdentifier)(callee.property, 'generateKeyPair', 'generateKeyPairSync')) {
if (callExpression.arguments.length < 2) {
return;
}
const [algorithmArg, options] = callExpression.arguments;
const optionsArg = (0, index_js_1.getValueOfExpression)(context, options, 'ObjectExpression');
if (!optionsArg) {
return;
}
const algorithm = (0, index_js_1.getValueOfExpression)(context, algorithmArg, 'Literal')?.value;
if (algorithm === 'rsa' || algorithm === 'dsa') {
checkRsaAndDsaOptions(algorithm, optionsArg);
}
else if (algorithm === 'ec') {
checkEcCurve(optionsArg);
}
}
},
};
},
};
;