From ce2d68a64d37f8b89c0e5e1911faf316e7da2052 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Sun, 6 Dec 2020 19:37:20 +0100 Subject: [PATCH] take into account include_sender_in_reverse_alias when creating reverse-alias --- app/api/views/alias.py | 2 +- app/dashboard/views/alias_contact_manager.py | 2 +- app/email_utils.py | 15 ++++++-- email_handler.py | 4 +- tests/test_email_utils.py | 40 +++++++++++++++++--- 5 files changed, 49 insertions(+), 14 deletions(-) diff --git a/app/api/views/alias.py b/app/api/views/alias.py index 14d65ae4..e737f1c9 100644 --- a/app/api/views/alias.py +++ b/app/api/views/alias.py @@ -410,7 +410,7 @@ def create_contact_route(alias_id): alias_id=alias.id, website_email=contact_email, name=contact_name, - reply_email=generate_reply_email(contact_email), + reply_email=generate_reply_email(contact_email, user), ) LOG.d("create reverse-alias for %s %s", contact_addr, alias) diff --git a/app/dashboard/views/alias_contact_manager.py b/app/dashboard/views/alias_contact_manager.py index fad783d1..31e3750b 100644 --- a/app/dashboard/views/alias_contact_manager.py +++ b/app/dashboard/views/alias_contact_manager.py @@ -203,7 +203,7 @@ def alias_contact_manager(alias_id): alias_id=alias.id, website_email=contact_email, name=contact_name, - reply_email=generate_reply_email(contact_email), + reply_email=generate_reply_email(contact_email, current_user), ) LOG.d("create reverse-alias for %s", contact_addr) diff --git a/app/email_utils.py b/app/email_utils.py index 654e16fd..bacc9ce2 100644 --- a/app/email_utils.py +++ b/app/email_utils.py @@ -802,7 +802,7 @@ def replace(msg: Message, old, new) -> Message: return msg -def generate_reply_email(contact_email: str) -> str: +def generate_reply_email(contact_email: str, user: User) -> str: """ generate a reply_email (aka reverse-alias), make sure it isn't used by any contact """ @@ -811,7 +811,14 @@ def generate_reply_email(contact_email: str) -> str: # "The maximum total length of a user name or other local-part is 64 # octets." - if contact_email: + # todo: turns this to False after Dec 20 2020 + include_sender_in_reverse_alias = True + + # user has chosen an option explicitly + if user.include_sender_in_reverse_alias is not None: + include_sender_in_reverse_alias = user.include_sender_in_reverse_alias + + if include_sender_in_reverse_alias and contact_email: # control char: 4 chars (ra+, +) # random suffix: max 10 chars # maximum: 64 @@ -825,13 +832,13 @@ def generate_reply_email(contact_email: str) -> str: # not use while to avoid infinite loop for _ in range(1000): - if contact_email: + if include_sender_in_reverse_alias and contact_email: random_length = random.randint(5, 10) reply_email = ( f"ra+{contact_email}+{random_string(random_length)}@{EMAIL_DOMAIN}" ) else: - random_length = random.randint(10, 50) + random_length = random.randint(20, 50) reply_email = f"ra+{random_string(random_length)}@{EMAIL_DOMAIN}" if not Contact.get_by(reply_email=reply_email): diff --git a/email_handler.py b/email_handler.py index ec47bc5e..2134a6e9 100644 --- a/email_handler.py +++ b/email_handler.py @@ -229,7 +229,7 @@ def get_or_create_contact(from_header: str, mail_from: str, alias: Alias) -> Con name=contact_name, mail_from=mail_from, from_header=from_header, - reply_email=generate_reply_email(contact_email) + reply_email=generate_reply_email(contact_email, alias.user) if is_valid_email(contact_email) else NOREPLY, ) @@ -292,7 +292,7 @@ def replace_header_when_forward(msg: Message, alias: Alias, header: str): alias_id=alias.id, website_email=contact_email, name=contact_name, - reply_email=generate_reply_email(contact_email), + reply_email=generate_reply_email(contact_email, alias.user), is_cc=header.lower() == "cc", from_header=addr, ) diff --git a/tests/test_email_utils.py b/tests/test_email_utils.py index 5f46bffc..1a0646f5 100644 --- a/tests/test_email_utils.py +++ b/tests/test_email_utils.py @@ -25,6 +25,7 @@ from app.email_utils import ( ) from app.extensions import db from app.models import User, CustomDomain +from tests.utils import login def test_get_email_domain_part(): @@ -464,23 +465,50 @@ def test_to_bytes(): def test_generate_reply_email(flask_client): - reply_email = generate_reply_email("test@example.org") + user = User.create( + email="a@b.c", + password="password", + name="Test User", + activated=True, + ) + reply_email = generate_reply_email("test@example.org", user) # return something like - # ra+test.at.example.org+gjbnnddll@sl.local - assert reply_email.startswith("ra+test.at.example.org+") + # ra+@sl.local assert reply_email.endswith(EMAIL_DOMAIN) - reply_email = generate_reply_email("") + reply_email = generate_reply_email("", user) # return something like # ra+qdrcxzppngmvtajklnhqvvuyyzgkyityrzjwikk@sl.local assert reply_email.startswith("ra+") assert reply_email.endswith(EMAIL_DOMAIN) - reply_email = generate_reply_email("👌汉字@example.org") + +def test_generate_reply_email_include_sender_in_reverse_alias(flask_client): + # user enables include_sender_in_reverse_alias + user = User.create( + email="a@b.c", + password="password", + name="Test User", + activated=True, + include_sender_in_reverse_alias=True, + ) + reply_email = generate_reply_email("test@example.org", user) + # return something like + # ra+test.at.example.org+gjbnnddll@sl.local + assert reply_email.startswith("ra+test.at.example.org+") + assert reply_email.endswith(EMAIL_DOMAIN) + + reply_email = generate_reply_email("", user) + # return something like + # ra+qdrcxzppngmvtajklnhqvvuyyzgkyityrzjwikk@sl.local + assert reply_email.startswith("ra+") + assert reply_email.endswith(EMAIL_DOMAIN) + + reply_email = generate_reply_email("👌汉字@example.org", user) assert reply_email.startswith("ra+yizi.at.example.org+") # make sure reply_email only contain lowercase - reply_email = generate_reply_email("TEST@example.org") + reply_email = generate_reply_email("TEST@example.org", user) assert reply_email.startswith("ra+test.at.example.org")