-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
fix(s3): Add Content-MD5 header for DeleteObjects to fix AWS SDK v3.339.0+ compatibility #60195
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
Open
skjnldsv
wants to merge
2
commits into
master
Choose a base branch
from
fix/s3-content-md5-deleteobjects
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+215
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
tests/lib/Files/ObjectStore/S3ContentMd5MiddlewareTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace Test\Files\ObjectStore; | ||
|
|
||
| use GuzzleHttp\Psr7\Request; | ||
| use GuzzleHttp\Psr7\Utils; | ||
| use Test\TestCase; | ||
|
|
||
| /** | ||
| * Test suite for S3 Content-MD5 middleware | ||
| * Verifies AWS SDK PHP v3.339.0+ compatibility fix for DeleteObjects operations | ||
| * @see https://github.com/aws/aws-sdk-php/issues/3068 | ||
| */ | ||
| #[\PHPUnit\Framework\Attributes\Group('objectstore')] | ||
| class S3ContentMd5MiddlewareTest extends TestCase { | ||
|
|
||
| /** | ||
| * Helper: Apply middleware logic to a request | ||
| * Mirrors the logic from S3ConnectionTrait::addDeleteObjectsContentMd5Middleware() | ||
| */ | ||
| private function applyContentMd5Middleware(Request $request): Request { | ||
| if ($request->getUri()->getQuery() !== 'delete') { | ||
| return $request; | ||
| } | ||
|
|
||
| if (!$request->hasHeader('Content-MD5')) { | ||
| $body = $request->getBody(); | ||
| $contentMd5 = base64_encode(Utils::hash($body, 'md5', true)); | ||
| return $request->withHeader('Content-MD5', $contentMd5); | ||
| } | ||
|
|
||
| return $request; | ||
| } | ||
|
|
||
| /** | ||
| * Test that Content-MD5 header is added to DeleteObjects requests | ||
| */ | ||
| public function testContentMd5HeaderAddedToDeleteObjects(): void { | ||
| $testBody = '<?xml version="1.0" encoding="UTF-8"?><Delete xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Object><Key>test-key</Key></Object></Delete>'; | ||
| $request = new Request('POST', 'http://s3.example.com/bucket?delete', [], $testBody); | ||
|
|
||
| // Calculate expected MD5 | ||
| $expectedMd5 = base64_encode(md5($testBody, true)); | ||
|
|
||
| // Apply middleware logic | ||
| $resultRequest = $this->applyContentMd5Middleware($request); | ||
|
|
||
| // Verify header was added | ||
| $this->assertTrue($resultRequest->hasHeader('Content-MD5')); | ||
| $this->assertEquals($expectedMd5, $resultRequest->getHeaderLine('Content-MD5')); | ||
| } | ||
|
|
||
| /** | ||
| * Test that Content-MD5 header is NOT added to non-DeleteObjects requests | ||
| */ | ||
| public function testContentMd5NotAddedToNonDeleteRequests(): void { | ||
| $testCases = [ | ||
| 'GET request' => new Request('GET', 'http://s3.example.com/bucket/key'), | ||
| 'PUT request' => new Request('PUT', 'http://s3.example.com/bucket/key'), | ||
| 'HEAD request' => new Request('HEAD', 'http://s3.example.com/bucket/key'), | ||
| 'POST with different query' => new Request('POST', 'http://s3.example.com/bucket?uploads'), | ||
| ]; | ||
|
|
||
| foreach ($testCases as $label => $request) { | ||
| $resultRequest = $this->applyContentMd5Middleware($request); | ||
|
|
||
| // Verify header was NOT added for non-delete requests | ||
| $this->assertFalse($resultRequest->hasHeader('Content-MD5'), "Content-MD5 should not be added for: $label"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Test that existing Content-MD5 header is preserved | ||
| */ | ||
| public function testExistingContentMd5HeaderPreserved(): void { | ||
| $testBody = 'test data'; | ||
| $existingMd5 = 'existing-md5-value'; | ||
| $request = new Request( | ||
| 'POST', | ||
| 'http://s3.example.com/bucket?delete', | ||
| ['Content-MD5' => $existingMd5], | ||
| $testBody | ||
| ); | ||
|
|
||
| // Apply middleware logic | ||
| $resultRequest = $this->applyContentMd5Middleware($request); | ||
|
|
||
| // Verify existing header was preserved | ||
| $this->assertTrue($resultRequest->hasHeader('Content-MD5')); | ||
| $this->assertEquals($existingMd5, $resultRequest->getHeaderLine('Content-MD5')); | ||
| } | ||
|
|
||
| /** | ||
| * Test MD5 calculation with various body sizes | ||
| */ | ||
| public function testMd5CalculationWithVariousSizes(): void { | ||
| $testBodies = [ | ||
| 'small' => 'x', | ||
| 'medium' => str_repeat('y', 1000), | ||
| 'large' => str_repeat('z', 10000), | ||
| 'xml_payload' => '<?xml version="1.0"?><Delete xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Object><Key>file1.txt</Key></Object><Object><Key>file2.txt</Key></Object></Delete>', | ||
| ]; | ||
|
|
||
| foreach ($testBodies as $label => $body) { | ||
| $request = new Request('POST', 'http://s3.example.com/bucket?delete', [], $body); | ||
| $expectedMd5 = base64_encode(md5($body, true)); | ||
|
|
||
| $resultRequest = $this->applyContentMd5Middleware($request); | ||
|
|
||
| $this->assertEquals( | ||
| $expectedMd5, | ||
| $resultRequest->getHeaderLine('Content-MD5'), | ||
| "MD5 mismatch for $label body size" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Test MD5 header format is base64-encoded | ||
| */ | ||
| public function testMd5HeaderFormatIsBase64(): void { | ||
| $testBody = 'test data for base64 validation'; | ||
| $request = new Request('POST', 'http://s3.example.com/bucket?delete', [], $testBody); | ||
|
|
||
| $resultRequest = $this->applyContentMd5Middleware($request); | ||
|
|
||
| $md5Header = $resultRequest->getHeaderLine('Content-MD5'); | ||
|
|
||
| // Verify it's a valid base64 string | ||
| $this->assertNotEmpty($md5Header); | ||
| $this->assertEquals($md5Header, base64_encode(base64_decode($md5Header, true))); | ||
|
|
||
| // Verify MD5 is typically 24 chars when base64-encoded (16 bytes) | ||
| $this->assertEquals(24, strlen($md5Header)); | ||
| } | ||
|
|
||
| /** | ||
| * Test edge case: Empty body in DeleteObjects request | ||
| */ | ||
| public function testMd5CalculationWithEmptyBody(): void { | ||
| $request = new Request('POST', 'http://s3.example.com/bucket?delete', [], ''); | ||
|
|
||
| $resultRequest = $this->applyContentMd5Middleware($request); | ||
|
|
||
| // MD5 of empty string should still produce a valid header | ||
| $this->assertTrue($resultRequest->hasHeader('Content-MD5')); | ||
| $this->assertNotEmpty($resultRequest->getHeaderLine('Content-MD5')); | ||
| } | ||
|
|
||
| /** | ||
| * Test that middleware is idempotent (doesn't double-hash) | ||
| */ | ||
| public function testMiddlewareIsIdempotent(): void { | ||
| $testBody = 'test data'; | ||
| $request = new Request('POST', 'http://s3.example.com/bucket?delete', [], $testBody); | ||
|
|
||
| // Apply middleware twice | ||
| $resultRequest1 = $this->applyContentMd5Middleware($request); | ||
| $resultRequest2 = $this->applyContentMd5Middleware($resultRequest1); | ||
|
|
||
| // Headers should be identical | ||
| $this->assertEquals( | ||
| $resultRequest1->getHeaderLine('Content-MD5'), | ||
| $resultRequest2->getHeaderLine('Content-MD5'), | ||
| 'Middleware should be idempotent' | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.