|
| 1 | +package io.codemodder.remediation.xxe; |
| 2 | + |
| 3 | +import static io.codemodder.remediation.RemediationMessages.multipleCallsFound; |
| 4 | +import static io.codemodder.remediation.RemediationMessages.noCallsAtThatLocation; |
| 5 | + |
| 6 | +import com.github.javaparser.Position; |
| 7 | +import com.github.javaparser.ast.CompilationUnit; |
| 8 | +import com.github.javaparser.ast.Node; |
| 9 | +import com.github.javaparser.ast.body.MethodDeclaration; |
| 10 | +import com.github.javaparser.ast.body.Parameter; |
| 11 | +import com.github.javaparser.ast.body.VariableDeclarator; |
| 12 | +import com.github.javaparser.ast.expr.Expression; |
| 13 | +import com.github.javaparser.ast.expr.MethodCallExpr; |
| 14 | +import com.github.javaparser.ast.expr.NameExpr; |
| 15 | +import com.github.javaparser.ast.nodeTypes.NodeWithType; |
| 16 | +import com.github.javaparser.ast.stmt.Statement; |
| 17 | +import io.codemodder.ast.ASTs; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Optional; |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +/** Fix XXEs that are reported at the DocumentBuilder.parse() call locations. */ |
| 23 | +final class DocumentBuilderFactoryAtParseFixer implements XXEFixer { |
| 24 | + |
| 25 | + @Override |
| 26 | + public XXEFixAttempt tryFix(final int line, final Integer column, CompilationUnit cu) { |
| 27 | + List<MethodCallExpr> candidateMethods = |
| 28 | + cu.findAll(MethodCallExpr.class).stream() |
| 29 | + .filter(m -> "parse".equals(m.getNameAsString())) |
| 30 | + .filter(m -> m.getScope().isPresent()) |
| 31 | + .filter(m -> m.getScope().get().isNameExpr()) |
| 32 | + .filter(m -> m.getRange().isPresent() && m.getRange().get().begin.line == line) |
| 33 | + .toList(); |
| 34 | + |
| 35 | + if (candidateMethods.size() > 1 && column != null) { |
| 36 | + candidateMethods = |
| 37 | + candidateMethods.stream() |
| 38 | + .filter(m -> m.getRange().get().contains(new Position(line, column))) |
| 39 | + .toList(); |
| 40 | + } |
| 41 | + |
| 42 | + if (candidateMethods.isEmpty()) { |
| 43 | + return new XXEFixAttempt(false, false, noCallsAtThatLocation); |
| 44 | + } else if (candidateMethods.size() > 1) { |
| 45 | + return new XXEFixAttempt(false, false, multipleCallsFound); |
| 46 | + } |
| 47 | + |
| 48 | + // find the variable that we're calling parse() on |
| 49 | + MethodCallExpr parseCall = candidateMethods.get(0); |
| 50 | + |
| 51 | + // we know from the filter that it's scope is a name expression |
| 52 | + NameExpr documentBuilder = parseCall.getScope().get().asNameExpr(); |
| 53 | + |
| 54 | + // so, we need to see if we can see the DocumentBuilderFactory from whence this came |
| 55 | + Optional<MethodDeclaration> methodBody = ASTs.findMethodBodyFrom(documentBuilder); |
| 56 | + if (methodBody.isEmpty()) { |
| 57 | + return new XXEFixAttempt(false, false, "No method body found for the call"); |
| 58 | + } |
| 59 | + |
| 60 | + Optional<Node> documentBuilderAssignment = |
| 61 | + ASTs.findNonCallableSimpleNameSource(documentBuilder.getName()); |
| 62 | + if (documentBuilderAssignment.isEmpty()) { |
| 63 | + return new XXEFixAttempt(false, false, "No assignment found for the DocumentBuilder"); |
| 64 | + } |
| 65 | + Node parserAssignmentNode = documentBuilderAssignment.get(); |
| 66 | + if (!(parserAssignmentNode instanceof NodeWithType<?, ?>)) { |
| 67 | + return new XXEFixAttempt(false, false, "Unknown DocumentBuilder assignment"); |
| 68 | + } |
| 69 | + String parserType = ((NodeWithType<?, ?>) parserAssignmentNode).getTypeAsString(); |
| 70 | + if (!Set.of("DocumentBuilder", "javax.xml.parsers.DocumentBuilder").contains(parserType)) { |
| 71 | + return new XXEFixAttempt(false, false, "Parsing method is not a DocumentBuilder"); |
| 72 | + } |
| 73 | + |
| 74 | + if (parserAssignmentNode instanceof VariableDeclarator dbVar) { |
| 75 | + Optional<Expression> initializer = dbVar.getInitializer(); |
| 76 | + if (initializer.isEmpty()) { |
| 77 | + return new XXEFixAttempt( |
| 78 | + false, false, "DocumentBuilder was not initialized in an expected way"); |
| 79 | + } else if (!(initializer.get() instanceof MethodCallExpr)) { |
| 80 | + return new XXEFixAttempt( |
| 81 | + false, false, "DocumentBuilder was not initialized with a factory call"); |
| 82 | + } |
| 83 | + MethodCallExpr potentialFactoryCall = (MethodCallExpr) initializer.get(); |
| 84 | + if (!"newDocumentBuilder".equals(potentialFactoryCall.getNameAsString())) { |
| 85 | + return new XXEFixAttempt( |
| 86 | + false, false, "DocumentBuilder was initialized with newDocumentBuilder"); |
| 87 | + } |
| 88 | + if (potentialFactoryCall.getScope().isEmpty()) { |
| 89 | + return new XXEFixAttempt( |
| 90 | + true, false, "DocumentBuilder was initialized with a factory call without a scope"); |
| 91 | + } |
| 92 | + if (!(potentialFactoryCall.getScope().get() instanceof NameExpr)) { |
| 93 | + return new XXEFixAttempt( |
| 94 | + false, |
| 95 | + false, |
| 96 | + "DocumentBuilder was initialized with a factory call with a non-name scope"); |
| 97 | + } |
| 98 | + NameExpr factoryNameExpr = (NameExpr) potentialFactoryCall.getScope().get(); |
| 99 | + Optional<Statement> newDocumentBuilderStatement = ASTs.findParentStatementFrom(dbVar); |
| 100 | + if (newDocumentBuilderStatement.isEmpty()) { |
| 101 | + return new XXEFixAttempt( |
| 102 | + false, |
| 103 | + false, |
| 104 | + "DocumentBuilder was initialized with a factory call without a statement"); |
| 105 | + } |
| 106 | + return XMLFeatures.addFeatureDisablingStatements( |
| 107 | + cu, factoryNameExpr, newDocumentBuilderStatement.get(), true); |
| 108 | + } else if (parserAssignmentNode instanceof Parameter) { |
| 109 | + return new XXEFixAttempt(true, false, "DocumentBuilder came from outside the method scope"); |
| 110 | + } |
| 111 | + |
| 112 | + return new XXEFixAttempt(true, false, "DocumentBuilder was not initialized in an expected way"); |
| 113 | + } |
| 114 | +} |
0 commit comments