Class XSSFilter

  • All Implemented Interfaces:
    javax.servlet.Filter

    public class XSSFilter
    extends java.lang.Object
    implements javax.servlet.Filter
    Filter that wraps requests to XSSRequestWrapper to sanitize inputs for XSS.

    Utilizes HttpServletRequestWrapper that sanitize requests inputs to mitigate risks of XSS scripts being passed over. This code is based on free and non-restricted code found at: http://ricardozuasti.com/2012/stronger-anti-cross-site-scripting-xss-filter-for-java-web-apps/

    • Field Detail

      • LOG

        protected static org.apache.log4j.Logger LOG
      • CONFIG_PARAM_PREFIX

        public static final java.lang.String CONFIG_PARAM_PREFIX
        See Also:
        Constant Field Values
      • CONFIG_RULE_PREFIX_REGEXP

        public static final java.lang.String CONFIG_RULE_PREFIX_REGEXP
        See Also:
        Constant Field Values
      • CONFIG_HEADER_PREFIX_REGEXP

        public static final java.lang.String CONFIG_HEADER_PREFIX_REGEXP
        See Also:
        Constant Field Values
      • CONFIG_HEADER_PREFIX

        protected static final java.lang.String CONFIG_HEADER_PREFIX
        See Also:
        Constant Field Values
      • HOST_HEADER_WHITE_LIST_PREFIX

        public static final java.lang.String HOST_HEADER_WHITE_LIST_PREFIX
        See Also:
        Constant Field Values
      • REJECTED_REQUEST_RESP_CONTENT

        protected static final java.lang.String REJECTED_REQUEST_RESP_CONTENT
        See Also:
        Constant Field Values
    • Constructor Detail

      • XSSFilter

        public XSSFilter()
    • Method Detail

      • init

        public void init​(javax.servlet.FilterConfig filterConfig)
                  throws javax.servlet.ServletException
        Specified by:
        init in interface javax.servlet.Filter
        Throws:
        javax.servlet.ServletException
      • initPatternsAndHeaders

        protected void initPatternsAndHeaders​(boolean enabled,
                                              java.util.Map<java.lang.String,​java.lang.String> patternDefinitions,
                                              java.util.Map<java.lang.String,​java.lang.String> headers)
      • reloadOnConfigChange

        public void reloadOnConfigChange()
      • compilePatterns

        protected java.util.List<java.util.regex.Pattern> compilePatterns​(java.util.Map<java.lang.String,​java.lang.String> rules)
      • getSetupInfo

        protected java.lang.String getSetupInfo()
      • doFilter

        public void doFilter​(javax.servlet.ServletRequest servletRequest,
                             javax.servlet.ServletResponse servletResponse,
                             javax.servlet.FilterChain filterChain)
                      throws java.io.IOException,
                             javax.servlet.ServletException
        Specified by:
        doFilter in interface javax.servlet.Filter
        Throws:
        java.io.IOException
        javax.servlet.ServletException
      • processPatternsAndDoFilter

        protected void processPatternsAndDoFilter​(javax.servlet.ServletRequest servletRequest,
                                                  javax.servlet.ServletResponse servletResponse,
                                                  javax.servlet.FilterChain filterChain)
                                           throws java.io.IOException,
                                                  javax.servlet.ServletException
        Throws:
        java.io.IOException
        javax.servlet.ServletException
      • setRejectResponseCodes

        protected void setRejectResponseCodes​(javax.servlet.http.HttpServletResponse httpResponse)
                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • destroy

        public void destroy()
        Specified by:
        destroy in interface javax.servlet.Filter