|
| 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; |
| 18 | + |
| 19 | +import java.util.regex.Pattern; |
| 20 | +import java.util.stream.Stream; |
| 21 | +import javax.annotation.Nullable; |
| 22 | +import org.sonar.check.Rule; |
| 23 | +import org.sonar.plugins.python.api.PythonSubscriptionCheck; |
| 24 | +import org.sonar.plugins.python.api.SubscriptionContext; |
| 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.FunctionDef; |
| 28 | +import org.sonar.plugins.python.api.tree.Name; |
| 29 | +import org.sonar.plugins.python.api.tree.RegularArgument; |
| 30 | +import org.sonar.plugins.python.api.tree.StringLiteral; |
| 31 | +import org.sonar.plugins.python.api.tree.Tree; |
| 32 | +import org.sonar.python.checks.utils.AwsLambdaChecksUtils; |
| 33 | +import org.sonar.python.checks.utils.Expressions; |
| 34 | +import org.sonar.python.semantic.v2.SymbolV2; |
| 35 | +import org.sonar.python.semantic.v2.UsageV2; |
| 36 | +import org.sonar.python.tree.TreeUtils; |
| 37 | +import org.sonar.python.types.v2.TypeCheckBuilder; |
| 38 | + |
| 39 | +@Rule(key = "S7620") |
| 40 | +public class AwsLambdaTmpCleanupCheck extends PythonSubscriptionCheck { |
| 41 | + |
| 42 | + private static final String MESSAGE = "Clean up this temporary file before the Lambda function completes."; |
| 43 | + private static final String SECONDARY_LOCATION_MESSAGE = "The temporary folder is used in this Lambda function."; |
| 44 | + private static final Pattern TMP_PATH_PATTERN = Pattern.compile("^/tmp/.*"); |
| 45 | + |
| 46 | + private TypeCheckBuilder openType; |
| 47 | + private TypeCheckBuilder osRemoveType; |
| 48 | + private TypeCheckBuilder osUnlinkType; |
| 49 | + |
| 50 | + @Override |
| 51 | + public void initialize(Context context) { |
| 52 | + context.registerSyntaxNodeConsumer(Tree.Kind.FILE_INPUT, this::initializeTypeChecker); |
| 53 | + context.registerSyntaxNodeConsumer(Tree.Kind.CALL_EXPR, this::visitCallExpression); |
| 54 | + } |
| 55 | + |
| 56 | + private void initializeTypeChecker(SubscriptionContext ctx) { |
| 57 | + openType = ctx.typeChecker().typeCheckBuilder().isBuiltinWithName("open"); |
| 58 | + osRemoveType = ctx.typeChecker().typeCheckBuilder().isTypeWithFqn("os.remove"); |
| 59 | + osUnlinkType = ctx.typeChecker().typeCheckBuilder().isTypeWithFqn("os.unlink"); |
| 60 | + } |
| 61 | + |
| 62 | + private void visitCallExpression(SubscriptionContext ctx) { |
| 63 | + CallExpression callExpression = (CallExpression) ctx.syntaxNode(); |
| 64 | + |
| 65 | + Tree functionDefAncestor = TreeUtils.firstAncestorOfKind(callExpression, Tree.Kind.FUNCDEF); |
| 66 | + if (functionDefAncestor == null) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + FunctionDef lambdaHandler = (FunctionDef) functionDefAncestor; |
| 71 | + |
| 72 | + if (!AwsLambdaChecksUtils.isLambdaHandler(ctx, lambdaHandler)) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + if (openType.check(callExpression.callee().typeV2()).isTrue() && !isTempFileCleanedUp(callExpression, lambdaHandler)) { |
| 77 | + ctx.addIssue(callExpression.callee(), MESSAGE) |
| 78 | + .secondary(lambdaHandler.name(), SECONDARY_LOCATION_MESSAGE); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private boolean isTempFileCleanedUp(CallExpression callExpression, FunctionDef lambdaHandler) { |
| 83 | + RegularArgument regularArg = TreeUtils.nthArgumentOrKeyword(0, "file", callExpression.arguments()); |
| 84 | + |
| 85 | + if (regularArg == null) { |
| 86 | + return true; |
| 87 | + } |
| 88 | + |
| 89 | + Expression pathExpression = regularArg.expression(); |
| 90 | + String pathValue = getStringValue(pathExpression); |
| 91 | + |
| 92 | + return pathValue == null || |
| 93 | + !TMP_PATH_PATTERN.matcher(pathValue).matches() || |
| 94 | + hasCleanupCall(lambdaHandler, pathValue) || |
| 95 | + isPathPassedToOtherFunction(pathExpression); |
| 96 | + } |
| 97 | + |
| 98 | + private boolean isPathPassedToOtherFunction(Expression pathExpression) { |
| 99 | + if (!(pathExpression instanceof Name)) { |
| 100 | + return true; |
| 101 | + } |
| 102 | + Name name = (Name) pathExpression; |
| 103 | + SymbolV2 symbol = name.symbolV2(); |
| 104 | + if (symbol == null) { |
| 105 | + return true; |
| 106 | + } |
| 107 | + return getUsagesInCallExpr(symbol) |
| 108 | + .anyMatch(callExpr -> !isOsRemoveOrUnlinkCall(callExpr) && !openType.check(callExpr.callee().typeV2()).isTrue()); |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + private static Stream<CallExpression> getUsagesInCallExpr(SymbolV2 symbol) { |
| 113 | + return symbol.usages().stream() |
| 114 | + .filter(usage -> usage.kind().equals(UsageV2.Kind.OTHER)) |
| 115 | + .map(usage -> usage.tree().parent()) |
| 116 | + .filter(parent -> parent.is(Tree.Kind.REGULAR_ARGUMENT)) |
| 117 | + .map(parent -> TreeUtils.firstAncestorOfKind(parent, Tree.Kind.CALL_EXPR)) |
| 118 | + .flatMap(TreeUtils.toStreamInstanceOfMapper(CallExpression.class)); |
| 119 | + } |
| 120 | + |
| 121 | + private boolean hasCleanupCall(FunctionDef function, String filePath) { |
| 122 | + return TreeUtils.firstChild(function, child -> child instanceof CallExpression callExpr && isOsRemoveOrUnlinkCall(callExpr) && hasMatchingPath(callExpr, filePath)).isPresent(); |
| 123 | + } |
| 124 | + |
| 125 | + private boolean isOsRemoveOrUnlinkCall(CallExpression callExpression) { |
| 126 | + return osRemoveType.check(callExpression.callee().typeV2()).isTrue() || |
| 127 | + osUnlinkType.check(callExpression.callee().typeV2()).isTrue(); |
| 128 | + } |
| 129 | + |
| 130 | + private static boolean hasMatchingPath(CallExpression call, String targetPath) { |
| 131 | + RegularArgument regularArg = TreeUtils.nthArgumentOrKeyword(0, "path", call.arguments()); |
| 132 | + |
| 133 | + if (regularArg == null) { |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + String pathValue = getStringValue(regularArg.expression()); |
| 138 | + return targetPath.equals(pathValue); |
| 139 | + } |
| 140 | + |
| 141 | + @Nullable |
| 142 | + private static String getStringValue(Expression expression) { |
| 143 | + if (expression instanceof StringLiteral stringLiteral) { |
| 144 | + return stringLiteral.trimmedQuotesValue(); |
| 145 | + } |
| 146 | + if (expression instanceof Name name) { |
| 147 | + var assignedValue = Expressions.singleAssignedValue(name); |
| 148 | + if (assignedValue instanceof StringLiteral stringLiteral) { |
| 149 | + return stringLiteral.trimmedQuotesValue(); |
| 150 | + } |
| 151 | + } |
| 152 | + return null; |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments