|
35 | 35 | import org.sonar.plugins.java.api.semantic.Type; |
36 | 36 | import org.sonar.plugins.java.api.tree.AssignmentExpressionTree; |
37 | 37 | import org.sonar.plugins.java.api.tree.BinaryExpressionTree; |
| 38 | +import org.sonar.plugins.java.api.tree.ConditionalExpressionTree; |
38 | 39 | import org.sonar.plugins.java.api.tree.ExpressionTree; |
39 | 40 | import org.sonar.plugins.java.api.tree.IdentifierTree; |
40 | 41 | import org.sonar.plugins.java.api.tree.MethodInvocationTree; |
@@ -226,15 +227,22 @@ private static boolean isDynamicConcatenation(ExpressionTree arg) { |
226 | 227 |
|
227 | 228 | private static boolean hasDynamicConcatenationOperand(ExpressionTree arg) { |
228 | 229 | ExpressionTree expression = ExpressionUtils.skipParentheses(arg); |
229 | | - if (expression.asConstant().isPresent() || isSafeClassNameExpression(expression, new HashSet<>())) { |
| 230 | + if (expression.asConstant().isPresent() || isPrimitiveOrWrapper(expression.symbolType()) || isSafeClassNameExpression(expression, new HashSet<>())) { |
230 | 231 | return false; |
231 | 232 | } |
| 233 | + if (expression instanceof ConditionalExpressionTree conditional) { |
| 234 | + return hasDynamicConcatenationOperand(conditional.trueExpression()) || hasDynamicConcatenationOperand(conditional.falseExpression()); |
| 235 | + } |
232 | 236 | if (expression instanceof BinaryExpressionTree binaryExpression && expression.is(Tree.Kind.PLUS)) { |
233 | 237 | return hasDynamicConcatenationOperand(binaryExpression.leftOperand()) || hasDynamicConcatenationOperand(binaryExpression.rightOperand()); |
234 | 238 | } |
235 | 239 | return true; |
236 | 240 | } |
237 | 241 |
|
| 242 | + private static boolean isPrimitiveOrWrapper(Type type) { |
| 243 | + return type.isPrimitive() || type.isPrimitiveWrapper(); |
| 244 | + } |
| 245 | + |
238 | 246 | private static boolean isSafeClassNameExpression(ExpressionTree arg, Set<Symbol> visitedSymbols) { |
239 | 247 | ExpressionTree expression = ExpressionUtils.skipParentheses(arg); |
240 | 248 | if (expression instanceof MethodInvocationTree methodInvocation) { |
@@ -266,7 +274,7 @@ private static boolean hasDynamicStringParameters(MethodInvocationTree mit) { |
266 | 274 | // `format` has a variant with Locale as the first argument - we do not need to check that parameter. |
267 | 275 | boolean isFirstLocaleArgument = firstArg && type.is("java.util.Locale"); |
268 | 276 | // Primitives will not lead to SQL injection, so the code is compliant. |
269 | | - if (!isFirstLocaleArgument && !type.isUnknown() && !type.isPrimitive() && !type.isPrimitiveWrapper() |
| 277 | + if (!isFirstLocaleArgument && !type.isUnknown() && !isPrimitiveOrWrapper(type) |
270 | 278 | && arg.asConstant().isEmpty() && !isSafeClassNameExpression(arg, new HashSet<>())) { |
271 | 279 | return true; |
272 | 280 | } |
|
0 commit comments