-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServletFileUploadXSS.java
61 lines (55 loc) · 2.53 KB
/
ServletFileUploadXSS.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext;
import org.apache.commons.fileupload.util.Streams;
/**
*
* @author dbligh
*/
public class ServletFileUploadXSS extends ServletFileUpload {
public ServletFileUploadXSS() {
super();
}
public ServletFileUploadXSS(FileItemFactory fileItemFactory) {
super(fileItemFactory);
}
@Override
public List<FileItem> parseRequest(HttpServletRequest request) throws FileUploadException {
List<FileItem> list = parseRequest(new ServletRequestContext(request));
List<FileItem> cleanList = new ArrayList();
for (FileItem item : list) {
if( item.isFormField() ){
try{
System.out.println("Cleaning inputs on fileitem: " + item.getName());
/* call a function here to actually perform the cross site script cleansing to your needs,
* there is a good example you can use here:
* https://www.javacodegeeks.com/2012/07/anti-cross-site-scripting-xss-filter.html
*/
String cleaned = YourAppSecurity.cleanXSS(item.getString());
InputStream stream = new ByteArrayInputStream(cleaned.getBytes(StandardCharsets.UTF_8));
FileItemFactory fac = getFileItemFactory();
FileItem fileItem = fac.createItem(item.getFieldName(), item.getContentType(),
item.isFormField(), item.getName());
cleanList.add(fileItem);
Streams.copy(stream, fileItem.getOutputStream(), true);
System.out.println("Cleansed input successfully");
}catch(IOException e){
System.out.println("Failed to cleanse inputs: " + e.getLocalizedMessage());
e.printStackTrace();
}
}else{
cleanList.add(item);
}
}
return cleanList;
}
}