Skip to content

Commit 124574e

Browse files
author
Dave Syer
committed
Add mediaTypes (extension to media type mapping) in MVC resources
Allows users to configure "allowed" file extensions for controller mappings, so that browsers will not switch to downloading "f.txt" (part of the recent RFD attack fixes in Spring MVC). See gh-4220
1 parent 2118242 commit 124574e

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919
import org.springframework.boot.context.properties.ConfigurationProperties;
2020

2121
/**
22-
* Properties used to configure resource handling.
23-
*
24-
* @author Phillip Webb
25-
* @since 1.1.0
22+
* Properties used to configure resource handling0
2623
*/
2724
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
2825
public class ResourceProperties {

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Collections;
2323
import java.util.Date;
2424
import java.util.List;
25+
import java.util.Map;
2526

2627
import javax.servlet.Servlet;
2728

@@ -56,6 +57,7 @@
5657
import org.springframework.format.Formatter;
5758
import org.springframework.format.FormatterRegistry;
5859
import org.springframework.format.datetime.DateFormatter;
60+
import org.springframework.http.MediaType;
5961
import org.springframework.http.converter.HttpMessageConverter;
6062
import org.springframework.util.StringUtils;
6163
import org.springframework.validation.DefaultMessageCodesResolver;
@@ -67,6 +69,7 @@
6769
import org.springframework.web.servlet.LocaleResolver;
6870
import org.springframework.web.servlet.View;
6971
import org.springframework.web.servlet.ViewResolver;
72+
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
7073
import org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration;
7174
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
7275
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
@@ -169,6 +172,14 @@ public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
169172
converters.addAll(this.messageConverters.getConverters());
170173
}
171174

175+
@Override
176+
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
177+
Map<String, MediaType> mediaTypes = this.mvcProperties.getMediaTypes();
178+
for (String extension : mediaTypes.keySet()) {
179+
configurer.mediaType(extension, mediaTypes.get(extension));
180+
}
181+
}
182+
172183
@Bean
173184
@ConditionalOnMissingBean(InternalResourceViewResolver.class)
174185
public InternalResourceViewResolver defaultViewResolver() {

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcProperties.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
package org.springframework.boot.autoconfigure.web;
1818

19+
import java.util.LinkedHashMap;
20+
import java.util.Map;
21+
1922
import org.springframework.boot.context.properties.ConfigurationProperties;
23+
import org.springframework.http.MediaType;
2024
import org.springframework.validation.DefaultMessageCodesResolver;
2125

2226
/**
@@ -49,6 +53,11 @@ public class WebMvcProperties {
4953
*/
5054
private boolean ignoreDefaultModelOnRedirect = true;
5155

56+
/**
57+
* Maps file extensions to media types for content negotiation, e.g. yml->text/yaml.
58+
*/
59+
private Map<String, MediaType> mediaTypes = new LinkedHashMap<String, MediaType>();
60+
5261
public DefaultMessageCodesResolver.Format getMessageCodesResolverFormat() {
5362
return this.messageCodesResolverFormat;
5463
}
@@ -82,4 +91,12 @@ public void setIgnoreDefaultModelOnRedirect(boolean ignoreDefaultModelOnRedirect
8291
this.ignoreDefaultModelOnRedirect = ignoreDefaultModelOnRedirect;
8392
}
8493

94+
public Map<String, MediaType> getMediaTypes() {
95+
return this.mediaTypes;
96+
}
97+
98+
public void setMediaTypes(Map<String, MediaType> mediaTypes) {
99+
this.mediaTypes = mediaTypes;
100+
}
101+
85102
}

0 commit comments

Comments
 (0)