Skip to content

Commit

Permalink
WW-5340 Mild refactor StrutsOgnlGuard for easier subclassing
Browse files Browse the repository at this point in the history
  • Loading branch information
kusalk committed Oct 5, 2023
1 parent ff9ecbe commit 20eafb6
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions core/src/main/java/org/apache/struts2/ognl/StrutsOgnlGuard.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,38 @@ public boolean isRawExpressionBlocked(String expr) {

@Override
public boolean isParsedTreeBlocked(Object tree) {
return containsExcludedNodeType(tree);
if (!(tree instanceof Node) || skipTreeCheck((Node) tree)) {
return false;
}
return recurseNodes((Node) tree);
}

protected boolean containsExcludedNodeType(Object tree) {
if (!(tree instanceof Node) || excludedNodeTypes.isEmpty()) {
return false;
protected boolean skipTreeCheck(Node tree) {
return excludedNodeTypes.isEmpty();
}

protected boolean recurseNodes(Node node) {
if (checkNode(node)) {
return true;
}
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
if (recurseNodes(node.jjtGetChild(i))) {
return true;
}
}
return recurseExcludedNodeType((Node) tree);
return false;
}

protected boolean checkNode(Node node) {
return containsExcludedNodeType(node);
}

protected boolean recurseExcludedNodeType(Node node) {
protected boolean containsExcludedNodeType(Node node) {
String nodeClassName = node.getClass().getName();
if (excludedNodeTypes.contains(nodeClassName)) {
LOG.warn("Expression contains blocked node type [{}]", nodeClassName);
return true;
} else {
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
if (recurseExcludedNodeType(node.jjtGetChild(i))) {
return true;
}
}
return false;
}
return false;
}
}

0 comments on commit 20eafb6

Please sign in to comment.