Skip to content

Commit 4037363

Browse files
authored
SONARPY-1419: FP on S6463 when using AWS from_security_group_id function (#1544)
1 parent 4a2edd0 commit 4037363

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

python-checks/src/main/java/org/sonar/python/checks/cdk/UnrestrictedOutboundCommunicationsCheck.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
package org.sonar.python.checks.cdk;
2121

2222
import java.util.List;
23+
import java.util.Optional;
24+
2325
import org.sonar.check.Rule;
26+
import org.sonar.plugins.python.api.SubscriptionContext;
27+
import org.sonar.plugins.python.api.symbols.Symbol;
28+
import org.sonar.plugins.python.api.tree.CallExpression;
2429

2530
import static org.sonar.python.checks.cdk.CdkPredicate.isTrue;
2631
import static org.sonar.python.checks.cdk.CdkUtils.getArgument;
@@ -31,13 +36,23 @@ public class UnrestrictedOutboundCommunicationsCheck extends AbstractCdkResource
3136
public static final String OMITTING_MESSAGE = "Omitting \"allow_all_outbound\" enables unrestricted outbound communications. Make sure it is safe here.";
3237
public static final String UNRESTRICTED_MESSAGE = "Make sure that allowing unrestricted outbound communications is safe here.";
3338

39+
private static final String SECURITY_GROUP_FQN = "aws_cdk.aws_ec2.SecurityGroup";
40+
3441
@Override
3542
protected void registerFqnConsumer() {
36-
checkFqns(List.of("aws_cdk.aws_ec2.SecurityGroup", "aws_cdk.aws_ec2.SecurityGroup.from_security_group_id"), (subscriptionContext, callExpression) ->
43+
checkFqns(List.of(SECURITY_GROUP_FQN, "aws_cdk.aws_ec2.SecurityGroup.from_security_group_id"), (subscriptionContext, callExpression) ->
3744
getArgument(subscriptionContext, callExpression, "allow_all_outbound").ifPresentOrElse(
3845
argument -> argument.addIssueIf(isTrue(), UNRESTRICTED_MESSAGE),
39-
() -> subscriptionContext.addIssue(callExpression.callee(), OMITTING_MESSAGE)
46+
() -> raiseIssue(subscriptionContext, callExpression)
4047
)
4148
);
4249
}
50+
51+
private static void raiseIssue(SubscriptionContext subscriptionContext, CallExpression callExpression) {
52+
Optional.ofNullable(callExpression.calleeSymbol())
53+
.map(Symbol::fullyQualifiedName)
54+
.filter(fqn -> fqn.equals(SECURITY_GROUP_FQN))
55+
.ifPresent(s -> subscriptionContext.addIssue(callExpression.callee(), OMITTING_MESSAGE));
56+
}
57+
4358
}

python-checks/src/test/resources/checks/cdk/unrestrictedOutboundCommunicationsCheck.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,15 @@ def __init__(self, vpc):
2121
"sg-1234",
2222
allow_all_outbound=True # NonCompliant{{Make sure that allowing unrestricted outbound communications is safe here.}}
2323
)
24+
25+
ec2.SecurityGroup.from_security_group_id(
26+
self,
27+
"SensitiveExplicit",
28+
"sg-1234",
29+
allow_all_outbound=False
30+
)
2431

25-
ec2.SecurityGroup.from_security_group_id( # NonCompliant{{Omitting "allow_all_outbound" enables unrestricted outbound communications. Make sure it is safe here.}}
32+
ec2.SecurityGroup.from_security_group_id( # Compliant ref: SONARPY-1159 and SONARPY-1419
2633
self,
2734
"SensitiveDefault",
2835
vpc=vpc

0 commit comments

Comments
 (0)