HeaderPatternFilter extends ComponentSupport implements Filter { @Override public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) { HttpServletRequest httpRequest = (HttpServletRequest) request; HttpServletResponse httpResponse = (HttpServletResponse) response; for (Entry<String, Pattern> entry : validHeaderPatterns.entrySet()) { if (checkForBadHeader(httpRequest.getHeaders(entry.getKey()), entry.getValue())) { log.warn("rejecting request from {} due to invalid header '{}: {}'", request.getRemoteHost(), entry.getKey(), Joiner.on(",").join(Collections.list(httpRequest.getHeaders(entry.getKey())))); httpResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST); return; } } } chain.doFilter(request, response); }  @Override void init(final FilterConfig filterConfig); @Override void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain); @Override void destroy();  }
@Test public void testFilter_goodValue() throws Exception { testHeaderHostValue("example.com"); verify(filterChain).doFilter(request, response); }
@Test public void testFilter_localhost() throws Exception { testHeaderHostValue("localhost"); verify(filterChain).doFilter(request, response); }
@Test public void testFilter_nonroutableIp() throws Exception { testHeaderHostValue("10.0.0.1"); verify(filterChain).doFilter(request, response); }
@Test public void testFilter_localhostWithPort() throws Exception { testHeaderHostValue("localhost:8080"); verify(filterChain).doFilter(request, response); }
@Test public void testFilter_goodValueWithPort() throws Exception { testHeaderHostValue("example.com:8080"); verify(filterChain).doFilter(request, response); }
@Test public void testFilter_nonroutableWithPort() throws Exception { testHeaderHostValue("10.0.0.1:8080"); verify(filterChain).doFilter(request, response); }
@Test public void testFilter_ipv6() throws Exception { testHeaderHostValue("[1762:0:0:0:0:B03:1:AF18]"); verify(filterChain).doFilter(request, response); }
@Test public void testFilter_ipv6WithPort() throws Exception { testHeaderHostValue("[1762:0:0:0:0:B03:1:AF18]:8080"); verify(filterChain).doFilter(request, response); }
@Test public void testFilter_JustProperProto() throws Exception { testHeaderForwardedValue("proto=http"); verify(filterChain).doFilter(request, response); }
@Test public void testFilter_ProperProto() throws Exception { testHeaderForwardedValue("for=192.0.2.60;proto=http;by=203.0.113.43"); verify(filterChain).doFilter(request, response); }
@Test public void testFilter_AcceptsQuotedProto() throws Exception { testHeaderForwardedValue("for=192.0.2.60;proto=\"http\";by=203.0.113.43"); verify(filterChain).doFilter(request, response); }
@Test public void testFilter_NoProtoIP4() throws Exception { testHeaderForwardedValue("for=192.0.2.43, for=198.51.100.17"); verify(filterChain).doFilter(request, response); }
@Test public void testFilter_NoProtoIP6() throws Exception { testHeaderForwardedValue("For=\"[2001:db8:cafe::17]:4711\""); verify(filterChain).doFilter(request, response); }
@Test public void testFilter_ProtoAtStart() throws Exception { testHeaderForwardedValue("proto=http; by=203.0.113.43;for=192.0.2.60;"); verify(filterChain).doFilter(request, response); }
@Test public void testFilter_ProtoAtStartWithSpace() throws Exception { testHeaderForwardedValue(" proto=https; by=203.0.113.43;for=192.0.2.60;"); verify(filterChain).doFilter(request, response); }