diff options
| -rw-r--r-- | src/mailman/rules/approved.py | 10 | ||||
| -rw-r--r-- | src/mailman/rules/tests/test_approved.py | 29 |
2 files changed, 32 insertions, 7 deletions
diff --git a/src/mailman/rules/approved.py b/src/mailman/rules/approved.py index 5cb8de8e5..a289a90be 100644 --- a/src/mailman/rules/approved.py +++ b/src/mailman/rules/approved.py @@ -74,8 +74,14 @@ class Approved: payload = part.get_payload(decode=True) break if payload is not None: - charset = part.get_content_charset('us-ascii') - payload = payload.decode(charset, 'replace') + try: + # Do the decoding inside the try/except so that if the + # charset conversion fails, we'll just drop back to ascii. + charset = part.get_content_charset('us-ascii') + payload = payload.decode(charset, 'replace') + except (LookupError, TypeError): + # Unknown or empty charset. + payload = payload.decode('us-ascii', 'replace') line = '' lines = payload.splitlines(True) for lineno, line in enumerate(lines): diff --git a/src/mailman/rules/tests/test_approved.py b/src/mailman/rules/tests/test_approved.py index 73812f030..401e26634 100644 --- a/src/mailman/rules/tests/test_approved.py +++ b/src/mailman/rules/tests/test_approved.py @@ -400,8 +400,14 @@ class TestApprovedNonASCII(unittest.TestCase): def setUp(self): self._mlist = create_list('test@example.com') + self._mlist.moderator_password = config.password_context.encrypt( + 'super secret') self._rule = approved.Approved() - self._msg = mfs("""\ + + def test_nonascii_body_missing_header(self): + # When the message body contains non-ascii, the rule should not throw + # unicode errors. LP: #949924. + msg = mfs("""\ From: anne@example.com To: test@example.com Subject: A Message with non-ascii body @@ -412,11 +418,24 @@ Content-Transfer-Encoding: quoted-printable This is a message body with a non-ascii character =E4 """) + result = self._rule.check(self._mlist, msg, {}) + self.assertFalse(result) - def test_nonascii_body_missing_header(self): - # When the message body contains non-ascii, the rule should not throw - # unicode errors. LP: #949924. - result = self._rule.check(self._mlist, self._msg, {}) + def test_unknown_charset(self): + # When the charset is unknown, the rule should not crash. GL: #203 + msg = mfs("""\ +From: anne@example.com +To: test@example.com +Subject: A Message with non-ascii body +Message-ID: <ant> +MIME-Version: 1.0 +Content-Type: text/plain; charset=unknown-8bit +Content-Disposition: inline +Content-Transfer-Encoding: quoted-printable + +This is a message body with a non-ascii character =E4 +""") + result = self._rule.check(self._mlist, msg, {}) self.assertFalse(result) |
