-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
205 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
* | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
package org.openmrs.web.xss; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.FilterConfig; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import org.apache.commons.fileupload.servlet.ServletFileUpload; | ||
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest; | ||
|
||
public class XSSFilter implements Filter { | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, | ||
ServletException { | ||
|
||
if (!"GET".equalsIgnoreCase(((HttpServletRequest) request).getMethod())) { | ||
if (ServletFileUpload.isMultipartContent((HttpServletRequest) request)) { | ||
request = new XSSMultipartRequestWrapper((DefaultMultipartHttpServletRequest) request); | ||
} else { | ||
request = new XSSRequestWrapper((HttpServletRequest) request); | ||
} | ||
} | ||
|
||
chain.doFilter(request, response); | ||
} | ||
|
||
@Override | ||
public void init(FilterConfig filterConfig) throws ServletException { | ||
|
||
} | ||
|
||
@Override | ||
public void destroy() { | ||
|
||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
omod/src/main/java/org/openmrs/web/xss/XSSMultipartRequestWrapper.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,72 @@ | ||
/** | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
* | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
package org.openmrs.web.xss; | ||
|
||
import java.util.Enumeration; | ||
|
||
import org.owasp.encoder.Encode; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest; | ||
|
||
public class XSSMultipartRequestWrapper extends DefaultMultipartHttpServletRequest { | ||
|
||
public XSSMultipartRequestWrapper(DefaultMultipartHttpServletRequest request) { | ||
super(request); | ||
} | ||
|
||
@Override | ||
public String getParameter(String name) { | ||
|
||
String value = getRequest().getParameter(name); | ||
if (value == null) { | ||
return null; | ||
} | ||
|
||
return Encode.forHtml(value); | ||
} | ||
|
||
@Override | ||
public String[] getParameterValues(String name) { | ||
|
||
String[] values = getRequest().getParameterValues(name); | ||
if (values == null) { | ||
return null; | ||
} | ||
|
||
int count = values.length; | ||
String[] encodedValues = new String[count]; | ||
for (int i = 0; i < count; i++) { | ||
encodedValues[i] = Encode.forHtml(values[i]); | ||
} | ||
|
||
return encodedValues; | ||
} | ||
|
||
@Override | ||
public DefaultMultipartHttpServletRequest getRequest() { | ||
return (DefaultMultipartHttpServletRequest) super.getRequest(); | ||
} | ||
|
||
@Override | ||
public MultipartFile getFile(String name) { | ||
return getRequest().getFile(name); | ||
} | ||
|
||
@Override | ||
public MultiValueMap<String, MultipartFile> getMultiFileMap() { | ||
return getRequest().getMultiFileMap(); | ||
} | ||
|
||
@Override | ||
public Enumeration<String> getParameterNames() { | ||
return getRequest().getParameterNames(); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
omod/src/main/java/org/openmrs/web/xss/XSSRequestWrapper.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,73 @@ | ||
/** | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
* | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
package org.openmrs.web.xss; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import javax.servlet.ServletInputStream; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletRequestWrapper; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.owasp.encoder.Encode; | ||
|
||
public class XSSRequestWrapper extends HttpServletRequestWrapper { | ||
|
||
public XSSRequestWrapper(HttpServletRequest request) { | ||
super(request); | ||
} | ||
|
||
@Override | ||
public String[] getParameterValues(String parameter) { | ||
|
||
String[] values = super.getParameterValues(parameter); | ||
if (values == null) { | ||
return null; | ||
} | ||
|
||
int count = values.length; | ||
String[] encodedValues = new String[count]; | ||
for (int i = 0; i < count; i++) { | ||
encodedValues[i] = Encode.forHtml(values[i]); | ||
} | ||
|
||
return encodedValues; | ||
} | ||
|
||
@Override | ||
public String getParameter(String name) { | ||
|
||
String value = super.getParameter(name); | ||
if (value == null) { | ||
return null; | ||
} | ||
|
||
return Encode.forHtml(value); | ||
} | ||
|
||
@Override | ||
public ServletInputStream getInputStream() throws IOException { | ||
|
||
String requestBody = IOUtils.toString(super.getInputStream(), StandardCharsets.UTF_8.name()); | ||
String sanitizedBody = Encode.forHtmlContent(requestBody); | ||
|
||
return new ServletInputStream() { | ||
|
||
private final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(sanitizedBody.getBytes()); | ||
|
||
@Override | ||
public int read() throws IOException { | ||
return byteArrayInputStream.read(); | ||
} | ||
}; | ||
} | ||
} |
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