Skip to content

Commit

Permalink
WW-5340 Sanitize field names before logging
Browse files Browse the repository at this point in the history
  • Loading branch information
kusalk committed Oct 6, 2023
1 parent 276ede4 commit f4029f8
Showing 1 changed file with 6 additions and 3 deletions.
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 Down Expand Up @@ -143,7 +143,7 @@ protected void processNormalFormField(FileItem item, String charset) throws Unsu

long size = item.getSize();
if (size > maxStringLength) {
LOG.debug("Form field {} of size {} bytes exceeds limit of {}.", item.getFieldName(), 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});
Expand Down Expand Up @@ -362,4 +362,7 @@ public void cleanUp() {
}
}

private String sanitizeNewlines(String before) {
return before.replaceAll("[\n\r]", "_");
}
}

0 comments on commit f4029f8

Please sign in to comment.