Show
Ignore:
Timestamp:
05/05/10 13:27:39 (2 years ago)
Author:
Menno Smits <menno@…>
Branch:
default
Message:

#21: High ascii bytes in folder names can break folder handling

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.

Location:
imapclient
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • imapclient/imap_utf7.py

    r62 r147  
    2424 
    2525 
     26class FolderNameError(ValueError): 
     27    pass 
     28 
     29 
    2630def encode(s): 
     31    if isinstance(s, str) and sum(n for n in (ord(c) for c in s) if n > 127): 
     32        raise FolderNameError("%r contains characters not valid in a str folder name. " 
     33                              "Convert to unicode first?" % s) 
     34 
    2735    r = [] 
    2836    _in = [] 
  • imapclient/test/test_imap_utf7.py

    r146 r147  
    2323# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
    2424 
    25 from imapclient.imap_utf7 import decode, encode 
     25from imapclient.imap_utf7 import decode, encode, FolderNameError 
    2626import unittest 
    2727 
     
    3838        [u'~peter/mail/\u65e5\u672c\u8a9e/\u53f0\u5317', 
    3939         '~peter/mail/&ZeVnLIqe-/&U,BTFw-'], # example from RFC 2060 
     40        ['\x00foo', '&AAA-foo'], 
    4041    ] 
    4142 
     
    4849        for (input, output) in self.tests: 
    4950            self.assertEquals(input, decode(output)) 
     51 
     52 
     53    def test_illegal_chars(self): 
     54        not_valid_as_str = [ 
     55            'blah' + chr(0x80) + 'sne', 
     56            chr(0xaa) + 'foo', 
     57            'blah' + chr(0xff) + 'sne'] 
     58 
     59        for name in not_valid_as_str: 
     60            self.assertRaises(FolderNameError, encode, name) 
     61 
     62        unicode_names = [unicode(name, 'latin-1') for name in not_valid_as_str] 
     63        for name in unicode_names: 
     64            assert isinstance(encode(name), str) 
    5065 
    5166 
     
    6277        self.failUnlessEqual(decode('&-'), '&') 
    6378 
     79 
     80    def test_FolderNameError_super(self): 
     81        self.assert_(issubclass(FolderNameError, ValueError)) 
     82 
     83 
    6484if __name__ == '__main__': 
    6585    unittest.main()