From cd39778824970a696c221f394e70bcee6a9d0001 Mon Sep 17 00:00:00 2001 From: John Dickinson Date: Tue, 7 Mar 2017 15:06:13 -0800 Subject: [PATCH] add name_check to /info Also removed a bunch of unnecessary unquotes. Just use path_info instead (it's already unquoted). Partial-Bug: #1670915 Change-Id: If1af43485b4708cab6c4b5d7f6f0a334d8752518 --- api-ref/source/storage-object-services.inc | 5 +++++ swift/common/middleware/name_check.py | 20 +++++++++++------ .../unit/common/middleware/test_name_check.py | 22 +++++++++++++++++++ 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/api-ref/source/storage-object-services.inc b/api-ref/source/storage-object-services.inc index 53b5873d2d..72927a6052 100644 --- a/api-ref/source/storage-object-services.inc +++ b/api-ref/source/storage-object-services.inc @@ -165,6 +165,11 @@ is a normal object and not a copy of the manifest. Instead it is a concatenation of all the segment objects. This means that you cannot copy objects larger than 5 GB. +Note that the provider may have limited the characters which are allowed +in an object name. Any name limits are exposed under the ``name_check`` key +in the ``/info`` discoverability response. Regardless of ``name_check`` +limitations, names must be URL quoted UTF-8. + To create custom metadata, use the ``X-Object-Meta-name`` header, where ``name`` is the name of the metadata item. diff --git a/swift/common/middleware/name_check.py b/swift/common/middleware/name_check.py index c6fb974011..a3d485004e 100644 --- a/swift/common/middleware/name_check.py +++ b/swift/common/middleware/name_check.py @@ -40,10 +40,8 @@ The filter returns HTTPBadRequest if path is invalid. @author: eamonn-otoole ''' -from six.moves.urllib.parse import unquote - import re -from swift.common.utils import get_logger +from swift.common.utils import get_logger, register_swift_info from swift.common.swob import Request, HTTPBadRequest @@ -69,6 +67,15 @@ class NameCheckMiddleware(object): self.forbidden_regexp_compiled = None self.logger = get_logger(self.conf, log_route='name_check') + self.register_info() + + def register_info(self): + register_swift_info('name_check', + forbidden_chars=self.forbidden_chars, + maximum_length=self.maximum_length, + forbidden_regexp=self.forbidden_regexp + ) + def check_character(self, req): ''' Checks req.path for any forbidden characters @@ -79,7 +86,7 @@ class NameCheckMiddleware(object): self.logger.debug("name_check: self.forbidden_chars %s" % self.forbidden_chars) - return any((c in unquote(req.path)) for c in self.forbidden_chars) + return any((c in req.path_info) for c in self.forbidden_chars) def check_length(self, req): ''' @@ -87,7 +94,7 @@ class NameCheckMiddleware(object): Returns True if the length exceeds the maximum Returns False if the length is <= the maximum ''' - length = len(unquote(req.path)) + length = len(req.path_info) return length > self.maximum_length def check_regexp(self, req): @@ -103,8 +110,7 @@ class NameCheckMiddleware(object): self.logger.debug("name_check: self.forbidden_regexp %s" % self.forbidden_regexp) - unquoted_path = unquote(req.path) - match = self.forbidden_regexp_compiled.search(unquoted_path) + match = self.forbidden_regexp_compiled.search(req.path_info) return (match is not None) def __call__(self, env, start_response): diff --git a/test/unit/common/middleware/test_name_check.py b/test/unit/common/middleware/test_name_check.py index 89be2daf56..644b348aff 100644 --- a/test/unit/common/middleware/test_name_check.py +++ b/test/unit/common/middleware/test_name_check.py @@ -21,10 +21,12 @@ Created on February 29, 2012 @author: eamonn-otoole ''' +import numbers import unittest from swift.common.swob import Request, Response from swift.common.middleware import name_check +from swift.common import utils MAX_LENGTH = 255 FORBIDDEN_CHARS = '\'\"<>`' @@ -116,5 +118,25 @@ class TestNameCheckMiddleware(unittest.TestCase): self.assertEqual(resp.body, 'OK') +class TestSwiftInfo(unittest.TestCase): + def setUp(self): + utils._swift_info = {} + utils._swift_admin_info = {} + + def test_registered_defaults(self): + name_check.filter_factory({})(FakeApp()) + swift_info = utils.get_swift_info() + self.assertTrue('name_check' in swift_info) + self.assertTrue(isinstance( + swift_info['name_check'].get('maximum_length'), + numbers.Integral)) + self.assertTrue(isinstance( + swift_info['name_check'].get('forbidden_chars'), + str)) + self.assertTrue(isinstance( + swift_info['name_check'].get('forbidden_regexp'), + str)) + + if __name__ == '__main__': unittest.main()