Repository navigation
Closed
Labels
BugFor general bugs on Jetty sideFor general bugs on Jetty side
Description
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);
}
}
}
}Metadata
Metadata
Assignees
Labels
BugFor general bugs on Jetty sideFor general bugs on Jetty side
Type
Projects
- StatusShow more project fieldsβ Done
Activity
[-]Request.getBeginNanoTime[/-][+]Request.getBeginNanoTime returns invalid values[/+]gregw commentedon Dec 11, 2024
I just tried to reproduce with a unit test. I added to following test to
HttpServerTestBase(which gets run with HTTP and HTTPS):The test passes all for variants mostly in a few hundred ms.
gregw commentedon Dec 11, 2024
The implementation of
getBeginNanoTimeis in theHttpParserand it records theNanoTime.now()when the parser leaves the start state. This should not be affected by TLS? Unless there is a bad request wrapper... looking...mpfau commentedon Dec 11, 2024
With the example provided, it always fails with the second request but we tested with http2
gregw commentedon Dec 11, 2024
@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!
sbordet commentedon Dec 11, 2024
@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'sNanoTimeclass.mpfau commentedon Dec 11, 2024
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!
Fixes #12625 - Request.getBeginNanoTime returns invalid values.
sbordet commentedon Dec 11, 2024
It was an actual bug, fixed by #12627.
Fixes #12625 - Request.getBeginNanoTime returns invalid values.
mpfau commentedon Dec 16, 2024
@sbordet Perfect, thanks for the immediate fix! :)