utf8 encode tempurl key

In tempurl middleware, hmac uses the value of account metadata to
generate HMAC-SHA1 signature and hmac must accept a str-type string, not
a unicode string. The meta dict returned from get_info stroges special
chars as unicode however. So just encode it for tempurl using.

Closes-Bug: #1242644
Change-Id: I4be62eea014a573efc4748470de57dccf00e431d
This commit is contained in:
Kun Huang 2013-10-23 21:19:01 +08:00
parent d7dcb48511
commit abcecd26a7
2 changed files with 9 additions and 4 deletions

View File

@ -97,9 +97,8 @@ from urllib import urlencode
from urlparse import parse_qs from urlparse import parse_qs
from swift.proxy.controllers.base import get_account_info from swift.proxy.controllers.base import get_account_info
from swift.common.swob import HeaderKeyDict from swift.common.swob import HeaderKeyDict, HTTPUnauthorized
from swift.common.utils import split_path from swift.common.utils import split_path, get_valid_utf8_str
from swift.common.swob import HTTPUnauthorized
#: Default headers to remove from incoming requests. Simply a whitespace #: Default headers to remove from incoming requests. Simply a whitespace
@ -136,7 +135,7 @@ def get_tempurl_keys_from_metadata(meta):
meta = get_account_info(...)['meta'] meta = get_account_info(...)['meta']
keys = get_tempurl_keys_from_metadata(meta) keys = get_tempurl_keys_from_metadata(meta)
""" """
return [value for key, value in meta.iteritems() return [get_valid_utf8_str(value) for key, value in meta.iteritems()
if key.lower() in ('temp-url-key', 'temp-url-key-2')] if key.lower() in ('temp-url-key', 'temp-url-key-2')]

View File

@ -803,6 +803,12 @@ class TestTempURL(unittest.TestCase):
self.assertTrue('test-header-yes' not in hdrs) self.assertTrue('test-header-yes' not in hdrs)
self.assertTrue('test-header-yes-this' in hdrs) self.assertTrue('test-header-yes-this' in hdrs)
def test_unicode_metadata_value(self):
meta = {"temp-url-key": "test", "temp-url-key-2": u"test2"}
results = tempurl.get_tempurl_keys_from_metadata(meta)
for str_value in results:
self.assertTrue(isinstance(str_value, str))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()