eslint-plugin-sonarjs
Version:
SonarJS rules for ESLint
76 lines (75 loc) • 3.19 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/S5042/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");
exports.rule = {
meta: (0, index_js_1.generateMeta)(meta_js_1.meta, {
messages: {
safeExpanding: 'Make sure that expanding this archive file is safe here.',
},
}),
create(context) {
function canBeProperty(prop, name) {
return (prop.type === 'SpreadElement' ||
(0, index_js_1.isIdentifier)(prop.key, name) ||
((0, index_js_1.isLiteral)(prop.key) && prop.key.value === name));
}
function isSensitiveTarCall(call, fqn) {
if (fqn === 'tar.x') {
const firstArg = call.arguments.length > 0 ? call.arguments[0] : null;
if (!firstArg) {
return false;
}
const firstArgValue = (0, index_js_1.getValueOfExpression)(context, firstArg, 'ObjectExpression');
return (!!firstArgValue && !firstArgValue.properties.some(prop => canBeProperty(prop, 'filter')));
}
return false;
}
function isSensitiveExtractZipCall(call, fqn) {
if (fqn === 'extract-zip') {
const secondArg = call.arguments.length > 1 ? call.arguments[1] : null;
if (!secondArg) {
return false;
}
const secondArgValue = (0, index_js_1.getValueOfExpression)(context, secondArg, 'ObjectExpression');
return (!!secondArgValue &&
!secondArgValue.properties.some(prop => canBeProperty(prop, 'onEntry')));
}
return false;
}
return {
CallExpression(node) {
const call = node;
const fqn = (0, index_js_1.getFullyQualifiedName)(context, call);
if (isSensitiveTarCall(call, fqn) ||
isSensitiveExtractZipCall(call, fqn) ||
fqn === 'jszip.loadAsync' ||
fqn === 'yauzl.open' ||
fqn === 'adm-zip.extractAllTo') {
context.report({
messageId: 'safeExpanding',
node: call.callee,
});
}
},
};
},
};
;