Skip to content

Prevent caching of non-document requests in HttpSessionRequestCache #17687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.security.web.savedrequest;

import jakarta.servlet.DispatcherType;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
Expand All @@ -38,6 +39,7 @@
*
* @author Luke Taylor
* @author Eddú Meléndez
* @author Andrey Litvitski
* @since 3.0
*/
public class HttpSessionRequestCache implements RequestCache {
Expand All @@ -61,6 +63,17 @@ public class HttpSessionRequestCache implements RequestCache {
*/
@Override
public void saveRequest(HttpServletRequest request, HttpServletResponse response) {
boolean documentRequest = "document".equals(request.getHeader("Sec-Fetch-Dest"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take a look at RequestCacheConfigurer, which constructs a request matcher that skips /favicon.* requests. If that is in place and you are still seeing this problem, then that's where the fix should go.

Let's first see if we can figure out why the existing request matcher provided in RequestCacheConfigurer is not sufficient. It may be that the best fix is to check this header and the dispatcher type; however, since there is already logic for /favicon.ico in place, I'm curious to first establish why that isn't working here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also try and keep the fix as small as possible to make it more likely to backport to earlier versions of Spring Security that may also have the bug. Adding support for request dispatcher type and the Sec-Fetch-Dest headers are good ideas, but they are more of an improvement and will likely not be backported.

boolean dispatchError = DispatcherType.ERROR.equals(request.getDispatcherType());

if (!documentRequest && dispatchError) {
if (this.logger.isTraceEnabled()) {
this.logger
.trace("Did not save request because it is an ERROR dispatcher and not a primary document request");
}
return;
}

if (!this.requestMatcher.matches(request)) {
if (this.logger.isTraceEnabled()) {
this.logger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Locale;
import java.util.Map;

import jakarta.servlet.DispatcherType;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand All @@ -40,6 +41,7 @@
/**
* @author Luke Taylor
* @author Eddú Meléndez
* @author Andrey Litvitski
* @since 3.0
*/
public class HttpSessionRequestCacheTests {
Expand Down Expand Up @@ -168,6 +170,18 @@ public void getMatchingRequestWhenMatchingRequestParameterNameSetThenDoesNotInvo
verify(request, never()).getParameterMap();
}

// gh-17686
@Test
public void saveRequestShouldNotSaveRequestWhenErrorDispatcherAndNonDocumentRequest() {
HttpSessionRequestCache cache = new HttpSessionRequestCache();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/destination");
request.setDispatcherType(DispatcherType.ERROR);
request.addHeader("Sec-Fetch-Dest", "image");
MockHttpServletResponse response = new MockHttpServletResponse();
cache.saveRequest(request, response);
assertThat(request.getSession().getAttribute(HttpSessionRequestCache.SAVED_REQUEST)).isNull();
}

private static final class CustomSavedRequest implements SavedRequest {

private final SavedRequest delegate;
Expand Down