File tree 3 files changed +25
-2
lines changed
main/java/org/springframework/web/server/adapter
test/java/org/springframework/web
3 files changed +25
-2
lines changed Original file line number Diff line number Diff line change 1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
53
53
* in which case it removes but does not use the headers.
54
54
*
55
55
* @author Rossen Stoyanchev
56
+ * @author Sebastien Deleuze
56
57
* @since 5.1
57
58
* @see <a href="https://tools.ietf.org/html/rfc7239">https://tools.ietf.org/html/rfc7239</a>
58
59
* @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 @@ private static String getForwardedPrefix(ServerHttpRequest request) {
165
166
String [] rawPrefixes = StringUtils .tokenizeToStringArray (header , "," );
166
167
for (String rawPrefix : rawPrefixes ) {
167
168
int endIndex = rawPrefix .length ();
168
- while (endIndex > 1 && rawPrefix .charAt (endIndex - 1 ) == '/' ) {
169
+ while (endIndex > 0 && rawPrefix .charAt (endIndex - 1 ) == '/' ) {
169
170
endIndex --;
170
171
}
171
172
prefix .append ((endIndex != rawPrefix .length () ? rawPrefix .substring (0 , endIndex ) : rawPrefix ));
Original file line number Diff line number Diff line change 48
48
* @author Eddú Meléndez
49
49
* @author Rob Winch
50
50
* @author Brian Clozel
51
+ * @author Sebastien Deleuze
51
52
*/
52
53
class ForwardedHeaderFilterTests {
53
54
@@ -442,6 +443,15 @@ void shouldConcatenatePrefixesWithTrailingSlashes() throws Exception {
442
443
assertThat (actual .getRequestURL ().toString ()).isEqualTo ("http://localhost/first/second/mvc-showcase" );
443
444
}
444
445
446
+ @ Test
447
+ void shouldRemoveSingleTrailingSlash () throws Exception {
448
+ request .addHeader (X_FORWARDED_PREFIX , "/prefix,/" );
449
+ request .setRequestURI ("/mvc-showcase" );
450
+
451
+ HttpServletRequest actual = filterAndGetWrappedRequest ();
452
+ assertThat (actual .getRequestURL ().toString ()).isEqualTo ("http://localhost/prefix/mvc-showcase" );
453
+ }
454
+
445
455
@ Test
446
456
void requestURLNewStringBuffer () throws Exception {
447
457
request .addHeader (X_FORWARDED_PREFIX , "/prefix/" );
Original file line number Diff line number Diff line change 32
32
* Tests for {@link ForwardedHeaderTransformer}.
33
33
*
34
34
* @author Rossen Stoyanchev
35
+ * @author Sebastien Deleuze
35
36
*/
36
37
class ForwardedHeaderTransformerTests {
37
38
@@ -170,6 +171,17 @@ void shouldConcatenatePrefixesWithTrailingSlashes() {
170
171
assertForwardedHeadersRemoved (request );
171
172
}
172
173
174
+ @ Test // gh-33465
175
+ void shouldRemoveSingleTrailingSlash () {
176
+ HttpHeaders headers = new HttpHeaders ();
177
+ headers .add ("X-Forwarded-Prefix" , "/prefix,/" );
178
+ ServerHttpRequest request = this .requestMutator .apply (getRequest (headers ));
179
+
180
+ assertThat (request .getURI ()).isEqualTo (URI .create ("https://example.com/prefix/path" ));
181
+ assertThat (request .getPath ().value ()).isEqualTo ("/prefix/path" );
182
+ assertForwardedHeadersRemoved (request );
183
+ }
184
+
173
185
@ Test
174
186
void forwardedForNotPresent () {
175
187
HttpHeaders headers = new HttpHeaders ();
You can’t perform that action at this time.
0 commit comments