diff options
| author | Abhilash Raj | 2016-10-14 12:29:57 -0700 |
|---|---|---|
| committer | Barry Warsaw | 2016-11-25 11:45:06 -0500 |
| commit | 5a02cc713f9897cd9ec0df48dd7718f019a6e510 (patch) | |
| tree | 4d97da4c62a75f1054c2098b2dd08f89905486c8 /src | |
| parent | 055082f2694c84d5dd46ba194224b1b9f51f835f (diff) | |
| download | mailman-5a02cc713f9897cd9ec0df48dd7718f019a6e510.tar.gz mailman-5a02cc713f9897cd9ec0df48dd7718f019a6e510.tar.zst mailman-5a02cc713f9897cd9ec0df48dd7718f019a6e510.zip | |
Diffstat (limited to 'src')
| -rw-r--r-- | src/mailman/rest/post_moderation.py | 8 | ||||
| -rw-r--r-- | src/mailman/rest/tests/data/bad_email | 8 | ||||
| -rw-r--r-- | src/mailman/rest/tests/test_moderation.py | 19 |
3 files changed, 34 insertions, 1 deletions
diff --git a/src/mailman/rest/post_moderation.py b/src/mailman/rest/post_moderation.py index fc38af359..ee31e8694 100644 --- a/src/mailman/rest/post_moderation.py +++ b/src/mailman/rest/post_moderation.py @@ -71,7 +71,13 @@ class _HeldMessageBase(_ModerationBase): # resource. XXX See LP: #967954 key = resource.pop('key') msg = getUtility(IMessageStore).get_message_by_id(key) - resource['msg'] = msg.as_string() + try: + resource['msg'] = msg.as_string() + except KeyError: + # If the message can't be parsed, return a generic message instead + # of raising an error. + # See http://bugs.python.org/issue27321 and #256 + resource['msg'] = 'this message is defective' # Some of the _mod_* keys we want to rename and place into the JSON # resource. Others we can drop. Since we're mutating the dictionary, # we need to make a copy of the keys. When you port this to Python 3, diff --git a/src/mailman/rest/tests/data/bad_email b/src/mailman/rest/tests/data/bad_email new file mode 100644 index 000000000..83f9337dd --- /dev/null +++ b/src/mailman/rest/tests/data/bad_email @@ -0,0 +1,8 @@ +To: <test@example.com> +Subject: =?koi8-r?B?UF9AX/NfQ1/5X+xfS1/p?= +From: =?koi8-r?B?8sXL0sXB1MnXzs/FIMHHxc7U09TXzw==?= +Content-Type: text/plain; charset=koi8-r +Message-Id: <20160614102505.9OFQ19L1C> + +ώτο ταλοε ςελμανξαρ ςασσωμλα? +λΑΛΟΚ ΟΤΛΜΙΛ ΦΔΑΤΨ ΟΤ άΤΟΗΟ ΝΕΤΟΔΑ ΠΟΙΣΛΑ ΛΜΙΕΞΤΟΧ? diff --git a/src/mailman/rest/tests/test_moderation.py b/src/mailman/rest/tests/test_moderation.py index e0c3f1ccf..03d52dfc4 100644 --- a/src/mailman/rest/tests/test_moderation.py +++ b/src/mailman/rest/tests/test_moderation.py @@ -17,8 +17,10 @@ """REST moderation tests.""" +import os import unittest +from email import message_from_binary_file from mailman.app.lifecycle import create_list from mailman.app.moderator import hold_message from mailman.database.transaction import transaction @@ -206,6 +208,23 @@ class TestSubscriptionModeration(unittest.TestCase): emails = set(json['email'] for json in content['entries']) self.assertEqual(emails, {'anne@example.com', 'bart@example.com'}) + def test_view_malformed_held_message(self): + # Opening a bad (i.e. bad structure) email and holding it. + pwd = os.path.dirname(os.path.realpath(__file__)) + email_path = os.path.join(pwd, 'data/bad_email') + msg = message_from_binary_file(open(email_path, 'rb')) + msg.sender = 'bogussender@example.com' + with transaction(): + hold_message(self._mlist, msg) + # Now trying to access held messages from REST API should not give + # 500 server error if one of the messages can't be parsed properly. + content, response = call_api( + 'http://localhost:9001/3.0/lists/ant@example.com/held') + self.assertEqual(response.status, 200) + self.assertEqual(len(content['entries']), 1) + self.assertEqual(content['entries'][0]['msg'], + 'this message is defective') + def test_individual_request(self): # We can view an individual request. with transaction(): |
