Skip to content

Request.getBeginNanoTime returns invalid valuesΒ #12625

Closed

Description

@mpfau

Jetty version(s)
12.0.15

Jetty Environment
core

Java version/vendor (use: java -version)
21

OS type/version
linux

Description
Request#getBeginNanoTime only returns valid values if run without TLS. If we use TLS, it returns invalid values, e.g. it might return values that are seconds before the client has actually sent the request.

This has been mentioned on the mailing list:
https://www.eclipse.org/lists/jetty-users/msg10847.html

How to reproduce?

package bugs.nanoTime;

import org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.http2.client.HTTP2Client;
import org.eclipse.jetty.http2.client.transport.HttpClientTransportOverHTTP2;
import org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory;
import org.eclipse.jetty.io.ClientConnector;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.ssl.SslContextFactory;

import java.io.File;

/**
 * Check if the nano time on the request is correct.
 */
public class HttpsNanoTimeoutTest {

	private static final String BEGIN_TIME = "BEGIN_TIME";

	public static void main(String[] args) throws Exception {
		File keystore = new File("/opt/etc/keystore");
		if (!keystore.exists()) {
			throw new RuntimeException("No keystore found at " + keystore.getAbsolutePath());
		}

		// ATTENTION: SHA1 is automatically excluded if we call the empty constructor of SslContextFactory(). To avoid this us the constructor with the keyStorePath
		var cf = new SslContextFactory.Server();
		cf.setKeyStorePath(keystore.getAbsolutePath());
		cf.setKeyStorePassword("mykeystorepass");
		cf.setKeyManagerPassword("mykeystorepass");

		Server server = new Server();
		HttpConfiguration httpsConfig = new HttpConfiguration();
		httpsConfig.setSecurePort(7004);
		ALPNServerConnectionFactory alpn = new ALPNServerConnectionFactory();
		HttpConnectionFactory http1 = new HttpConnectionFactory(httpsConfig);
		alpn.setDefaultProtocol(http1.getProtocol());
		SslConnectionFactory ssl = new SslConnectionFactory(cf, alpn.getProtocol());

		HTTP2ServerConnectionFactory http2 = new HTTP2ServerConnectionFactory(httpsConfig);

		ServerConnector connector = new ServerConnector(server, ssl, alpn, http2, http1);


		connector.setPort(7004);
		connector.setIdleTimeout(1000 * 20);
		server.addConnector(connector);

		var handler = new Handler.Abstract() {

			@Override
			public boolean handle(Request request, Response response, Callback callback) throws Exception {
				var requestBeginTime = request.getBeginNanoTime();
				var headersBeginTime = Long.parseLong(request.getHeaders().get("BEGIN_TIME"));
				if (NanoTime.isBefore(requestBeginTime, headersBeginTime)) {
					// jetty can't have received the request before it was actually sent
					callback.failed(new RuntimeException("begin time is " + requestBeginTime + "; time when client send request is " + headersBeginTime));
				}
				callback.succeeded();
				return true;
			}
		};


		server.setHandler(handler);
		server.start();


		var sslContextFactory = new SslContextFactory.Client();
		sslContextFactory.setTrustAll(true);

		var clientConnector = new ClientConnector();
		clientConnector.setSslContextFactory(sslContextFactory);

		HTTP2Client http2Client = new HTTP2Client(clientConnector);
		http2Client.setInitialSessionRecvWindow(64 * 1024 * 1024);

		HttpClientTransportOverHTTP2 transport = new HttpClientTransportOverHTTP2(http2Client);
		transport.setUseALPN(true);

		HttpClient client = new HttpClient(transport);
		client.start();

                int i = 0;
		while (true) {
                        i++;
			var res = client.newRequest("https://mydomain.com:7004")
					.method("GET")
					.headers(headers -> headers.put(BEGIN_TIME, System.nanoTime()))
					.send();
			if (res.getStatus() != 200) {
				System.out.println("stopped after " + i + " attempts");
				server.stop();
				System.exit(0);
			}
		}
	}


}

