Skip to content

8359709: java.net.HttpURLConnection sends unexpected "Host" request header in some cases after JDK-8344190 #25844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

jaikiran
Copy link
Member

@jaikiran jaikiran commented Jun 17, 2025

Can I please get a review for this change which addresses a regression that was introduced in HttpURLConnection in Java 24 when we cleaned up the code by removing the references to SecurityManager APIs.

When a HTTP request is issued through java.net.HttpURLConnection, then the request URL is used to determine the Host header to set in the request. By default, the application cannot set a Host header to a different value. However the JDK allows a system property to be enabled to allow applications to explicitly set a Host request header when issuing the request.

Due to an oversight in the change that was done in https://bugs.openjdk.org/browse/JDK-8344190, the Host header that is set by the application, may not get used for that request causing this regression. Turns out we don't have tests in this area to catch this issue.

The commit in this PR fixes the regression and also introduces a new jtreg test which reproduces the issue and verifies the fix.

I've also checked the original change which introduced this regression #22232 to see if there's anything else that needs attention. I haven't stopped anything else.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8359709: java.net.HttpURLConnection sends unexpected "Host" request header in some cases after JDK-8344190 (Bug - P2)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/25844/head:pull/25844
$ git checkout pull/25844

Update a local copy of the PR:
$ git checkout pull/25844
$ git pull https://git.openjdk.org/jdk.git pull/25844/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 25844

View PR using the GUI difftool:
$ git pr show -t 25844

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/25844.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Jun 17, 2025

👋 Welcome back jpai! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Jun 17, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk openjdk bot added the rfr Pull request is ready for review label Jun 17, 2025
@openjdk
Copy link

openjdk bot commented Jun 17, 2025

@jaikiran The following label will be automatically applied to this pull request:

  • net

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@mlbridge
Copy link

mlbridge bot commented Jun 17, 2025

Webrevs

static void beforeAll() throws Exception {
final InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
server = HttpServer.create(addr, 0);
server.createContext("/", new Handler());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be a good idea to salt the handler path a bit (e.g., with the class name) to avoid unexpected connections from tests running in parallel.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello Volkan, the server handler in this test is implemented to allow more than one request during its lifetime. So any unexpected requests from other processes would still allow this test to be unaffected by those requests. Did I misunderstand your suggestion for registering the handler to a test specific context?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So any unexpected requests from other processes would still allow this test to be unaffected by those requests.

Yes, this test will not be affected, but the other test might. Consider a test running in parallel, unexpectedly connecting to HostHeaderTest's server, and it is

  1. either not expecting a response at all
  2. or not expecting the response returned from the HostHeaderTest's handler

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the PR to register the handler at a test specific context root. Having said that, I wasn't aware we were doing this in our tests.

Copy link
Member

@dfuch dfuch Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had some tests failing randmonly in the past because they got additional connections from other processes running on the same host (some time other tests). So now we tend to add a bit of salt in the path - it makes it easier to figure out that the strange things you see in the logs were not caused by a legit client. Also if you see 404 being returned and you know that your custom handler never returns 404 then it's easier to figure out that you somehow connected to some other server.

@@ -621,8 +621,9 @@ private void writeRequests() throws IOException {
if (port != -1 && port != url.getDefaultPort()) {
host += ":" + String.valueOf(port);
}
String reqHost = requests.findValue("Host");
if (reqHost == null || !reqHost.equalsIgnoreCase(host)) {
if (requests.findValue("Host") == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use setIfNotSet here like for "Accept" below?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems reasonable. I had a look at the implementation in setIfNotSet() and it matches this current semantic. So I've updated the PR to use setIfNotSet().

@@ -57,6 +57,7 @@
*/
class HostHeaderTest {

private static final String SERVER_CTX_ROOT = "/8359709";
Copy link
Member

@dfuch dfuch Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static final String SERVER_CTX_ROOT = "/8359709";
private static final String SERVER_CTX_ROOT = "/8359709/";

Preferably always terminate context roots with /

https://docs.oracle.com/en/java/javase/24/docs/api/jdk.httpserver/com/sun/net/httpserver/HttpServer.html#createContext(java.lang.String,com.sun.net.httpserver.HttpHandler)

API Note:
The path should generally, but is not required to, end with '/'. If the path does not end with '/', eg such as with "/foo" then this would match requests with a path of "/foobar" or "/foo/bar".

Copy link
Member Author

@jaikiran jaikiran Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I've now updated the PR with this change. Test continues to pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
net [email protected] rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

3 participants