From: Menno Smits Date: Wed, 5 May 2010 12:27:39 +0000 (+0100) Subject: #21: High ascii bytes in folder names can break folder handling X-Git-Tag: 0.7~39 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=b637d29ad620143fb9eaf5515981386706049209;p=imapclient.git #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. --- diff --git a/imapclient/imap_utf7.py b/imapclient/imap_utf7.py index 379ae6a..c745ddf 100644 --- a/imapclient/imap_utf7.py +++ b/imapclient/imap_utf7.py @@ -23,7 +23,15 @@ # 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: diff --git a/imapclient/test/test_imap_utf7.py b/imapclient/test/test_imap_utf7.py index 9921e3d..9d620d1 100644 --- a/imapclient/test/test_imap_utf7.py +++ b/imapclient/test/test_imap_utf7.py @@ -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() diff --git a/livetest.py b/livetest.py index 0e41c32..ec03413 100755 --- a/livetest.py +++ b/livetest.py @@ -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)