*/
public class SecurityServletFilter implements Filter {
- private static final Set<String> ALLOWED_HTTP_METHODS = ImmutableSet.of("DELETE", "GET", "POST", "PUT");
+ private static final Set<String> ALLOWED_HTTP_METHODS = ImmutableSet.of("DELETE", "GET", "HEAD", "POST", "PUT");
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
private static void doHttpFilter(HttpServletRequest httpRequest, HttpServletResponse httpResponse, FilterChain chain) throws IOException, ServletException {
- // SONAR-6881 Disable OPTIONS, HEAD and TRACE methods
+ // SONAR-6881 Disable OPTIONS and TRACE methods
if (!ALLOWED_HTTP_METHODS.contains(httpRequest.getMethod())) {
httpResponse.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
return;
FilterChain chain = mock(FilterChain.class);
@Test
- public void accept_GET_method() throws IOException, ServletException {
- HttpServletRequest request = newRequest("GET");
- underTest.doFilter(request, response, chain);
- verify(response, never()).setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
- verify(chain).doFilter(request, response);
+ public void allow_GET_method() throws IOException, ServletException {
+ assertThatMethodIsAllowed("GET");
}
@Test
- public void deny_HEAD_method() throws IOException, ServletException {
- underTest.doFilter(newRequest("HEAD"), response, chain);
- verify(response).setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
+ public void allow_HEAD_method() throws IOException, ServletException {
+ assertThatMethodIsAllowed("HEAD");
+ }
+
+ @Test
+ public void allow_PUT_method() throws IOException, ServletException {
+ assertThatMethodIsAllowed("PUT");
+ }
+
+ @Test
+ public void allow_POST_method() throws IOException, ServletException {
+ assertThatMethodIsAllowed("POST");
+ }
+
+ private void assertThatMethodIsAllowed(String httpMethod) throws IOException, ServletException {
+ HttpServletRequest request = newRequest(httpMethod);
+ underTest.doFilter(request, response, chain);
+ verify(response, never()).setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
+ verify(chain).doFilter(request, response);
}
@Test
public void deny_OPTIONS_method() throws IOException, ServletException {
- underTest.doFilter(newRequest("OPTIONS"), response, chain);
- verify(response).setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
+ assertThatMethodIsDenied("OPTIONS");
}
@Test
public void deny_TRACE_method() throws IOException, ServletException {
- underTest.doFilter(newRequest("TRACE"), response, chain);
+ assertThatMethodIsDenied("TRACE");
+ }
+
+ private void assertThatMethodIsDenied(String httpMethod) throws IOException, ServletException {
+ underTest.doFilter(newRequest(httpMethod), response, chain);
verify(response).setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
}