Ran into some issues on some of our Java sites today and needed a quick fix to protect the sites from malicious Cross Site Scripting (XSS) attempts. If you’re not aware of what XSS is and have websites that have sensitive user data, you may want to read up, you’re probably vulnerable, which means your users are vulnerable. I’m not claiming this is a perfect solution, but it was easy to implement and corrected the vulnerabilities with form and url injection. We basically have a Servlet Filter that’s going to intercept every request sent to the web application and then we use an HttpServletRequestWrapper to wrap and override the getParameter methods and clean any potential script injection.


Here’s the Filter:

  1.  
  2. package com.greatwebguy.filter;  
  3.  
  4. import java.io.IOException;  
  5. import javax.servlet.Filter;  
  6. import javax.servlet.FilterChain;  
  7. import javax.servlet.FilterConfig;  
  8. import javax.servlet.ServletException;  
  9. import javax.servlet.ServletRequest;  
  10. import javax.servlet.ServletResponse;  
  11. import javax.servlet.http.HttpServletRequest;  
  12.  
  13.  
  14. public class CrossScriptingFilter implements Filter {  
  15.  
  16.     public void init(FilterConfig filterConfig) throws ServletException {  
  17.         this.filterConfig = filterConfig;  
  18.     }  
  19.  
  20.     public void destroy() {  
  21.         this.filterConfig = null;  
  22.     }  
  23.  
  24.     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)   
  25.         throws IOException, ServletException {  
  26.          
  27.         chain.doFilter(new RequestWrapper((HttpServletRequest) request), response);  
  28.  
  29.     }  
  30.  
  31. }  

Here’s the wrapper:

  1. package com.greatwebguy.filter;  
  2.  
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpServletRequestWrapper;  
  5.  
  6. public final class RequestWrapper extends HttpServletRequestWrapper {  
  7.       
  8.     public RequestWrapper(HttpServletRequest servletRequest) {  
  9.         super(servletRequest);  
  10.     }  
  11.       
  12.     public String[] getParameterValues(String parameter) {  
  13.  
  14.       String[] values = super.getParameterValues(parameter);  
  15.       if (values==null)  {  
  16.                   return null;  
  17.           }  
  18.       int count = values.length;  
  19.       String[] encodedValues = new String[count];  
  20.       for (int i = 0; i < count; i++) {  
  21.                  encodedValues[i] = cleanXSS(values[i]);  
  22.        }    
  23.       return encodedValues;   
  24.     }  
  25.       
  26.     public String getParameter(String parameter) {  
  27.           String value = super.getParameter(parameter);  
  28.           if (value == null) {  
  29.                  return null;   
  30.                   }  
  31.           return cleanXSS(value);  
  32.     }  
  33.       
  34.     public String getHeader(String name) {  
  35.         String value = super.getHeader(name);  
  36.         if (value == null)  
  37.             return null;  
  38.         return cleanXSS(value);  
  39.           
  40.     }  
  41.  
  42.     private String cleanXSS(String value) {  
  43.  
  44.         value = value.replaceAll("<", "& lt;").replaceAll(">", "& gt;");  
  45.         value = value.replaceAll("\\(", "& #40;").replaceAll("\\)", "& #41;");  
  46.         value = value.replaceAll("'", "& #39;");            
  47.         value = value.replaceAll("eval\\((.*)\\)", "");  
  48.         value = value.replaceAll("[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']", "\"\"");  
  49.         value = value.replaceAll("script", "");  
  50.         return value;  
  51.     }  
  52. }  

There’s a space in the entity character of the cleanXSS method above, you’ll need to remove it if you copy the code above, or you can copy this plain text copy of the method to get the correct syntax.

Add this to the top of your web.xml:

  1. <FILTER> 
  2.     <FILTER-NAME>XSS</FILTER-NAME> 
  3.     <DISPLAY-NAME>XSS</DISPLAY-NAME> 
  4.     <DESCRIPTION></DESCRIPTION> 
  5.     <FILTER-CLASS>com.greatwebguy.filter.CrossScriptingFilter</FILTER-CLASS> 
  6. </FILTER> 
  7. <FILTER-MAPPING> 
  8.     <FILTER-NAME>XSS</FILTER-NAME> 
  9.     <URL-PATTERN>/*</URL-PATTERN> 
  10. </FILTER-MAPPING> 

I’m sure the cleanXSS replacements aren’t the most efficient way of doing this, you could replace it StringEscapeUtils.escapeHtml from commons lang to simplify it a little, it’s up to you, it all depends on what your site is doing and whether it’s going to be a pain having all the html escaped, you could also adjust the url-pattern of the filter to be more specific to your application urls, so that everything under your app isn’t running through the filter.

Some things to be aware of with this approach, you’ll need to account for what you’ve encoded or in some cases you’ll end up with weird characters in your database and possibly in validation of your input boxes. Some would recommend a more positive validation rather than negative validation and only allow a certain range of characters, it’s up to you, but it is something to think about.