From 2419df2730c5879092f0058e63a7dfbae6519fa0 Mon Sep 17 00:00:00 2001 From: Samuel Merritt Date: Wed, 9 Oct 2013 12:25:42 -0700 Subject: [PATCH] Fix 500 from account-quota middleware If a user had set X-Account-Meta-Quota-Bytes to something non-integer prior to the installation of the account-quota middleware, then the quota check would choke on it. Now a non-integer value is treated as "no quota". Change-Id: I5c38911be1f66fa293aea9c78590d4ce7d184113 --- swift/common/middleware/account_quotas.py | 5 ++++- test/unit/common/middleware/test_account_quotas.py | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/swift/common/middleware/account_quotas.py b/swift/common/middleware/account_quotas.py index c687f983df..917fd4f45b 100644 --- a/swift/common/middleware/account_quotas.py +++ b/swift/common/middleware/account_quotas.py @@ -114,7 +114,10 @@ class AccountQuotaMiddleware(object): return self.app new_size = int(account_info['bytes']) + content_length - quota = int(account_info['meta'].get('quota-bytes', -1)) + try: + quota = int(account_info['meta'].get('quota-bytes', -1)) + except ValueError: + return self.app if 0 <= quota < new_size: return HTTPRequestEntityTooLarge() diff --git a/test/unit/common/middleware/test_account_quotas.py b/test/unit/common/middleware/test_account_quotas.py index a058439b6e..9854fb605a 100644 --- a/test/unit/common/middleware/test_account_quotas.py +++ b/test/unit/common/middleware/test_account_quotas.py @@ -113,6 +113,19 @@ class TestAccountQuota(unittest.TestCase): res = req.get_response(app) self.assertEquals(res.status_int, 200) + def test_bogus_quota_is_ignored(self): + # This can happen if the metadata was set by a user prior to the + # activation of the account-quota middleware + headers = [('x-account-bytes-used', '1000'), + ('x-account-meta-quota-bytes', 'pasty-plastogene')] + app = account_quotas.AccountQuotaMiddleware(FakeApp(headers)) + cache = FakeCache(None) + req = Request.blank('/v1/a/c/o', + environ={'REQUEST_METHOD': 'PUT', + 'swift.cache': cache}) + res = req.get_response(app) + self.assertEquals(res.status_int, 200) + def test_exceed_bytes_quota(self): headers = [('x-account-bytes-used', '1000'), ('x-account-meta-quota-bytes', '0')]