Skip to content

Conversation

@nizarbenalla
Copy link
Member

@nizarbenalla nizarbenalla commented May 8, 2024

Passes Tier 1-3
Please review this change that aims to fix a bug when parsing the client's request.

RFC 9110 states

  1. HTTP Authentication 11.1. Authentication Scheme
    HTTP provides a general framework for access control and authentication, via an extensible set of challenge-response authentication schemes, which can be used by a server to challenge a client request and by a client to provide authentication information. It uses a case-insensitive token to identify the authentication scheme:
    auth-scheme = token

But in BasicAuthenticator#authenticate it was done in a case sensitive manner

TIA


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-8144100: Incorrect case-sensitive equality in com.sun.net.httpserver.BasicAuthenticator (Bug - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 19133

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

Using diff file

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

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented May 8, 2024

👋 Welcome back nizarbenalla! 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 May 8, 2024

@nizarbenalla 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:

8144100: Incorrect case-sensitive equality in com.sun.net.httpserver.BasicAuthenticator

Reviewed-by: jpai, dfuchs

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 224 new commits pushed to the master branch:

  • 1dac34f: 8331098: [Aarch64] Fix crash in Arrays.equals() intrinsic with -CCP
  • 5e8e8ef: 8315431: ArchiveHeapWriter::get_filler_size_at() cannot handle buffer expansion
  • 1b476f5: 8293345: SunPKCS11 provider checks on PKCS11 Mechanism are problematic
  • 1c5f150: 8331734: Atomic MemorySegment VarHandle operations fails for element layouts
  • 65abf24: 8331866: Add warnings for locale data dependence
  • d215bc4: 8332066: AArch64: Math test failures since JDK-8331558
  • d11e70a: 8331646: Add specific regression leap year tests
  • f95c937: 8331577: RISC-V: C2 CountLeadingZerosV
  • 675fbe6: 8331993: Add counting leading/trailing zero tests for Integer
  • 242446b: 8331931: JFR: Avoid loading regex classes during startup
  • ... and 214 more: https://git.openjdk.org/jdk/compare/74b11ccf143b335c0e3f21e9fe5dc024742b1bc4...master

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.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@jaikiran, @dfuch) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk
Copy link

openjdk bot commented May 8, 2024

@nizarbenalla 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 May 8, 2024
static HttpServer server() throws Exception {
String realm = "someRealm";
ServerAuthenticator authenticator = new ServerAuthenticator(realm);
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
Copy link
Member

Choose a reason for hiding this comment

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

Hello Nizar, we should use loopback address to bind the server to. That's the common practice we follow in our networking tests. So:

new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)) ...

HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
server.createContext(someContext, exchange -> {
if (authenticator.authenticate(exchange) instanceof Authenticator.Failure) {
exchange.sendResponseHeaders(401, 0);
Copy link
Member

Choose a reason for hiding this comment

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

The second parameter here should be -1 implying no response body. 0 implies chunked response.

exchange.close();
return;
}
exchange.sendResponseHeaders(200, 1);
Copy link
Member

Choose a reason for hiding this comment

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

Second parameter here should be -1. 1 implies the response will contain a body of 1 byte in length.

String statusLine = reader.readLine();
System.err.println(statusLine);

if (statusLine.startsWith("HTTP/1.1 401")) {
Copy link
Member

Choose a reason for hiding this comment

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

It might be better to check that the status line is 200 and anything other than that should result in an exception from here.

}

static class ServerAuthenticator extends BasicAuthenticator {
ServerAuthenticator(String realm) {
Copy link
Member

Choose a reason for hiding this comment

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

Since this test relies on the authenticator being invoked, just as an additional check, you might want to have a boolean field in this class (named something like invoked) which will be set to true only when the checkCredentials gets called. Then after checking the response code in the test, you can additionally assert that this field's value is true, just to be certain that this authenticator was indeed invoked.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed, thanks

* @test
* @bug 8144100
* @run main/othervm BasicAuthToken
* @summary checking token sent by client should be done in case-insensitive manner
Copy link
Member

Choose a reason for hiding this comment

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

As a guideline, jtreg recommended order of test definition tags is here https://openjdk.org/jtreg/tag-spec.html#ORDER. As per that the @summary should follow the @bug before the @run.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

}

static void client(int port) throws Exception {
try (Socket socket = new Socket("localhost", port)) {
Copy link
Member

Choose a reason for hiding this comment

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

Once you change the server to bind to loopback address, the Socket creation should then use loopback address too: new Socket(InetAddress.getLoopbackAddress(), port)

@nizarbenalla nizarbenalla marked this pull request as ready for review May 9, 2024 11:56
@openjdk openjdk bot added the rfr Pull request is ready for review label May 9, 2024
@mlbridge
Copy link

mlbridge bot commented May 9, 2024

Webrevs



static class ServerAuthenticator extends BasicAuthenticator {
private static boolean invoked = false;
Copy link
Member

Choose a reason for hiding this comment

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

this should be declared volatile since it will be mutated in one thread and read in the other.

Copy link
Member Author

@nizarbenalla nizarbenalla May 9, 2024

Choose a reason for hiding this comment

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

Fixed in 5003802.
I ran the tests again and it passes Tier 1-3.

* @test
* @bug 8144100
* @summary checking token sent by client should be done in case-insensitive manner
* @run main/othervm BasicAuthToken
Copy link
Member

Choose a reason for hiding this comment

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

Hello Nizar, I couldn't spot anything in the test that forces the use of "othervm". Is the othervm intentional? If not, I would recommend changing it to just "main".

Copy link
Member Author

@nizarbenalla nizarbenalla May 10, 2024

Choose a reason for hiding this comment

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

Sure, was just being extra careful.
Will fix it.

System.err.println(statusLine);

if (!statusLine.startsWith("HTTP/1.1 200")) {
throw new RuntimeException("Basic Authentication failed: case-insensitive token" +
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Might be better to just state:

throw new RuntimeException("unexpected status line: " + statusLine);

throw new RuntimeException("Basic Authentication failed: case-insensitive token" +
" used to identify authentication scheme sent by client parsed incorrectly");
}
assert ServerAuthenticator.wasChecked() : "Authenticator was not correctly invoked";
Copy link
Member

Choose a reason for hiding this comment

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

As far as I know, the jtreg tests that we launch (through make) will always have asserts enabled. So I think using assert here is OK. However, to be consistent with other conditional checks in this test, I think it would be better to change this to a if block, something like:

if (!ServerAuthenticator.wasChecked()) { 
   throw new RuntimeException("Authenticator wasn't invoked");
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed it! And yes, all tests passed using assert

* questions.
*/

/**
Copy link
Member

@dfuch dfuch May 10, 2024

Choose a reason for hiding this comment

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

Suggested change
/**
/*

It was recently suggested that test comments are not API documentation comments, and that we should avoid /** in that case. Maybe we will do a global pass on the test base at some point (or not) but in the mean time let's avoid propagating this pattern in new tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe we can use a script to fix them all in one a large change, similar to #18268

Copy link
Member

@jaikiran jaikiran left a comment

Choose a reason for hiding this comment

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

The changes look good to me and I don't have any more review comments. Thank you for the updates as well as fixing this issue.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label May 10, 2024
@nizarbenalla
Copy link
Member Author

Thank you.

@nizarbenalla
Copy link
Member Author

Only tests failing tier 1-3 are Math related tests, https://bugs.openjdk.org/browse/JDK-8332066.
Test8210461.java,WorstCaseTests.java and SinCosCornerCasesTests.java. Completely unrelated to my changes.

@nizarbenalla
Copy link
Member Author

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label May 11, 2024
@openjdk
Copy link

openjdk bot commented May 11, 2024

@nizarbenalla
Your change (at version 35a32c2) is now ready to be sponsored by a Committer.

@jaikiran
Copy link
Member

/sponsor

@openjdk
Copy link

openjdk bot commented May 11, 2024

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

  • 1dac34f: 8331098: [Aarch64] Fix crash in Arrays.equals() intrinsic with -CCP
  • 5e8e8ef: 8315431: ArchiveHeapWriter::get_filler_size_at() cannot handle buffer expansion
  • 1b476f5: 8293345: SunPKCS11 provider checks on PKCS11 Mechanism are problematic
  • 1c5f150: 8331734: Atomic MemorySegment VarHandle operations fails for element layouts
  • 65abf24: 8331866: Add warnings for locale data dependence
  • d215bc4: 8332066: AArch64: Math test failures since JDK-8331558
  • d11e70a: 8331646: Add specific regression leap year tests
  • f95c937: 8331577: RISC-V: C2 CountLeadingZerosV
  • 675fbe6: 8331993: Add counting leading/trailing zero tests for Integer
  • 242446b: 8331931: JFR: Avoid loading regex classes during startup
  • ... and 214 more: https://git.openjdk.org/jdk/compare/74b11ccf143b335c0e3f21e9fe5dc024742b1bc4...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label May 11, 2024
@openjdk openjdk bot closed this May 11, 2024
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review sponsor Pull request is ready to be sponsored labels May 11, 2024
@openjdk
Copy link

openjdk bot commented May 11, 2024

@jaikiran @nizarbenalla Pushed as commit b87a7e9.

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

@nizarbenalla nizarbenalla deleted the basicAuthenticator-token branch September 19, 2024 00:35
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.

3 participants