component email gateway: convert autocrypt header:

Autocrypt header must be transmitted in both directions to allow opportunistic end-to-end
encryption with this protocol.

Moved email validation regex to `tools/common/regex.py`, as it can be used in other
locations.

rel 456
This commit is contained in:
Goffi 2025-02-28 09:23:35 +01:00
commit 2a0f4e28f8
2 changed files with 11 additions and 4 deletions

View file

@ -57,7 +57,7 @@ from libervia.backend.plugins.plugin_comp_email_gateway.pubsub_service import (
EmailGWPubsubService,
)
from libervia.backend.plugins.plugin_exp_gre import GRE, GetDataHandler
from libervia.backend.plugins.plugin_sec_gre_encrypted_openpgp import NS_GRE_OPENPGP
from libervia.backend.plugins.plugin_sec_gre_encrypter_openpgp import NS_GRE_OPENPGP
from libervia.backend.plugins.plugin_sec_gre_formatter_mime import NS_GRE_MIME
from libervia.backend.plugins.plugin_xep_0033 import (
AddressType,
@ -69,6 +69,7 @@ from libervia.backend.plugins.plugin_xep_0106 import XEP_0106
from libervia.backend.plugins.plugin_xep_0131 import HeadersData, Urgency, XEP_0131
from libervia.backend.plugins.plugin_xep_0373 import binary_to_ascii_armor
from libervia.backend.plugins.plugin_xep_0498 import XEP_0498
from libervia.backend.tools.common import regex
from libervia.backend.tools.utils import aio
from .imap import IMAPClientFactory
@ -105,8 +106,6 @@ CONF_SECTION = f"component {IMPORT_NAME}"
PREFIX_KEY_CREDENTIALS = "CREDENTIALS_"
KEY_CREDENTIALS = f"{PREFIX_KEY_CREDENTIALS}{{from_jid}}"
email_pattern = re.compile(r"[^@]+@[^@]+\.[^@]+")
class FileMetadata(NamedTuple):
path: Path
@ -535,6 +534,8 @@ class EmailGatewayComponent(GetDataHandler):
else:
importance = urgency
msg["Importance"] = importance
if getattr(extra.headers, "autocrypt", None):
msg["Autocrypt"] = extra.headers.autocrypt
await smtp.sendmail(
credentials["smtp_host"].encode(),
@ -722,7 +723,7 @@ class EmailGatewayComponent(GetDataHandler):
if key == "user_email":
# XXX: This is a minimal check. A complete email validation is notoriously
# difficult.
if not email_pattern.match(value):
if not regex.RE_EMAIL.match(value):
raise StanzaError(
"bad-request", text=f"Invalid email address: {value}"
)
@ -907,6 +908,10 @@ class EmailGatewayComponent(GetDataHandler):
else:
log.warning("Ignoring invalid importance header: {importance!r}")
autocrypt = email["autocrypt"]
if autocrypt:
headers["autocrypt"] = autocrypt
if headers:
extra["headers"] = HeadersData(**headers).model_dump(
mode="json", exclude_none=True

View file

@ -31,6 +31,8 @@ RE_TEXT_URL = re.compile(r"[^a-zA-Z0-9,_]+")
TEXT_MAX_LEN = 60
# min lenght is currently deactivated
TEXT_WORD_MIN_LENGHT = 0
# basic email validation
RE_EMAIL = re.compile(r"[^@]+@[^@]+\.[^@]+")
def re_join(exps):