From 9ea5d0b3a181a4c5159acad6fd41d14867fb4103 Mon Sep 17 00:00:00 2001 From: Michael Barton Date: Wed, 22 Sep 2010 19:34:52 +0000 Subject: [PATCH] Make check_xml_encodable to only validate utf-8 correctness --- swift/account/server.py | 4 ++-- swift/common/constraints.py | 11 ++++++----- swift/container/server.py | 4 ++-- swift/obj/server.py | 4 ++-- swift/proxy/server.py | 4 ++-- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/swift/account/server.py b/swift/account/server.py index a1fbdfb06c..18bd3f212b 100644 --- a/swift/account/server.py +++ b/swift/account/server.py @@ -32,7 +32,7 @@ from swift.common.db import AccountBroker from swift.common.utils import get_logger, get_param, hash_path, \ normalize_timestamp, split_path, storage_directory from swift.common.constraints import ACCOUNT_LISTING_LIMIT, \ - check_mount, check_float, check_xml_encodable + check_mount, check_float, check_utf8 from swift.common.db_replicator import ReplicatorRpc @@ -294,7 +294,7 @@ class AccountController(object): def __call__(self, env, start_response): start_time = time.time() req = Request(env) - if not check_xml_encodable(req.path_info): + if not check_utf8(req.path_info): res = HTTPPreconditionFailed(body='Invalid UTF8') else: try: diff --git a/swift/common/constraints.py b/swift/common/constraints.py index 9c5e4bd5b2..74611ec5d2 100644 --- a/swift/common/constraints.py +++ b/swift/common/constraints.py @@ -108,7 +108,7 @@ def check_object_creation(req, object_name): if 'Content-Type' not in req.headers: return HTTPBadRequest(request=req, content_type='text/plain', body='No content type') - if not check_xml_encodable(req.headers['Content-Type']): + if not check_utf8(req.headers['Content-Type']): return HTTPBadRequest(request=req, body='Invalid Content-Type', content_type='text/plain') return check_metadata(req, 'object') @@ -148,14 +148,15 @@ _invalid_xml = re.compile(ur'[^\x09\x0a\x0d\x20-\uD7FF\uE000-\uFFFD%s-%s]' % (unichr(0x10000), unichr(0x10FFFF))) -def check_xml_encodable(string): +def check_utf8(string): """ - Validate if a string can be encoded in xml. + Validate if a string is valid UTF-8. :param string: string to be validated - :returns: True if the string can be encoded in xml, False otherwise + :returns: True if the string is valid utf-8, False otherwise """ try: - return not _invalid_xml.search(string.decode('UTF-8')) + string.decode('UTF-8') + return True except UnicodeDecodeError: return False diff --git a/swift/container/server.py b/swift/container/server.py index 4134c15e11..ff8dc76684 100644 --- a/swift/container/server.py +++ b/swift/container/server.py @@ -33,7 +33,7 @@ from swift.common.db import ContainerBroker from swift.common.utils import get_logger, get_param, hash_path, \ normalize_timestamp, storage_directory, split_path from swift.common.constraints import CONTAINER_LISTING_LIMIT, \ - check_mount, check_float, check_xml_encodable + check_mount, check_float, check_utf8 from swift.common.bufferedhttp import http_connect from swift.common.exceptions import ConnectionTimeout from swift.common.db_replicator import ReplicatorRpc @@ -386,7 +386,7 @@ class ContainerController(object): def __call__(self, env, start_response): start_time = time.time() req = Request(env) - if not check_xml_encodable(req.path_info): + if not check_utf8(req.path_info): res = HTTPPreconditionFailed(body='Invalid UTF8') else: try: diff --git a/swift/obj/server.py b/swift/obj/server.py index 49f707bdb3..0a73421923 100644 --- a/swift/obj/server.py +++ b/swift/obj/server.py @@ -40,7 +40,7 @@ from swift.common.utils import mkdirs, normalize_timestamp, \ split_path, drop_buffer_cache, get_logger from swift.common.bufferedhttp import http_connect from swift.common.constraints import check_object_creation, check_mount, \ - check_float, check_xml_encodable + check_float, check_utf8 from swift.common.exceptions import ConnectionTimeout from swift.obj.replicator import get_hashes, invalidate_hash, \ recalculate_hashes @@ -557,7 +557,7 @@ class ObjectController(object): """WSGI Application entry point for the Swift Object Server.""" start_time = time.time() req = Request(env) - if not check_xml_encodable(req.path_info): + if not check_utf8(req.path_info): res = HTTPPreconditionFailed(body='Invalid UTF8') else: try: diff --git a/swift/proxy/server.py b/swift/proxy/server.py index dd868dcd0c..0145857236 100644 --- a/swift/proxy/server.py +++ b/swift/proxy/server.py @@ -36,7 +36,7 @@ from swift.common.utils import get_logger, normalize_timestamp, split_path, \ cache_from_env from swift.common.bufferedhttp import http_connect from swift.common.constraints import check_metadata, check_object_creation, \ - check_xml_encodable, MAX_ACCOUNT_NAME_LENGTH, MAX_CONTAINER_NAME_LENGTH, \ + check_utf8, MAX_ACCOUNT_NAME_LENGTH, MAX_CONTAINER_NAME_LENGTH, \ MAX_FILE_SIZE from swift.common.exceptions import ChunkReadTimeout, \ ChunkWriteTimeout, ConnectionTimeout @@ -1298,7 +1298,7 @@ class BaseApplication(object): controller, path_parts = self.get_controller(req.path) except ValueError: return HTTPNotFound(request=req) - if not check_xml_encodable(req.path_info): + if not check_utf8(req.path_info): return HTTPPreconditionFailed(request=req, body='Invalid UTF8') if not controller: return HTTPPreconditionFailed(request=req, body='Bad URL')