Skip to content

Commit 6a6ad05

Browse files
committed
Retain brackets for IPV6 address in MockHttpServletRequest
According to the Javadoc for ServletRequest's getServerName() method, when the `Host` header is set, the server name is "the value of the part before ':' in the Host header value ...". For a value representing an IPV6 address such as `[::ffff:abcd:abcd]`, the enclosing square brackets should therefore not be stripped from the enclosed IPV6 address. However, the changes made in conjunction with gh-16704 introduced a regression in Spring Framework 4.1 for the getServerName() method in MockHttpServletRequest by stripping the enclosing brackets from the IPV6 address in the `Host` header. Similarly, the changes made in conjunction with gh-20686 introduced a regression in Spring Framework 4.3.13 and 5.0.2 in the getRequestURL() method in MockHttpServletRequest by delegating to the getServerName() method which strips the enclosing brackets. This commit fixes the implementation of getServerName() so that the enclosing brackets are no longer stripped from an IPV6 address in the `Host` header. The implementation of getRequestURL() is therefore also fixed. In addition, in order to avoid a NullPointerException, the implementations of getServerName() and getServerPort() now assert that an IPV6 address present in the `Host` header correctly contains an opening and closing bracket and throw an IllegalStateException if that is not the case. Closes gh-24916
1 parent 4ec2844 commit 6a6ad05

File tree

3 files changed

+91
-17
lines changed

3 files changed

+91
-17
lines changed

spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -654,11 +654,14 @@ public void setServerName(String serverName) {
654654

655655
@Override
656656
public String getServerName() {
657-
String host = getHeader(HttpHeaders.HOST);
657+
String rawHostHeader = getHeader(HttpHeaders.HOST);
658+
String host = rawHostHeader;
658659
if (host != null) {
659660
host = host.trim();
660661
if (host.startsWith("[")) {
661-
host = host.substring(1, host.indexOf(']'));
662+
int indexOfClosingBracket = host.indexOf(']');
663+
Assert.state(indexOfClosingBracket > -1, () -> "Invalid Host header: " + rawHostHeader);
664+
host = host.substring(0, indexOfClosingBracket + 1);
662665
}
663666
else if (host.contains(":")) {
664667
host = host.substring(0, host.indexOf(':'));
@@ -676,12 +679,15 @@ public void setServerPort(int serverPort) {
676679

677680
@Override
678681
public int getServerPort() {
679-
String host = getHeader(HttpHeaders.HOST);
682+
String rawHostHeader = getHeader(HttpHeaders.HOST);
683+
String host = rawHostHeader;
680684
if (host != null) {
681685
host = host.trim();
682686
int idx;
683687
if (host.startsWith("[")) {
684-
idx = host.indexOf(':', host.indexOf(']'));
688+
int indexOfClosingBracket = host.indexOf(']');
689+
Assert.state(indexOfClosingBracket > -1, () -> "Invalid Host header: " + rawHostHeader);
690+
idx = host.indexOf(':', indexOfClosingBracket);
685691
}
686692
else {
687693
idx = host.indexOf(':');

spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.mock.web;
1818

1919
import java.io.IOException;
20+
import java.net.URL;
2021
import java.nio.charset.Charset;
2122
import java.util.ArrayList;
2223
import java.util.Arrays;
@@ -37,6 +38,7 @@
3738
import org.springframework.util.FileCopyUtils;
3839
import org.springframework.util.StreamUtils;
3940

41+
import static org.hamcrest.CoreMatchers.startsWith;
4042
import static org.junit.Assert.*;
4143

4244
/**
@@ -347,16 +349,23 @@ public void getServerNameViaHostHeaderWithPort() {
347349

348350
@Test
349351
public void getServerNameViaHostHeaderAsIpv6AddressWithoutPort() {
350-
String ipv6Address = "[2001:db8:0:1]";
351-
request.addHeader(HOST, ipv6Address);
352-
assertEquals("2001:db8:0:1", request.getServerName());
352+
String host = "[2001:db8:0:1]";
353+
request.addHeader(HOST, host);
354+
assertEquals(host, request.getServerName());
353355
}
354356

355357
@Test
356358
public void getServerNameViaHostHeaderAsIpv6AddressWithPort() {
357-
String ipv6Address = "[2001:db8:0:1]:8081";
358-
request.addHeader(HOST, ipv6Address);
359-
assertEquals("2001:db8:0:1", request.getServerName());
359+
request.addHeader(HOST, "[2001:db8:0:1]:8081");
360+
assertEquals("[2001:db8:0:1]", request.getServerName());
361+
}
362+
363+
@Test
364+
public void getServerNameWithInvalidIpv6AddressViaHostHeader() {
365+
request.addHeader(HOST, "[::ffff:abcd:abcd"); // missing closing bracket
366+
exception.expect(IllegalStateException.class);
367+
exception.expectMessage(startsWith("Invalid Host header: "));
368+
request.getServerName();
360369
}
361370

362371
@Test
@@ -370,6 +379,22 @@ public void getServerPortWithCustomPort() {
370379
assertEquals(8080, request.getServerPort());
371380
}
372381

382+
@Test
383+
public void getServerPortWithInvalidIpv6AddressViaHostHeader() {
384+
request.addHeader(HOST, "[::ffff:abcd:abcd:8080"); // missing closing bracket
385+
exception.expect(IllegalStateException.class);
386+
exception.expectMessage(startsWith("Invalid Host header: "));
387+
request.getServerPort();
388+
}
389+
390+
@Test
391+
public void getServerPortWithIpv6AddressAndInvalidPortViaHostHeader() {
392+
request.addHeader(HOST, "[::ffff:abcd:abcd]:bogus"); // "bogus" is not a port number
393+
exception.expect(NumberFormatException.class);
394+
exception.expectMessage("bogus");
395+
request.getServerPort();
396+
}
397+
373398
@Test
374399
public void getServerPortViaHostHeaderAsIpv6AddressWithoutPort() {
375400
String testServer = "[2001:db8:0:1]";
@@ -434,6 +459,43 @@ public void getRequestURLWithHostHeaderAndPort() {
434459
assertEquals("http://" + testServer, requestURL.toString());
435460
}
436461

462+
@Test
463+
public void getRequestURLWithIpv6AddressViaServerNameWithoutPort() throws Exception {
464+
request.setServerName("[::ffff:abcd:abcd]");
465+
URL url = new java.net.URL(request.getRequestURL().toString());
466+
assertEquals("http://[::ffff:abcd:abcd]", url.toString());
467+
}
468+
469+
@Test
470+
public void getRequestURLWithIpv6AddressViaServerNameWithPort() throws Exception {
471+
request.setServerName("[::ffff:abcd:abcd]");
472+
request.setServerPort(9999);
473+
URL url = new java.net.URL(request.getRequestURL().toString());
474+
assertEquals("http://[::ffff:abcd:abcd]:9999", url.toString());
475+
}
476+
477+
@Test
478+
public void getRequestURLWithInvalidIpv6AddressViaHostHeader() {
479+
request.addHeader(HOST, "[::ffff:abcd:abcd"); // missing closing bracket
480+
exception.expect(IllegalStateException.class);
481+
exception.expectMessage(startsWith("Invalid Host header: "));
482+
request.getRequestURL();
483+
}
484+
485+
@Test
486+
public void getRequestURLWithIpv6AddressViaHostHeaderWithoutPort() throws Exception {
487+
request.addHeader(HOST, "[::ffff:abcd:abcd]");
488+
URL url = new java.net.URL(request.getRequestURL().toString());
489+
assertEquals("http://[::ffff:abcd:abcd]", url.toString());
490+
}
491+
492+
@Test
493+
public void getRequestURLWithIpv6AddressViaHostHeaderWithPort() throws Exception {
494+
request.addHeader(HOST, "[::ffff:abcd:abcd]:9999");
495+
URL url = new java.net.URL(request.getRequestURL().toString());
496+
assertEquals("http://[::ffff:abcd:abcd]:9999", url.toString());
497+
}
498+
437499
@Test
438500
public void getRequestURLWithNullRequestUri() {
439501
request.setRequestURI(null);

spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -654,11 +654,14 @@ public void setServerName(String serverName) {
654654

655655
@Override
656656
public String getServerName() {
657-
String host = getHeader(HttpHeaders.HOST);
657+
String rawHostHeader = getHeader(HttpHeaders.HOST);
658+
String host = rawHostHeader;
658659
if (host != null) {
659660
host = host.trim();
660661
if (host.startsWith("[")) {
661-
host = host.substring(1, host.indexOf(']'));
662+
int indexOfClosingBracket = host.indexOf(']');
663+
Assert.state(indexOfClosingBracket > -1, () -> "Invalid Host header: " + rawHostHeader);
664+
host = host.substring(0, indexOfClosingBracket + 1);
662665
}
663666
else if (host.contains(":")) {
664667
host = host.substring(0, host.indexOf(':'));
@@ -676,12 +679,15 @@ public void setServerPort(int serverPort) {
676679

677680
@Override
678681
public int getServerPort() {
679-
String host = getHeader(HttpHeaders.HOST);
682+
String rawHostHeader = getHeader(HttpHeaders.HOST);
683+
String host = rawHostHeader;
680684
if (host != null) {
681685
host = host.trim();
682686
int idx;
683687
if (host.startsWith("[")) {
684-
idx = host.indexOf(':', host.indexOf(']'));
688+
int indexOfClosingBracket = host.indexOf(']');
689+
Assert.state(indexOfClosingBracket > -1, () -> "Invalid Host header: " + rawHostHeader);
690+
idx = host.indexOf(':', indexOfClosingBracket);
685691
}
686692
else {
687693
idx = host.indexOf(':');

0 commit comments

Comments
 (0)