Merge branch '6.1.x'

This commit is contained in:
Sébastien Deleuze 2024-09-04 16:13:04 +02:00
commit 2f56a59ba0
3 changed files with 25 additions and 2 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -53,6 +53,7 @@ import org.springframework.web.util.UriComponents;
* in which case it removes but does not use the headers.
*
* @author Rossen Stoyanchev
* @author Sebastien Deleuze
* @since 5.1
* @see <a href="https://tools.ietf.org/html/rfc7239">https://tools.ietf.org/html/rfc7239</a>
* @see <a href="https://docs.spring.io/spring-framework/reference/web/webflux/reactive-spring.html#webflux-forwarded-headers">Forwarded Headers</a>
@ -165,7 +166,7 @@ public class ForwardedHeaderTransformer implements Function<ServerHttpRequest, S
String[] rawPrefixes = StringUtils.tokenizeToStringArray(header, ",");
for (String rawPrefix : rawPrefixes) {
int endIndex = rawPrefix.length();
while (endIndex > 1 && rawPrefix.charAt(endIndex - 1) == '/') {
while (endIndex > 0 && rawPrefix.charAt(endIndex - 1) == '/') {
endIndex--;
}
prefix.append((endIndex != rawPrefix.length() ? rawPrefix.substring(0, endIndex) : rawPrefix));

View File

@ -48,6 +48,7 @@ import static org.mockito.Mockito.mock;
* @author Eddú Meléndez
* @author Rob Winch
* @author Brian Clozel
* @author Sebastien Deleuze
*/
class ForwardedHeaderFilterTests {
@ -442,6 +443,15 @@ class ForwardedHeaderFilterTests {
assertThat(actual.getRequestURL().toString()).isEqualTo("http://localhost/first/second/mvc-showcase");
}
@Test
void shouldRemoveSingleTrailingSlash() throws Exception {
request.addHeader(X_FORWARDED_PREFIX, "/prefix,/");
request.setRequestURI("/mvc-showcase");
HttpServletRequest actual = filterAndGetWrappedRequest();
assertThat(actual.getRequestURL().toString()).isEqualTo("http://localhost/prefix/mvc-showcase");
}
@Test
void requestURLNewStringBuffer() throws Exception {
request.addHeader(X_FORWARDED_PREFIX, "/prefix/");

View File

@ -32,6 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link ForwardedHeaderTransformer}.
*
* @author Rossen Stoyanchev
* @author Sebastien Deleuze
*/
class ForwardedHeaderTransformerTests {
@ -170,6 +171,17 @@ class ForwardedHeaderTransformerTests {
assertForwardedHeadersRemoved(request);
}
@Test // gh-33465
void shouldRemoveSingleTrailingSlash() {
HttpHeaders headers = new HttpHeaders();
headers.add("X-Forwarded-Prefix", "/prefix,/");
ServerHttpRequest request = this.requestMutator.apply(getRequest(headers));
assertThat(request.getURI()).isEqualTo(URI.create("https://example.com/prefix/path"));
assertThat(request.getPath().value()).isEqualTo("/prefix/path");
assertForwardedHeadersRemoved(request);
}
@Test
void forwardedForNotPresent() {
HttpHeaders headers = new HttpHeaders();