From abcecd26a7b5871f75f0fbddf0d08bbac95bb089 Mon Sep 17 00:00:00 2001 From: Kun Huang Date: Wed, 23 Oct 2013 21:19:01 +0800 Subject: [PATCH] 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 --- swift/common/middleware/tempurl.py | 7 +++---- test/unit/common/middleware/test_tempurl.py | 6 ++++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/swift/common/middleware/tempurl.py b/swift/common/middleware/tempurl.py index a97bd95e31..c2655883f8 100644 --- a/swift/common/middleware/tempurl.py +++ b/swift/common/middleware/tempurl.py @@ -97,9 +97,8 @@ from urllib import urlencode from urlparse import parse_qs from swift.proxy.controllers.base import get_account_info -from swift.common.swob import HeaderKeyDict -from swift.common.utils import split_path -from swift.common.swob import HTTPUnauthorized +from swift.common.swob import HeaderKeyDict, HTTPUnauthorized +from swift.common.utils import split_path, get_valid_utf8_str #: 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'] 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')] diff --git a/test/unit/common/middleware/test_tempurl.py b/test/unit/common/middleware/test_tempurl.py index 86d5b8934f..580f617dc0 100644 --- a/test/unit/common/middleware/test_tempurl.py +++ b/test/unit/common/middleware/test_tempurl.py @@ -803,6 +803,12 @@ class TestTempURL(unittest.TestCase): self.assertTrue('test-header-yes' not 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__': unittest.main()