From bb5fba8a3ef8469f57db7e003b2827286d31fbe8 Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Wed, 16 Jan 2019 12:07:24 +0100 Subject: [PATCH 01/19] FIX - Django settings fix to unicode --- django_mailbox/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_mailbox/utils.py b/django_mailbox/utils.py index 48e76be3..2abec690 100644 --- a/django_mailbox/utils.py +++ b/django_mailbox/utils.py @@ -66,7 +66,7 @@ def get_settings(): ), 'default_charset': getattr( settings, - 'DJANGO_MAILBOX_default_charset', + 'DJANGO_MAILBOX_DEFAULT_CHARSET', 'iso8859-1', ) } From 724e804bcb3a49f85c9c52e93ec8141255b90a65 Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Wed, 16 Jan 2019 12:25:27 +0100 Subject: [PATCH 02/19] FIX - Fix default charset --- django_mailbox/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_mailbox/utils.py b/django_mailbox/utils.py index 2abec690..eda4f35f 100644 --- a/django_mailbox/utils.py +++ b/django_mailbox/utils.py @@ -67,7 +67,7 @@ def get_settings(): 'default_charset': getattr( settings, 'DJANGO_MAILBOX_DEFAULT_CHARSET', - 'iso8859-1', + 'utf8', ) } From b797216c7fa0ccfff6c1ab1a9632ea0de5fbd7e0 Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Thu, 17 Jan 2019 16:55:44 +0100 Subject: [PATCH 03/19] Update models.py --- django_mailbox/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 9f7ffc21..a84ac258 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -353,7 +353,7 @@ def _process_message(self, message): msg.mailbox = self if 'subject' in message: msg.subject = ( - utils.convert_header_to_unicode(message['subject'])[0:255] + utils.convert_header_to_unicode(unicode(message['subject']).decode('utf-8'))[0:255] ) if 'message-id' in message: msg.message_id = message['message-id'][0:255].strip() From 3f22f481e5a49a29c8d73606921edd493c69bb85 Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Fri, 18 Jan 2019 12:02:07 +0100 Subject: [PATCH 04/19] Update models.py --- django_mailbox/models.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index a84ac258..11f766af 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -351,10 +351,12 @@ def _process_message(self, message): if settings['store_original_message']: self._process_save_original_message(message, msg) msg.mailbox = self + # Fix to accept subject emojis in utf-8 if 'subject' in message: msg.subject = ( utils.convert_header_to_unicode(unicode(message['subject']).decode('utf-8'))[0:255] ) + msg.subject = repr(email.header.decode_header(msg.subject)[0][0]) if 'message-id' in message: msg.message_id = message['message-id'][0:255].strip() if 'from' in message: From b390b28f11a1b4116286ca1e7fb36feab9c1c9af Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Fri, 18 Jan 2019 12:38:03 +0100 Subject: [PATCH 05/19] Update models.py --- django_mailbox/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 11f766af..a3439895 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -356,7 +356,7 @@ def _process_message(self, message): msg.subject = ( utils.convert_header_to_unicode(unicode(message['subject']).decode('utf-8'))[0:255] ) - msg.subject = repr(email.header.decode_header(msg.subject)[0][0]) + msg.subject = repr(email.header.decode_header(msg.subject)[0][0]).replace("'", "") if 'message-id' in message: msg.message_id = message['message-id'][0:255].strip() if 'from' in message: From 72582abb2d33aff03f08918d006a20142bc7aaca Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Tue, 9 Apr 2019 12:07:00 +0200 Subject: [PATCH 06/19] Update admin.py --- django_mailbox/admin.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/django_mailbox/admin.py b/django_mailbox/admin.py index c586b406..9ee4fbc2 100644 --- a/django_mailbox/admin.py +++ b/django_mailbox/admin.py @@ -8,6 +8,7 @@ import logging +from django import forms from django.conf import settings from django.contrib import admin from django.utils.translation import ugettext_lazy as _ @@ -39,8 +40,15 @@ def resend_message_received_signal(message_admin, request, queryset): _('Re-send message received signal') ) +class MailboxForm(forms.ModelForm): + uri = forms.CharField(widget=forms.PasswordInput) + + class Meta: + model = Mailbox + fields = ('name', 'uri', 'from_email', 'active',) class MailboxAdmin(admin.ModelAdmin): + form = MailboxForm list_display = ( 'name', 'uri', @@ -51,6 +59,10 @@ class MailboxAdmin(admin.ModelAdmin): readonly_fields = ['last_polling', ] actions = [get_new_mail] + def save_model(self, request, obj, form, change): + if request: + obj.uri = obj.encrypt_uri() + obj.save() class MessageAttachmentAdmin(admin.ModelAdmin): raw_id_fields = ('message', ) From 11a05a8e0980c876c7a69b29cf4af3ecd146c596 Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Tue, 9 Apr 2019 12:09:55 +0200 Subject: [PATCH 07/19] Encrypt, decrypt and padding in model methods --- django_mailbox/models.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index a3439895..221c410e 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -17,6 +17,7 @@ import sys import uuid from tempfile import NamedTemporaryFile +from Crypto.Cipher import AES import six from six.moves.urllib.parse import parse_qs, unquote, urlparse @@ -113,6 +114,33 @@ class Mailbox(models.Model): objects = models.Manager() active_mailboxes = ActiveMailboxManager() + def pad(self, key): + length = 32 - (len(key) % 32) + return key + chr(length).encode('utf-8') * length + + def unpad(self, key): + return key[0:-ord(key[-1])] + + def encrypt_uri(self): + secret_key = self.pad(django_settings.SECRET_KEY) + self.uri = unicode(self.pad(self.uri)).encode('utf-8') + + cipher = AES.new(secret_key) + self.uri = cipher.encrypt(self.uri) + self.uri = base64.b64encode(self.uri) + + return self.uri + + def decrypt_uri(self): + secret_key = self.pad(django_settings.SECRET_KEY) + self.uri = self.pad(self.uri) + + cipher = AES.new(secret_key) + self.uri = base64.b64decode(self.uri) + self.uri = cipher.decrypt(self.uri) + + return self.unpad(self.uri) + @property def _protocol_info(self): return urlparse(self.uri) From 850f86aef70f1939f098e9107057cfeeb85897cf Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Tue, 9 Apr 2019 12:11:06 +0200 Subject: [PATCH 08/19] Pycryto added in requirements --- rtd_requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/rtd_requirements.txt b/rtd_requirements.txt index 18684b76..8e20bd23 100644 --- a/rtd_requirements.txt +++ b/rtd_requirements.txt @@ -1,2 +1,3 @@ django>=1.9,<1.10 six +pycrypto==2.6.1 From e073a8c32c36fa757a17f7d654c7d7df805078f9 Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Tue, 9 Apr 2019 13:54:08 +0200 Subject: [PATCH 09/19] Remove URI from list_display --- django_mailbox/admin.py | 1 - 1 file changed, 1 deletion(-) diff --git a/django_mailbox/admin.py b/django_mailbox/admin.py index 9ee4fbc2..857bf21c 100644 --- a/django_mailbox/admin.py +++ b/django_mailbox/admin.py @@ -51,7 +51,6 @@ class MailboxAdmin(admin.ModelAdmin): form = MailboxForm list_display = ( 'name', - 'uri', 'from_email', 'active', 'last_polling', From cd1e4de42b5b09dc4576201c43c18f47a7c2dcdb Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Tue, 9 Apr 2019 14:06:01 +0200 Subject: [PATCH 10/19] Help text in uri form --- django_mailbox/admin.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/django_mailbox/admin.py b/django_mailbox/admin.py index 857bf21c..b953947c 100644 --- a/django_mailbox/admin.py +++ b/django_mailbox/admin.py @@ -41,7 +41,19 @@ def resend_message_received_signal(message_admin, request, queryset): ) class MailboxForm(forms.ModelForm): - uri = forms.CharField(widget=forms.PasswordInput) + uri = forms.CharField(widget=forms.PasswordInput, + help_text="For security, the URI will not be shown and will " + "be encrypted in database " + "
" + "Example: imap+ssl://myusername:mypassword@someserver
" + "
" + "Internet transports include 'imap' and 'pop3'; " + "common local file transports include 'maildir', 'mbox', " + "and less commonly 'babyl', 'mh', and 'mmdf'.
" + "
" + "Be sure to urlencode your username and password should they " + "contain illegal characters (like @, :, etc)." + ) class Meta: model = Mailbox From 6f23c59ca8305aa93b87596702670be77c1173f1 Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Tue, 9 Apr 2019 15:18:24 +0200 Subject: [PATCH 11/19] Decrypt URI in _protocol_info() method --- django_mailbox/models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 221c410e..9d60a78b 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -143,6 +143,7 @@ def decrypt_uri(self): @property def _protocol_info(self): + self.uri = self.decrypt_uri() return urlparse(self.uri) @property From 934c7992c19971283f6e909b87148c4176f23c92 Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Tue, 9 Apr 2019 15:41:59 +0200 Subject: [PATCH 12/19] Update setup.py --- setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index d6e46258..7eaed7e2 100755 --- a/setup.py +++ b/setup.py @@ -54,6 +54,7 @@ packages=find_packages(), include_package_data=True, install_requires=[ - 'six>=1.6.1' + 'six>=1.6.1', + 'pycrypto==2.6.1' ] ) From 0d5f395c0004129079924f45a65ae9a218980359 Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Fri, 12 Apr 2019 10:30:02 +0200 Subject: [PATCH 13/19] Fix decrypt_uri() and _protocol_info() methods --- django_mailbox/models.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 9d60a78b..650d523c 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -133,18 +133,20 @@ def encrypt_uri(self): def decrypt_uri(self): secret_key = self.pad(django_settings.SECRET_KEY) - self.uri = self.pad(self.uri) + uri = self.uri + uri = self.pad(uri) + uri = base64.b64decode(uri) cipher = AES.new(secret_key) - self.uri = base64.b64decode(self.uri) - self.uri = cipher.decrypt(self.uri) + uri = cipher.decrypt(uri) + + return self.unpad(uri) - return self.unpad(self.uri) - @property def _protocol_info(self): - self.uri = self.decrypt_uri() - return urlparse(self.uri) + uri = self.uri + uri = self.decrypt_uri() + return urlparse(uri) @property def _query_string(self): From 6c6b23ce71161b42a6b2252e6d92ad27b457ea98 Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Fri, 24 May 2019 12:28:13 +0200 Subject: [PATCH 14/19] Update models.py --- django_mailbox/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 650d523c..0508f363 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -385,9 +385,9 @@ def _process_message(self, message): # Fix to accept subject emojis in utf-8 if 'subject' in message: msg.subject = ( - utils.convert_header_to_unicode(unicode(message['subject']).decode('utf-8'))[0:255] + utils.convert_header_to_unicode(message['subject']).encode('raw-unicode-escape'))[0:255] ) - msg.subject = repr(email.header.decode_header(msg.subject)[0][0]).replace("'", "") + msg.subject = unicode(email.header.decode_header(msg.subject)[0][0]), errors='ignore') if 'message-id' in message: msg.message_id = message['message-id'][0:255].strip() if 'from' in message: From e713b5c82fffe837b5c7344385373a513a8542f9 Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Fri, 24 May 2019 12:36:19 +0200 Subject: [PATCH 15/19] Update models.py --- django_mailbox/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 0508f363..156fd668 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -385,7 +385,7 @@ def _process_message(self, message): # Fix to accept subject emojis in utf-8 if 'subject' in message: msg.subject = ( - utils.convert_header_to_unicode(message['subject']).encode('raw-unicode-escape'))[0:255] + utils.convert_header_to_unicode(message['subject'].encode('raw-unicode-escape'))[0:255] ) msg.subject = unicode(email.header.decode_header(msg.subject)[0][0]), errors='ignore') if 'message-id' in message: From 6136dea28fdaf037250443216aaa0c7ad191d725 Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Fri, 24 May 2019 12:46:24 +0200 Subject: [PATCH 16/19] Update models.py --- django_mailbox/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 156fd668..1ed72446 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -387,7 +387,7 @@ def _process_message(self, message): msg.subject = ( utils.convert_header_to_unicode(message['subject'].encode('raw-unicode-escape'))[0:255] ) - msg.subject = unicode(email.header.decode_header(msg.subject)[0][0]), errors='ignore') + msg.subject = unicode(email.header.decode_header(msg.subject)[0][0], errors='ignore') if 'message-id' in message: msg.message_id = message['message-id'][0:255].strip() if 'from' in message: From 204d67a57d9a78d4cf048eddca405f982b2da4b2 Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Fri, 24 May 2019 13:08:05 +0200 Subject: [PATCH 17/19] Update models.py --- django_mailbox/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 1ed72446..c3bf09b6 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -385,7 +385,7 @@ def _process_message(self, message): # Fix to accept subject emojis in utf-8 if 'subject' in message: msg.subject = ( - utils.convert_header_to_unicode(message['subject'].encode('raw-unicode-escape'))[0:255] + utils.convert_header_to_unicode(message['subject'].decode('raw-unicode-escape'))[0:255] ) msg.subject = unicode(email.header.decode_header(msg.subject)[0][0], errors='ignore') if 'message-id' in message: From 625e16647aab6dc17a8762d0642ad604e53c216c Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Tue, 25 Jun 2019 14:17:45 +0200 Subject: [PATCH 18/19] Update __init__.py --- django_mailbox/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_mailbox/__init__.py b/django_mailbox/__init__.py index 176119c3..0b472167 100644 --- a/django_mailbox/__init__.py +++ b/django_mailbox/__init__.py @@ -1,3 +1,3 @@ -__version__ = '4.7.1' +__version__ = '4.7.2' default_app_config = 'django_mailbox.apps.MailBoxConfig' From be3163e0f22a22d7635189d84310a66ee4a8b725 Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Thu, 31 Oct 2019 16:00:43 +0100 Subject: [PATCH 19/19] Remove fix UTF-8 Remove fix UTF-8 --- django_mailbox/models.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index c3bf09b6..f24dc4e2 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -382,12 +382,10 @@ def _process_message(self, message): if settings['store_original_message']: self._process_save_original_message(message, msg) msg.mailbox = self - # Fix to accept subject emojis in utf-8 if 'subject' in message: msg.subject = ( - utils.convert_header_to_unicode(message['subject'].decode('raw-unicode-escape'))[0:255] + utils.convert_header_to_unicode(message['subject'])[0:255] ) - msg.subject = unicode(email.header.decode_header(msg.subject)[0][0], errors='ignore') if 'message-id' in message: msg.message_id = message['message-id'][0:255].strip() if 'from' in message: