Skip to content

Commit 327bef3

Browse files
committed
Enable hostname verification by default in Mail auto-config
Fixes gh-50742
1 parent 4218bd7 commit 327bef3

3 files changed

Lines changed: 48 additions & 12 deletions

File tree

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mail/MailProperties.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ public static class Ssl {
153153
*/
154154
private boolean enabled;
155155

156+
/**
157+
* Whether to enable hostname verification.
158+
*/
159+
private boolean verifyHostname = true;
160+
156161
/**
157162
* SSL bundle name. If set, 'mail.(protocol).ssl.socketFactory' property is set to
158163
* an SSLSocketFactory obtained from the corresponding SSL bundle.
@@ -170,6 +175,14 @@ public void setEnabled(boolean enabled) {
170175
this.enabled = enabled;
171176
}
172177

178+
public boolean isVerifyHostname() {
179+
return this.verifyHostname;
180+
}
181+
182+
public void setVerifyHostname(boolean verifyHostname) {
183+
this.verifyHostname = verifyHostname;
184+
}
185+
173186
public String getBundle() {
174187
return this.bundle;
175188
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mail/MailSenderPropertiesConfiguration.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,18 @@ private void applyProperties(MailProperties properties, JavaMailSenderImpl sende
6666
String protocol = properties.getProtocol();
6767
protocol = (!StringUtils.hasLength(protocol)) ? "smtp" : protocol;
6868
Ssl ssl = properties.getSsl();
69-
if (ssl.isEnabled()) {
70-
javaMailProperties.setProperty("mail." + protocol + ".ssl.enable", "true");
71-
}
72-
if (StringUtils.hasLength(ssl.getBundle())) {
73-
SslBundle sslBundle = sslBundles.getBundle(ssl.getBundle());
74-
javaMailProperties.put("mail." + protocol + ".ssl.socketFactory",
75-
sslBundle.createSslContext().getSocketFactory());
69+
if (ssl.isEnabled() || StringUtils.hasLength(ssl.getBundle())) {
70+
if (ssl.isVerifyHostname()) {
71+
javaMailProperties.setProperty("mail." + protocol + ".ssl.checkserveridentity", "true");
72+
}
73+
if (ssl.isEnabled()) {
74+
javaMailProperties.setProperty("mail." + protocol + ".ssl.enable", "true");
75+
}
76+
if (StringUtils.hasLength(ssl.getBundle())) {
77+
SslBundle sslBundle = sslBundles.getBundle(ssl.getBundle());
78+
javaMailProperties.put("mail." + protocol + ".ssl.socketFactory",
79+
sslBundle.createSslContext().getSocketFactory());
80+
}
7681
}
7782
if (!javaMailProperties.isEmpty()) {
7883
sender.setJavaMailProperties(javaMailProperties);

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mail/MailSenderAutoConfigurationTests.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,22 +266,38 @@ void smtpSslEnabled() {
266266
.run((context) -> {
267267
assertThat(context).hasSingleBean(JavaMailSenderImpl.class);
268268
JavaMailSenderImpl mailSender = context.getBean(JavaMailSenderImpl.class);
269-
assertThat(mailSender.getJavaMailProperties()).containsEntry("mail.smtp.ssl.enable", "true");
269+
assertThat(mailSender.getJavaMailProperties()).containsEntry("mail.smtp.ssl.enable", "true")
270+
.containsEntry("mail.smtp.ssl.checkserveridentity", "true");
271+
});
272+
}
273+
274+
@Test
275+
void smtpSslEnabledWithHostnameVerificationDisabled() {
276+
this.contextRunner
277+
.withPropertyValues("spring.mail.host:localhost", "spring.mail.ssl.enabled:true",
278+
"spring.mail.ssl.verify-hostname:false")
279+
.run((context) -> {
280+
assertThat(context).hasSingleBean(JavaMailSenderImpl.class);
281+
JavaMailSenderImpl mailSender = context.getBean(JavaMailSenderImpl.class);
282+
assertThat(mailSender.getJavaMailProperties()).containsEntry("mail.smtp.ssl.enable", "true")
283+
.doesNotContainKey("mail.smtp.ssl.checkserveridentity");
270284
});
271285
}
272286

273287
@Test
274288
@WithPackageResources("test.jks")
275-
void smtpSslBundle() {
289+
void smtpSslBundleWithHostnameVerificationDisabled() {
276290
this.contextRunner
277291
.withPropertyValues("spring.mail.host:localhost", "spring.mail.ssl.bundle:test-bundle",
292+
"spring.mail.ssl.verify-hostname:false",
278293
"spring.ssl.bundle.jks.test-bundle.keystore.location:classpath:test.jks",
279294
"spring.ssl.bundle.jks.test-bundle.keystore.password:secret",
280295
"spring.ssl.bundle.jks.test-bundle.key.password:password")
281296
.run((context) -> {
282297
assertThat(context).hasSingleBean(JavaMailSenderImpl.class);
283298
JavaMailSenderImpl mailSender = context.getBean(JavaMailSenderImpl.class);
284-
assertThat(mailSender.getJavaMailProperties()).doesNotContainKey("mail.smtp.ssl.enable");
299+
assertThat(mailSender.getJavaMailProperties()).doesNotContainKey("mail.smtp.ssl.enable")
300+
.doesNotContainKey("mail.smtp.ssl.checkserveridentity");
285301
Object property = mailSender.getJavaMailProperties().get("mail.smtp.ssl.socketFactory");
286302
assertThat(property).isInstanceOf(SSLSocketFactory.class);
287303
});
@@ -295,7 +311,8 @@ void smtpsSslEnabled() {
295311
.run((context) -> {
296312
assertThat(context).hasSingleBean(JavaMailSenderImpl.class);
297313
JavaMailSenderImpl mailSender = context.getBean(JavaMailSenderImpl.class);
298-
assertThat(mailSender.getJavaMailProperties()).containsEntry("mail.smtps.ssl.enable", "true");
314+
assertThat(mailSender.getJavaMailProperties()).containsEntry("mail.smtps.ssl.enable", "true")
315+
.containsEntry("mail.smtps.ssl.checkserveridentity", "true");
299316
});
300317
}
301318

@@ -311,7 +328,8 @@ void smtpsSslBundle() {
311328
.run((context) -> {
312329
assertThat(context).hasSingleBean(JavaMailSenderImpl.class);
313330
JavaMailSenderImpl mailSender = context.getBean(JavaMailSenderImpl.class);
314-
assertThat(mailSender.getJavaMailProperties()).doesNotContainKey("mail.smtps.ssl.enable");
331+
assertThat(mailSender.getJavaMailProperties()).doesNotContainKey("mail.smtps.ssl.enable")
332+
.containsEntry("mail.smtps.ssl.checkserveridentity", "true");
315333
Object property = mailSender.getJavaMailProperties().get("mail.smtps.ssl.socketFactory");
316334
assertThat(property).isInstanceOf(SSLSocketFactory.class);
317335
});

0 commit comments

Comments
 (0)