Fix NumberFormatException with X-Forwarded-Host

This commit fixes `NumberFormatException`s that were thrown when parsing
IPv6 host values in `X-Forwarded-Host` request headers.

Issue: SPR-14761
(cherry picked from ea5ff87)
This commit is contained in:
Brian Clozel
2016-10-06 15:36:48 +02:00
parent ea9e00f819
commit 306452720f
2 changed files with 93 additions and 63 deletions

View File

@@ -51,6 +51,7 @@ import org.springframework.web.util.HierarchicalUriComponents.PathComponent;
* @author Rossen Stoyanchev
* @author Phillip Webb
* @author Oliver Gierke
* @author Brian Clozel
* @since 3.1
* @see #newInstance()
* @see #fromPath(String)
@@ -686,11 +687,11 @@ public class UriComponentsBuilder implements Cloneable {
else {
String hostHeader = headers.getFirst("X-Forwarded-Host");
if (StringUtils.hasText(hostHeader)) {
String hostToUse = StringUtils.commaDelimitedListToStringArray(hostHeader)[0];
if (hostToUse.contains(":")) {
String[] hostAndPort = StringUtils.split(hostToUse, ":");
host(hostAndPort[0]);
port(Integer.parseInt(hostAndPort[1]));
String hostToUse = StringUtils.tokenizeToStringArray(hostHeader, ",")[0];
int portSeparatorIdx = hostToUse.lastIndexOf(":");
if (portSeparatorIdx > hostToUse.lastIndexOf("]")) {
host(hostToUse.substring(0, portSeparatorIdx));
port(Integer.parseInt(hostToUse.substring(portSeparatorIdx + 1)));
}
else {
host(hostToUse);