Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Content-MD5 header for s3transfer #2221

Merged
merged 2 commits into from Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion botocore/utils.py
Expand Up @@ -2215,7 +2215,7 @@ def conditionally_calculate_md5(params, **kwargs):
"""Only add a Content-MD5 if the system supports it."""
headers = params['headers']
body = params['body']
if MD5_AVAILABLE and body and 'Content-MD5' not in headers:
if MD5_AVAILABLE and body is not None and 'Content-MD5' not in headers:
md5_digest = calculate_md5(body, **kwargs)
params['headers']['Content-MD5'] = md5_digest

Expand Down
12 changes: 12 additions & 0 deletions tests/functional/test_s3.py
Expand Up @@ -919,6 +919,18 @@ def test_content_md5_set(self):
self.client.put_object(Bucket='foo', Key='bar', Body='baz')
self.assertIn('content-md5', self.get_sent_headers())

def test_content_md5_set_empty_body(self):
with self.http_stubber:
self.client.put_object(Bucket='foo', Key='bar', Body='')
self.assertIn('content-md5', self.get_sent_headers())

def test_content_md5_set_empty_file(self):
with self.http_stubber:
with temporary_file('rb') as f:
assert f.read() == b''
self.client.put_object(Bucket='foo', Key='bar', Body=f)
self.assertIn('content-md5', self.get_sent_headers())

def test_content_sha256_set_if_config_value_is_true(self):
config = Config(signature_version='s3v4', s3={
'payload_signing_enabled': True
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/test_handlers.py
Expand Up @@ -1217,6 +1217,17 @@ def test_add_md5_with_bytes_object(self):
request_dict['headers']['Content-MD5'],
'OFj2IjCsPJFfMAxmQxLGPw==')

def test_add_md5_with_empty_body(self):
request_dict = {
'body': b'',
'headers': {}
}
self.md5_digest.return_value = b'8X\xf6"0\xac<\x91_0\x0cfC\x12\xc6?'
conditionally_calculate_md5(request_dict)
self.assertEqual(
request_dict['headers']['Content-MD5'],
'OFj2IjCsPJFfMAxmQxLGPw==')

def test_add_md5_with_bytearray_object(self):
request_dict = {
'body': bytearray(b'foobar'),
Expand Down