Selaa lähdekoodia

sanitize envelope mail_from and rcpt_tos

Son NK 4 vuotta sitten
vanhempi
commit
ed2e748d1e
3 muutettua tiedostoa jossa 18 lisäystä ja 15 poistoa
  1. 0 1
      app/alias_utils.py
  2. 0 1
      app/greylisting.py
  3. 18 13
      email_handler.py

+ 0 - 1
app/alias_utils.py

@@ -150,7 +150,6 @@ def try_auto_create_catch_all_domain(address: str) -> Optional[Alias]:
         return alias
         return alias
 
 
 
 
-
 def delete_alias(alias: Alias, user: User):
 def delete_alias(alias: Alias, user: User):
     """
     """
     Delete an alias and add it to either global or domain trash
     Delete an alias and add it to either global or domain trash

+ 0 - 1
app/greylisting.py

@@ -95,7 +95,6 @@ def greylisting_needed_reply_phase(reply_email: str) -> bool:
 
 
 def greylisting_needed(mail_from: str, rcpt_tos: [str]) -> bool:
 def greylisting_needed(mail_from: str, rcpt_tos: [str]) -> bool:
     for rcpt_to in rcpt_tos:
     for rcpt_to in rcpt_tos:
-        rcpt_to = rcpt_to.lower().strip().replace(" ", "")
         if rcpt_to.startswith("reply+") or rcpt_to.startswith("ra+"):
         if rcpt_to.startswith("reply+") or rcpt_to.startswith("ra+"):
             if greylisting_needed_reply_phase(rcpt_to):
             if greylisting_needed_reply_phase(rcpt_to):
                 return True
                 return True

+ 18 - 13
email_handler.py

@@ -1391,42 +1391,47 @@ def handle_sender_email(envelope: Envelope):
 
 
 async def handle(envelope: Envelope, smtp: SMTP) -> str:
 async def handle(envelope: Envelope, smtp: SMTP) -> str:
     """Return SMTP status"""
     """Return SMTP status"""
+
+    # sanitize mail_from, rcpt_tos
+    mail_from = envelope.mail_from.lower().strip().replace(" ", "")
+    rcpt_tos = [
+        rcpt_to.lower().strip().replace(" ", "") for rcpt_to in envelope.rcpt_tos
+    ]
+    envelope.mail_from = mail_from
+    envelope.rcpt_tos = rcpt_tos
+
     # unsubscribe request
     # unsubscribe request
-    if UNSUBSCRIBER and envelope.rcpt_tos == [UNSUBSCRIBER]:
-        LOG.d("Handle unsubscribe request from %s", envelope.mail_from)
+    if UNSUBSCRIBER and rcpt_tos == [UNSUBSCRIBER]:
+        LOG.d("Handle unsubscribe request from %s", mail_from)
         return handle_unsubscribe(envelope)
         return handle_unsubscribe(envelope)
 
 
     # emails sent to sender. Probably bounce emails
     # emails sent to sender. Probably bounce emails
-    if SENDER and envelope.rcpt_tos == [SENDER]:
-        LOG.d("Handle email sent to sender from %s", envelope.mail_from)
+    if SENDER and rcpt_tos == [SENDER]:
+        LOG.d("Handle email sent to sender from %s", mail_from)
         return handle_sender_email(envelope)
         return handle_sender_email(envelope)
 
 
     # Whether it's necessary to apply greylisting
     # Whether it's necessary to apply greylisting
-    if greylisting_needed(envelope.mail_from, envelope.rcpt_tos):
-        LOG.warning(
-            "Grey listing applied for %s %s", envelope.mail_from, envelope.rcpt_tos
-        )
+    if greylisting_needed(mail_from, rcpt_tos):
+        LOG.warning("Grey listing applied for %s %s", mail_from, rcpt_tos)
         return "421 SL Retry later"
         return "421 SL Retry later"
 
 
     # result of all deliveries
     # result of all deliveries
     # each element is a couple of whether the delivery is successful and the smtp status
     # each element is a couple of whether the delivery is successful and the smtp status
     res: [(bool, str)] = []
     res: [(bool, str)] = []
 
 
-    for rcpt_to in envelope.rcpt_tos:
+    for rcpt_to in rcpt_tos:
         msg = email.message_from_bytes(envelope.original_content)
         msg = email.message_from_bytes(envelope.original_content)
 
 
         # Reply case
         # Reply case
         # recipient starts with "reply+" or "ra+" (ra=reverse-alias) prefix
         # recipient starts with "reply+" or "ra+" (ra=reverse-alias) prefix
         if rcpt_to.startswith("reply+") or rcpt_to.startswith("ra+"):
         if rcpt_to.startswith("reply+") or rcpt_to.startswith("ra+"):
-            LOG.debug(
-                ">>> Reply phase %s(%s) -> %s", envelope.mail_from, msg["From"], rcpt_to
-            )
+            LOG.debug(">>> Reply phase %s(%s) -> %s", mail_from, msg["From"], rcpt_to)
             is_delivered, smtp_status = await handle_reply(envelope, smtp, msg, rcpt_to)
             is_delivered, smtp_status = await handle_reply(envelope, smtp, msg, rcpt_to)
             res.append((is_delivered, smtp_status))
             res.append((is_delivered, smtp_status))
         else:  # Forward case
         else:  # Forward case
             LOG.debug(
             LOG.debug(
                 ">>> Forward phase %s(%s) -> %s",
                 ">>> Forward phase %s(%s) -> %s",
-                envelope.mail_from,
+                mail_from,
                 msg["From"],
                 msg["From"],
                 rcpt_to,
                 rcpt_to,
             )
             )