]> khamidou — GitHub mirrors - imapclient.git/commitdiff
#21: High ascii bytes in folder names can break folder handling
authorMenno Smits <menno@freshfoo.com>
Wed, 5 May 2010 12:27:39 +0000 (13:27 +0100)
committerMenno Smits <menno@freshfoo.com>
Wed, 5 May 2010 12:27:39 +0000 (13:27 +0100)
Don't allow characters in str based folder names that can't be
converted to unicode without knowing the encoding. The idea is that
IMAPClient's user should convert to unicode themselves.

imapclient/imap_utf7.py
imapclient/test/test_imap_utf7.py
livetest.py

index 379ae6a3ed9f341c868ecfee93d180eabef210be..c745ddf2b3ec995139fb919d269a3dedfc17742f 100644 (file)
 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 
+class FolderNameError(ValueError):
+    pass
+
+
 def encode(s):
+    if isinstance(s, str) and sum(n for n in (ord(c) for c in s) if n > 127):
+        raise FolderNameError("%r contains characters not valid in a str folder name. "
+                              "Convert to unicode first?" % s)
+
     r = []
     _in = []
     for c in s:
index 9921e3d17a885ae5ff73562d9f9350347e20e7f4..9d620d14494e61506e254984918f9bf506358c58 100644 (file)
@@ -22,7 +22,7 @@
 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-from imapclient.imap_utf7 import decode, encode
+from imapclient.imap_utf7 import decode, encode, FolderNameError
 import unittest
 
 
@@ -37,6 +37,7 @@ class IMAP4UTF7TestCase(unittest.TestCase):
         [u'\xff\xfe\xfd\xfc', '&AP8A,gD9APw-'],
         [u'~peter/mail/\u65e5\u672c\u8a9e/\u53f0\u5317',
          '~peter/mail/&ZeVnLIqe-/&U,BTFw-'], # example from RFC 2060
+        ['\x00foo', '&AAA-foo'],
     ]
 
     def test_encode(self):
@@ -49,6 +50,20 @@ class IMAP4UTF7TestCase(unittest.TestCase):
             self.assertEquals(input, decode(output))
 
 
+    def test_illegal_chars(self):
+        not_valid_as_str = [
+            'blah' + chr(0x80) + 'sne',
+            chr(0xaa) + 'foo',
+            'blah' + chr(0xff) + 'sne']
+
+        for name in not_valid_as_str:
+            self.assertRaises(FolderNameError, encode, name)
+
+        unicode_names = [unicode(name, 'latin-1') for name in not_valid_as_str]
+        for name in unicode_names:
+            assert isinstance(encode(name), str)
+
+
     def test_printableSingletons(self):
         """
         The IMAP4 modified UTF-7 implementation encodes all printable
@@ -61,5 +76,10 @@ class IMAP4UTF7TestCase(unittest.TestCase):
         self.failUnlessEqual(encode('&'), '&-')
         self.failUnlessEqual(decode('&-'), '&')
 
+
+    def test_FolderNameError_super(self):
+        self.assert_(issubclass(FolderNameError, ValueError))
+
+
 if __name__ == '__main__':
     unittest.main()
index 0e41c3274b6101fc45a018292cc0c87aeeeb710f..ec03413edc82acd3eede2a08d4f416b9d7198846 100755 (executable)
@@ -61,7 +61,7 @@ def test_capabilities(client):
 
 def test_list_folders(client):
     clear_folders(client)
-    some_folders = ['simple', r'foo\bar', r'test"folder"']
+    some_folders = ['simple', r'foo\bar', r'test"folder"', u'L\xffR']
     for name in some_folders:
         client.create_folder(name)