Skip to content

Commit a1dda12

Browse files
committed
Disable Server header by default when using SSL with Jetty 9
Closes gh-7359
1 parent 72e696b commit a1dda12

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,7 @@ private static class Jetty9SslServerConnectorFactory
692692
public ServerConnector getConnector(Server server,
693693
SslContextFactory sslContextFactory, int port) {
694694
HttpConfiguration config = new HttpConfiguration();
695+
config.setSendServerVersion(false);
695696
config.addCustomizer(new SecureRequestCustomizer());
696697
HttpConnectionFactory connectionFactory = new HttpConnectionFactory(config);
697698
SslConnectionFactory sslConnectionFactory = new SslConnectionFactory(

spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,41 @@ public void sslGetScheme() throws Exception { // gh-2232
420420
.contains("scheme=https");
421421
}
422422

423+
@Test
424+
public void serverHeaderIsDisabledByDefaultWhenUsingSsl() throws Exception {
425+
AbstractEmbeddedServletContainerFactory factory = getFactory();
426+
factory.setSsl(getSsl(null, "password", "src/test/resources/test.jks"));
427+
this.container = factory.getEmbeddedServletContainer(
428+
new ServletRegistrationBean(new ExampleServlet(true, false), "/hello"));
429+
this.container.start();
430+
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
431+
new SSLContextBuilder()
432+
.loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
433+
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory)
434+
.build();
435+
ClientHttpResponse response = getClientResponse(getLocalUrl("https", "/hello"),
436+
HttpMethod.GET, new HttpComponentsClientHttpRequestFactory(httpClient));
437+
assertThat(response.getHeaders().get("Server")).isNullOrEmpty();
438+
}
439+
440+
@Test
441+
public void serverHeaderCanBeCustomizedWhenUsingSsl() throws Exception {
442+
AbstractEmbeddedServletContainerFactory factory = getFactory();
443+
factory.setServerHeader("MyServer");
444+
factory.setSsl(getSsl(null, "password", "src/test/resources/test.jks"));
445+
this.container = factory.getEmbeddedServletContainer(
446+
new ServletRegistrationBean(new ExampleServlet(true, false), "/hello"));
447+
this.container.start();
448+
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
449+
new SSLContextBuilder()
450+
.loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
451+
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory)
452+
.build();
453+
ClientHttpResponse response = getClientResponse(getLocalUrl("https", "/hello"),
454+
HttpMethod.GET, new HttpComponentsClientHttpRequestFactory(httpClient));
455+
assertThat(response.getHeaders().get("Server")).containsExactly("MyServer");
456+
}
457+
423458
protected final void testBasicSslWithKeyStore(String keyStore) throws Exception {
424459
AbstractEmbeddedServletContainerFactory factory = getFactory();
425460
addTestTxtFile(factory);

0 commit comments

Comments
 (0)