diff --git a/swift/common/constraints.py b/swift/common/constraints.py index eacc6c8d32..a437b32924 100644 --- a/swift/common/constraints.py +++ b/swift/common/constraints.py @@ -306,7 +306,8 @@ def check_utf8(string): if isinstance(string, unicode): string.encode('utf-8') else: - string.decode('UTF-8') + if string.decode('UTF-8').encode('UTF-8') != string: + return False return '\x00' not in string # If string is unicode, decode() will raise UnicodeEncodeError # So, we should catch both UnicodeDecodeError & UnicodeEncodeError diff --git a/test/unit/common/test_constraints.py b/test/unit/common/test_constraints.py index 8f17ec7e4c..d7f332d9b1 100644 --- a/test/unit/common/test_constraints.py +++ b/test/unit/common/test_constraints.py @@ -416,6 +416,10 @@ class TestConstraints(unittest.TestCase): valid_utf8_str]: self.assertTrue(constraints.check_utf8(true_argument)) + def test_check_utf8_non_canonical(self): + self.assertFalse(constraints.check_utf8('\xed\xa0\xbc\xed\xbc\xb8')) + self.assertFalse(constraints.check_utf8('\xed\xa0\xbd\xed\xb9\x88')) + def test_validate_bad_meta(self): req = Request.blank( '/v/a/c/o',