From 98243ef70ea6d081b5c4bc9fc02422fe31662916 Mon Sep 17 00:00:00 2001 From: titanism <101466223+titanism@users.noreply.github.com> Date: Wed, 8 Jan 2025 14:13:23 -0600 Subject: [PATCH] fix: fixed SRS rewrite for @forwardemail.net messages (it should not rewrite) --- helpers/on-data-mx.js | 31 ++++++++++++++++++++++++++----- helpers/process-email.js | 4 +++- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/helpers/on-data-mx.js b/helpers/on-data-mx.js index 4981af0eb5..fb68a1823f 100644 --- a/helpers/on-data-mx.js +++ b/helpers/on-data-mx.js @@ -1100,12 +1100,33 @@ async function forward(recipient, headers, session, body) { }; } + // + // TODO: check DSN issues (e.g. specify `recipient` field) + // + // + // > The DSN MUST be addressed (in both the message header and the + // transport envelope) to the return address from the transport envelope + // which accompanied the original message for which the DSN was + // generated. (For a message that arrived via SMTP, the envelope return + // address appears in the MAIL FROM command.) + // + // + // let from; - if (isSANB(session.envelope.mailFrom.address)) - from = srs.forward( - checkSRS(session.envelope.mailFrom.address), - env.WEB_HOST - ); + if (isSANB(session.envelope.mailFrom.address)) { + if ( + session.envelope.mailFrom.address + .toLowerCase() + .endsWith(`@${env.WEB_HOST}`) + ) { + from = session.envelope.mailFrom.address; + } else { + from = srs.forward( + checkSRS(session.envelope.mailFrom.address), + env.WEB_HOST + ); + } + } // NOTE: only webhooks use array for `to`, send email uses a string (we convert it) if (!_.isArray(recipient.to) || recipient.to.length > 1) diff --git a/helpers/process-email.js b/helpers/process-email.js index 475ab30fac..bd403c7d1e 100644 --- a/helpers/process-email.js +++ b/helpers/process-email.js @@ -645,7 +645,9 @@ async function processEmail({ email, port = 25, resolver, client }) { // rewrite envelope From with SRS using CNAME // (custom Return-Path but we don't add this header since IMAP does) const envelope = { - from: srs.forward(email.envelope.from, srsDomain), + from: email.envelope.from.toLowerCase().endsWith(`@${env.WEB_HOST}`) + ? email.envelope.from + : srs.forward(email.envelope.from, srsDomain), to: [...email.envelope.to] };