|
| 1 | +package io.codemodder.codemods.util; |
| 2 | + |
| 3 | +import com.github.javaparser.ast.CompilationUnit; |
| 4 | +import com.github.javaparser.ast.expr.MethodCallExpr; |
| 5 | +import io.codemodder.CodemodChange; |
| 6 | +import io.codemodder.CodemodFileScanningResult; |
| 7 | +import io.codemodder.CodemodInvocationContext; |
| 8 | +import io.codemodder.codemods.SQLParameterizer; |
| 9 | +import io.codemodder.codemods.SQLParameterizerWithCleanup; |
| 10 | +import io.codemodder.codetf.DetectorRule; |
| 11 | +import io.codemodder.codetf.FixedFinding; |
| 12 | +import io.codemodder.codetf.UnfixedFinding; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.Collection; |
| 15 | +import java.util.List; |
| 16 | +import java.util.function.Function; |
| 17 | + |
| 18 | +/** |
| 19 | + * Default implementation of the JavaParserSQLInjectionRemediatorStrategy interface. This class |
| 20 | + * provides the logic to visit a CompilationUnit and process findings for potential SQL injections. |
| 21 | + */ |
| 22 | +final class DefaultJavaParserSQLInjectionRemediatorStrategy |
| 23 | + implements JavaParserSQLInjectionRemediatorStrategy { |
| 24 | + |
| 25 | + /** |
| 26 | + * Visits the provided CompilationUnit and processes findings for potential SQL injections. |
| 27 | + * |
| 28 | + * @param context the context of the codemod invocation |
| 29 | + * @param cu the compilation unit to be scanned |
| 30 | + * @param pathFindings a collection of findings to be processed |
| 31 | + * @param detectorRule the rule used to detect potential issues |
| 32 | + * @param findingIdExtractor a function to extract the ID from a finding |
| 33 | + * @param findingLineExtractor a function to extract the line number from a finding |
| 34 | + * @param <T> the type of the findings |
| 35 | + * @return a result object containing the changes and unfixed findings |
| 36 | + */ |
| 37 | + public <T> CodemodFileScanningResult visit( |
| 38 | + final CodemodInvocationContext context, |
| 39 | + final CompilationUnit cu, |
| 40 | + final Collection<T> pathFindings, |
| 41 | + final DetectorRule detectorRule, |
| 42 | + final Function<T, String> findingIdExtractor, |
| 43 | + final Function<T, Integer> findingLineExtractor) { |
| 44 | + |
| 45 | + final List<MethodCallExpr> allMethodCalls = cu.findAll(MethodCallExpr.class); |
| 46 | + |
| 47 | + if (pathFindings.isEmpty()) { |
| 48 | + return CodemodFileScanningResult.none(); |
| 49 | + } |
| 50 | + |
| 51 | + final List<UnfixedFinding> unfixedFindings = new ArrayList<>(); |
| 52 | + final List<CodemodChange> changes = new ArrayList<>(); |
| 53 | + |
| 54 | + for (T finding : pathFindings) { |
| 55 | + final String id = findingIdExtractor.apply(finding); |
| 56 | + final Integer line = findingLineExtractor.apply(finding); |
| 57 | + |
| 58 | + if (line == null) { |
| 59 | + final UnfixedFinding unfixableFinding = |
| 60 | + new UnfixedFinding( |
| 61 | + id, detectorRule, context.path().toString(), null, "No line number provided"); |
| 62 | + unfixedFindings.add(unfixableFinding); |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + final List<MethodCallExpr> supportedSqlMethodCallsOnThatLine = |
| 67 | + allMethodCalls.stream() |
| 68 | + .filter(methodCallExpr -> methodCallExpr.getRange().get().begin.line == line) |
| 69 | + .filter(SQLParameterizer::isSupportedJdbcMethodCall) |
| 70 | + .toList(); |
| 71 | + |
| 72 | + if (supportedSqlMethodCallsOnThatLine.isEmpty()) { |
| 73 | + final UnfixedFinding unfixableFinding = |
| 74 | + new UnfixedFinding( |
| 75 | + id, |
| 76 | + detectorRule, |
| 77 | + context.path().toString(), |
| 78 | + line, |
| 79 | + "No supported SQL methods found on the given line"); |
| 80 | + unfixedFindings.add(unfixableFinding); |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + if (supportedSqlMethodCallsOnThatLine.size() > 1) { |
| 85 | + final UnfixedFinding unfixableFinding = |
| 86 | + new UnfixedFinding( |
| 87 | + id, |
| 88 | + detectorRule, |
| 89 | + context.path().toString(), |
| 90 | + line, |
| 91 | + "Multiple supported SQL methods found on the given line"); |
| 92 | + unfixedFindings.add(unfixableFinding); |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + final MethodCallExpr methodCallExpr = supportedSqlMethodCallsOnThatLine.get(0); |
| 97 | + if (SQLParameterizerWithCleanup.checkAndFix(methodCallExpr)) { |
| 98 | + changes.add(CodemodChange.from(line, new FixedFinding(id, detectorRule))); |
| 99 | + } else { |
| 100 | + final UnfixedFinding unfixableFinding = |
| 101 | + new UnfixedFinding( |
| 102 | + id, |
| 103 | + detectorRule, |
| 104 | + context.path().toString(), |
| 105 | + line, |
| 106 | + "State changing effects possible or unrecognized code shape"); |
| 107 | + unfixedFindings.add(unfixableFinding); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return CodemodFileScanningResult.from(changes, unfixedFindings); |
| 112 | + } |
| 113 | +} |
0 commit comments