Skip to content

Commit

Permalink
Handle COPY verb in container quota middleware
Browse files Browse the repository at this point in the history
The patch fix the ability to bypass the account
quota using the COPY verb. The quotas for destination
container are now checked before the copy.

Change-Id: I9f409ea6c0893f36972aa1fabe31fb143e151ab5
Fixes: bug #1259565
  • Loading branch information
morucci committed Feb 4, 2014
1 parent 766ab4f commit 946984c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
25 changes: 19 additions & 6 deletions swift/common/middleware/container_quotas.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,21 @@ def __call__(self, req):
return HTTPBadRequest(body='Invalid count quota.')

# check user uploads against quotas
elif obj and req.method == 'PUT':
container_info = get_container_info(
req.environ, self.app, swift_source='CQ')
elif obj and req.method in ('PUT', 'COPY'):
container_info = None
if req.method == 'PUT':
container_info = get_container_info(
req.environ, self.app, swift_source='CQ')
if req.method == 'COPY' and 'Destination' in req.headers:
dest = req.headers.get('Destination').lstrip('/')
path_info = req.environ['PATH_INFO']
req.environ['PATH_INFO'] = "/%s/%s/%s" % (
version, account, dest)
try:
container_info = get_container_info(
req.environ, self.app, swift_source='CQ')
finally:
req.environ['PATH_INFO'] = path_info
if not container_info or not is_success(container_info['status']):
# this will hopefully 404 later
return self.app
Expand All @@ -90,10 +102,11 @@ def __call__(self, req):
'bytes' in container_info and \
container_info['meta']['quota-bytes'].isdigit():
content_length = (req.content_length or 0)
if 'x-copy-from' in req.headers:
src_cont, src_obj = check_copy_from_header(req)
if 'x-copy-from' in req.headers or req.method == 'COPY':
if 'x-copy-from' in req.headers:
container, obj = check_copy_from_header(req)
path = '/%s/%s/%s/%s' % (version, account,
src_cont, src_obj)
container, obj)
object_info = get_object_info(req.environ, self.app, path)
if not object_info or not object_info['length']:
content_length = 0
Expand Down
54 changes: 54 additions & 0 deletions test/unit/common/middleware/test_quotas.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ def test_exceed_bytes_quota_copy_from(self):
res = req.get_response(app)
self.assertEquals(res.status_int, 413)

def test_exceed_bytes_quota_copy_verb(self):
app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {})
cache = FakeCache({'bytes': 0, 'meta': {'quota-bytes': '2'}})

req = Request.blank('/v1/a/c2/o2',
environ={'REQUEST_METHOD': 'COPY',
'swift.object/a/c2/o2': {'length': 10},
'swift.cache': cache},
headers={'Destination': '/c/o'})
res = req.get_response(app)
self.assertEquals(res.status_int, 413)

def test_not_exceed_bytes_quota(self):
app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {})
cache = FakeCache({'bytes': 0, 'meta': {'quota-bytes': '100'}})
Expand All @@ -127,6 +139,17 @@ def test_not_exceed_bytes_quota_copy_from(self):
res = req.get_response(app)
self.assertEquals(res.status_int, 200)

def test_not_exceed_bytes_quota_copy_verb(self):
app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {})
cache = FakeCache({'bytes': 0, 'meta': {'quota-bytes': '100'}})
req = Request.blank('/v1/a/c2/o2',
environ={'REQUEST_METHOD': 'COPY',
'swift.object/a/c2/o2': {'length': 10},
'swift.cache': cache},
headers={'Destination': '/c/o'})
res = req.get_response(app)
self.assertEquals(res.status_int, 200)

def test_bytes_quota_copy_from_no_src(self):
app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {})
cache = FakeCache({'bytes': 0, 'meta': {'quota-bytes': '100'}})
Expand All @@ -148,6 +171,17 @@ def test_bytes_quota_copy_from_bad_src(self):
res = req.get_response(app)
self.assertEquals(res.status_int, 412)

def test_bytes_quota_copy_verb_no_src(self):
app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {})
cache = FakeCache({'bytes': 0, 'meta': {'quota-bytes': '100'}})
req = Request.blank('/v1/a/c2/o3',
environ={'REQUEST_METHOD': 'COPY',
'swift.object/a/c2/o2': {'length': 10},
'swift.cache': cache},
headers={'Destination': '/c/o'})
res = req.get_response(app)
self.assertEquals(res.status_int, 200)

def test_exceed_counts_quota(self):
app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {})
cache = FakeCache({'object_count': 1, 'meta': {'quota-count': '1'}})
Expand All @@ -169,6 +203,16 @@ def test_exceed_counts_quota_copy_from(self):
res = req.get_response(app)
self.assertEquals(res.status_int, 413)

def test_exceed_counts_quota_copy_verb(self):
app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {})
cache = FakeCache({'object_count': 1, 'meta': {'quota-count': '1'}})
req = Request.blank('/v1/a/c2/o2',
environ={'REQUEST_METHOD': 'COPY',
'swift.cache': cache},
headers={'Destination': '/c/o'})
res = req.get_response(app)
self.assertEquals(res.status_int, 413)

def test_not_exceed_counts_quota(self):
app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {})
cache = FakeCache({'object_count': 1, 'meta': {'quota-count': '2'}})
Expand All @@ -189,6 +233,16 @@ def test_not_exceed_counts_quota_copy_from(self):
res = req.get_response(app)
self.assertEquals(res.status_int, 200)

def test_not_exceed_counts_quota_copy_verb(self):
app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {})
cache = FakeCache({'object_count': 1, 'meta': {'quota-count': '2'}})
req = Request.blank('/v1/a/c2/o2',
environ={'REQUEST_METHOD': 'COPY',
'swift.cache': cache},
headers={'Destination': '/c/o'})
res = req.get_response(app)
self.assertEquals(res.status_int, 200)

def test_invalid_quotas(self):
req = Request.blank(
'/v1/a/c',
Expand Down

0 comments on commit 946984c

Please sign in to comment.