rename is_reply_email -> is_reverse_alias, make sure reverse-alias must end with EMAIL_DOMAIN

This commit is contained in:
Son 2021-10-19 12:14:16 +02:00
parent c5987bcfbb
commit be7ae3021a
4 changed files with 16 additions and 13 deletions

View File

@ -6,7 +6,7 @@ from app.config import (
MAX_ACTIVITY_DURING_MINUTE_PER_MAILBOX,
)
from app.db import Session
from app.email_utils import is_reply_email
from app.email_utils import is_reverse_alias
from app.log import LOG
from app.models import Alias, EmailLog, Contact
@ -97,7 +97,7 @@ def rate_limited(mail_from: str, rcpt_tos: [str]) -> bool:
return False
for rcpt_to in rcpt_tos:
if is_reply_email(rcpt_to):
if is_reverse_alias(rcpt_to):
if rate_limited_reply_phase(rcpt_to):
return True
else:

View File

@ -1032,8 +1032,8 @@ def generate_reply_email(contact_email: str, user: User) -> str:
raise Exception("Cannot generate reply email")
def is_reply_email(address: str) -> bool:
return address.startswith("reply+") or address.startswith("ra+")
def is_reverse_alias(address: str) -> bool:
return address.endswith(f"@{EMAIL_DOMAIN}") and (address.startswith("reply+") or address.startswith("ra+"))
# allow also + and @ that are present in a reply address

View File

@ -109,7 +109,7 @@ from app.email_utils import (
add_header,
get_header_unicode,
generate_reply_email,
is_reply_email,
is_reverse_alias,
normalize_reply_email,
is_valid_email,
replace,
@ -1937,7 +1937,7 @@ def handle(envelope: Envelope) -> str:
except ValueError:
LOG.d("cannot parse the From header %s", from_header)
else:
if is_reply_email(from_header_address):
if is_reverse_alias(from_header_address):
LOG.e("email sent from a reverse alias %s", from_header_address)
# get more info for debug
contact = Contact.get_by(reply_email=from_header_address)
@ -1975,7 +1975,7 @@ def handle(envelope: Envelope) -> str:
# Handle "out of office" auto notice. An automatic response is sent for every forwarded email
# todo: remove logging
if len(rcpt_tos) == 1 and is_reply_email(rcpt_tos[0]) and mail_from == "<>":
if len(rcpt_tos) == 1 and is_reverse_alias(rcpt_tos[0]) and mail_from == "<>":
LOG.w(
"out-of-office email to reverse alias %s. %s", rcpt_tos[0], msg.as_string()
)
@ -2001,7 +2001,7 @@ def handle(envelope: Envelope) -> str:
# Reply case
# recipient starts with "reply+" or "ra+" (ra=reverse-alias) prefix
if is_reply_email(rcpt_to):
if is_reverse_alias(rcpt_to):
LOG.d(
"Reply phase %s(%s) -> %s", mail_from, copy_msg[headers.FROM], rcpt_to
)

View File

@ -2,7 +2,7 @@ from flask import url_for
from app.config import PAGE_LIMIT
from app.db import Session
from app.email_utils import is_reply_email
from app.email_utils import is_reverse_alias
from app.models import User, ApiKey, Alias, Contact, EmailLog, Mailbox
from tests.utils import login
@ -649,7 +649,10 @@ def test_get_alias(flask_client):
assert "pinned" in res
def test_is_reply_email(flask_client):
assert is_reply_email("ra+abcd@test.org")
assert is_reply_email("reply+abcd@test.org")
assert not is_reply_email("abcd@test.org")
def test_is_reverse_alias(flask_client):
assert is_reverse_alias("ra+abcd@sl.local")
assert is_reverse_alias("reply+abcd@sl.local")
assert not is_reverse_alias("ra+abcd@test.org")
assert not is_reverse_alias("reply+abcd@test.org")
assert not is_reverse_alias("abcd@test.org")