Set the mail.from to avoid looking up the local hostname

Closes #38353

Signed-off-by: Alexander Schwartz <aschwart@redhat.com>
This commit is contained in:
Alexander Schwartz 2025-04-03 16:51:50 +02:00 committed by GitHub
parent 65540d8eb2
commit 73a20cbf89
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 0 deletions

View File

@ -115,6 +115,11 @@ public class DefaultEmailSenderProvider implements EmailSenderProvider {
throw new EmailException("No sender address configured in the realm settings for emails");
}
// Specify 'mail.from' as InternetAddress.getLocalAddress() would otherwise do a InetAddress.getCanonicalHostName
// and add this as a mail header. This would both be slow, and would reveal internal IP addresses that we don't want.
// https://jakarta.ee/specifications/mail/2.0/jakarta-mail-spec-2.0#a823
props.setProperty("mail.from", from);
String fromDisplayName = config.get("fromDisplayName");
String replyTo = config.get("replyTo");
String replyToDisplayName = config.get("replyToDisplayName");

View File

@ -3,6 +3,8 @@ package org.keycloak.test.examples;
import com.nimbusds.oauth2.sdk.GeneralException;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.keycloak.events.email.EmailEventListenerProviderFactory;
@ -50,6 +52,7 @@ public class EmailTest {
mail.waitForIncomingEmail(1);
MimeMessage lastReceivedMessage = mail.getLastReceivedMessage();
Assertions.assertEquals("Login error", lastReceivedMessage.getSubject());
MatcherAssert.assertThat(lastReceivedMessage.getMessageID(), Matchers.endsWith("@keycloak.org>"));
}
public static class EmailSenderRealmConfig implements RealmConfig {