Skip to content

Commit 41928ae

Browse files
committed
8359709: java.net.HttpURLConnection sends unexpected "Host" request header in some cases after JDK-8344190
Reviewed-by: dfuchs Backport-of: 5726606
1 parent 3f6b0c6 commit 41928ae

File tree

2 files changed

+147
-4
lines changed

2 files changed

+147
-4
lines changed

src/java.base/share/classes/sun/net/www/protocol/http/HttpURLConnection.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -621,10 +621,10 @@ private void writeRequests() throws IOException {
621621
if (port != -1 && port != url.getDefaultPort()) {
622622
host += ":" + String.valueOf(port);
623623
}
624-
String reqHost = requests.findValue("Host");
625-
if (reqHost == null || !reqHost.equalsIgnoreCase(host)) {
626-
requests.set("Host", host);
627-
}
624+
// if the "Host" header hasn't been explicitly set, then set its
625+
// value to the one determined through the request URL
626+
requests.setIfNotSet("Host", host);
627+
628628
requests.setIfNotSet("Accept", acceptString);
629629

630630
/*
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.io.IOException;
25+
import java.io.InputStream;
26+
import java.io.OutputStream;
27+
import java.net.HttpURLConnection;
28+
import java.net.InetAddress;
29+
import java.net.InetSocketAddress;
30+
import java.net.Proxy;
31+
import java.net.URL;
32+
import java.net.URLConnection;
33+
import java.util.List;
34+
35+
import com.sun.net.httpserver.HttpExchange;
36+
import com.sun.net.httpserver.HttpHandler;
37+
import com.sun.net.httpserver.HttpServer;
38+
import jdk.test.lib.net.URIBuilder;
39+
import org.junit.jupiter.api.AfterAll;
40+
import org.junit.jupiter.api.BeforeAll;
41+
import org.junit.jupiter.api.Test;
42+
import static java.nio.charset.StandardCharsets.US_ASCII;
43+
import static org.junit.jupiter.api.Assertions.assertEquals;
44+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
45+
import static org.junit.jupiter.api.Assertions.assertNull;
46+
47+
/*
48+
* @test
49+
* @bug 8359709
50+
* @summary verify that if the Host header is allowed to be set by the application
51+
* then the correct value gets set in a HTTP request issued through
52+
* java.net.HttpURLConnection
53+
* @library /test/lib
54+
* @run junit HostHeaderTest
55+
* @run junit/othervm -Dsun.net.http.allowRestrictedHeaders=true HostHeaderTest
56+
* @run junit/othervm -Dsun.net.http.allowRestrictedHeaders=false HostHeaderTest
57+
*/
58+
class HostHeaderTest {
59+
60+
private static final String SERVER_CTX_ROOT = "/8359709/";
61+
private static final boolean allowsHostHeader = Boolean.getBoolean("sun.net.http.allowRestrictedHeaders");
62+
63+
private static HttpServer server;
64+
65+
@BeforeAll
66+
static void beforeAll() throws Exception {
67+
final InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
68+
server = HttpServer.create(addr, 0);
69+
server.createContext(SERVER_CTX_ROOT, new Handler());
70+
server.start();
71+
System.err.println("started server at " + server.getAddress());
72+
}
73+
74+
@AfterAll
75+
static void afterAll() throws Exception {
76+
if (server != null) {
77+
System.err.println("stopping server " + server.getAddress());
78+
server.stop(0);
79+
}
80+
}
81+
82+
@Test
83+
void testHostHeader() throws Exception {
84+
final InetSocketAddress serverAddr = server.getAddress();
85+
final URL reqURL = URIBuilder.newBuilder()
86+
.scheme("http")
87+
.loopback()
88+
.port(serverAddr.getPort())
89+
.path(SERVER_CTX_ROOT)
90+
.build().toURL();
91+
final URLConnection conn = reqURL.openConnection(Proxy.NO_PROXY);
92+
93+
conn.setRequestProperty("Host", "foobar");
94+
if (!allowsHostHeader) {
95+
// if restricted headers aren't allowed to be set by the user, then
96+
// we expect the previous call to setRequestProperty to not set the Host
97+
// header
98+
assertNull(conn.getRequestProperty("Host"), "Host header unexpectedly set");
99+
}
100+
101+
assertInstanceOf(HttpURLConnection.class, conn);
102+
final HttpURLConnection httpURLConn = (HttpURLConnection) conn;
103+
104+
// send the HTTP request
105+
System.err.println("sending request " + reqURL);
106+
final int respCode = httpURLConn.getResponseCode();
107+
assertEquals(200, respCode, "unexpected response code");
108+
// verify that the server side handler received the expected
109+
// Host header value in the request
110+
try (final InputStream is = httpURLConn.getInputStream()) {
111+
final byte[] resp = is.readAllBytes();
112+
// if Host header wasn't explicitly set, then we expect it to be
113+
// derived from the request URL
114+
final String expected = allowsHostHeader
115+
? "foobar"
116+
: reqURL.getHost() + ":" + reqURL.getPort();
117+
final String actual = new String(resp, US_ASCII);
118+
assertEquals(expected, actual, "unexpected Host header received on server side");
119+
}
120+
}
121+
122+
private static final class Handler implements HttpHandler {
123+
private static final int NO_RESPONSE_BODY = -1;
124+
125+
@Override
126+
public void handle(final HttpExchange exchange) throws IOException {
127+
final List<String> headerVals = exchange.getRequestHeaders().get("Host");
128+
System.err.println("Host header has value(s): " + headerVals);
129+
// unexpected Host header value, respond with 400 status code
130+
if (headerVals == null || headerVals.size() != 1) {
131+
System.err.println("Unexpected header value(s) for Host header: " + headerVals);
132+
exchange.sendResponseHeaders(400, NO_RESPONSE_BODY);
133+
return;
134+
}
135+
// respond back with the Host header value that we found in the request
136+
final byte[] response = headerVals.getFirst().getBytes(US_ASCII);
137+
exchange.sendResponseHeaders(200, response.length);
138+
try (final OutputStream os = exchange.getResponseBody()) {
139+
os.write(response);
140+
}
141+
}
142+
}
143+
}

0 commit comments

Comments
 (0)