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
This commit is contained in:
Brian Clozel
2016-10-06 15:36:48 +02:00
parent 53441f8962
commit ea5ff87f8e
2 changed files with 92 additions and 62 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)
@@ -687,10 +688,10 @@ public class UriComponentsBuilder implements Cloneable {
String hostHeader = headers.getFirst("X-Forwarded-Host");
if (StringUtils.hasText(hostHeader)) {
String hostToUse = StringUtils.tokenizeToStringArray(hostHeader, ",")[0];
String[] hostAndPort = StringUtils.split(hostToUse, ":");
if (hostAndPort != null) {
host(hostAndPort[0]);
port(Integer.parseInt(hostAndPort[1]));
int portSeparatorIdx = hostToUse.lastIndexOf(":");
if (portSeparatorIdx > hostToUse.lastIndexOf("]")) {
host(hostToUse.substring(0, portSeparatorIdx));
port(Integer.parseInt(hostToUse.substring(portSeparatorIdx + 1)));
}
else {
host(hostToUse);