From 3efa96020b520e1cea7656fc4b82739c2f0bf636 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Thu, 26 Nov 2020 10:06:16 +0100 Subject: [PATCH 1/3] use warning level for invalid contact email --- email_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/email_handler.py b/email_handler.py index 10b062f9..2f30a097 100644 --- a/email_handler.py +++ b/email_handler.py @@ -180,7 +180,7 @@ def get_or_create_contact( _, contact_email = parseaddr_unicode(mail_from) if not is_valid_email(contact_email): - LOG.exception( + LOG.warning( "invalid contact email %s. Parse from %s %s", contact_email, contact_from_header, From 5b9eb8686ad0aa3f25facfbde9e90b7230b2b109 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Thu, 26 Nov 2020 10:08:09 +0100 Subject: [PATCH 2/3] add id to mailbox repr --- app/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models.py b/app/models.py index 996ccdaa..7950329f 100644 --- a/app/models.py +++ b/app/models.py @@ -1735,7 +1735,7 @@ class Mailbox(db.Model, ModelMixin): return ret def __repr__(self): - return f"" + return f"" class AccountActivation(db.Model, ModelMixin): From adfbfe80265fc7f693501fb46caa899f2f28f4c6 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Thu, 26 Nov 2020 10:27:23 +0100 Subject: [PATCH 3/3] Use load_public_key_and_check when adding new PGP key --- app/dashboard/views/contact_detail.py | 6 ++++-- app/dashboard/views/mailbox_detail.py | 6 ++++-- app/pgp_utils.py | 10 ++++++---- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/app/dashboard/views/contact_detail.py b/app/dashboard/views/contact_detail.py index ead29120..4d617b0b 100644 --- a/app/dashboard/views/contact_detail.py +++ b/app/dashboard/views/contact_detail.py @@ -4,7 +4,7 @@ from flask_login import login_required, current_user from app.dashboard.base import dashboard_bp from app.extensions import db from app.models import Contact -from app.pgp_utils import PGPException, load_public_key +from app.pgp_utils import PGPException, load_public_key, load_public_key_and_check @dashboard_bp.route("/contact//", methods=["GET", "POST"]) @@ -28,7 +28,9 @@ def contact_detail_route(contact_id): contact.pgp_public_key = request.form.get("pgp") try: - contact.pgp_finger_print = load_public_key(contact.pgp_public_key) + contact.pgp_finger_print = load_public_key_and_check( + contact.pgp_public_key + ) except PGPException: flash("Cannot add the public key, please verify it", "error") else: diff --git a/app/dashboard/views/mailbox_detail.py b/app/dashboard/views/mailbox_detail.py index eef8a0b8..cf3d1076 100644 --- a/app/dashboard/views/mailbox_detail.py +++ b/app/dashboard/views/mailbox_detail.py @@ -16,7 +16,7 @@ from app.extensions import db from app.log import LOG from app.models import Alias, AuthorizedAddress from app.models import Mailbox -from app.pgp_utils import PGPException, load_public_key +from app.pgp_utils import PGPException, load_public_key, load_public_key_and_check class ChangeEmailForm(FlaskForm): @@ -133,7 +133,9 @@ def mailbox_detail_route(mailbox_id): mailbox.pgp_public_key = request.form.get("pgp") try: - mailbox.pgp_finger_print = load_public_key(mailbox.pgp_public_key) + mailbox.pgp_finger_print = load_public_key_and_check( + mailbox.pgp_public_key + ) except PGPException: flash("Cannot add the public key, please verify it", "error") else: diff --git a/app/pgp_utils.py b/app/pgp_utils.py index 6dee55a5..4983a9e9 100644 --- a/app/pgp_utils.py +++ b/app/pgp_utils.py @@ -33,18 +33,20 @@ def load_public_key_and_check(public_key: str) -> str: If the encryption fails, remove the newly created fingerprint. Return the fingerprint """ - import_result = gpg.import_keys(public_key) try: + import_result = gpg.import_keys(public_key) fingerprint = import_result.fingerprints[0] except Exception as e: raise PGPException("Cannot load key") from e else: dummy_data = BytesIO(b"test") - r = gpg.encrypt_file(dummy_data, fingerprint) - if not r.ok: + try: + r = encrypt_file(dummy_data, fingerprint) + except Exception as e: + LOG.exception("Cannot encrypt using the imported key") # remove the fingerprint gpg.delete_keys([fingerprint]) - raise PGPException("Encryption fails with the key") + raise PGPException("Encryption fails with the key") from e return fingerprint