You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by rc...@apache.org on 2024/03/18 03:25:56 UTC

(james-project) branch master updated: JAMES-4021 Relay for "abc@def"@domain.com

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

rcordier 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 c7e8170754 JAMES-4021 Relay for "abc@def"@domain.com
c7e8170754 is described below

commit c7e817075475592b4879fe677d12c803586e6642
Author: Benoit TELLIER <bt...@linagora.com>
AuthorDate: Fri Mar 15 16:12:16 2024 +0100

    JAMES-4021 Relay for "abc@def"@domain.com
---
 .../rrt/lib/AbstractRecipientRewriteTable.java     | 13 ++++++++-
 .../rrt/lib/RecipientRewriteTableContract.java     |  5 ++++
 .../mailets/RecipientRewriteTableProcessor.java    | 19 +++++++++---
 .../remote/delivery/MailDelivrerToHost.java        |  2 +-
 .../james/mock/smtp/server/MockSMTPServerTest.java | 34 ++++++++++++++++++++++
 .../org/apache/james/SubAddressingRelayTest.java   | 25 ++++++++++++++++
 6 files changed, 92 insertions(+), 6 deletions(-)

diff --git a/server/data/data-library/src/main/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTable.java b/server/data/data-library/src/main/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTable.java
index f7daacbdf9..316e8e4e04 100644
--- a/server/data/data-library/src/main/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTable.java
+++ b/server/data/data-library/src/main/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTable.java
@@ -109,7 +109,18 @@ public abstract class AbstractRecipientRewriteTable implements RecipientRewriteT
     @Override
     public Mappings getResolvedMappings(String user, Domain domain, EnumSet<Type> mappingTypes) throws ErrorMappingException, RecipientRewriteTableException {
         Preconditions.checkState(this.configuration != null, "RecipientRewriteTable is not configured");
-        return getMappings(Username.fromLocalPartWithDomain(user, domain), configuration.getMappingLimit(), mappingTypes);
+        return asUsername(user, domain)
+            .map(Throwing.<Username, Mappings>function(username -> getMappings(username, configuration.getMappingLimit(), mappingTypes)).sneakyThrow())
+            .orElse(MappingsImpl.empty());
+    }
+
+    private static Optional<Username> asUsername(String user, Domain domain) {
+        try {
+            return Optional.of(Username.fromLocalPartWithDomain(user, domain));
+        } catch (IllegalArgumentException e) {
+            LOGGER.info("Valid mail address {}@{} could not be converted into a username. Assuming empty mappigns.", user, domain.asString(), e);
+            return Optional.empty();
+        }
     }
 
     private Mappings getMappings(Username username, int mappingLimit, EnumSet<Type> mappingTypes) throws ErrorMappingException, RecipientRewriteTableException {
diff --git a/server/data/data-library/src/test/java/org/apache/james/rrt/lib/RecipientRewriteTableContract.java b/server/data/data-library/src/test/java/org/apache/james/rrt/lib/RecipientRewriteTableContract.java
index fcfb49e4f6..a2767895bf 100644
--- a/server/data/data-library/src/test/java/org/apache/james/rrt/lib/RecipientRewriteTableContract.java
+++ b/server/data/data-library/src/test/java/org/apache/james/rrt/lib/RecipientRewriteTableContract.java
@@ -104,6 +104,11 @@ public interface RecipientRewriteTableContract {
         assertThat(virtualUserTable().getResolvedMappings("prefix_abc", domain)).isNotEmpty();
     }
 
+    @Test
+    default void testQuotedLocalPart() {
+        assertThatCode(() -> virtualUserTable().getResolvedMappings("\"a@b\"", Domain.of("test"))).doesNotThrowAnyException();
+    }
+
     @Test
     default void notConfiguredResolutionShouldThrow() throws Exception {
         setNotConfiguredRecipientRewriteTable();
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RecipientRewriteTableProcessor.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RecipientRewriteTableProcessor.java
index 1e2bd047ea..c5805137f9 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RecipientRewriteTableProcessor.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RecipientRewriteTableProcessor.java
@@ -343,10 +343,21 @@ public class RecipientRewriteTableProcessor {
     }
 
     private ImmutableSet<Mapping> getForwards(MailAddress recipient) throws RecipientRewriteTableException {
-        return virtualTableStore.getStoredMappings(MappingSource.fromMailAddress(recipient))
-            .select(Mapping.Type.Forward)
-            .asStream()
-            .collect(ImmutableSet.toImmutableSet());
+        return getSource(recipient)
+            .map(Throwing.<MappingSource, ImmutableSet<Mapping>>function(source -> virtualTableStore.getStoredMappings(source)
+                .select(Mapping.Type.Forward)
+                .asStream()
+                .collect(ImmutableSet.toImmutableSet())).sneakyThrow())
+            .orElse(ImmutableSet.of());
+    }
+
+    private static Optional<MappingSource> getSource(MailAddress recipient) {
+        try {
+            return Optional.of(MappingSource.fromMailAddress(recipient));
+        } catch (IllegalArgumentException e) {
+            LOGGER.info("Valid mail address {} could not be converted into a username. Assuming empty mappigns.", recipient.asString(), e);
+            return Optional.empty();
+        }
     }
 
     private void applyDecisionOnDSNParameters(Mail mail, List<Decision> decisions) {
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/MailDelivrerToHost.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/MailDelivrerToHost.java
index 34a369e5ff..cd56b9ce8e 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/MailDelivrerToHost.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/MailDelivrerToHost.java
@@ -184,7 +184,7 @@ public class MailDelivrerToHost {
                 mail.dsnParameters()
                     .flatMap(Throwing.<DsnParameters, Optional<DsnParameters.RecipientDsnParameters>>function(
                         dsn -> Optional.ofNullable(dsn.getRcptParameters().get(new MailAddress(address.toString()))))
-                        .sneakyThrow())
+                        .orReturn(Optional.empty()))
                     .flatMap(DsnParameters.RecipientDsnParameters::getNotifyParameter)
                     .map(this::toJavaxNotify),
                 address))
diff --git a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSMTPServerTest.java b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSMTPServerTest.java
index f423722b03..a0fad977cb 100644
--- a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSMTPServerTest.java
+++ b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSMTPServerTest.java
@@ -135,6 +135,40 @@ class MockSMTPServerTest {
                         }));
                 });
         }
