Skip to content

Fix ChannelProcessingFilter to skip decision if response already committed #17668

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 @@ -83,6 +83,7 @@
* over HTTPS.
*
* @author Ben Alex
* @author Andrey Litvitski
* @deprecated see {@link org.springframework.security.web.transport.HttpsRedirectFilter}
*/
@Deprecated
Expand Down Expand Up @@ -125,10 +126,12 @@ public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
HttpServletResponse response = (HttpServletResponse) res;
FilterInvocation filterInvocation = new FilterInvocation(request, response, chain);
Collection<ConfigAttribute> attributes = this.securityMetadataSource.getAttributes(filterInvocation);
if (attributes != null) {
boolean committedBefore = filterInvocation.getResponse().isCommitted();
if (attributes != null && !committedBefore) {
this.logger.debug(LogMessage.format("Request: %s; ConfigAttributes: %s", filterInvocation, attributes));
this.channelDecisionManager.decide(filterInvocation, attributes);
if (filterInvocation.getResponse().isCommitted()) {
boolean committedAfter = filterInvocation.getResponse().isCommitted();
if (committedAfter) {
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.springframework.security.web.servlet.TestMockHttpServletRequests.get;

/**
* Tests {@link ChannelProcessingFilter}.
*
* @author Ben Alex
* @author Andrey Litvitski
*/
public class ChannelProcessingFilterTests {

Expand Down Expand Up @@ -123,6 +125,21 @@ public void testGetterSetters() {
filter.afterPropertiesSet();
}

@Test
public void testDoFilterWhenResponseAlreadyCommittedBeforeFilter() throws Exception {
ChannelProcessingFilter filter = new ChannelProcessingFilter();
filter.setChannelDecisionManager(new MockChannelDecisionManager(false, "MOCK"));
MockFilterInvocationDefinitionMap fids = new MockFilterInvocationDefinitionMap("/path", true, "MOCK");
filter.setSecurityMetadataSource(fids);
MockHttpServletRequest request = get("/path").build();
MockHttpServletResponse response = new MockHttpServletResponse();
// this will set response.isCommitted() == true
response.flushBuffer();
FilterChain chain = mock(FilterChain.class);
filter.doFilter(request, response, chain);
verify(chain).doFilter(request, response);
}

private class MockChannelDecisionManager implements ChannelDecisionManager {

private String supportAttribute;
Expand Down