|
| 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.hotspots; |
| 18 | + |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Optional; |
| 25 | +import java.util.stream.Collectors; |
| 26 | +import org.sonar.check.Rule; |
| 27 | +import org.sonar.plugins.python.api.PythonSubscriptionCheck; |
| 28 | +import org.sonar.plugins.python.api.SubscriptionContext; |
| 29 | +import org.sonar.plugins.python.api.tree.CallExpression; |
| 30 | +import org.sonar.plugins.python.api.tree.DictionaryLiteral; |
| 31 | +import org.sonar.plugins.python.api.tree.Expression; |
| 32 | +import org.sonar.plugins.python.api.tree.ExpressionList; |
| 33 | +import org.sonar.plugins.python.api.tree.KeyValuePair; |
| 34 | +import org.sonar.plugins.python.api.tree.ListLiteral; |
| 35 | +import org.sonar.plugins.python.api.tree.Name; |
| 36 | +import org.sonar.plugins.python.api.tree.QualifiedExpression; |
| 37 | +import org.sonar.plugins.python.api.tree.RegularArgument; |
| 38 | +import org.sonar.plugins.python.api.tree.Tree; |
| 39 | +import org.sonar.plugins.python.api.tree.UnpackingExpression; |
| 40 | +import org.sonar.plugins.python.api.types.v2.TriBool; |
| 41 | +import org.sonar.python.checks.utils.Expressions; |
| 42 | +import org.sonar.python.tree.TreeUtils; |
| 43 | +import org.sonar.python.types.v2.TypeCheckBuilder; |
| 44 | +import org.sonar.python.types.v2.TypeCheckMap; |
| 45 | + |
| 46 | +import static org.sonar.python.checks.hotspots.CommonValidationUtils.singleAssignedString; |
| 47 | + |
| 48 | +@Rule(key = "S6377") |
| 49 | +public class XMLSignatureValidationCheck extends PythonSubscriptionCheck { |
| 50 | + |
| 51 | + private static final List<String> VERIFY_REQUIRED_ONE_OF = List.of( |
| 52 | + "x509_cert", |
| 53 | + "cert_subject_name", |
| 54 | + "cert_resolver", |
| 55 | + "ca_pem_file", |
| 56 | + "ca_path", |
| 57 | + "hmac_key" |
| 58 | + ); |
| 59 | + private static final String MESSAGE = "Change this code to only accept signatures computed from a trusted party."; |
| 60 | + private static final String MESSAGE_SECONDARY = "Unsafe parameter set here"; |
| 61 | + |
| 62 | + private TypeCheckBuilder xmlVerifierVerifyTypeChecker; |
| 63 | + private TypeCheckBuilder dictTypeChecker; |
| 64 | + private TypeCheckBuilder signatureConfigurationTypeChecker; |
| 65 | + private TypeCheckMap<Boolean> signatureMethodTypeCheckMap; |
| 66 | + |
| 67 | + @Override |
| 68 | + public void initialize(Context context) { |
| 69 | + context.registerSyntaxNodeConsumer(Tree.Kind.FILE_INPUT, this::registerTypeCheckers); |
| 70 | + context.registerSyntaxNodeConsumer(Tree.Kind.CALL_EXPR, this::checkCallExpr); |
| 71 | + } |
| 72 | + |
| 73 | + private void registerTypeCheckers(SubscriptionContext subscriptionContext) { |
| 74 | + xmlVerifierVerifyTypeChecker = subscriptionContext.typeChecker().typeCheckBuilder().isTypeWithFqn("signxml.XMLVerifier"); |
| 75 | + dictTypeChecker = subscriptionContext.typeChecker().typeCheckBuilder().isTypeWithName("dict"); |
| 76 | + signatureConfigurationTypeChecker = subscriptionContext.typeChecker().typeCheckBuilder().isTypeWithFqn("signxml.SignatureConfiguration"); |
| 77 | + signatureMethodTypeCheckMap = TypeCheckMap.ofEntries( |
| 78 | + Map.entry(subscriptionContext.typeChecker().typeCheckBuilder().isTypeWithFqn("signxml.SignatureMethod.HMAC_SHA224"), true), |
| 79 | + Map.entry(subscriptionContext.typeChecker().typeCheckBuilder().isTypeWithFqn("signxml.SignatureMethod.HMAC_SHA256"), true), |
| 80 | + Map.entry(subscriptionContext.typeChecker().typeCheckBuilder().isTypeWithFqn("signxml.SignatureMethod.HMAC_SHA384"), true), |
| 81 | + Map.entry(subscriptionContext.typeChecker().typeCheckBuilder().isTypeWithFqn("signxml.SignatureMethod.HMAC_SHA512"), true) |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + private void checkCallExpr(SubscriptionContext subscriptionContext) { |
| 86 | + var callExpression = ((CallExpression) subscriptionContext.syntaxNode()); |
| 87 | + var qualifiedExprCallee = Optional.of(callExpression) |
| 88 | + .map(CallExpression::callee) |
| 89 | + .flatMap(TreeUtils.toOptionalInstanceOfMapper(QualifiedExpression.class)) |
| 90 | + .orElse(null); |
| 91 | + if (qualifiedExprCallee == null) { |
| 92 | + return; |
| 93 | + } |
| 94 | + var ok = qualifiedExpressionIsVerifyCall(qualifiedExprCallee); |
| 95 | + if (!ok) { |
| 96 | + return; |
| 97 | + } |
| 98 | + Map<String, Tree> argToTree = new HashMap<>(); |
| 99 | + for (var arg : callExpression.arguments()) { |
| 100 | + if (arg instanceof RegularArgument regularArgument) { |
| 101 | + String argumentName = Optional.ofNullable(regularArgument.keywordArgument()).map(Name::name).orElse(""); |
| 102 | + argToTree.put(argumentName, regularArgument.expression()); |
| 103 | + } else { |
| 104 | + var keys = TreeUtils.toOptionalInstanceOf(UnpackingExpression.class, arg) |
| 105 | + .map(UnpackingExpression::expression) |
| 106 | + .flatMap(TreeUtils.toOptionalInstanceOfMapper(Name.class)) |
| 107 | + .flatMap(Expressions::singleAssignedNonNameValue) |
| 108 | + .map(this::keysInUnpacking) |
| 109 | + .orElseGet(Map::of); |
| 110 | + argToTree.putAll(keys); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (Collections.disjoint(argToTree.keySet(), VERIFY_REQUIRED_ONE_OF)) { |
| 115 | + subscriptionContext.addIssue(callExpression.callee(), MESSAGE); |
| 116 | + } |
| 117 | + |
| 118 | + var expectConfigValue = argToTree.get("expect_config"); |
| 119 | + if (expectConfigValue != null) { |
| 120 | + checkExpectConfig(subscriptionContext, expectConfigValue, callExpression); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private void checkExpectConfig(SubscriptionContext subscriptionContext, Tree expectConfigValue, CallExpression verifyCallExpression) { |
| 125 | + expectConfigValue = replaceBySingleAssigned(expectConfigValue); |
| 126 | + if (expectConfigValue instanceof CallExpression callExpression |
| 127 | + && signatureConfigurationTypeChecker.check(callExpression.callee().typeV2()) == TriBool.TRUE) { |
| 128 | + TreeUtils.nthArgumentOrKeywordOptional(0, "require_x509", callExpression.arguments()) |
| 129 | + .filter(arg -> Expressions.isFalsy(arg.expression())) |
| 130 | + .ifPresent(arg -> subscriptionContext.addIssue(verifyCallExpression, MESSAGE).secondary(arg, MESSAGE_SECONDARY)); |
| 131 | + |
| 132 | + TreeUtils.nthArgumentOrKeywordOptional(3, "signature_methods", callExpression.arguments()) |
| 133 | + .map(this::getOffendingInList) |
| 134 | + .filter(list -> !list.isEmpty()) |
| 135 | + .ifPresent(list -> { |
| 136 | + var issue = subscriptionContext.addIssue(verifyCallExpression, MESSAGE); |
| 137 | + list.forEach(secondary -> issue.secondary(secondary, MESSAGE_SECONDARY)); |
| 138 | + }); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private static Tree replaceBySingleAssigned(Tree expectConfigValue) { |
| 143 | + if (expectConfigValue.is(Tree.Kind.NAME)) { |
| 144 | + expectConfigValue = Expressions.singleAssignedNonNameValue((Name) expectConfigValue).orElse(null); |
| 145 | + } |
| 146 | + return expectConfigValue; |
| 147 | + } |
| 148 | + |
| 149 | + private List<Expression> getOffendingInList(RegularArgument regularArgument) { |
| 150 | + var expression = replaceBySingleAssigned(regularArgument.expression()); |
| 151 | + return TreeUtils.toOptionalInstanceOf(ListLiteral.class, expression) |
| 152 | + .map(ListLiteral::elements) |
| 153 | + .map(ExpressionList::expressions) |
| 154 | + .stream() |
| 155 | + .flatMap(Collection::stream) |
| 156 | + .filter(e -> signatureMethodTypeCheckMap.getOptionalForType(e.typeV2()).isEmpty()) |
| 157 | + .toList(); |
| 158 | + } |
| 159 | + |
| 160 | + |
| 161 | + private Map<String, Tree> keysInUnpacking(Expression expression) { |
| 162 | + if (expression instanceof CallExpression callExpression && dictTypeChecker.check(callExpression.callee().typeV2()) == TriBool.TRUE) { |
| 163 | + return callExpression.arguments().stream() |
| 164 | + .flatMap(TreeUtils.toStreamInstanceOfMapper(RegularArgument.class)) |
| 165 | + .filter(regularArgument -> regularArgument.keywordArgument() != null) |
| 166 | + .collect(Collectors.toMap( |
| 167 | + regularArgument -> regularArgument.keywordArgument().name(), |
| 168 | + RegularArgument::expression |
| 169 | + )); |
| 170 | + } |
| 171 | + if (expression instanceof DictionaryLiteral dictionaryLiteral) { |
| 172 | + var output = new HashMap<String, Tree>(); |
| 173 | + for (var element : dictionaryLiteral.elements()) { |
| 174 | + if (element instanceof KeyValuePair keyValuePair) { |
| 175 | + |
| 176 | + var key = singleAssignedString(keyValuePair.key()); |
| 177 | + if (!key.isEmpty()) { |
| 178 | + output.put(key, keyValuePair.value()); |
| 179 | + } |
| 180 | + } else { |
| 181 | + var unpackedKeys = TreeUtils.toOptionalInstanceOf(UnpackingExpression.class, element) |
| 182 | + .map(UnpackingExpression::expression) |
| 183 | + .flatMap(TreeUtils.toOptionalInstanceOfMapper(Name.class)) |
| 184 | + .flatMap(Expressions::singleAssignedNonNameValue) |
| 185 | + .map(this::keysInUnpacking) |
| 186 | + .orElseGet(Map::of); |
| 187 | + output.putAll(unpackedKeys); |
| 188 | + } |
| 189 | + } |
| 190 | + return output; |
| 191 | + } |
| 192 | + return Map.of(); |
| 193 | + } |
| 194 | + |
| 195 | + private boolean qualifiedExpressionIsVerifyCall(QualifiedExpression qualifiedExprCallee) { |
| 196 | + if (qualifiedExprCallee.qualifier() instanceof CallExpression callExpression) { |
| 197 | + return xmlVerifierVerifyTypeChecker.check(callExpression.callee().typeV2()) == TriBool.TRUE; |
| 198 | + } |
| 199 | + if (qualifiedExprCallee.qualifier() instanceof Name name) { |
| 200 | + // This check can never be true because we don't have stubs for signxml. |
| 201 | + // Currently, if we try to instantiate the XMLVerifier and then call the verify method on the variable, |
| 202 | + // the name will have an unknown type. |
| 203 | + return xmlVerifierVerifyTypeChecker.check(name.typeV2()) == TriBool.TRUE; |
| 204 | + } |
| 205 | + return false; |
| 206 | + } |
| 207 | +} |
0 commit comments