Skip to content

Commit f2694a8

Browse files
committed
Restrict HTTP methods on Servlet HiddenHttpMethodFilter
This commit restricts the allowed HTTP methods on HiddenHttpMethodFilter (Servlet variant) to the following: PUT, DELETE, PATCH. This filter is meant to be used to simulate those methods from HTML forms sent by browsers, so no other methods are allowed. Issue: SPR-16836 (Cherry-picked from f64fa3d)
1 parent 82f421b commit f2694a8

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

spring-web/src/main/java/org/springframework/web/filter/HiddenHttpMethodFilter.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,13 +17,17 @@
1717
package org.springframework.web.filter;
1818

1919
import java.io.IOException;
20+
import java.util.Arrays;
21+
import java.util.Collections;
22+
import java.util.List;
2023
import java.util.Locale;
2124
import javax.servlet.FilterChain;
2225
import javax.servlet.ServletException;
2326
import javax.servlet.http.HttpServletRequest;
2427
import javax.servlet.http.HttpServletRequestWrapper;
2528
import javax.servlet.http.HttpServletResponse;
2629

30+
import org.springframework.http.HttpMethod;
2731
import org.springframework.util.Assert;
2832
import org.springframework.util.StringUtils;
2933
import org.springframework.web.util.WebUtils;
@@ -35,6 +39,7 @@
3539
* is to use a normal POST with an additional hidden form field ({@code _method})
3640
* to pass the "real" HTTP method along. This filter reads that parameter and changes
3741
* the {@link HttpServletRequestWrapper#getMethod()} return value accordingly.
42+
* Only {@code "PUT"}, {@code "DELETE"} and {@code "PATCH"} HTTP methods are allowed.
3843
*
3944
* <p>The name of the request parameter defaults to {@code _method}, but can be
4045
* adapted via the {@link #setMethodParam(String) methodParam} property.
@@ -50,6 +55,10 @@
5055
*/
5156
public class HiddenHttpMethodFilter extends OncePerRequestFilter {
5257

58+
private static final List<String> ALLOWED_METHODS =
59+
Collections.unmodifiableList(Arrays.asList(HttpMethod.PUT.name(),
60+
HttpMethod.DELETE.name(), HttpMethod.PATCH.name()));
61+
5362
/** Default method parameter: {@code _method} */
5463
public static final String DEFAULT_METHOD_PARAM = "_method";
5564

@@ -74,7 +83,10 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
7483
if ("POST".equals(request.getMethod()) && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) {
7584
String paramValue = request.getParameter(this.methodParam);
7685
if (StringUtils.hasLength(paramValue)) {
77-
requestToUse = new HttpMethodRequestWrapper(request, paramValue);
86+
String method = paramValue.toUpperCase(Locale.ENGLISH);
87+
if (ALLOWED_METHODS.contains(method)) {
88+
requestToUse = new HttpMethodRequestWrapper(request, method);
89+
}
7890
}
7991
}
8092

@@ -92,7 +104,7 @@ private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper
92104

93105
public HttpMethodRequestWrapper(HttpServletRequest request, String method) {
94106
super(request);
95-
this.method = method.toUpperCase(Locale.ENGLISH);
107+
this.method = method;
96108
}
97109

98110
@Override

spring-web/src/test/java/org/springframework/web/filter/HiddenHttpMethodFilterTests.java

+23-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,45 +31,52 @@
3131
import static org.junit.Assert.*;
3232

3333
/**
34+
* Tests for {@link HiddenHttpMethodFilter}.
35+
*
3436
* @author Arjen Poutsma
37+
* @author Brian Clozel
3538
*/
3639
public class HiddenHttpMethodFilterTests {
3740

3841
private final HiddenHttpMethodFilter filter = new HiddenHttpMethodFilter();
3942

4043
@Test
4144
public void filterWithParameter() throws IOException, ServletException {
42-
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
43-
request.addParameter("_method", "delete");
44-
MockHttpServletResponse response = new MockHttpServletResponse();
45-
46-
FilterChain filterChain = new FilterChain() {
45+
filterWithParameterForMethod("delete", "DELETE");
46+
filterWithParameterForMethod("put", "PUT");
47+
filterWithParameterForMethod("patch", "PATCH");
48+
}
4749

48-
@Override
49-
public void doFilter(ServletRequest filterRequest,
50-
ServletResponse filterResponse) throws IOException, ServletException {
51-
assertEquals("Invalid method", "DELETE",
52-
((HttpServletRequest) filterRequest).getMethod());
53-
}
54-
};
55-
filter.doFilter(request, response, filterChain);
50+
@Test
51+
public void filterWithParameterDisallowedMethods() throws IOException, ServletException {
52+
filterWithParameterForMethod("trace", "POST");
53+
filterWithParameterForMethod("head", "POST");
54+
filterWithParameterForMethod("options", "POST");
5655
}
5756

5857
@Test
5958
public void filterWithNoParameter() throws IOException, ServletException {
59+
filterWithParameterForMethod(null, "POST");
60+
}
61+
62+
private void filterWithParameterForMethod(String methodParam, String expectedMethod)
63+
throws IOException, ServletException {
6064
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
65+
if(methodParam != null) {
66+
request.addParameter("_method", methodParam);
67+
}
6168
MockHttpServletResponse response = new MockHttpServletResponse();
6269

6370
FilterChain filterChain = new FilterChain() {
6471

6572
@Override
6673
public void doFilter(ServletRequest filterRequest,
6774
ServletResponse filterResponse) throws IOException, ServletException {
68-
assertEquals("Invalid method", "POST",
75+
assertEquals("Invalid method", expectedMethod,
6976
((HttpServletRequest) filterRequest).getMethod());
7077
}
7178
};
72-
filter.doFilter(request, response, filterChain);
79+
this.filter.doFilter(request, response, filterChain);
7380
}
7481

7582
}

0 commit comments

Comments
 (0)