Add forwarded email extraction to email_import module#791
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 58cbf1c89d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if attachment.get_content_type() == "message/rfc822": | ||
| continue |
There was a problem hiding this comment.
Do not drop rfc822 attachments when extraction is off
When callers set extract_forwarded_emails to false to avoid creating dependent email objects, this unconditional skip removes message/rfc822 attachments from the normal attachment path and the later extraction block is not run, so attached .eml forwards disappear from the import results entirely. The skip should be tied to successful forwarded-email extraction or fall back to emitting the attachment as a file object when extraction is disabled.
Useful? React with 👍 / 👎.
| header_lines = [] | ||
| body_lines = [] | ||
| in_headers = True | ||
| known_headers = {"from", "to", "cc", "bcc", "reply-to", "subject", "date", "sent"} |
There was a problem hiding this comment.
Map localized From headers before requiring from
When an inline forwarded block starts with De: or Von: (which the start detector explicitly accepts), the builder still only stores English header names and later requires a stored from header, so those French/German forwards are discarded even if the rest of the header block is present. Normalize de/von to from here, or remove them from the detector if they are not intended to work.
Useful? React with 👍 / 👎.
Motivation
message/rfc822attachments) were not being emitted as dependent email objects, reducing analyst visibility and matching behavior inmail_to_misp.mail_to_mispto detect inline-forward blocks and attached.emlforwards.Description
extract_forwarded_emails(enabled by default) and wire it intodict_handlerto produce dependent email objects for forwards.message/rfc822parts in the regular attachment path and add helper functionsiter_attached_email_bytes,iter_decoded_bodies,find_inline_forwarded_email_bytes,build_forwarded_message,add_forwarded_email_object, anditer_file_attachmentsto extract forwards robustly.message_from_byteswithpolicyfor parsing constructed RFC822 snippets and extendHTMLTextParserto preserve structural line breaks to improve forwarded-header detection in HTML bodies.tests/test_email_import_forwarded.pycovering plain-text inline forwards, HTML inline forwards, and attached.emlforwards.Testing
pytestfor the new regression suite withpython -m pytest tests/test_email_import_forwarded.py -qand all tests passed (3 passed).python -m py_compile misp_modules/modules/import_mod/email_import.pyand style checks withpython -m ruff check misp_modules/modules/import_mod/email_import.py tests/test_email_import_forwarded.py, both of which succeeded.email_headerstest is still skipped (Need Rewrite) and was not modified by this change.Codex Task