|
| 1 | +package io.codemodder.remediation.javadeserialization; |
| 2 | + |
| 3 | +import static io.codemodder.javaparser.JavaParserTransformer.replace; |
| 4 | + |
| 5 | +import com.github.javaparser.ast.CompilationUnit; |
| 6 | +import com.github.javaparser.ast.Node; |
| 7 | +import com.github.javaparser.ast.body.VariableDeclarator; |
| 8 | +import com.github.javaparser.ast.expr.Expression; |
| 9 | +import com.github.javaparser.ast.expr.MethodCallExpr; |
| 10 | +import com.github.javaparser.ast.expr.ObjectCreationExpr; |
| 11 | +import io.codemodder.CodemodChange; |
| 12 | +import io.codemodder.CodemodFileScanningResult; |
| 13 | +import io.codemodder.DependencyGAV; |
| 14 | +import io.codemodder.ast.ASTs; |
| 15 | +import io.codemodder.ast.LocalDeclaration; |
| 16 | +import io.codemodder.codetf.DetectorRule; |
| 17 | +import io.codemodder.codetf.FixedFinding; |
| 18 | +import io.codemodder.codetf.UnfixedFinding; |
| 19 | +import io.codemodder.remediation.FixCandidate; |
| 20 | +import io.codemodder.remediation.FixCandidateSearchResults; |
| 21 | +import io.codemodder.remediation.FixCandidateSearcher; |
| 22 | +import io.github.pixee.security.ObjectInputFilters; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Optional; |
| 26 | +import java.util.function.Function; |
| 27 | + |
| 28 | +final class DefaultJavaDeserializationRemediator implements JavaDeserializationRemediator { |
| 29 | + |
| 30 | + @Override |
| 31 | + public <T> CodemodFileScanningResult remediateAll( |
| 32 | + final CompilationUnit cu, |
| 33 | + final String path, |
| 34 | + final DetectorRule detectorRule, |
| 35 | + final List<T> issuesForFile, |
| 36 | + final Function<T, String> getKey, |
| 37 | + final Function<T, Integer> getLine, |
| 38 | + final Function<T, Integer> getColumn) { |
| 39 | + FixCandidateSearcher<T> searcher = |
| 40 | + new FixCandidateSearcher.Builder<T>() |
| 41 | + .withMethodName("readObject") |
| 42 | + .withMatcher(mce -> mce.getScope().isPresent()) |
| 43 | + .withMatcher(mce -> mce.getArguments().isEmpty()) |
| 44 | + .build(); |
| 45 | + |
| 46 | + FixCandidateSearchResults<T> results = |
| 47 | + searcher.search(cu, path, detectorRule, issuesForFile, getKey, getLine, getColumn); |
| 48 | + |
| 49 | + List<CodemodChange> changes = new ArrayList<>(); |
| 50 | + List<UnfixedFinding> unfixedFindings = new ArrayList<>(); |
| 51 | + for (FixCandidate<T> fixCandidate : results.fixCandidates()) { |
| 52 | + List<T> issues = fixCandidate.issues(); |
| 53 | + MethodCallExpr call = fixCandidate.methodCall(); |
| 54 | + // get the declaration of the ObjectInputStream |
| 55 | + Expression callScope = call.getScope().get(); |
| 56 | + if (!callScope.isNameExpr()) { |
| 57 | + // can't fix these |
| 58 | + issues.stream() |
| 59 | + .map( |
| 60 | + i -> |
| 61 | + new UnfixedFinding( |
| 62 | + getKey.apply(i), detectorRule, path, getLine.apply(i), "Unexpected shape")) |
| 63 | + .forEach(unfixedFindings::add); |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + Optional<LocalDeclaration> declaration = |
| 68 | + ASTs.findEarliestLocalDeclarationOf(callScope.asNameExpr().getName()); |
| 69 | + if (declaration.isEmpty()) { |
| 70 | + issues.stream() |
| 71 | + .map( |
| 72 | + i -> |
| 73 | + new UnfixedFinding( |
| 74 | + getKey.apply(i), |
| 75 | + detectorRule, |
| 76 | + path, |
| 77 | + getLine.apply(i), |
| 78 | + "No declaration found")) |
| 79 | + .forEach(unfixedFindings::add); |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + LocalDeclaration localDeclaration = declaration.get(); |
| 84 | + Node varDeclarationAndExpr = localDeclaration.getDeclaration(); |
| 85 | + if (varDeclarationAndExpr instanceof VariableDeclarator varDec) { |
| 86 | + Optional<Expression> initializer = varDec.getInitializer(); |
| 87 | + if (initializer.isEmpty()) { |
| 88 | + issues.stream() |
| 89 | + .map( |
| 90 | + i -> |
| 91 | + new UnfixedFinding( |
| 92 | + getKey.apply(i), |
| 93 | + detectorRule, |
| 94 | + path, |
| 95 | + getLine.apply(i), |
| 96 | + "No initializer found")) |
| 97 | + .forEach(unfixedFindings::add); |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + Expression expression = initializer.get(); |
| 102 | + if (expression instanceof ObjectCreationExpr objCreation) { |
| 103 | + fixObjectInputStreamCreation(objCreation); |
| 104 | + CodemodChange change = |
| 105 | + CodemodChange.from( |
| 106 | + getLine.apply(issues.get(0)), |
| 107 | + List.of(DependencyGAV.JAVA_SECURITY_TOOLKIT), |
| 108 | + issues.stream() |
| 109 | + .map(i -> new FixedFinding(getKey.apply(i), detectorRule)) |
| 110 | + .toList()); |
| 111 | + changes.add(change); |
| 112 | + } |
| 113 | + } else { |
| 114 | + issues.stream() |
| 115 | + .map( |
| 116 | + i -> |
| 117 | + new UnfixedFinding( |
| 118 | + getKey.apply(i), |
| 119 | + detectorRule, |
| 120 | + path, |
| 121 | + getLine.apply(i), |
| 122 | + "Unexpected declaration type")) |
| 123 | + .forEach(unfixedFindings::add); |
| 124 | + } |
| 125 | + } |
| 126 | + return CodemodFileScanningResult.from(changes, unfixedFindings); |
| 127 | + } |
| 128 | + |
| 129 | + private void fixObjectInputStreamCreation(final ObjectCreationExpr objCreation) { |
| 130 | + replace(objCreation) |
| 131 | + .withStaticMethod(ObjectInputFilters.class.getName(), "createSafeObjectInputStream") |
| 132 | + .withStaticImport() |
| 133 | + .withSameArguments(); |
| 134 | + } |
| 135 | +} |
0 commit comments