This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git


The following commit(s) were added to refs/heads/master by this push:
     new 99956108d0 [FIX] DKIMHook: Lenient address mode (#2990)
99956108d0 is described below

commit 99956108d0396faf600130533c7a317dae81e504
Author: Benoit TELLIER <[email protected]>
AuthorDate: Fri Apr 3 13:59:59 2026 +0700

    [FIX] DKIMHook: Lenient address mode (#2990)
    
    The exception was propagated and thus bypassed
    error recovery down the stack.
---
 .../java/org/apache/james/smtpserver/DKIMHook.java |  9 +++--
 .../org/apache/james/smtpserver/DKIMHookTest.java  | 45 ++++++++++++++++++++++
 2 files changed, 50 insertions(+), 4 deletions(-)

diff --git 
a/server/protocols/protocols-smtp-dkim/src/main/java/org/apache/james/smtpserver/DKIMHook.java
 
b/server/protocols/protocols-smtp-dkim/src/main/java/org/apache/james/smtpserver/DKIMHook.java
index 746a86ca42..7589798a9c 100644
--- 
a/server/protocols/protocols-smtp-dkim/src/main/java/org/apache/james/smtpserver/DKIMHook.java
+++ 
b/server/protocols/protocols-smtp-dkim/src/main/java/org/apache/james/smtpserver/DKIMHook.java
@@ -51,6 +51,7 @@ import org.apache.mailet.Mail;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.github.fge.lambdas.Throwing;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
@@ -111,7 +112,7 @@ public class DKIMHook implements JamesMessageHook {
                 try {
                     return StreamUtils.ofNullable(mail.getMessage().getFrom())
                         .distinct()
-                        .flatMap(DKIMCheckNeeded::parseMailAddress)
+                        
.flatMap(Throwing.function(DKIMCheckNeeded::parseMailAddress).sneakyThrow())
                         .findFirst()
                         .map(MailAddress::getDomain)
                         .map(domain::equals)
@@ -127,13 +128,13 @@ public class DKIMHook implements JamesMessageHook {
             };
         }
 
-        private static Stream<MailAddress> parseMailAddress(Address from) {
+        private static Stream<MailAddress> parseMailAddress(Address from) 
throws MessagingException {
             if (from instanceof InternetAddress internetAddress) {
                 try {
                     return Stream.of(new 
MailAddress(internetAddress.getAddress()));
                 } catch (AddressException e) {
-                    // Never happens as valid InternetAddress are valid 
MailAddress
-                    throw new RuntimeException(e);
+                    // Can happen when InternetAddress.getAddress() returns a 
malformed value (e.g. "addr>")
+                    throw new MessagingException("Failed parsing mail 
address", e);
                 }
             }
             return Stream.empty();
diff --git 
a/server/protocols/protocols-smtp-dkim/src/test/java/org/apache/james/smtpserver/DKIMHookTest.java
 
b/server/protocols/protocols-smtp-dkim/src/test/java/org/apache/james/smtpserver/DKIMHookTest.java
index 3838c5a967..dbb62771af 100644
--- 
a/server/protocols/protocols-smtp-dkim/src/test/java/org/apache/james/smtpserver/DKIMHookTest.java
+++ 
b/server/protocols/protocols-smtp-dkim/src/test/java/org/apache/james/smtpserver/DKIMHookTest.java
@@ -21,6 +21,7 @@ package org.apache.james.smtpserver;
 
 import static 
org.apache.james.smtpserver.DKIMHook.Config.DEFAULT_VALIDATED_ENTITIES;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatCode;
 
 import java.util.List;
 import java.util.Optional;
@@ -115,6 +116,50 @@ class DKIMHookTest {
                     .build()))
                 .isTrue();
         }
+
+        @Test
+        void onlyForHeaderFromDomainShouldNotThrowWhenFromAddressIsMalformed() 
throws Exception {
+            // Reproduces a production error: with lenient address parsing 
(mail.mime.address.strict=false),
+            // InternetAddress.getAddress() can return "[email protected]>" 
which is rejected by MailAddress.
+            System.setProperty("mail.mime.address.strict", "false");
+            try {
+                var mimeMessage = MimeMessageBuilder.mimeMessageBuilder()
+                    .setText("This is my email")
+                    .build();
+                mimeMessage.setHeader("From", "[email protected]>");
+
+                assertThatCode(() -> 
DKIMHook.DKIMCheckNeeded.onlyForHeaderFromDomain(Domain.of("fedex.com"))
+                    .test(FakeMail.builder()
+                        .name("mail")
+                        .sender("[email protected]")
+                        .mimeMessage(mimeMessage)
+                        .build()))
+                    .doesNotThrowAnyException();
+            } finally {
+                System.clearProperty("mail.mime.address.strict");
+            }
+        }
+
+        @Test
+        void 
onlyForHeaderFromDomainShouldReturnFalseWhenFromAddressIsMalformed() throws 
Exception {
+            System.setProperty("mail.mime.address.strict", "false");
+            try {
+                var mimeMessage = MimeMessageBuilder.mimeMessageBuilder()
+                    .setText("This is my email")
+                    .build();
+                mimeMessage.setHeader("From", "[email protected]>");
+
+                
assertThat(DKIMHook.DKIMCheckNeeded.onlyForHeaderFromDomain(Domain.of("fedex.com"))
+                    .test(FakeMail.builder()
+                        .name("mail")
+                        .sender("[email protected]")
+                        .mimeMessage(mimeMessage)
+                        .build()))
+                    .isFalse();
+            } finally {
+                System.clearProperty("mail.mime.address.strict");
+            }
+        }
     }
 
     @Nested


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to