Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WW-5340 Mild refactor StrutsOgnlGuard for easier subclassing #760

Merged
merged 3 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void parse(HttpServletRequest request, String saveDir) throws IOException
protected void processUpload(HttpServletRequest request, String saveDir) throws FileUploadException, UnsupportedEncodingException {
if (ServletFileUpload.isMultipartContent(request)) {
for (FileItem item : parseRequest(request, saveDir)) {
LOG.debug("Found file item: [{}]", item.getFieldName());
LOG.debug("Found file item: [{}]", sanitizeNewlines(item.getFieldName()));
if (item.isFormField()) {
processNormalFormField(item, request.getCharacterEncoding());
} else {
Expand All @@ -115,7 +115,7 @@ protected void processFileField(FileItem item) {

// Skip file uploads that don't have a file name - meaning that no file was selected.
if (item.getName() == null || item.getName().trim().isEmpty()) {
LOG.debug("No file has been uploaded for the field: {}", item.getFieldName());
LOG.debug("No file has been uploaded for the field: {}", sanitizeNewlines(item.getFieldName()));
return;
}

Expand All @@ -142,26 +142,22 @@ protected void processNormalFormField(FileItem item, String charset) throws Unsu
}

long size = item.getSize();
if (size == 0) {
values.add(StringUtils.EMPTY);
} else if (size > maxStringLength) {
if (size > maxStringLength) {
LOG.debug("Form field {} of size {} bytes exceeds limit of {}.", sanitizeNewlines(item.getFieldName()), size, maxStringLength);
String errorKey = "struts.messages.upload.error.parameter.too.long";
LocalizedMessage localizedMessage = new LocalizedMessage(this.getClass(), errorKey, null,
new Object[]{item.getFieldName(), maxStringLength, size});

new Object[]{item.getFieldName(), maxStringLength, size});
if (!errors.contains(localizedMessage)) {
errors.add(localizedMessage);
}
return;

} else if (charset != null) {
values.add(item.getString(charset));
}
if (size == 0) {
values.add(StringUtils.EMPTY);
} else if (charset == null) {
values.add(item.getString()); // WW-633
} else {
// note: see https://issues.apache.org/jira/browse/WW-633
// basically, in some cases the charset may be null, so
// we're just going to try to "other" method (no idea if this
// will work)
values.add(item.getString());
values.add(item.getString(charset));
}
params.put(item.getFieldName(), values);
} finally {
Expand Down Expand Up @@ -366,4 +362,7 @@ public void cleanUp() {
}
}

private String sanitizeNewlines(String before) {
return before.replaceAll("[\n\r]", "_");
}
}
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) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I separated the recursion logic from the node checking logic so that subclasses don't need to unnecessarily duplicate that code when overriding.

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;
}
}