From 4edaeb706a1b3f235f6e7acb177337fb18fc0a10 Mon Sep 17 00:00:00 2001 From: Cathy Hu Date: Fri, 11 Sep 2020 22:03:02 +0200 Subject: [PATCH] [email] Limit messages to subscribers --- kibicara/platforms/email/mda.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/kibicara/platforms/email/mda.py b/kibicara/platforms/email/mda.py index 0a08b76..7876520 100644 --- a/kibicara/platforms/email/mda.py +++ b/kibicara/platforms/email/mda.py @@ -7,9 +7,10 @@ from asyncio import run as asyncio_run from email.parser import BytesParser from email.policy import default +from email.utils import parseaddr from fastapi import status from kibicara.config import args, config -from kibicara.platforms.email.model import Email +from kibicara.platforms.email.model import Email, EmailSubscribers from logging import getLogger from ormantic import NoMatch from re import sub @@ -36,6 +37,25 @@ class Main: # read mail from STDIN and parse to EmailMessage object message = BytesParser(policy=default).parsebytes(stdin.buffer.read()) + sender = '' + if message.get('sender'): + sender = message.get('sender') + elif message.get('from'): + sender = message.get('from') + else: + logger.error('No Sender of From header') + exit(1) + + sender = parseaddr(sender)[1] + if not sender: + logger.error('Could not parse sender') + exit(1) + + maybe_subscriber = await EmailSubscribers.objects.filter(email=sender).all() + if len(maybe_subscriber) != 1 or maybe_subscriber[0].hood.id != email.id: + logger.error('Not a subscriber') + exit(1) + # extract relevant data from mail text = sub( r'<[^>]*>',