Skip to content

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

@jaikiran This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

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

Reviewed-by: dfuchs, djelinski, michaelm, vyazici

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 18 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@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.

@openjdk openjdk bot added the net net-dev@openjdk.org label Jun 17, 2025
@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.

}
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().

*/
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.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jun 18, 2025
@jaikiran
Copy link
Member Author

Thank you all for the reviews. I'll go ahead and integrate this now.

@Michael-Mc-Mahon
Copy link
Member

I'm trying to understand how this regression happened. I was looking at the code change for https://bugs.openjdk.org/browse/JDK-8344190 at https://openjdk.github.io/cr/?repo=jdk&pr=22232&range=01#sdiff-4-src/java.base/share/classes/sun/net/www/protocol/http/HttpURLConnection.java and I can't see how it happened. The change looks pretty innocuous..?

@Michael-Mc-Mahon
Copy link
Member

I'm trying to understand how this regression happened. I was looking at the code change for https://bugs.openjdk.org/browse/JDK-8344190 at https://openjdk.github.io/cr/?repo=jdk&pr=22232&range=01#sdiff-4-src/java.base/share/classes/sun/net/www/protocol/http/HttpURLConnection.java and I can't see how it happened. The change looks pretty innocuous..?

Never mind. I see it's explained clearly in the bug report.

@jaikiran
Copy link
Member Author

Hello Michael, I see that you already found out what caused the regression, but yes it's the if block check which went wrong in our clean up. The checkSetHost() method which was being used previously would always return true when SecurityManager wasn't set and that meant that we wouldn't enter the if block when the requestHost was non-null. That check broke after our clean up and is now fixed in this PR.

@jaikiran
Copy link
Member Author

/integrate

@openjdk
Copy link

openjdk bot commented Jun 18, 2025

Going to push as commit 5726606.
Since your change was applied there have been 18 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Jun 18, 2025
@openjdk openjdk bot closed this Jun 18, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Jun 18, 2025
@openjdk
Copy link

openjdk bot commented Jun 18, 2025

@jaikiran Pushed as commit 5726606.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@jaikiran jaikiran deleted the 8359709 branch June 18, 2025 09:05
@jaikiran
Copy link
Member Author

/backport :jdk25

@openjdk
Copy link

openjdk bot commented Jun 18, 2025

@jaikiran the backport was successfully created on the branch backport-jaikiran-57266064-jdk25 in my personal fork of openjdk/jdk. To create a pull request with this backport targeting openjdk/jdk:jdk25, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 57266064 from the openjdk/jdk repository.

The commit being backported was authored by Jaikiran Pai on 18 Jun 2025 and was reviewed by Daniel Fuchs, Daniel Jeliński, Michael McMahon and Volkan Yazici.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk:

$ git fetch https://github.com/openjdk-bots/jdk.git backport-jaikiran-57266064-jdk25:backport-jaikiran-57266064-jdk25
$ git checkout backport-jaikiran-57266064-jdk25
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk.git backport-jaikiran-57266064-jdk25

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integrated Pull request has been integrated net net-dev@openjdk.org

Development

Successfully merging this pull request may close these issues.

5 participants