Activity

  1. added
    BugFor general bugs on Jetty side
    on Dec 11, 2024
  2. changed the title [-]Request.getBeginNanoTime[/-] [+]Request.getBeginNanoTime returns invalid values[/+] on Dec 11, 2024
  3. gregw commented on Dec 11, 2024

    @gregw
    Contributor

    I just tried to reproduce with a unit test. I added to following test to HttpServerTestBase (which gets run with HTTP and HTTPS):

        @Test
        public void testBeginNanoTime() throws Exception
        {
            startServer(new Handler.Abstract()
            {
                @Override
                public boolean handle(Request request, Response response, Callback callback) throws Exception
                {
                    Content.Sink.write(response, true, "" + request.getBeginNanoTime(), callback);
                    return true;
                }
            });
    
            try (Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort()))
            {
                OutputStream os = client.getOutputStream();
    
                String request = """
                    GET / HTTP/1.1
                    Host: localhost
                    Connection: close
                    
                    """;
                long sent = NanoTime.now();
                os.write(request.getBytes(StandardCharsets.ISO_8859_1));
                os.flush();
    
                // Read the response.
                String rawResponse = readResponse(client);
    
                assertThat(rawResponse, containsString("HTTP/1.1 200 OK"));
                HttpTester.Response response = HttpTester.parseResponse(rawResponse);
    
                long received = NanoTime.now();
                long beginTime = Long.parseLong(response.getContent());
    
                assertTrue(NanoTime.isBefore(sent, beginTime));
                assertTrue(NanoTime.isBefore(beginTime, received));
            }
        }

    The test passes all for variants mostly in a few hundred ms.

  4. gregw commented on Dec 11, 2024

    @gregw
    Contributor

    The implementation of getBeginNanoTime is in the HttpParser and it records the NanoTime.now() when the parser leaves the start state. This should not be affected by TLS? Unless there is a bad request wrapper... looking...

  5. mpfau commented on Dec 11, 2024

    @mpfau
    Author

    With the example provided, it always fails with the second request but we tested with http2

  6. gregw commented on Dec 11, 2024

    @gregw
    Contributor

    @sbordet could the client be sending white space over TLS before the request line?
    The parser is capturing the timestamp on the first character, but it doesn't check for whitespace!

    Oh I just noticed this is HTTP2.... that changes everything!

  7. sbordet commented on Dec 11, 2024

    @sbordet
    Contributor

    @gregw I'll write test cases for all the transports, but HTTP/2 should be ok too.

    @mpfau are you communicating via localhost, or between two different machines?

    In your first example you call mydomain.com, so if that is a different machine you cannot compare the nanoTimes of two different JVMs.

    Also, you cannot compare the nanoTimes using < (less than), you must subtract them or use Jetty's NanoTime class.

  8. mpfau commented on Dec 11, 2024

    @mpfau
    Author

    Yes, we are communicating via localhost. We just defined a domain name to be able to use https.

    If you look at the example, we run the server and the client from the same JVM...

    Thanks for the hint regarding comparisons of nano time. I updated the example to use NanoTime!

  9. added a commit that references this issue on Dec 11, 2024
    0316e68
  10. self-assigned this
    on Dec 11, 2024
  11. moved this to πŸ— In progress in Jetty 12.0.17 - FROZENon Dec 11, 2024
  12. sbordet commented on Dec 11, 2024

    @sbordet
    Contributor

    It was an actual bug, fixed by #12627.

  13. added a commit that references this issue on Dec 12, 2024
    86a1c58
  14. moved this from πŸ— In progress to βœ… Done in Jetty 12.0.17 - FROZENon Dec 12, 2024
  15. mpfau commented on Dec 16, 2024

    @mpfau
    Author

    @sbordet Perfect, thanks for the immediate fix! :)

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

Metadata

Metadata

Assignees

Labels

BugFor general bugs on Jetty side

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions