Barry Warsaw pushed to branch master at mailman / Mailman

Commits:
aae1f922 by Aurélien Bompard at 2016-03-10T17:43:11-05:00
Protect the approved rule against unknown charsets

Fixes #203

- - - - -
363342a9 by Aurélien Bompard at 2016-03-10T17:43:11-05:00
Implement review suggestions

- - - - -
7f1ad186 by Barry Warsaw at 2016-03-10T18:07:11-05:00
Add NEWS and modify a comment.

- - - - -


3 changed files:

- src/mailman/docs/NEWS.rst
- src/mailman/rules/approved.py
- src/mailman/rules/tests/test_approved.py


Changes:

=====================================
src/mailman/docs/NEWS.rst
=====================================
--- a/src/mailman/docs/NEWS.rst
+++ b/src/mailman/docs/NEWS.rst
@@ -69,6 +69,8 @@ Bugs
    which is already subscribed with that role produces a server error.
    Originally given by Anirudh Dahiya.  (Closes #198)
  * Cross-posting messages held on both lists no longer fails.  (Closes #176)
+ * Don't let unknown charsets crash the "approved" rule.  Given by Aurélien
+   Bompard.  (Closes #203)
 
 Configuration
 -------------


=====================================
src/mailman/rules/approved.py
=====================================
--- a/src/mailman/rules/approved.py
+++ b/src/mailman/rules/approved.py
@@ -75,7 +75,13 @@ class Approved:
                 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 is unknown, we'll just drop back to ascii.
+                    payload = payload.decode(charset, 'replace')
+                except LookupError:
+                    # Unknown or empty charset.
+                    payload = payload.decode('us-ascii', 'replace')
                 line = ''
                 lines = payload.splitlines(True)
                 for lineno, line in enumerate(lines):


=====================================
src/mailman/rules/tests/test_approved.py
=====================================
--- 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('t...@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: a...@example.com
 To: t...@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: a...@example.com
+To: t...@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)
 
 



View it on GitLab: 
https://gitlab.com/mailman/mailman/compare/cb8ddd1671e424478f002e527871f0c5839425d9...7f1ad186e7de219e3f45c7ac1a8a2f63217ffa3b
_______________________________________________
Mailman-checkins mailing list
Mailman-checkins@python.org
Unsubscribe: 
https://mail.python.org/mailman/options/mailman-checkins/archive%40jab.org

Reply via email to