|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) 2011-2025 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the Sonar Source-Available License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Sonar Source-Available License |
| 15 | + * along with this program; if not, see https://sonarsource.com/license/ssal/ |
| 16 | + */ |
| 17 | +package org.sonar.python.checks.cdk; |
| 18 | + |
| 19 | +import java.util.Optional; |
| 20 | +import java.util.function.BiConsumer; |
| 21 | +import org.sonar.check.Rule; |
| 22 | +import org.sonar.plugins.python.api.SubscriptionContext; |
| 23 | +import org.sonar.plugins.python.api.symbols.Usage; |
| 24 | +import org.sonar.plugins.python.api.tree.AssignmentStatement; |
| 25 | +import org.sonar.plugins.python.api.tree.CallExpression; |
| 26 | +import org.sonar.plugins.python.api.tree.Expression; |
| 27 | +import org.sonar.plugins.python.api.tree.ListLiteral; |
| 28 | +import org.sonar.plugins.python.api.tree.Name; |
| 29 | +import org.sonar.plugins.python.api.tree.QualifiedExpression; |
| 30 | +import org.sonar.plugins.python.api.tree.RegularArgument; |
| 31 | +import org.sonar.plugins.python.api.tree.Tree; |
| 32 | +import org.sonar.python.tree.TreeUtils; |
| 33 | + |
| 34 | +@Rule(key = "S6249") |
| 35 | +public class S3BucketHTTPCommunicationCheck extends AbstractS3BucketCheck { |
| 36 | + |
| 37 | + private static final String MESSAGE_NO_POLICY = "No bucket policy enforces HTTPS-only access to this bucket. Make sure it is safe here."; |
| 38 | + private static final String MESSAGE_HTTP_ALLOWED = "Make sure authorizing HTTP requests is safe here."; |
| 39 | + private static final String POLICY_STATEMENT_FQN = "aws_cdk.aws_iam.PolicyStatement"; |
| 40 | + |
| 41 | + @Override |
| 42 | + BiConsumer<SubscriptionContext, CallExpression> visitBucketConstructor() { |
| 43 | + return S3BucketHTTPCommunicationCheck::checkBucketConstructor; |
| 44 | + } |
| 45 | + |
| 46 | + private static void checkBucketConstructor(SubscriptionContext ctx, CallExpression bucketConstructorCall) { |
| 47 | + |
| 48 | + var enforceSslArg = CdkUtils.getArgument(ctx, bucketConstructorCall, "enforce_ssl"); |
| 49 | + |
| 50 | + if (enforceSslArg.isEmpty()) { |
| 51 | + // No enforce_ssl parameter specified |
| 52 | + // check if this bucket has add_to_resource_policy calls and a correct policy statement |
| 53 | + getBucketPolicy(bucketConstructorCall) |
| 54 | + .flatMap(S3BucketHTTPCommunicationCheck::getPolicyStatementConstructor) |
| 55 | + .ifPresentOrElse(policyStatementConstructorCall -> visitPolicyStatement(ctx, policyStatementConstructorCall), |
| 56 | + () -> ctx.addIssue(bucketConstructorCall.callee(), MESSAGE_NO_POLICY)); |
| 57 | + } else { |
| 58 | + enforceSslArg.get().addIssueIf(CdkPredicate.isFalse(), MESSAGE_HTTP_ALLOWED, bucketConstructorCall); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private static Optional<CallExpression> getBucketPolicy(CallExpression bucketConstructor) { |
| 63 | + return Optional.ofNullable(TreeUtils.firstAncestorOfKind(bucketConstructor, Tree.Kind.ASSIGNMENT_STMT)) |
| 64 | + .flatMap(TreeUtils.toOptionalInstanceOfMapper(AssignmentStatement.class)) |
| 65 | + .flatMap(S3BucketHTTPCommunicationCheck::getFirstAndOnlyAssignedVariable) |
| 66 | + .flatMap(TreeUtils.toOptionalInstanceOfMapper(Name.class)) |
| 67 | + .flatMap(S3BucketHTTPCommunicationCheck::getAddToResourcePolicyCall); |
| 68 | + } |
| 69 | + |
| 70 | + private static Optional<Expression> getFirstAndOnlyAssignedVariable(AssignmentStatement assignmentStatement) { |
| 71 | + return assignmentStatement.lhsExpressions().stream() |
| 72 | + .filter(lhs -> lhs.expressions().size() == 1) |
| 73 | + .map(lhs -> lhs.expressions().get(0)) |
| 74 | + .findFirst(); |
| 75 | + } |
| 76 | + |
| 77 | + private static Optional<CallExpression> getAddToResourcePolicyCall(Name variableName) { |
| 78 | + var symbol = TreeUtils.getSymbolFromTree(variableName); |
| 79 | + if (symbol.isEmpty()) { |
| 80 | + return Optional.empty(); |
| 81 | + } |
| 82 | + |
| 83 | + // Check if the usage is part of a qualified expression like "bucket.add_to_resource_policy" |
| 84 | + return symbol.get().usages().stream() |
| 85 | + .filter(usage -> usage.kind() != Usage.Kind.ASSIGNMENT_LHS) |
| 86 | + .map(usage -> usage.tree().parent()) |
| 87 | + .flatMap(TreeUtils.toStreamInstanceOfMapper(QualifiedExpression.class)) |
| 88 | + .filter(qualifiedExpr -> CdkPredicate.isFqn("add_to_resource_policy").test(qualifiedExpr.name())) |
| 89 | + .map(QualifiedExpression::parent) |
| 90 | + .flatMap(TreeUtils.toStreamInstanceOfMapper(CallExpression.class)) |
| 91 | + .findFirst(); |
| 92 | + } |
| 93 | + |
| 94 | + private static Optional<CallExpression> getPolicyStatementConstructor(CallExpression addResourceToPolicyCall) { |
| 95 | + if (!addResourceToPolicyCall.arguments().isEmpty()) { |
| 96 | + return Optional.of(addResourceToPolicyCall.arguments().get(0)) |
| 97 | + .flatMap(TreeUtils.toOptionalInstanceOfMapper(RegularArgument.class)) |
| 98 | + .map(RegularArgument::expression) |
| 99 | + .flatMap(TreeUtils.toOptionalInstanceOfMapper(CallExpression.class)) |
| 100 | + .filter(CdkPredicate.isFqn(POLICY_STATEMENT_FQN)::test); |
| 101 | + } |
| 102 | + return Optional.empty(); |
| 103 | + } |
| 104 | + |
| 105 | + private static void visitPolicyStatement(SubscriptionContext ctx, CallExpression policyStatementConstructorCall) { |
| 106 | + if (!isPolicyCompliantForHttpsDeny(ctx, policyStatementConstructorCall)) { |
| 107 | + ctx.addIssue(policyStatementConstructorCall.callee(), MESSAGE_HTTP_ALLOWED); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private static boolean isPolicyCompliantForHttpsDeny(SubscriptionContext ctx, CallExpression policyStatementConstructorCall) { |
| 112 | + // A compliant policy should have at least the following keyword arguments: |
| 113 | + // 1. effect=iam.Effect.DENY |
| 114 | + // 2. resources=["*"] (wildcard) |
| 115 | + // 3. actions=["s3:*"] (wildcard) |
| 116 | + // 4. principals=["*"] (wildcard) |
| 117 | + // 5. conditions=["SecureTransport:False"] |
| 118 | + |
| 119 | + boolean hasDenyEffect = hasArgumentWithFqn(ctx, policyStatementConstructorCall, "effect", "aws_cdk.aws_iam.Effect.DENY"); |
| 120 | + boolean hasWildcardResources = argumentListContainsExpectedValue(ctx, policyStatementConstructorCall, "resources", "*"); |
| 121 | + boolean hasWildcardActions = argumentListContainsExpectedValue(ctx, policyStatementConstructorCall, "actions", "s3:*"); |
| 122 | + boolean hasWildcardPrincipals = argumentListContainsExpectedValue(ctx, policyStatementConstructorCall, "principals", "*"); |
| 123 | + boolean hasSecureTransportCondition = argumentListContainsExpectedValue(ctx, policyStatementConstructorCall, "conditions", "SecureTransport:False"); |
| 124 | + |
| 125 | + return hasDenyEffect && hasWildcardResources && hasWildcardActions && hasWildcardPrincipals && hasSecureTransportCondition; |
| 126 | + } |
| 127 | + |
| 128 | + private static boolean hasArgumentWithFqn(SubscriptionContext ctx, CallExpression call, String argName, String expectedFqn) { |
| 129 | + return CdkUtils.getArgument(ctx, call, argName) |
| 130 | + .map(flow -> flow.hasExpression(CdkPredicate.isFqn(expectedFqn))) |
| 131 | + .orElse(false); |
| 132 | + } |
| 133 | + |
| 134 | + private static boolean argumentListContainsExpectedValue(SubscriptionContext ctx, CallExpression call, String argName, String expectedValue) { |
| 135 | + return CdkUtils.getArgument(ctx, call, argName) |
| 136 | + .flatMap(flow -> flow.getExpression(e -> e.is(Tree.Kind.LIST_LITERAL))) |
| 137 | + .map(ListLiteral.class::cast) |
| 138 | + .map(list -> list.elements().expressions().stream() |
| 139 | + .anyMatch(expr -> CdkUtils.getString(expr).filter(expectedValue::equals).isPresent())) |
| 140 | + .orElse(false); |
| 141 | + } |
| 142 | +} |
0 commit comments