Skip to content

Commit 75e2e2c

Browse files
committed
Consistent formatting of forward for host
Closes gh-34253
1 parent c4fe18c commit 75e2e2c

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

spring-web/src/main/java/org/springframework/web/util/ForwardedHeaderUtils.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 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.
@@ -181,6 +181,7 @@ private static void adaptForwardedHost(UriComponentsBuilder uriComponentsBuilder
181181
String forHeader = headers.getFirst("X-Forwarded-For");
182182
if (StringUtils.hasText(forHeader)) {
183183
String host = StringUtils.tokenizeToStringArray(forHeader, ",")[0];
184+
host = (!host.startsWith("[") && !host.endsWith("]") ? "[" + host + "]" : host);
184185
return InetSocketAddress.createUnresolved(host, port);
185186
}
186187

spring-web/src/test/java/org/springframework/web/filter/ForwardedHeaderFilterTests.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 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.
@@ -114,7 +114,7 @@ void forwardedRequest(String protocol) throws Exception {
114114
this.request.addHeader(X_FORWARDED_HOST, "84.198.58.199");
115115
this.request.addHeader(X_FORWARDED_PORT, "443");
116116
this.request.addHeader("foo", "bar");
117-
this.request.addHeader(X_FORWARDED_FOR, "203.0.113.195");
117+
this.request.addHeader(X_FORWARDED_FOR, "[203.0.113.195]");
118118

119119
this.filter.doFilter(this.request, new MockHttpServletResponse(), this.filterChain);
120120
HttpServletRequest actual = (HttpServletRequest) this.filterChain.getRequest();
@@ -125,7 +125,7 @@ void forwardedRequest(String protocol) throws Exception {
125125
assertThat(actual.getServerName()).isEqualTo("84.198.58.199");
126126
assertThat(actual.getServerPort()).isEqualTo(443);
127127
assertThat(actual.isSecure()).isTrue();
128-
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("203.0.113.195");
128+
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("[203.0.113.195]");
129129

130130
assertThat(actual.getHeader(X_FORWARDED_PROTO)).isNull();
131131
assertThat(actual.getHeader(X_FORWARDED_HOST)).isNull();
@@ -481,7 +481,7 @@ void xForwardedForSingleIdentifier() throws Exception {
481481
request.addHeader(X_FORWARDED_FOR, "203.0.113.195");
482482
HttpServletRequest actual = filterAndGetWrappedRequest();
483483

484-
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("203.0.113.195");
484+
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("[203.0.113.195]");
485485
assertThat(actual.getRemotePort()).isEqualTo(MockHttpServletRequest.DEFAULT_SERVER_PORT);
486486
}
487487

@@ -490,7 +490,7 @@ void xForwardedForMultipleIdentifiers() throws Exception {
490490
request.addHeader(X_FORWARDED_FOR, "203.0.113.195, 70.41.3.18, 150.172.238.178");
491491
HttpServletRequest actual = filterAndGetWrappedRequest();
492492

493-
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("203.0.113.195");
493+
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("[203.0.113.195]");
494494
assertThat(actual.getRemotePort()).isEqualTo(MockHttpServletRequest.DEFAULT_SERVER_PORT);
495495
}
496496

spring-web/src/test/java/org/springframework/web/server/adapter/ForwardedHeaderTransformerTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 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,7 +230,7 @@ void xForwardedFor() {
230230

231231
request = this.requestMutator.apply(request);
232232
assertThat(request.getRemoteAddress()).isNotNull();
233-
assertThat(request.getRemoteAddress().getHostName()).isEqualTo("203.0.113.195");
233+
assertThat(request.getRemoteAddress().getHostName()).isEqualTo("[203.0.113.195]");
234234
}
235235

236236

spring-web/src/test/java/org/springframework/web/util/ForwardedHeaderUtilsTests.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 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.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.web.util;
1818

19+
import java.net.InetSocketAddress;
1920
import java.net.URI;
2021
import java.util.Collections;
2122
import java.util.Map;
@@ -539,4 +540,15 @@ void fromHttpRequestForwardedHeaderComma() {
539540
assertThat(result.toUriString()).isEqualTo("https://192.0.2.3:9090/rest/mobile/users/1");
540541
}
541542

543+
@Test // gh-34253
544+
void fromHttpRequestXForwardedHeaderForIpv6Formatting() {
545+
HttpHeaders headers = new HttpHeaders();
546+
headers.add("X-Forwarded-For", "fd00:fefe:1::4, 192.168.0.1");
547+
548+
InetSocketAddress address =
549+
ForwardedHeaderUtils.parseForwardedFor(URI.create("http://example.com"), headers, null);
550+
551+
assertThat(address.getHostName()).isEqualTo("[fd00:fefe:1::4]");
552+
}
553+
542554
}

0 commit comments

Comments
 (0)