s3api: Catch binascii.Error

...instead of raising AttributeError.

Change-Id: I6515c36cb7f5b98f715bc8c33f1f822b1cfad668
Closes-Bug: #1908412
This commit is contained in:
Tim Burke 2020-12-16 22:09:14 -08:00
parent e0d46d77fa
commit 4415f45138
2 changed files with 15 additions and 1 deletions

View File

@ -761,7 +761,7 @@ class S3Request(swob.Request):
try:
self.headers['ETag'] = binascii.b2a_hex(
binascii.a2b_base64(value))
except binascii.error:
except binascii.Error:
# incorrect padding, most likely
raise InvalidDigest(content_md5=value)

View File

@ -576,6 +576,20 @@ class TestS3ApiMiddleware(S3ApiTestCase):
status, headers, body = self.call_s3api(req)
self.assertEqual(self._get_error_code(body), 'InvalidDigest')
def test_object_create_bad_md5_bad_padding(self):
too_short_digest = md5(b'hey', usedforsecurity=False).digest()
md5_str = base64.b64encode(too_short_digest).strip(b'=\n')
if not six.PY2:
md5_str = md5_str.decode('ascii')
req = Request.blank(
'/bucket/object',
environ={'REQUEST_METHOD': 'PUT',
'HTTP_AUTHORIZATION': 'AWS X:Y:Z',
'HTTP_CONTENT_MD5': md5_str},
headers={'Date': self.get_date_header()})
status, headers, body = self.call_s3api(req)
self.assertEqual(self._get_error_code(body), 'InvalidDigest')
def test_object_create_bad_md5_too_long(self):
too_long_digest = md5(
b'hey', usedforsecurity=False).digest() + b'suffix'