Skip to content

Commit 050e79e

Browse files
committed
Skip Content-Disposition header if status != 2xx
Issue: SPR-13588
1 parent 994a11d commit 050e79e

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
@@ -315,11 +315,12 @@ private MediaType getMostSpecificMediaType(MediaType acceptType, MediaType produ
315315
}
316316

317317
/**
318-
* Check if the path has a file extension and whether the extension is either
319-
* {@link #WHITELISTED_EXTENSIONS whitelisted} or
320-
* {@link ContentNegotiationManager#getAllFileExtensions() explicitly
321-
* registered}. If not add a 'Content-Disposition' header with a safe
322-
* attachment file name ("f.txt") to prevent RFD exploits.
318+
* Check if the path has a file extension and whether the extension is
319+
* either {@link #WHITELISTED_EXTENSIONS whitelisted} or explicitly
320+
* {@link ContentNegotiationManager#getAllFileExtensions() registered}.
321+
* If not, and the status is in the 2xx range, a 'Content-Disposition'
322+
* header with a safe attachment file name ("f.txt") is added to prevent
323+
* RFD exploits.
323324
*/
324325
private void addContentDispositionHeader(ServletServerHttpRequest request,
325326
ServletServerHttpResponse response) {
@@ -329,6 +330,16 @@ private void addContentDispositionHeader(ServletServerHttpRequest request,
329330
return;
330331
}
331332

333+
try {
334+
int status = response.getServletResponse().getStatus();
335+
if (status < 200 || status > 299) {
336+
return;
337+
}
338+
}
339+
catch (Throwable ex) {
340+
// Ignore
341+
}
342+
332343
HttpServletRequest servletRequest = request.getServletRequest();
333344
String requestUri = RAW_URL_PATH_HELPER.getOriginatingRequestUri(servletRequest);
334345

0 commit comments

Comments
 (0)