pfwb: properly truncate mail subject (#64737)
gitea/docbow/pipeline/head Build started... Details

This commit is contained in:
Emmanuel Cazenave 2022-05-03 15:23:37 +02:00
parent 38bd3f102a
commit f2dd0da209
2 changed files with 42 additions and 2 deletions

View File

@ -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'):

View File

@ -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