From d6c65c34aa927911b2fbb82d62913f204e58b2c0 Mon Sep 17 00:00:00 2001 From: David Goetz Date: Fri, 25 Oct 2013 19:44:46 +0000 Subject: [PATCH] catch decompression errors Change-Id: Ica380edc2364a5e18cefc26f70710e18ea329cfa --- swift/common/middleware/bulk.py | 3 ++- test/unit/common/middleware/test_bulk.py | 14 +++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/swift/common/middleware/bulk.py b/swift/common/middleware/bulk.py index ea34980f92..bec689c656 100644 --- a/swift/common/middleware/bulk.py +++ b/swift/common/middleware/bulk.py @@ -17,6 +17,7 @@ import tarfile from urllib import quote, unquote from xml.sax import saxutils from time import time +import zlib from swift.common.swob import Request, HTTPBadGateway, \ HTTPCreated, HTTPBadRequest, HTTPNotFound, HTTPUnauthorized, HTTPOk, \ HTTPPreconditionFailed, HTTPRequestEntityTooLarge, HTTPNotAcceptable, \ @@ -500,7 +501,7 @@ class Bulk(object): except HTTPException as err: resp_dict['Response Status'] = err.status resp_dict['Response Body'] = err.body - except tarfile.TarError as tar_error: + except (tarfile.TarError, zlib.error) as tar_error: resp_dict['Response Status'] = HTTPBadRequest().status resp_dict['Response Body'] = 'Invalid Tar File: %s' % tar_error except Exception: diff --git a/test/unit/common/middleware/test_bulk.py b/test/unit/common/middleware/test_bulk.py index 29181b3c3c..f307394aa6 100644 --- a/test/unit/common/middleware/test_bulk.py +++ b/test/unit/common/middleware/test_bulk.py @@ -17,6 +17,7 @@ import unittest import os import tarfile import urllib +import zlib from shutil import rmtree from tempfile import mkdtemp from StringIO import StringIO @@ -270,6 +271,16 @@ class TestUntar(unittest.TestCase): resp_body = self.handle_extract_and_iter(req, '') self.assertTrue('411 Length Required' in resp_body) + def test_bad_tar(self): + req = Request.blank('/create_cont_fail/acc/cont', body='') + + def bad_open(*args, **kwargs): + raise zlib.error('bad tar') + + with patch.object(tarfile, 'open', bad_open): + resp_body = self.handle_extract_and_iter(req, '') + self.assertTrue('400 Bad Request' in resp_body) + def build_tar(self, dir_tree=None): if not dir_tree: dir_tree = [ @@ -354,7 +365,8 @@ class TestUntar(unittest.TestCase): resp_data = json.loads(resp_body) self.assertEquals(resp_data['Response Status'], '400 Bad Request') self.assertEquals( - resp_data['Response Body'], 'Invalid Tar File: not a gzip file') + resp_data['Response Body'].lower(), + 'invalid tar file: not a gzip file') def test_extract_tar_fail_max_failed_extractions(self): self.build_tar()