From bd18a57a5dc4f544de09275163a57f92ea072857 Mon Sep 17 00:00:00 2001 From: Trenton Holmes Date: Thu, 1 Sep 2022 08:51:14 -0700 Subject: [PATCH] Updates imap_tools and updates to use the new login method for UTF-8 --- Pipfile.lock | 11 +++++------ src/paperless_mail/mail.py | 18 ++---------------- src/paperless_mail/tests/test_mail.py | 20 ++++++++++++++++---- 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/Pipfile.lock b/Pipfile.lock index 18d6bc315..bcdf26a35 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -501,10 +501,12 @@ "imap-tools": { "hashes": [ "sha256:4fe4c07c4cc4aab83492d126e221c39118ff530268149b721296fc0fb87de3c2", - "sha256:56942853be2125d509365b84eacf0f3a87ae58ea8f82bca7a6943634a60cfb60" + "sha256:56942853be2125d509365b84eacf0f3a87ae58ea8f82bca7a6943634a60cfb60", + "sha256:6f5572b2e747a81a607438e0ef61ff28f323f6697820493c8ca4467465baeb76", + "sha256:9e2658f267311051e04d437aca06d5b4c7c091fc4f411f7d13ca610e0cead5cb" ], "index": "pypi", - "version": "==0.56.0" + "version": "==0.57.0" }, "img2pdf": { "hashes": [ @@ -1754,9 +1756,6 @@ "version": "==0.4.5" }, "coverage": { - "extras": [ - "toml" - ], "hashes": [ "sha256:01778769097dbd705a24e221f42be885c544bb91251747a8a3efdec6eb4788f2", "sha256:08002f9251f51afdcc5e3adf5d5d66bb490ae893d9e21359b085f0e03390a820", @@ -2320,7 +2319,7 @@ "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc", "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f" ], - "markers": "python_version >= '3.7'", + "markers": "python_full_version < '3.11.0a7'", "version": "==2.0.1" }, "tornado": { diff --git a/src/paperless_mail/mail.py b/src/paperless_mail/mail.py index ad1358ebe..ebab59a88 100644 --- a/src/paperless_mail/mail.py +++ b/src/paperless_mail/mail.py @@ -4,7 +4,6 @@ import tempfile from datetime import date from datetime import timedelta from fnmatch import fnmatch -from imaplib import IMAP4 import magic import pathvalidate @@ -187,22 +186,9 @@ class MailAccountHandler(LoggingMixin): except UnicodeEncodeError: self.log("debug", "Falling back to AUTH=PLAIN") - try: - # rfc2595 section 6 - PLAIN SASL mechanism - client: IMAP4 = M.client - encoded = ( - b"\0" - + account.username.encode("utf8") - + b"\0" - + account.password.encode("utf8") - ) - # Assumption is the server supports AUTH=PLAIN capability - # Could check the list with client.capability(), but then what? - # We're failing anyway then - client.authenticate("PLAIN", lambda x: encoded) - # Need to transition out of AUTH state to SELECTED - M.folder.set("INBOX") + try: + M.login_utf8(account.username, account.password) except Exception as err: self.log( "error", diff --git a/src/paperless_mail/tests/test_mail.py b/src/paperless_mail/tests/test_mail.py index a0957a64d..be016a79a 100644 --- a/src/paperless_mail/tests/test_mail.py +++ b/src/paperless_mail/tests/test_mail.py @@ -73,6 +73,13 @@ class BogusClient: class BogusMailBox(ContextManager): + + # Common values so tests don't need to remember an accepted login + USERNAME: str = "admin" + ASCII_PASSWORD: str = "secret" + # Note the non-ascii characters here + UTF_PASSWORD: str = "w57äöüw4b6huwb6nhu" + def __enter__(self): return self @@ -93,7 +100,12 @@ class BogusMailBox(ContextManager): # This will raise a UnicodeEncodeError if the password is not ASCII only password.encode("ascii") # Otherwise, check for correct values - if username != "admin" or password not in {"secret"}: + if username != self.USERNAME or password != self.ASCII_PASSWORD: + raise MailboxLoginError("BAD", "OK") + + def login_utf8(self, username, password): + # Expected to only be called with the UTF-8 password + if username != self.USERNAME or password != self.UTF_PASSWORD: raise MailboxLoginError("BAD", "OK") def fetch(self, criteria, mark_seen, charset=""): @@ -931,9 +943,9 @@ class TestMail(DirectoriesMixin, TestCase): account = MailAccount.objects.create( name="test", imap_server="", - username="admin", + username=BogusMailBox.USERNAME, # Note the non-ascii characters here - password="w57äöüw4b6huwb6nhu", + password=BogusMailBox.UTF_PASSWORD, ) _ = MailRule.objects.create( @@ -963,7 +975,7 @@ class TestMail(DirectoriesMixin, TestCase): account = MailAccount.objects.create( name="test", imap_server="", - username="admin", + username=BogusMailBox.USERNAME, # Note the non-ascii characters here # Passes the check in login, not in authenticate password="réception",