eslint-plugin-sonarjs
Version:
SonarJS rules for ESLint
106 lines (105 loc) • 4.89 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/S6329/javascript
Object.defineProperty(exports, "__esModule", { value: true });
exports.rule = void 0;
const cdk_js_1 = require("../helpers/aws/cdk.js");
const result_js_1 = require("../helpers/result.js");
const index_js_1 = require("../helpers/index.js");
const meta_js_1 = require("./meta.js");
const PROPERTIES_POSITION = 2;
const PRIVATE_SUBNETS = [
'aws_cdk_lib.aws_ec2.SubnetType.PRIVATE_ISOLATED',
'aws_cdk_lib.aws_ec2.SubnetType.PRIVATE_WITH_EGRESS',
'aws_cdk_lib.aws_ec2.SubnetType.PRIVATE_WITH_NAT',
];
const PUBLIC_SUBNET = 'aws_cdk_lib.aws_ec2.SubnetType.PUBLIC';
exports.rule = (0, cdk_js_1.AwsCdkTemplate)({
'aws-cdk-lib.aws-ec2.Instance': (0, cdk_js_1.AwsCdkCheckArguments)('publicNetwork', false, ['vpcSubnets', 'subnetType'], { fqns: { invalid: [PUBLIC_SUBNET] } }),
'aws-cdk-lib.aws-ec2.CfnInstance': checkCfnInstance,
'aws-cdk-lib.aws_rds.DatabaseInstance': checkDatabaseInstance,
'aws-cdk-lib.aws_rds.CfnDBInstance': (0, cdk_js_1.AwsCdkCheckArguments)('publicNetwork', false, 'publiclyAccessible', { primitives: { invalid: [true] } }),
'aws-cdk-lib.aws_dms.CfnReplicationInstance': (0, cdk_js_1.AwsCdkCheckArguments)('publicNetwork', true, 'publiclyAccessible', { primitives: { invalid: [true] } }),
}, (0, index_js_1.generateMeta)(meta_js_1.meta, {
messages: {
publicNetwork: 'Make sure allowing public network access is safe here.',
},
}));
function checkCfnInstance(expr, ctx) {
const properties = (0, result_js_1.getResultOfExpression)(ctx, expr).getArgument(PROPERTIES_POSITION);
const networkInterfaces = properties.getProperty('networkInterfaces');
const sensitiveNetworkInterface = networkInterfaces.findInArray(result => getSensitiveNetworkInterface(result, ctx));
if (sensitiveNetworkInterface.isFound) {
ctx.report({
messageId: 'publicNetwork',
node: sensitiveNetworkInterface.node,
});
}
}
function getSensitiveNetworkInterface(networkInterface, ctx) {
const associatePublicIpAddress = networkInterface.getProperty('associatePublicIpAddress');
if (associatePublicIpAddress.isTrue && !isFoundPrivateSubnet(networkInterface, ctx)) {
return associatePublicIpAddress;
}
else {
return null;
}
}
function isFoundPrivateSubnet(networkInterface, ctx) {
const subnetId = networkInterface.getProperty('subnetId');
const selectSubnetsCall = getSelectSubnetsCall(subnetId);
const argument = selectSubnetsCall.getArgument(0);
const subnetType = argument.getProperty('subnetType');
return subnetType.isFound && isPrivateSubnet(subnetType.node, ctx);
}
function getSelectSubnetsCall(subnetId) {
let current = subnetId;
while (current.ofType('MemberExpression')) {
current = current.getMemberObject();
}
return current.filter(n => n.type === 'CallExpression' && (0, index_js_1.isCallingMethod)(n, 1, 'selectSubnets'));
}
function checkDatabaseInstance(expr, ctx) {
const properties = (0, result_js_1.getResultOfExpression)(ctx, expr).getArgument(PROPERTIES_POSITION);
const vpcSubnets = properties.getProperty('vpcSubnets');
const subnetType = vpcSubnets.getProperty('subnetType');
const publiclyAccessible = properties.getProperty('publiclyAccessible');
if (subnetType.isFound && isPrivateSubnet(subnetType.node, ctx)) {
return;
}
if (publiclyAccessible.isTrue && subnetType.isFound && isPublicSubnet(subnetType.node, ctx)) {
ctx.report({
messageId: 'publicNetwork',
node: publiclyAccessible.node,
});
}
else if (!publiclyAccessible.isFound &&
subnetType.isFound &&
isPublicSubnet(subnetType.node, ctx)) {
ctx.report({
messageId: 'publicNetwork',
node: subnetType.node,
});
}
}
function isPrivateSubnet(node, ctx) {
return PRIVATE_SUBNETS.some(net => net === (0, index_js_1.getFullyQualifiedName)(ctx, node)?.replace(/-/g, '_'));
}
function isPublicSubnet(node, ctx) {
return PUBLIC_SUBNET === (0, index_js_1.getFullyQualifiedName)(ctx, node)?.replace(/-/g, '_');
}
;