From f2dd0da20929c466b58d61b513b8dbff5d48c77f Mon Sep 17 00:00:00 2001 From: Emmanuel Cazenave Date: Tue, 3 May 2022 15:23:37 +0200 Subject: [PATCH] pfwb: properly truncate mail subject (#64737) --- .../pfwb/management/commands/sendmail.py | 5 ++- tests/pfwb/test_pfwb.py | 39 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/docbow_project/pfwb/management/commands/sendmail.py b/docbow_project/pfwb/management/commands/sendmail.py index b06f21c..4d9a9d4 100644 --- a/docbow_project/pfwb/management/commands/sendmail.py +++ b/docbow_project/pfwb/management/commands/sendmail.py @@ -24,6 +24,7 @@ from docbow_project.docbow.email_utils import u2u_decode from django_journal.journal import record from django.db.transaction import atomic +from docbow_project.docbow import app_settings as docbow_app_settings from docbow_project.pfwb import app_settings logger = logging.getLogger('docbow.mail_interface') @@ -216,8 +217,8 @@ In case of failure the following return value is returned: self.error('7.7.1 The subject cannot be decoded', exit_code=3) try: name = subject.split(':', 1)[1].strip() - # be defensive, truncate at 230 characters ! - name = utils.truncate_filename(name) + # be defensive, truncate + name = name[: docbow_app_settings.TRUNCATE_FILENAME] except IndexError: self.error('7.7.1 Filename cannot be extracted from the subject', exit_code=3) if not name.endswith('.pdf'): diff --git a/tests/pfwb/test_pfwb.py b/tests/pfwb/test_pfwb.py index f36c90e..e7d05ca 100644 --- a/tests/pfwb/test_pfwb.py +++ b/tests/pfwb/test_pfwb.py @@ -422,3 +422,42 @@ class ArchiveTestCase(TestCase): ) ) self.assertTrue(os.path.exists(os.path.join(archive_dir, 'journal.txt'))) + + +def test_send_mail_truncate_filename(db, settings): + settings.DOCBOW_PFWB_SENDMAIL_TABELLIO_EXPEDITION_EMAIL = EXPEDITION_EMAIL + settings.DOCBOW_PFWB_SENDMAIL_TABELLIO_EXPEDITION_USER_ID = 1 + settings.MEDIA_ROOT = MEDIA_ROOT + + pjd_filetype = FileType.objects.create(name='PJD', id=2) + tabellio_doc_type = TabellioDocType.objects.create(filetype=pjd_filetype, tabellio_doc_type='PJD') + expedition_user = User.objects.create(username='expedition', id=1) + to_user = User.objects.create(username='recipient', email=RECIPIENT_EMAIL, id=2) + + subject = 'foo: ' + 'aaaa M.' + 'b' * 300 + content = '''\ +Message-ID: <232323232@example.com> +From: %(expedition_email)s +To: %(recipient_email)s +Subject: foo: %(subject)s +MIME-Version: 1.0 +Content-Type: text/plain; charset=us-ascii +Content-Disposition: inline +User-Agent: Mutt/1.5.20 (2009-06-14) +X-Tabellio-Doc-Type: %(doc_type)s +X-Tabellio-Doc-URL: https://buildmedia.readthedocs.org/media/pdf/django/2.2.x/django.pdf +Status: RO +Coucou +''' % { + 'expedition_email': EXPEDITION_EMAIL, + 'recipient_email': RECIPIENT_EMAIL, + 'doc_type': 'PJD', + 'subject': subject, + } + with tempfile.NamedTemporaryFile() as f: + f.write(force_bytes(content)) + f.flush() + with mock.patch('docbow_project.pfwb.management.commands.sendmail.urlopen', mockurllib.urlopen): + management.call_command('sendmail', RECIPIENT_EMAIL, file=f.name, sender=EXPEDITION_EMAIL) + + assert Document.objects.count() == 1