|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) 2011-2023 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 GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonar.python.checks; |
| 21 | + |
| 22 | +import com.google.gson.Gson; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.InputStreamReader; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Optional; |
| 28 | +import java.util.function.Function; |
| 29 | +import java.util.function.Predicate; |
| 30 | +import java.util.stream.Collectors; |
| 31 | +import java.util.stream.Stream; |
| 32 | +import org.sonar.check.Rule; |
| 33 | +import org.sonar.plugins.python.api.PythonSubscriptionCheck; |
| 34 | +import org.sonar.plugins.python.api.SubscriptionContext; |
| 35 | +import org.sonar.plugins.python.api.symbols.Symbol; |
| 36 | +import org.sonar.plugins.python.api.tree.CallExpression; |
| 37 | +import org.sonar.plugins.python.api.tree.Expression; |
| 38 | +import org.sonar.plugins.python.api.tree.Name; |
| 39 | +import org.sonar.plugins.python.api.tree.QualifiedExpression; |
| 40 | +import org.sonar.plugins.python.api.tree.RegularArgument; |
| 41 | +import org.sonar.plugins.python.api.tree.StringLiteral; |
| 42 | +import org.sonar.plugins.python.api.tree.Tree; |
| 43 | +import org.sonar.python.tree.TreeUtils; |
| 44 | + |
| 45 | +@Rule(key = "S6437") |
| 46 | +public class HardcodedCredentialsCallCheck extends PythonSubscriptionCheck { |
| 47 | + |
| 48 | + private static final String MESSAGE = "Revoke and change this password, as it is compromised."; |
| 49 | + private final Map<String, CredentialMethod> methods; |
| 50 | + |
| 51 | + public HardcodedCredentialsCallCheck() { |
| 52 | + methods = new CredentialMethodsLoader().load(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void initialize(Context context) { |
| 57 | + context.registerSyntaxNodeConsumer(Tree.Kind.CALL_EXPR, this::processCallExpression); |
| 58 | + } |
| 59 | + |
| 60 | + private void processCallExpression(SubscriptionContext ctx) { |
| 61 | + Optional.of(ctx.syntaxNode()) |
| 62 | + .filter(CallExpression.class::isInstance) |
| 63 | + .map(CallExpression.class::cast) |
| 64 | + .filter(this::callHasToBeChecked) |
| 65 | + .ifPresent(call -> checkCallArguments(ctx, call)); |
| 66 | + } |
| 67 | + |
| 68 | + private void checkCallArguments(SubscriptionContext ctx, CallExpression call) { |
| 69 | + getMethod(call) |
| 70 | + .ifPresent(method -> method.indices() |
| 71 | + .forEach(argumentIndex -> { |
| 72 | + var argumentName = method.args().get(argumentIndex); |
| 73 | + var argument = TreeUtils.nthArgumentOrKeyword(argumentIndex, argumentName, call.arguments()); |
| 74 | + if (argument != null) { |
| 75 | + checkArgument(ctx, argument); |
| 76 | + } |
| 77 | + })); |
| 78 | + } |
| 79 | + |
| 80 | + private static void checkArgument(SubscriptionContext ctx, RegularArgument argument) { |
| 81 | + var argExp = argument.expression(); |
| 82 | + if (argExp.is(Tree.Kind.STRING_LITERAL)) { |
| 83 | + Optional.of(argExp) |
| 84 | + .filter(StringLiteral.class::isInstance) |
| 85 | + .map(StringLiteral.class::cast) |
| 86 | + .filter(HardcodedCredentialsCallCheck::isNotEmpty) |
| 87 | + .ifPresent(string -> ctx.addIssue(argument, MESSAGE)); |
| 88 | + } else if (argExp.is(Tree.Kind.NAME)) { |
| 89 | + findAssignment((Name) argExp, 0) |
| 90 | + .filter(StringLiteral.class::isInstance) |
| 91 | + .map(StringLiteral.class::cast) |
| 92 | + .filter(HardcodedCredentialsCallCheck::isNotEmpty) |
| 93 | + .ifPresent(assignedValue -> ctx.addIssue(argument, MESSAGE).secondary(assignedValue, MESSAGE)); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private static boolean isNotEmpty(StringLiteral stringLiteral) { |
| 98 | + return Optional.of(stringLiteral) |
| 99 | + .map(StringLiteral::trimmedQuotesValue) |
| 100 | + .filter(Predicate.not(String::isEmpty)) |
| 101 | + .isPresent(); |
| 102 | + } |
| 103 | + |
| 104 | + private static Optional<Tree> findAssignment(Name name, int depth) { |
| 105 | + if (depth > 99) { |
| 106 | + return Optional.empty(); |
| 107 | + } |
| 108 | + return Optional.of(name) |
| 109 | + .map(Expressions::singleAssignedValue) |
| 110 | + .map(v -> findValue(v, depth)); |
| 111 | + } |
| 112 | + |
| 113 | + private static Tree findValue(Expression assignedValue, int depth) { |
| 114 | + if (assignedValue.is(Tree.Kind.NAME)) { |
| 115 | + return findAssignment((Name) assignedValue, depth + 1).orElse(null); |
| 116 | + } else if (assignedValue.is(Tree.Kind.CALL_EXPR)) { |
| 117 | + return Optional.of(assignedValue) |
| 118 | + .filter(CallExpression.class::isInstance) |
| 119 | + .map(CallExpression.class::cast) |
| 120 | + .map(CallExpression::callee) |
| 121 | + .filter(QualifiedExpression.class::isInstance) |
| 122 | + .map(QualifiedExpression.class::cast) |
| 123 | + .map(QualifiedExpression::qualifier) |
| 124 | + .map(v -> findValue(v, depth + 1)) |
| 125 | + .orElse(assignedValue); |
| 126 | + } |
| 127 | + return assignedValue; |
| 128 | + } |
| 129 | + |
| 130 | + private Boolean callHasToBeChecked(CallExpression call) { |
| 131 | + return Optional.of(call) |
| 132 | + .map(CallExpression::calleeSymbol) |
| 133 | + .map(Symbol::fullyQualifiedName) |
| 134 | + .map(methods::containsKey) |
| 135 | + .orElse(false); |
| 136 | + } |
| 137 | + |
| 138 | + private Optional<CredentialMethod> getMethod(CallExpression call) { |
| 139 | + return Optional.of(call) |
| 140 | + .map(CallExpression::calleeSymbol) |
| 141 | + .map(Symbol::fullyQualifiedName) |
| 142 | + .map(methods::get); |
| 143 | + } |
| 144 | + |
| 145 | + public static class CredentialMethod { |
| 146 | + private String name; |
| 147 | + private List<String> args; |
| 148 | + private List<Integer> indices; |
| 149 | + |
| 150 | + public String name() { |
| 151 | + return name; |
| 152 | + } |
| 153 | + |
| 154 | + public List<String> args() { |
| 155 | + return args; |
| 156 | + } |
| 157 | + |
| 158 | + public List<Integer> indices() { |
| 159 | + return indices; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private static class CredentialMethodsLoader { |
| 164 | + private static final String METHODS_RESOURCE_PATH = "/org/sonar/python/checks/hardcoded_credentials_call_check_meta.json"; |
| 165 | + private final Gson gson; |
| 166 | + |
| 167 | + private CredentialMethodsLoader() { |
| 168 | + gson = new Gson(); |
| 169 | + } |
| 170 | + |
| 171 | + private Map<String, CredentialMethod> load() { |
| 172 | + try (var is = HardcodedCredentialsCallCheck.class.getResourceAsStream(METHODS_RESOURCE_PATH)) { |
| 173 | + return Optional.ofNullable(is) |
| 174 | + .map(InputStreamReader::new) |
| 175 | + .map(r -> gson.fromJson(r, CredentialMethod[].class)) |
| 176 | + .stream() |
| 177 | + .flatMap(Stream::of) |
| 178 | + .collect(Collectors.toMap(CredentialMethod::name, Function.identity())); |
| 179 | + } catch (IOException e) { |
| 180 | + throw new IllegalStateException("Unable to read methods metadata from " + METHODS_RESOURCE_PATH, e); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments