Skip to content

Commit 8741ce0

Browse files
committed
Fix #33066: disable Saml2LogoutRequestFilter
1 parent b2f7d65 commit 8741ce0

2 files changed

Lines changed: 83 additions & 7 deletions

File tree

src/main/java/eu/openanalytics/containerproxy/auth/impl/SAMLAuthenticationBackend.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import eu.openanalytics.containerproxy.auth.IAuthenticationBackend;
2424
import eu.openanalytics.containerproxy.auth.impl.saml.AuthenticationFailureHandler;
25+
import eu.openanalytics.containerproxy.auth.impl.saml.DisableSaml2LogoutRequestFilterFilter;
2526
import eu.openanalytics.containerproxy.auth.impl.saml.Saml2MetadataFilter;
2627
import eu.openanalytics.containerproxy.util.ContextPathHelper;
2728
import jakarta.servlet.http.HttpServletRequest;
@@ -40,13 +41,15 @@
4041
import org.springframework.security.saml2.provider.service.metadata.OpenSamlMetadataResolver;
4142
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository;
4243
import org.springframework.security.saml2.provider.service.web.authentication.Saml2WebSsoAuthenticationFilter;
44+
import org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutRequestFilter;
4345
import org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutRequestResolver;
4446
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
4547
import org.springframework.security.web.authentication.logout.LogoutFilter;
4648
import org.springframework.security.web.util.matcher.AndRequestMatcher;
4749
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
4850
import org.springframework.security.web.util.matcher.RequestMatcher;
4951
import org.springframework.stereotype.Component;
52+
import org.springframework.web.filter.CorsFilter;
5053

5154
import javax.inject.Inject;
5255

@@ -117,11 +120,12 @@ public void configureHttpSecurity(HttpSecurity http) throws Exception {
117120
.authenticationManager(new ProviderManager(samlAuthenticationProvider))
118121
.failureHandler(failureHandler)
119122
.successHandler(successHandler))
120-
.saml2Logout(saml -> saml
121-
.logoutUrl(SAML_LOGOUT_SERVICE_LOCATION_PATH)
122-
.logoutResponse(r -> r.logoutUrl(SAML_LOGOUT_SERVICE_RESPONSE_LOCATION_PATH))
123-
.logoutRequest(r -> r.logoutRequestResolver(saml2LogoutRequestResolver))
124-
.addObjectPostProcessor(
123+
.saml2Logout(saml -> {
124+
saml.logoutUrl(SAML_LOGOUT_SERVICE_LOCATION_PATH)
125+
.logoutResponse(r -> r.logoutUrl(SAML_LOGOUT_SERVICE_RESPONSE_LOCATION_PATH))
126+
.logoutRequest(r -> r.logoutRequestResolver(saml2LogoutRequestResolver));
127+
128+
saml.addObjectPostProcessor(
125129
new ObjectPostProcessor<LogoutFilter>() {
126130
@Override
127131
public <O extends LogoutFilter> O postProcess(O object) {
@@ -132,8 +136,21 @@ public <O extends LogoutFilter> O postProcess(O object) {
132136
return object;
133137
}
134138
}
135-
))
136-
.addFilterBefore(metadataFilter, Saml2WebSsoAuthenticationFilter.class);
139+
);
140+
141+
saml.addObjectPostProcessor(
142+
new ObjectPostProcessor<Saml2LogoutRequestFilter>() {
143+
@Override
144+
public <O extends Saml2LogoutRequestFilter> O postProcess(O object) {
145+
// override the name of the filter, so it can be used in DisableSaml2LogoutRequestFilterFilter
146+
// See #33066.
147+
object.setBeanName("Saml2LogoutRequestFilter");
148+
return object;
149+
}
150+
});
151+
})
152+
.addFilterBefore(metadataFilter, Saml2WebSsoAuthenticationFilter.class)
153+
.addFilterAfter(new DisableSaml2LogoutRequestFilterFilter(), CorsFilter.class);
137154
}
138155

139156
@Override
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* ContainerProxy
3+
*
4+
* Copyright (C) 2016-2024 Open Analytics
5+
*
6+
* ===========================================================================
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the Apache License as published by
10+
* The Apache Software Foundation, either version 2 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* Apache License for more details.
17+
*
18+
* You should have received a copy of the Apache License
19+
* along with this program. If not, see <http://www.apache.org/licenses/>
20+
*/
21+
package eu.openanalytics.containerproxy.auth.impl.saml;
22+
23+
import eu.openanalytics.containerproxy.auth.impl.SAMLAuthenticationBackend;
24+
import jakarta.servlet.FilterChain;
25+
import jakarta.servlet.ServletException;
26+
import jakarta.servlet.ServletRequest;
27+
import jakarta.servlet.ServletResponse;
28+
import jakarta.servlet.http.HttpServletRequest;
29+
import org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutRequestFilter;
30+
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
31+
import org.springframework.security.web.util.matcher.OrRequestMatcher;
32+
import org.springframework.security.web.util.matcher.RequestMatcher;
33+
import org.springframework.web.filter.GenericFilterBean;
34+
35+
import java.io.IOException;
36+
37+
/**
38+
* Filter that disables the {@link Saml2LogoutRequestFilter} filter, except for the SAML single logout endpoint.
39+
* The SAML filter calls getParameter and thefore consumes the POST body.
40+
* The name of {@link Saml2LogoutRequestFilter must be fixed in order for this to work (see {@link SAMLAuthenticationBackend}
41+
* See #33066.
42+
*/
43+
public class DisableSaml2LogoutRequestFilterFilter extends GenericFilterBean {
44+
45+
private static final RequestMatcher REQUEST_MATCHER = new OrRequestMatcher(
46+
new AntPathRequestMatcher("/logout/saml2/slo"),
47+
new AntPathRequestMatcher("/logout/saml2/slo/*")
48+
);
49+
50+
@Override
51+
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
52+
if (!REQUEST_MATCHER.matches((HttpServletRequest) request)) {
53+
// set the filtered as already being executed, this is the only way to disable the filter
54+
request.setAttribute("Saml2LogoutRequestFilter.FILTERED", true);
55+
}
56+
chain.doFilter(request, response);
57+
}
58+
59+
}

0 commit comments

Comments
 (0)