Skip to content

Commit

Permalink
bugfix: add MD5 header for PutObject and UploadPart by default (#2603)
Browse files Browse the repository at this point in the history
* conditionally calculate md5 for putobject and uploadpart
  • Loading branch information
stobrien89 authored Dec 21, 2022
1 parent c020b28 commit 2c33b5d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
7 changes: 7 additions & 0 deletions .changes/nextrelease/checksum-patch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"type": "bugfix",
"category": "S3",
"description": "Fixes bug where MD5 header is not added for PutObject and UploadPart"
}
]
17 changes: 8 additions & 9 deletions src/S3/ApplyChecksumMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
class ApplyChecksumMiddleware
{
use CalculatesChecksumTrait;
private static $sha256 = [

private static $sha256AndMD5 = [
'PutObject',
'UploadPart',
];
Expand Down Expand Up @@ -91,27 +92,25 @@ public function __invoke(

if (!empty($checksumInfo)) {
//if the checksum member is absent, check if it's required
$checksumRequired = isset($checksumInfo['requestChecksumRequired'])
? $checksumInfo['requestChecksumRequired']
: null;
if (!empty($checksumRequired) && !$request->hasHeader('Content-MD5')) {
$checksumRequired = isset($checksumInfo['requestChecksumRequired'])
? $checksumInfo['requestChecksumRequired']
: null;
if ((!empty($checksumRequired) || in_array($name, self::$sha256AndMD5))
&& !$request->hasHeader('Content-MD5')) {
// Set the content MD5 header for operations that require it.
$request = $request->withHeader(
'Content-MD5',
base64_encode(Psr7\Utils::hash($body, 'md5', true))
);
return $next($command, $request);
}
}

if (in_array($name, self::$sha256) && $command['ContentSHA256']) {
if (in_array($name, self::$sha256AndMD5) && $command['ContentSHA256']) {
// Set the content hash header if provided in the parameters.
$request = $request->withHeader(
'X-Amz-Content-Sha256',
$command['ContentSHA256']
);
}

return $next($command, $request);
}
}
17 changes: 15 additions & 2 deletions tests/S3/ApplyChecksumMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class ApplyChecksumMiddlewareTest extends TestCase
public function testAddsContentMd5AsAppropriate($operation, $args, $md5Added, $md5Value)
{
$s3 = $this->getTestClient(
's3',
['api_provider' => ApiProvider::filesystem(__DIR__ . '/fixtures')]
's3'
);
$this->addMockResults($s3, [[]]);
$command = $s3->getCommand($operation, $args);
Expand Down Expand Up @@ -67,6 +66,20 @@ public function getContentMd5UseCases()
false,
null,
],
// Test MD5 added for operations which conditionally require it
[
'PutObject',
['Bucket' => 'foo', 'Key' => 'foo', 'Body' => 'test'],
true,
'CY9rzUYh03PK3k6DJie09g=='
],
// Test MD5 added for operations which conditionally require it
[
'UploadPart',
['Bucket' => 'foo', 'Key' => 'foo', 'Body' => 'test', 'PartNumber' => 1, 'UploadId' => 1],
true,
'CY9rzUYh03PK3k6DJie09g=='
]
];
}

Expand Down

0 comments on commit 2c33b5d

Please sign in to comment.