-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes cases of XXE where the tool points to the `XMLInputFactory#newInstance()` call.
- Loading branch information
Showing
8 changed files
with
215 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
.../src/main/java/io/codemodder/remediation/xxe/XMLInputFactoryAtNewInstanceFixStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package io.codemodder.remediation.xxe; | ||
|
||
import com.github.javaparser.ast.CompilationUnit; | ||
import com.github.javaparser.ast.Node; | ||
import com.github.javaparser.ast.body.VariableDeclarator; | ||
import com.github.javaparser.ast.expr.Expression; | ||
import com.github.javaparser.ast.expr.MethodCallExpr; | ||
import com.github.javaparser.ast.stmt.Statement; | ||
import io.codemodder.ast.ASTs; | ||
import io.codemodder.remediation.SuccessOrReason; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
final class XMLInputFactoryAtNewInstanceFixStrategy | ||
extends io.codemodder.remediation.MatchAndFixStrategy { | ||
|
||
/** | ||
* Matches (XmlInputFactory | var xif) y = XMLInputFactory.newInstance(), where the node is the | ||
* newDocumentBuilder call. | ||
* | ||
* @param node the node to match | ||
* @return true if this is an assignment to an XMLInputFactory | ||
*/ | ||
@Override | ||
public boolean match(final Node node) { | ||
Optional<MethodCallExpr> method = | ||
ASTs.isInitializedToType(node, "newInstance", List.of("var", "XMLInputFactory")); | ||
if (method.isEmpty()) { | ||
return false; | ||
} | ||
MethodCallExpr newInstance = method.get(); | ||
Optional<Expression> scope = newInstance.getScope(); | ||
if (scope.isEmpty()) { | ||
return false; | ||
} | ||
return scope.get().toString().equals("XMLInputFactory"); | ||
} | ||
|
||
@Override | ||
public SuccessOrReason fix(final CompilationUnit cu, final Node node) { | ||
MethodCallExpr newInstance = (MethodCallExpr) node; | ||
Optional<VariableDeclarator> initExpr = ASTs.isInitExpr(newInstance); | ||
if (initExpr.isEmpty()) { | ||
return SuccessOrReason.reason("Not an initialization expression"); | ||
} | ||
VariableDeclarator xmlInputFactoryVariable = initExpr.get(); | ||
Optional<Statement> variableDeclarationStmtRef = | ||
xmlInputFactoryVariable.findAncestor(Statement.class); | ||
|
||
if (variableDeclarationStmtRef.isEmpty()) { | ||
return SuccessOrReason.reason("Not assigned as part of statement"); | ||
} | ||
|
||
Statement stmt = variableDeclarationStmtRef.get(); | ||
return XMLFixBuilder.addXMLInputFactoryDisablingStatement( | ||
xmlInputFactoryVariable.getNameAsExpression(), stmt, false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.