Skip to content

Commit 5114190

Browse files
committed
Fix WebClient Memory Leaks
WebClient exchange requires that the body is consumed. Before this commit there were places where an Exception was thrown without consuming the body if the status was not successful. There was also the potential for the statusCode invocation to throw an Exception of the status code was not defined which would cause a leak. This commit ensures that before the Exception is thrown the body is consumed. It also uses the http status in a way that will ensure an Exception is not thrown. Fixes gh-7293
1 parent 93d1c7f commit 5114190

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/endpoint/WebClientReactiveClientCredentialsTokenResponseClient.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
*/
1616
package org.springframework.security.oauth2.client.endpoint;
1717

18+
import org.springframework.core.io.buffer.DataBuffer;
19+
import org.springframework.core.io.buffer.DataBufferUtils;
1820
import org.springframework.http.HttpHeaders;
21+
import org.springframework.http.HttpStatus;
1922
import org.springframework.http.MediaType;
2023
import org.springframework.security.oauth2.client.registration.ClientRegistration;
2124
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
@@ -65,15 +68,18 @@ public Mono<OAuth2AccessTokenResponse> getTokenResponse(OAuth2ClientCredentialsG
6568
.headers(headers(clientRegistration))
6669
.body(body)
6770
.exchange()
68-
.flatMap(response ->{
69-
if (!response.statusCode().is2xxSuccessful()){
71+
.flatMap(response -> {
72+
HttpStatus status = HttpStatus.resolve(response.rawStatusCode());
73+
if (status == null || !status.is2xxSuccessful()) {
7074
// extract the contents of this into a method named oauth2AccessTokenResponse but has an argument for the response
71-
throw WebClientResponseException.create(response.rawStatusCode(),
75+
return response.bodyToFlux(DataBuffer.class)
76+
.map(DataBufferUtils::release)
77+
.then(Mono.error(WebClientResponseException.create(response.rawStatusCode(),
7278
"Cannot get token, expected 2xx HTTP Status code",
7379
null,
7480
null,
7581
null
76-
);
82+
)));
7783
}
7884
return response.body(oauth2AccessTokenResponse()); })
7985
.map(response -> {

0 commit comments

Comments
 (0)