forked from vikramarsid/msg_parser
-
Notifications
You must be signed in to change notification settings - Fork 0
fix(parser): handle RTF-only message bodies without crashing #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| # msg_parser requirements | ||
|
|
||
| olefile>=0.46 | ||
|
|
||
| compressed_rtf>=1.0.5 | ||
| striprtf>=0.0.32 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| #!/usr/bin/env python3 | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| """Tests for EmailFormatter body handling.""" | ||
|
|
||
| import unittest | ||
|
|
||
| from msg_parser.email_builder import EmailFormatter | ||
|
|
||
|
|
||
| class _FakeAttachment: | ||
| AttachMimeTag = "application/pdf" | ||
| data = b"%PDF-1.4 fake attachment bytes" | ||
| Filename = "evidence.pdf" | ||
|
|
||
|
|
||
| class _FakeMsg: | ||
| """Minimal stand-in exposing the attributes EmailFormatter.build_email reads.""" | ||
|
|
||
| def __init__(self, body): | ||
| self.message_id = "<id@example.com>" | ||
| self.subject = "subject only message" | ||
| self.sent_date = "Mon, 13 Jan 2025 10:54:13 -0800" | ||
| self.sender = ["sender@example.com"] | ||
| self.reply_to = None | ||
| self.header_dict = {} | ||
| self.body = body | ||
| self.attachments = [_FakeAttachment()] | ||
|
|
||
|
|
||
| class TestEmptyBodyExport(unittest.TestCase): | ||
| """Body-less messages must still export instead of dropping the whole file.""" | ||
|
|
||
| def test_empty_body_still_builds_with_attachments(self): | ||
| """A subject-only / RTF-shell message (empty or missing body) must build | ||
| an EML with its attachments intact, not raise KeyError.""" | ||
| for empty_body in ("", None): | ||
| eml = EmailFormatter(_FakeMsg(empty_body)).build_email() | ||
| self.assertIsInstance(eml, str) | ||
| # an (empty) body part is attached rather than raising | ||
| self.assertIn("Content-Type: text/plain", eml) | ||
| # and the attachment survives | ||
| self.assertIn("Content-Disposition: attachment", eml) | ||
| self.assertIn("evidence.pdf", eml) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #!/usr/bin/env python3 | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| """Tests for compressed-RTF body handling in MsOxMessage._rtf_compressed_to_text.""" | ||
|
|
||
| import unittest | ||
| from unittest import mock | ||
|
|
||
| import compressed_rtf | ||
|
|
||
| from msg_parser.msg_parser import MsOxMessage | ||
|
|
||
|
|
||
| class TestRtfCompressedToText(unittest.TestCase): | ||
| """RTF-only message bodies must decode to text without taking down the parse.""" | ||
|
|
||
| def test_empty_rtf_shell_collapses_to_empty_string(self): | ||
| """An RTF formatting shell with no real text yields '' (so callers can | ||
| treat it as a body-less message), not raw RTF markup.""" | ||
| shell = ( | ||
| rb"{\rtf1\ansi\deff0{\fonttbl{\f0\fswiss Arial;}}" | ||
| rb"{\colortbl\red0\green0\blue0;}\f0\fs20 }" | ||
| ) | ||
| body = MsOxMessage._rtf_compressed_to_text(compressed_rtf.compress(shell)) | ||
| self.assertIsInstance(body, str) | ||
| self.assertEqual(body.strip(), "") | ||
|
|
||
| def test_real_rtf_text_is_extracted_as_string(self): | ||
| """An RTF body with real text comes back as that plain text (a str), | ||
| never as raw bytes of RTF markup.""" | ||
| doc = rb"{\rtf1\ansi\deff0{\fonttbl{\f0\fswiss Arial;}}\f0\fs20 Hello world.}" | ||
| body = MsOxMessage._rtf_compressed_to_text(compressed_rtf.compress(doc)) | ||
| self.assertIsInstance(body, str) | ||
| self.assertIn("Hello world.", body) | ||
|
|
||
| def test_striprtf_runtime_error_falls_back_to_decoded_rtf(self): | ||
| """If striprtf raises at runtime, keep the decoded RTF rather than | ||
| aborting the parse and losing already-readable headers/attachments.""" | ||
| compressed = compressed_rtf.compress(rb"{\rtf1\ansi\deff0 hello}") | ||
| with mock.patch("striprtf.striprtf.rtf_to_text", side_effect=ValueError("boom")): | ||
| body = MsOxMessage._rtf_compressed_to_text(compressed) | ||
| self.assertIsInstance(body, str) | ||
| self.assertIn("rtf1", body) # decoded RTF preserved, no exception raised | ||
|
|
||
| def test_corrupt_stream_still_raises(self): | ||
| """A genuinely unsupported/corrupt compressed stream must surface rather | ||
| than be silently swallowed, so callers can flag broken files.""" | ||
| with self.assertRaises(Exception): | ||
| MsOxMessage._rtf_compressed_to_text(b"not a valid compressed rtf stream") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the actual python version we're running, let's just set it in CI for this fork that's only used by us