Fixes the case where a mail attachment has no filename to use (#5117)

This commit is contained in:
Trenton H 2023-12-27 17:38:22 -08:00 committed by GitHub
parent 5576a073a5
commit ffad42615f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -703,6 +703,12 @@ class MailAccountHandler(LoggingMixin):
mime_type = magic.from_buffer(att.payload, mime=True) mime_type = magic.from_buffer(att.payload, mime=True)
if is_mime_type_supported(mime_type): if is_mime_type_supported(mime_type):
self.log.info(
f"Rule {rule}: "
f"Consuming attachment {att.filename} from mail "
f"{message.subject} from {message.from_}",
)
os.makedirs(settings.SCRATCH_DIR, exist_ok=True) os.makedirs(settings.SCRATCH_DIR, exist_ok=True)
temp_dir = Path( temp_dir = Path(
@ -711,14 +717,15 @@ class MailAccountHandler(LoggingMixin):
dir=settings.SCRATCH_DIR, dir=settings.SCRATCH_DIR,
), ),
) )
temp_filename = temp_dir / pathvalidate.sanitize_filename(att.filename)
temp_filename.write_bytes(att.payload)
self.log.info( attachment_name = pathvalidate.sanitize_filename(att.filename)
f"Rule {rule}: " if attachment_name:
f"Consuming attachment {att.filename} from mail " temp_filename = temp_dir / attachment_name
f"{message.subject} from {message.from_}", else: # pragma: no cover
) # Some cases may have no name (generally inline)
temp_filename = temp_dir / "no-name-attachment"
temp_filename.write_bytes(att.payload)
input_doc = ConsumableDocument( input_doc = ConsumableDocument(
source=DocumentSource.MailFetch, source=DocumentSource.MailFetch,