Skip to content

Commit 237439e

Browse files
committed
Whitelist extension if present in the request mapping
We know skip the Content-Disposition header for any extension if the chosen request mapping explicitly contains the URl extension. Issue: SPR-13629
1 parent 8893663 commit 237439e

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodProcessor.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,12 @@ private boolean safeExtension(HttpServletRequest request, String extension) {
375375
if (this.safeExtensions.contains(extension)) {
376376
return true;
377377
}
378+
String pattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
379+
if (pattern != null && pattern.endsWith("." + extension)) {
380+
return true;
381+
}
378382
if (extension.equals("html")) {
379-
String name = HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE;
380-
String pattern = (String) request.getAttribute(name);
381-
if (pattern != null && pattern.endsWith(".html")) {
382-
return true;
383-
}
384-
name = HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE;
383+
String name = HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE;
385384
Set<MediaType> mediaTypes = (Set<MediaType>) request.getAttribute(name);
386385
if (!CollectionUtils.isEmpty(mediaTypes) && mediaTypes.contains(MediaType.TEXT_HTML)) {
387386
return true;

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletAnnotationControllerHandlerMethodTests.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,6 +1711,32 @@ public void initialize(GenericWebApplicationContext wac) {
17111711
assertArrayEquals(content, response.getContentAsByteArray());
17121712
}
17131713

1714+
@Test
1715+
public void responseBodyAsTextWithCssExtension() throws Exception {
1716+
initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
1717+
@Override
1718+
public void initialize(GenericWebApplicationContext wac) {
1719+
ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
1720+
factoryBean.afterPropertiesSet();
1721+
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
1722+
adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
1723+
wac.registerBeanDefinition("handlerAdapter", adapterDef);
1724+
}
1725+
}, TextRestController.class);
1726+
1727+
byte[] content = "body".getBytes(Charset.forName("ISO-8859-1"));
1728+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a4.css");
1729+
request.setContent(content);
1730+
MockHttpServletResponse response = new MockHttpServletResponse();
1731+
1732+
getServlet().service(request, response);
1733+
1734+
assertEquals(200, response.getStatus());
1735+
assertEquals("text/css", response.getContentType());
1736+
assertNull(response.getHeader("Content-Disposition"));
1737+
assertArrayEquals(content, response.getContentAsByteArray());
1738+
}
1739+
17141740
/*
17151741
* Controllers
17161742
*/
@@ -3187,6 +3213,11 @@ public String a2(@RequestBody String body) {
31873213
public String a3(@RequestBody String body) throws IOException {
31883214
return body;
31893215
}
3216+
3217+
@RequestMapping(path = "/a4.css", method = RequestMethod.GET)
3218+
public String a4(@RequestBody String body) {
3219+
return body;
3220+
}
31903221
}
31913222

31923223

0 commit comments

Comments
 (0)