+
+        @Test
+        void quotedLocalPartsShouldBeRelayed() throws Exception {
+            SMTPMessageSender sender = new SMTPMessageSender(DOMAIN)
+                .connect("localhost", mockServer.getPort());
+
+            MimeMessage message = MimeMessageBuilder.mimeMessageBuilder()
+                .setSubject("test")
+                .setText("any text")
+                .build();
+
+            FakeMail mail = FakeMail.builder()
+                .name("name")
+                .sender(BOB)
+                .recipients("\"alice@whatever\"@james.org")
+                .mimeMessage(message)
+                .build();
+
+            sender.sendMessage(mail);
+
+            Awaitility.await().atMost(TEN_SECONDS)
+                .untilAsserted(() -> {
+                    List<Mail> mails = mailRepository.list();
+                    Mail.Envelope expectedEnvelope = Mail.Envelope.ofAddresses(
+                        new MailAddress(BOB),
+                        new MailAddress("\"alice@whatever\"@james.org"));
+                    assertThat(mails)
+                        .hasSize(1)
+                        .allSatisfy(Throwing.consumer(assertedMail -> {
+                            assertThat(assertedMail.getEnvelope()).isEqualTo(expectedEnvelope);
+                            assertThat(assertedMail.getMessage()).contains(MimeMessageUtil.asString(message));
+                        }));
+                });
+        }
     }
 
     @Nested
diff --git a/server/mailet/remote-delivery-integration-testing/src/test/java/org/apache/james/SubAddressingRelayTest.java b/server/mailet/remote-delivery-integration-testing/src/test/java/org/apache/james/SubAddressingRelayTest.java
index 896d9d44da..bf008ad1ac 100644
--- a/server/mailet/remote-delivery-integration-testing/src/test/java/org/apache/james/SubAddressingRelayTest.java
+++ b/server/mailet/remote-delivery-integration-testing/src/test/java/org/apache/james/SubAddressingRelayTest.java
@@ -145,6 +145,31 @@ class SubAddressingRelayTest {
                 .build()));
     }
 
+    @Test
+    void remoteDeliveryShouldAcceptQuotedMailAddress(DockerMockSmtp mockSmtp) throws Exception {
+        AuthenticatingSMTPClient smtpClient = new AuthenticatingSMTPClient("TLS", "UTF-8");
+
+        try {
+            smtpClient.connect("localhost", jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpPort().getValue());
+            smtpClient.ehlo(DEFAULT_DOMAIN);
+            smtpClient.mail("<" + FROM + ">");
+            smtpClient.rcpt("<\"...@other.com> NOTIFY=FAILURE,DELAY");
+            smtpClient.sendShortMessageData("A short message...");
+        } finally {
+            smtpClient.disconnect();
+        }
+
+        calmlyAwait.atMost(TEN_SECONDS).untilAsserted(() -> assertThat(mockSmtp.getConfigurationClient().listMails())
+            .hasSize(1)
+            .extracting(Mail::getEnvelope)
+            .containsOnly(Mail.Envelope.builder()
+                .from(new MailAddress(FROM))
+                .addRecipient(Mail.Recipient.builder()
+                    .address(new MailAddress("\"abc@def\"@other.com"))
+                    .build())
+                .build()));
+    }
+
     private ProcessorConfiguration.Builder directResolutionTransport() {
         return ProcessorConfiguration.transport()
             .addMailet(MailetConfiguration.BCC_STRIPPER)


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org