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
commit abecfe9a0b749e2239b498bc6f791f40f260eb4e Author: Benoit TELLIER <btell...@linagora.com> AuthorDate: Fri Feb 23 09:21:24 2024 +0100 fixup! [FIX] Avoid forwarding bounces --- .../modules/ROOT/partials/RecipientRewriteTable.adoc | 5 ++++- .../transport/mailets/RecipientRewriteTable.java | 9 +++++++-- .../mailets/RecipientRewriteTableProcessor.java | 19 ++++++++++++++----- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/server/apps/distributed-app/docs/modules/ROOT/partials/RecipientRewriteTable.adoc b/server/apps/distributed-app/docs/modules/ROOT/partials/RecipientRewriteTable.adoc index d3fef29688..ec94fac455 100644 --- a/server/apps/distributed-app/docs/modules/ROOT/partials/RecipientRewriteTable.adoc +++ b/server/apps/distributed-app/docs/modules/ROOT/partials/RecipientRewriteTable.adoc @@ -16,4 +16,7 @@ Example: The *rewriteSenderUponForward* option (default to true) can be used to prevent senders to be rewritten upon forwards in the transport envelope (JAMES 3.8.0 default behaviour). *WARNING*: Please note that not rewriting the sender will cause issues forwarding emails from external senders to external addresses as the DKIM and SPF records will not be matching the ones of the sending -domain. \ No newline at end of file +domain. + +The *forwardAutoSubmittedEmails* option (default to false) can be used to prevent forwarding bounces as such a scenario +can lead to an infinite loop if the forward recipient bounces the email. \ No newline at end of file diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RecipientRewriteTable.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RecipientRewriteTable.java index 05b0bc3e10..f146f76a82 100644 --- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RecipientRewriteTable.java +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RecipientRewriteTable.java @@ -50,10 +50,13 @@ import com.google.common.collect.ImmutableList; * </code> * </pre> * - * The <b>rewriteSenderUponForward</b> option (fault to true) can be used to prevent senders to be rewritten upon forwards in the transport enveloppe + * The <b>rewriteSenderUponForward</b> option (default to true) can be used to prevent senders to be rewritten upon forwards in the transport enveloppe * (JAMES 3.8.0 default behaviour). <b>WARNING</b>: Please note that not rewriting the sender will cause issues forwarding emails * from external senders to external addresses as the DKIM and SPF records will not be matching the ones of the sending * domain. + * + * The <b>forwardAutoSubmittedEmails</b> option (default to false) can be used to prevent forwarding bounces as such a scenario + * can lead to an infinite loop if the forward recipient bounces the email. */ public class RecipientRewriteTable extends GenericMailet { public static final String ERROR_PROCESSOR = "errorProcessor"; @@ -63,6 +66,7 @@ public class RecipientRewriteTable extends GenericMailet { private RecipientRewriteTableProcessor processor; private ProcessingState errorProcessor; private boolean rewriteSenderUponForward = true; + private boolean forwardAutoSubmittedEmails = false; @Inject public RecipientRewriteTable(org.apache.james.rrt.api.RecipientRewriteTable virtualTableStore, DomainList domainList) { @@ -74,8 +78,9 @@ public class RecipientRewriteTable extends GenericMailet { public void init() throws MessagingException { errorProcessor = new ProcessingState(getInitParameter(ERROR_PROCESSOR, Mail.ERROR)); rewriteSenderUponForward = getBooleanParameter("rewriteSenderUponForward", true); + forwardAutoSubmittedEmails = getBooleanParameter("forwardAutoSubmittedEmails", false); processor = new RecipientRewriteTableProcessor(virtualTableStore, domainList, getMailetContext(), errorProcessor, - rewriteSenderUponForward); + rewriteSenderUponForward, forwardAutoSubmittedEmails); } 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 3220bf55a6..7f49c92719 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 @@ -71,6 +71,7 @@ import com.google.common.collect.Sets; public class RecipientRewriteTableProcessor { private static final Logger LOGGER = LoggerFactory.getLogger(RecipientRewriteTableProcessor.class); private static final boolean REWRITE_SENDER_UPON_FORWARD = true; + private static final boolean FORWARD_AUTOMATED_EMAILS = true; private static class Decision { private final MailAddress originalAddress; @@ -162,16 +163,19 @@ public class RecipientRewriteTableProcessor { private final Supplier<Domain> defaultDomainSupplier; private final ProcessingState errorProcessor; private final boolean rewriteSenderUponForward; + private final boolean forwardAutoSubmittedEmails; private final EnumSet<Mapping.Type> mappingTypes; public RecipientRewriteTableProcessor(RecipientRewriteTable virtualTableStore, DomainList domainList, - MailetContext mailetContext, ProcessingState errorProcessor, boolean rewriteSenderUponForward) { + MailetContext mailetContext, ProcessingState errorProcessor, boolean rewriteSenderUponForward, + boolean forwardAutoSubmittedEmails) { this.virtualTableStore = virtualTableStore; this.mailetContext = mailetContext; this.defaultDomainSupplier = MemoizedSupplier.of( Throwing.supplier(() -> getDefaultDomain(domainList)).sneakyThrow()); this.errorProcessor = errorProcessor; this.rewriteSenderUponForward = rewriteSenderUponForward; + this.forwardAutoSubmittedEmails = forwardAutoSubmittedEmails; if (rewriteSenderUponForward) { EnumSet<Mapping.Type> types = EnumSet.allOf(Mapping.Type.class); types.remove(Mapping.Type.Forward); @@ -182,7 +186,8 @@ public class RecipientRewriteTableProcessor { } public RecipientRewriteTableProcessor(RecipientRewriteTable virtualTableStore, DomainList domainList, MailetContext mailetContext) { - this(virtualTableStore, domainList, mailetContext, new ProcessingState(Mail.ERROR), !REWRITE_SENDER_UPON_FORWARD); + this(virtualTableStore, domainList, mailetContext, new ProcessingState(Mail.ERROR), !REWRITE_SENDER_UPON_FORWARD, + false); } private Domain getDefaultDomain(DomainList domainList) throws MessagingException { @@ -279,9 +284,7 @@ public class RecipientRewriteTableProcessor { } public void processForwards(Mail mail) throws MessagingException { - if (Optional.ofNullable(mail.getMessage().getHeader("Auto-Submitted")).map(ImmutableList::copyOf).orElse(ImmutableList.of()) - .stream() - .anyMatch(value -> value.startsWith("auto-replied"))) { + if (!forwardAutoSubmittedEmails && isAutoSubmitted(mail)) { return; } if (rewriteSenderUponForward) { @@ -292,6 +295,12 @@ public class RecipientRewriteTableProcessor { } } + private static boolean isAutoSubmitted(Mail mail) throws MessagingException { + return Optional.ofNullable(mail.getMessage().getHeader("Auto-Submitted")).map(ImmutableList::copyOf).orElse(ImmutableList.of()) + .stream() + .anyMatch(value -> value.startsWith("auto-replied")); + } + private Stream<ForwardDecision> processForward(Mail mail, MailAddress recipient) throws RecipientRewriteTableException { ImmutableSet<Mapping> forwards = getForwards(recipient); if (forwards.isEmpty()) { --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org For additional commands, e-mail: notifications-h...@james.apache.org