XSSFilter for java

来源:互联网 发布:淘宝客服主要做什么 编辑:程序博客网 时间:2024/06/07 16:34
Here is a good and simple anti cross-site scripting (XSS) filter written for Java web applications. What it basically does is remove all suspicious strings from request parameters before returning them to the application. It’s an improvement over my previous post on the topic.
You should configure it as the first filter in your chain (web.xml) and it’s generally a good idea to let it catch every request made to your site.
The actual implementation consists of two classes, the actual filter is quite simple, it wraps the HTTP request object in a specialized HttpServletRequestWrapper that will perform our filtering.
[java] view plain copy
 print?
  1. public class XSSFilter implements Filter {  
  2.   
  3.     @Override  
  4.     public void init(FilterConfig filterConfig) throws ServletException {  
  5.     }  
  6.   
  7.     @Override  
  8.     public void destroy() {  
  9.     }  
  10.   
  11.     @Override  
  12.     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)  
  13.         throws IOException, ServletException {  
  14.         chain.doFilter(new XSSRequestWrapper((HttpServletRequest) request), response);  
  15.     }  
  16.   
  17. }  

The wrapper overrides the getParameterValues(), getParameter() and getHeader() methods to execute the filtering before returning the desired field to the caller. The actual XSS checking and striping is performed in the stripXSS() private method.

[java] view plain copy
 print?
  1. import java.util.regex.Pattern;  
  2. import javax.servlet.http.HttpServletRequest;  
  3. import javax.servlet.http.HttpServletRequestWrapper;  
  4.   
  5. public class XSSRequestWrapper extends HttpServletRequestWrapper {  
  6.   
  7.     public XSSRequestWrapper(HttpServletRequest servletRequest) {  
  8.         super(servletRequest);  
  9.     }  
  10.   
  11.     @Override  
  12.     public String[] getParameterValues(String parameter) {  
  13.         String[] values = super.getParameterValues(parameter);  
  14.   
  15.         if (values == null) {  
  16.             return null;  
  17.         }  
  18.   
  19.         int count = values.length;  
  20.         String[] encodedValues = new String[count];  
  21.         for (int i = 0; i < count; i++) {  
  22.             encodedValues[i] = stripXSS(values[i]);  
  23.         }  
  24.   
  25.         return encodedValues;  
  26.     }  
  27.   
  28.     @Override  
  29.     public String getParameter(String parameter) {  
  30.         String value = super.getParameter(parameter);  
  31.   
  32.         return stripXSS(value);  
  33.     }  
  34.   
  35.     @Override  
  36.     public String getHeader(String name) {  
  37.         String value = super.getHeader(name);  
  38.         return stripXSS(value);  
  39.     }  
  40.   
  41.     private String stripXSS(String value) {  
  42.         if (value != null) {  
  43.             // NOTE: It's highly recommended to use the ESAPI library and uncomment the following line to  
  44.             // avoid encoded attacks.  
  45.             // value = ESAPI.encoder().canonicalize(value);  
  46.   
  47.             // Avoid null characters  
  48.             value = value.replaceAll("""");  
  49.   
  50.             // Avoid anything between script tags  
  51.             Pattern scriptPattern = Pattern.compile("<script>(.*?)</script>", Pattern.CASE_INSENSITIVE);  
  52.             value = scriptPattern.matcher(value).replaceAll("");  
  53.   
  54.             // Avoid anything in a src='...' type of expression  
  55.             scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\'(.*?)\\\'", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);  
  56.             value = scriptPattern.matcher(value).replaceAll("");  
  57.   
  58.             scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);  
  59.             value = scriptPattern.matcher(value).replaceAll("");  
  60.   
  61.             // Remove any lonesome </script> tag  
  62.             scriptPattern = Pattern.compile("</script>", Pattern.CASE_INSENSITIVE);  
  63.             value = scriptPattern.matcher(value).replaceAll("");  
  64.   
  65.             // Remove any lonesome <script ...> tag  
  66.             scriptPattern = Pattern.compile("<script(.*?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);  
  67.             value = scriptPattern.matcher(value).replaceAll("");  
  68.   
  69.             // Avoid eval(...) expressions  
  70.             scriptPattern = Pattern.compile("eval\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);  
  71.             value = scriptPattern.matcher(value).replaceAll("");  
  72.   
  73.             // Avoid expression(...) expressions  
  74.             scriptPattern = Pattern.compile("expression\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);  
  75.             value = scriptPattern.matcher(value).replaceAll("");  
  76.   
  77.             // Avoid javascript:... expressions  
  78.             scriptPattern = Pattern.compile("javascript:", Pattern.CASE_INSENSITIVE);  
  79.             value = scriptPattern.matcher(value).replaceAll("");  
  80.   
  81.             // Avoid vbscript:... expressions  
  82.             scriptPattern = Pattern.compile("vbscript:", Pattern.CASE_INSENSITIVE);  
  83.             value = scriptPattern.matcher(value).replaceAll("");  
  84.   
  85.             // Avoid onload= expressions  
  86.             scriptPattern = Pattern.compile("onload(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);  
  87.             value = scriptPattern.matcher(value).replaceAll("");  
  88.         }  
  89.         return value;  
  90.     }  
  91. }  

Notice the comment about the ESAPI library, I strongly recommend you check it out and try to include it in your projects.

If you want to dig deeper on the topic I suggest you check out the OWASP page about XSS and RSnake’s XSS (Cross Site Scripting) Cheat Sheet.















http://searchsecurity.techtarget.in/tip/Using-ESAPI-to-fix-XSS-in-your-Java-code

http://www.javacodegeeks.com/2012/07/anti-cross-site-scripting-xss-filter.html

0 0
原创粉丝点击