From 53e92812043b31869445610bfd149ad03e02b382 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Tue, 9 Jun 2020 17:16:32 +0200 Subject: [PATCH] avoid forward email to invalid mailbox --- email_handler.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/email_handler.py b/email_handler.py index b4723360..db709505 100644 --- a/email_handler.py +++ b/email_handler.py @@ -31,7 +31,6 @@ It should contain the following info: """ import email -import os import time import uuid from email import encoders @@ -79,6 +78,7 @@ from app.email_utils import ( get_orig_message_from_spamassassin_report, parseaddr_unicode, send_email_with_rate_control, + get_email_domain_part, ) from app.extensions import db from app.greylisting import greylisting_needed @@ -405,6 +405,17 @@ def forward_email_to_mailbox( user, ) -> (bool, str): LOG.d("Forward %s -> %s -> %s", contact, alias, mailbox) + + # sanity check: make sure mailbox is not actually an alias + if get_email_domain_part(alias.email) == get_email_domain_part(mailbox.email): + LOG.error( + "Mailbox has the same domain as alias. %s -> %s -> %s", + contact, + alias, + mailbox, + ) + return False, "550 SL E14" + is_spam, spam_status = get_spam_info(msg) if is_spam: LOG.warning("Email detected as spam. Alias: %s, from: %s", alias, contact)