Skip to content

Commit a879897

Browse files
committed
Skip Content-Disposition header if status != 2xx
Issue: SPR-13588
1 parent 0016752 commit a879897

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,12 @@ private MediaType getMostSpecificMediaType(MediaType acceptType, MediaType produ
252252
}
253253

254254
/**
255-
* Check if the path has a file extension and whether the extension is either
256-
* {@link #WHITELISTED_EXTENSIONS whitelisted} or
257-
* {@link ContentNegotiationManager#getAllFileExtensions() explicitly
258-
* registered}. If not add a 'Content-Disposition' header with a safe
259-
* attachment file name ("f.txt") to prevent RFD exploits.
255+
* Check if the path has a file extension and whether the extension is
256+
* either {@link #WHITELISTED_EXTENSIONS whitelisted} or explicitly
257+
* {@link ContentNegotiationManager#getAllFileExtensions() registered}.
258+
* If not, and the status is in the 2xx range, a 'Content-Disposition'
259+
* header with a safe attachment file name ("f.txt") is added to prevent
260+
* RFD exploits.
260261
*/
261262
private void addContentDispositionHeader(ServletServerHttpRequest request,
262263
ServletServerHttpResponse response) {
@@ -266,6 +267,16 @@ private void addContentDispositionHeader(ServletServerHttpRequest request,
266267
return;
267268
}
268269

270+
try {
271+
int status = response.getServletResponse().getStatus();
272+
if (status < 200 || status > 299) {
273+
return;
274+
}
275+
}
276+
catch (Throwable ex) {
277+
// Ignore
278+
}
279+
269280
HttpServletRequest servletRequest = request.getServletRequest();
270281
String requestUri = RAW_URL_PATH_HELPER.getOriginatingRequestUri(servletRequest);
271282

0 commit comments

Comments
 (0)