From 9c2a35193cd10f45d7293797b5d4f76c49790aa5 Mon Sep 17 00:00:00 2001
From: MrMeeb <mrmeeb@noreply.git.mrmeeb.stream>
Date: Tue, 28 Jan 2025 12:00:06 +0000
Subject: [PATCH] 4.64.4

---
 app/app/handler/unsubscribe_generator.py |  8 ++++++--
 app/email_handler.py                     | 15 ++++++++++-----
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/app/app/handler/unsubscribe_generator.py b/app/app/handler/unsubscribe_generator.py
index b4737d6..2bec8b9 100644
--- a/app/app/handler/unsubscribe_generator.py
+++ b/app/app/handler/unsubscribe_generator.py
@@ -2,8 +2,8 @@ import urllib
 from email.header import Header
 from email.message import Message
 
-from app.email import headers
 from app import config
+from app.email import headers
 from app.email_utils import add_or_replace_header, delete_header
 from app.handler.unsubscribe_encoder import (
     UnsubscribeEncoder,
@@ -46,7 +46,11 @@ class UnsubscribeGenerator:
             if start == -1 or end == -1 or start >= end:
                 continue
             method = raw_method[start + 1 : end]
-            url_data = urllib.parse.urlparse(method)
+            try:
+                url_data = urllib.parse.urlparse(method)
+            except ValueError:
+                LOG.debug(f"Unsub has invalid method {method}. Ignoring.")
+                continue
             if url_data.scheme == "mailto":
                 if url_data.path == config.UNSUBSCRIBER:
                     LOG.debug(
diff --git a/app/email_handler.py b/app/email_handler.py
index eef1e79..114ab31 100644
--- a/app/email_handler.py
+++ b/app/email_handler.py
@@ -47,6 +47,11 @@ from typing import List, Tuple, Optional
 import newrelic.agent
 from aiosmtpd.controller import Controller
 from aiosmtpd.smtp import Envelope
+from email_validator import validate_email, EmailNotValidError
+from flanker.addresslib import address
+from flanker.addresslib.address import EmailAddress
+from sqlalchemy.exc import IntegrityError
+
 from app import pgp_utils, s3, config, contact_utils
 from app.alias_utils import (
     try_auto_create,
@@ -169,12 +174,8 @@ from app.pgp_utils import (
     load_public_key_and_check,
 )
 from app.utils import sanitize_email
-from email_validator import validate_email, EmailNotValidError
-from flanker.addresslib import address
-from flanker.addresslib.address import EmailAddress
 from init_app import load_pgp_public_keys
 from server import create_light_app
-from sqlalchemy.exc import IntegrityError
 
 
 def get_or_create_contact(
@@ -601,7 +602,11 @@ def handle_forward(envelope, msg: Message, rcpt_to: str) -> List[Tuple[bool, str
                 for reply_to in reply_to_header_contents.split(",")
                 if reply_to.strip()
             ]:
-                reply_to_name, reply_to_email = parse_full_address(reply_to)
+                try:
+                    reply_to_name, reply_to_email = parse_full_address(reply_to)
+                except ValueError:
+                    LOG.d(f"Could not parse reply-to address {reply_to}")
+                    continue
                 if reply_to_email == alias.email:
                     LOG.i("Reply-to same as alias %s", alias)
                 else: