|
16 | 16 | */ |
17 | 17 | package org.sonar.python.checks; |
18 | 18 |
|
19 | | -import java.util.Arrays; |
20 | | -import java.util.HashSet; |
| 19 | +import java.util.LinkedHashSet; |
21 | 20 | import java.util.Optional; |
22 | 21 | import java.util.Set; |
| 22 | +import java.util.function.Predicate; |
| 23 | +import java.util.stream.Collectors; |
23 | 24 | import java.util.stream.Stream; |
24 | 25 | import javax.annotation.CheckForNull; |
25 | 26 | import javax.annotation.Nullable; |
26 | 27 | import org.sonar.check.Rule; |
27 | 28 | import org.sonar.plugins.python.api.PythonSubscriptionCheck; |
| 29 | +import org.sonar.plugins.python.api.SubscriptionContext; |
28 | 30 | import org.sonar.plugins.python.api.symbols.Symbol; |
29 | 31 | import org.sonar.plugins.python.api.tree.CallExpression; |
30 | 32 | import org.sonar.plugins.python.api.tree.Expression; |
31 | 33 | import org.sonar.plugins.python.api.tree.Name; |
32 | 34 | import org.sonar.plugins.python.api.tree.RegularArgument; |
| 35 | +import org.sonar.plugins.python.api.tree.StringLiteral; |
33 | 36 | import org.sonar.plugins.python.api.tree.Tree; |
34 | 37 | import org.sonar.python.checks.utils.Expressions; |
35 | 38 | import org.sonar.python.tree.StringLiteralImpl; |
36 | 39 | import org.sonar.python.tree.TreeUtils; |
37 | 40 |
|
38 | | -import static java.util.Arrays.asList; |
39 | | - |
40 | 41 | // https://jira.sonarsource.com/browse/RSPEC-5547 (general) |
41 | 42 | // https://jira.sonarsource.com/browse/RSPEC-5552 (python-specific) |
42 | 43 | @Rule(key = "S5547") |
43 | 44 | public class RobustCipherAlgorithmCheck extends PythonSubscriptionCheck { |
44 | 45 |
|
45 | 46 | private static final String MESSAGE = "Use a strong cipher algorithm."; |
46 | | - private static final HashSet<String> sensitiveCalleeFqns = new HashSet<>(); |
47 | 47 |
|
48 | 48 | private static final Set<String> INSECURE_CIPHERS = Set.of( |
49 | 49 | "NULL", |
| 50 | + "aNULL", |
| 51 | + "eNULL", |
| 52 | + "COMPLEMENTOFALL", |
50 | 53 | "RC2", |
51 | 54 | "RC4", |
| 55 | + "IDEA", |
| 56 | + "SEED", |
52 | 57 | "DES", |
53 | 58 | "3DES", |
54 | 59 | "MD5", |
55 | | - "SHA" |
| 60 | + "SHA", |
| 61 | + "SHA1", |
| 62 | + "ADH", |
| 63 | + "AECDH", |
| 64 | + "CBC", |
| 65 | + "LOW", |
| 66 | + "@SECLEVEL=0", |
| 67 | + "@SECLEVEL=1", |
| 68 | + "DEFAULT@SECLEVEL=0", |
| 69 | + "DEFAULT@SECLEVEL=1" |
56 | 70 | ); |
57 | 71 |
|
58 | 72 | public static final String SSL_SET_CIPHERS_FQN = "ssl.SSLContext.set_ciphers"; |
59 | 73 |
|
60 | | - static { |
61 | | - // `pycryptodomex`, `pycryptodome`, and `pycrypto` all share the same names of the algorithms, |
62 | | - // moreover, `pycryptodome` is drop-in replacement for `pycrypto`, thus they share same name ("Crypto"). |
63 | | - for (String libraryName : asList("Cryptodome", "Crypto")) { |
64 | | - for (String vulnerableMethodName : asList("DES", "DES3", "ARC2", "ARC4", "Blowfish")) { |
65 | | - sensitiveCalleeFqns.add(String.format("%s.Cipher.%s.new", libraryName, vulnerableMethodName)); |
66 | | - } |
67 | | - } |
68 | | - |
69 | | - |
70 | | - // Idea is listed under "Weak Algorithms" in pyca/cryptography documentation |
71 | | - // https://cryptography.io/en/latest/hazmat/primitives/symmetric-encryption/\ |
72 | | - // #cryptography.hazmat.primitives.ciphers.algorithms.IDEA |
73 | | - // pyca (pyca/cryptography) |
74 | | - for (String methodName : asList("TripleDES", "Blowfish", "ARC4", "IDEA")) { |
75 | | - sensitiveCalleeFqns.add(String.format("cryptography.hazmat.primitives.ciphers.algorithms.%s", methodName)); |
76 | | - } |
77 | | - |
78 | | - // pydes |
79 | | - sensitiveCalleeFqns.add("pyDes.des"); |
80 | | - sensitiveCalleeFqns.add("pyDes.triple_des"); |
81 | | - } |
| 74 | + private static final Set<String> SENSITIVE_CALLEE_FQNS = Set.of( |
| 75 | + "Crypto.Cipher.ARC2.new", |
| 76 | + "Crypto.Cipher.ARC4.new", |
| 77 | + "Crypto.Cipher.Blowfish.new", |
| 78 | + "Crypto.Cipher.DES.new", |
| 79 | + "Crypto.Cipher.DES3.new", |
| 80 | + "Cryptodome.Cipher.ARC2.new", |
| 81 | + "Cryptodome.Cipher.ARC4.new", |
| 82 | + "Cryptodome.Cipher.Blowfish.new", |
| 83 | + "Cryptodome.Cipher.DES.new", |
| 84 | + "Cryptodome.Cipher.DES3.new", |
| 85 | + "cryptography.hazmat.primitives.ciphers.algorithms.ARC4", |
| 86 | + "cryptography.hazmat.primitives.ciphers.algorithms.Blowfish", |
| 87 | + "cryptography.hazmat.primitives.ciphers.algorithms.IDEA", |
| 88 | + "cryptography.hazmat.primitives.ciphers.algorithms.TripleDES", |
| 89 | + "pyDes.des", |
| 90 | + "pyDes.triple_des" |
| 91 | + ); |
82 | 92 |
|
83 | 93 | @Override |
84 | 94 | public void initialize(Context context) { |
85 | | - context.registerSyntaxNodeConsumer(Tree.Kind.CALL_EXPR, subscriptionContext -> { |
86 | | - CallExpression callExpr = (CallExpression) subscriptionContext.syntaxNode(); |
87 | | - Optional.ofNullable(callExpr) |
88 | | - .map(CallExpression::calleeSymbol) |
89 | | - .map(Symbol::fullyQualifiedName) |
90 | | - .filter(fqn -> sensitiveCalleeFqns.contains(fqn) || |
91 | | - (SSL_SET_CIPHERS_FQN.equals(fqn) && hasArgumentWithSensitiveAlgorithm(callExpr))) |
92 | | - .ifPresent(fqn -> subscriptionContext.addIssue(callExpr.callee(), MESSAGE)); |
93 | | - }); |
| 95 | + context.registerSyntaxNodeConsumer(Tree.Kind.CALL_EXPR, RobustCipherAlgorithmCheck::checkCallExpression); |
94 | 96 | } |
95 | 97 |
|
| 98 | + private static void checkCallExpression(SubscriptionContext subscriptionContext) { |
| 99 | + CallExpression callExpr = (CallExpression) subscriptionContext.syntaxNode(); |
| 100 | + Optional.of(callExpr) |
| 101 | + .map(CallExpression::calleeSymbol) |
| 102 | + .map(Symbol::fullyQualifiedName) |
| 103 | + .ifPresent(fullyQualifiedName -> { |
| 104 | + if (SENSITIVE_CALLEE_FQNS.contains(fullyQualifiedName)) { |
| 105 | + subscriptionContext.addIssue(callExpr.callee(), MESSAGE); |
| 106 | + } else if (SSL_SET_CIPHERS_FQN.equals(fullyQualifiedName)) { |
| 107 | + checkForInsecureCiphers(subscriptionContext, callExpr); |
| 108 | + } |
| 109 | + }); |
| 110 | + } |
96 | 111 |
|
97 | | - private static boolean hasArgumentWithSensitiveAlgorithm(CallExpression callExpression) { |
98 | | - return Optional.of(callExpression.arguments()) |
| 112 | + private static void checkForInsecureCiphers(SubscriptionContext ctx, CallExpression callExpression) { |
| 113 | + Optional.of(callExpression.arguments()) |
99 | 114 | .filter(list -> list.size() == 1) |
100 | 115 | .map(list -> list.get(0)) |
101 | 116 | .flatMap(TreeUtils.toOptionalInstanceOfMapper(RegularArgument.class)) |
102 | 117 | .map(RegularArgument::expression) |
103 | 118 | .map(RobustCipherAlgorithmCheck::unpackArgument) |
104 | | - .filter(RobustCipherAlgorithmCheck::containsInsecureCipher) |
105 | | - .isPresent(); |
| 119 | + .ifPresent(stringLiteral -> Optional.of(stringLiteral.trimmedQuotesValue()) |
| 120 | + .map(RobustCipherAlgorithmCheck::findInsecureCiphers) |
| 121 | + .filter(Predicate.not(Set::isEmpty)) |
| 122 | + .ifPresent(insecureCiphers -> { |
| 123 | + var secondaryMessage = insecureCiphers.size() > 1 ? "The following cipher strings are insecure: " : |
| 124 | + "The following cipher string is insecure: "; |
| 125 | + secondaryMessage = insecureCiphers.stream().collect(Collectors.joining("`, `", secondaryMessage + "`", "`")); |
| 126 | + |
| 127 | + ctx.addIssue(callExpression.callee(), MESSAGE) |
| 128 | + .secondary(stringLiteral, secondaryMessage); |
| 129 | + })); |
106 | 130 | } |
107 | 131 |
|
108 | 132 | @CheckForNull |
109 | | - private static String unpackArgument(@Nullable Expression expression) { |
| 133 | + private static StringLiteral unpackArgument(@Nullable Expression expression) { |
110 | 134 | if (expression == null) { |
111 | 135 | return null; |
112 | 136 | } else if (expression.is(Tree.Kind.STRING_LITERAL)) { |
113 | | - return ((StringLiteralImpl) expression).trimmedQuotesValue(); |
| 137 | + return ((StringLiteralImpl) expression); |
114 | 138 | } else if (expression.is(Tree.Kind.NAME)) { |
115 | 139 | return unpackArgument(Expressions.singleAssignedValue((Name) expression)); |
116 | 140 | } else { |
117 | 141 | return null; |
118 | 142 | } |
119 | 143 | } |
120 | 144 |
|
121 | | - private static boolean containsInsecureCipher(String ciphers) { |
| 145 | + private static Set<String> findInsecureCiphers(String ciphers) { |
122 | 146 | return Stream.of(ciphers) |
123 | | - .flatMap(str -> Arrays.stream(str.split(":"))) |
124 | | - .flatMap(str -> Arrays.stream(str.split("-"))) |
125 | | - .anyMatch(INSECURE_CIPHERS::contains); |
| 147 | + .flatMap(str -> Stream.of(str.split(":"))) |
| 148 | + .filter(str -> !str.startsWith("!") && !str.startsWith("-")) |
| 149 | + .flatMap(str -> Stream.of(str.split("\\+"))) |
| 150 | + .flatMap(str -> Stream.of(str.split("-"))) |
| 151 | + .filter(INSECURE_CIPHERS::contains) |
| 152 | + .collect(Collectors.toCollection(LinkedHashSet::new)); |
126 | 153 | } |
127 | | - |
128 | | - |
129 | 154 | } |
| 155 | + |
0 commit comments