Skip to content

Commit 8953f46

Browse files
ngocnhan-tran1996jzheaux
authored andcommitted
Add Switch for Processing GET Requests
Closes gh-17099 Signed-off-by: Tran Ngoc Nhan <[email protected]>
1 parent 9654e51 commit 8953f46

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/web/Saml2AuthenticationTokenConverter.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,6 +43,8 @@ public final class Saml2AuthenticationTokenConverter implements AuthenticationCo
4343

4444
private Saml2AuthenticationRequestRepository<AbstractSaml2AuthenticationRequest> authenticationRequestRepository;
4545

46+
private boolean shouldConvertGetRequests = true;
47+
4648
/**
4749
* Constructs a {@link Saml2AuthenticationTokenConverter} given a strategy for
4850
* resolving {@link RelyingPartyRegistration}s
@@ -86,16 +88,27 @@ public void setAuthenticationRequestRepository(
8688
this.authenticationRequestRepository = authenticationRequestRepository;
8789
}
8890

91+
/**
92+
* Use the given {@code shouldConvertGetRequests} to convert {@code GET} requests.
93+
* Default is {@code true}.
94+
* @param shouldConvertGetRequests the {@code shouldConvertGetRequests} to use
95+
* @since 7.0
96+
*/
97+
public void setShouldConvertGetRequests(boolean shouldConvertGetRequests) {
98+
this.shouldConvertGetRequests = shouldConvertGetRequests;
99+
}
100+
89101
private String decode(HttpServletRequest request) {
90102
String encoded = request.getParameter(Saml2ParameterNames.SAML_RESPONSE);
91103
if (encoded == null) {
92104
return null;
93105
}
106+
boolean isGet = HttpMethod.GET.matches(request.getMethod());
107+
if (!this.shouldConvertGetRequests && isGet) {
108+
return null;
109+
}
94110
try {
95-
return Saml2Utils.withEncoded(encoded)
96-
.requireBase64(true)
97-
.inflate(HttpMethod.GET.matches(request.getMethod()))
98-
.decode();
111+
return Saml2Utils.withEncoded(encoded).requireBase64(true).inflate(isGet).decode();
99112
}
100113
catch (Exception ex) {
101114
throw new Saml2AuthenticationException(new Saml2Error(Saml2ErrorCodes.INVALID_RESPONSE, ex.getMessage()),

saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/provider/service/web/Saml2AuthenticationTokenConverterTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -230,6 +230,21 @@ public void setAuthenticationRequestRepositoryWhenNullThenIllegalArgument() {
230230
.isThrownBy(() -> converter.setAuthenticationRequestRepository(null));
231231
}
232232

233+
@Test
234+
public void shouldNotConvertGetRequests() {
235+
Saml2AuthenticationTokenConverter converter = new Saml2AuthenticationTokenConverter(
236+
this.relyingPartyRegistrationResolver);
237+
converter.setShouldConvertGetRequests(false);
238+
given(this.relyingPartyRegistrationResolver.resolve(any(HttpServletRequest.class), any()))
239+
.willReturn(this.relyingPartyRegistration);
240+
MockHttpServletRequest request = new MockHttpServletRequest();
241+
request.setMethod("GET");
242+
request.setParameter(Saml2ParameterNames.SAML_RESPONSE,
243+
Saml2Utils.samlEncode("response".getBytes(StandardCharsets.UTF_8)));
244+
Saml2AuthenticationToken token = converter.convert(request);
245+
assertThat(token).isNull();
246+
}
247+
233248
private void validateSsoCircleXml(String xml) {
234249
assertThat(xml).contains("InResponseTo=\"ARQ9a73ead-7dcf-45a8-89eb-26f3c9900c36\"")
235250
.contains(" ID=\"s246d157446618e90e43fb79bdd4d9e9e19cf2c7c4\"")

0 commit comments

Comments
 (0)