-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Verifying ContentRange on response from GetObject #3604
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -825,6 +825,33 @@ namespace Aws | |
| return rangeStream.str(); | ||
| } | ||
|
|
||
| static bool VerifyContentRange(const Aws::String& requestedRange, const Aws::String& responseContentRange) | ||
| { | ||
| if (requestedRange.empty() || responseContentRange.empty()) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (requestedRange.find("bytes=") != 0) | ||
| { | ||
| return false; | ||
| } | ||
| Aws::String requestRange = requestedRange.substr(6); | ||
|
|
||
| if (responseContentRange.find("bytes ") != 0) | ||
| { | ||
| return false; | ||
| } | ||
| Aws::String responseRange = responseContentRange.substr(6); | ||
| size_t slashPos = responseRange.find('/'); | ||
| if (slashPos != Aws::String::npos) | ||
| { | ||
| responseRange = responseRange.substr(0, slashPos); | ||
| } | ||
|
|
||
| return requestRange == responseRange; | ||
| } | ||
|
|
||
| void TransferManager::DoSinglePartDownload(const std::shared_ptr<TransferHandle>& handle) | ||
| { | ||
| auto queuedParts = handle->GetQueuedParts(); | ||
|
|
@@ -1091,7 +1118,6 @@ namespace Aws | |
| const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context) | ||
| { | ||
| AWS_UNREFERENCED_PARAM(client); | ||
| AWS_UNREFERENCED_PARAM(request); | ||
|
|
||
| std::shared_ptr<TransferHandleAsyncContext> transferContext = | ||
| std::const_pointer_cast<TransferHandleAsyncContext>(std::static_pointer_cast<const TransferHandleAsyncContext>(context)); | ||
|
|
@@ -1110,6 +1136,37 @@ namespace Aws | |
| } | ||
| else | ||
| { | ||
| if (request.RangeHasBeenSet()) | ||
| { | ||
| const auto& requestedRange = request.GetRange(); | ||
| const auto& responseContentRange = outcome.GetResult().GetContentRange(); | ||
|
|
||
| if (!responseContentRange.empty()) | ||
|
||
| { | ||
| if (!VerifyContentRange(requestedRange, responseContentRange)) | ||
| { | ||
| Aws::Client::AWSError<Aws::S3::S3Errors> error(Aws::S3::S3Errors::INTERNAL_FAILURE, | ||
| "ContentRangeMismatch", | ||
| "ContentRange in response does not match requested range", | ||
| false); | ||
| AWS_LOGSTREAM_ERROR(CLASS_TAG, "Transfer handle [" << handle->GetId() | ||
| << "] ContentRange mismatch. Requested: [" << requestedRange | ||
| << "] Received: [" << responseContentRange << "]"); | ||
| handle->ChangePartToFailed(partState); | ||
| handle->SetError(error); | ||
| TriggerErrorCallback(handle, error); | ||
| handle->Cancel(); | ||
|
|
||
| if(partState->GetDownloadBuffer()) | ||
| { | ||
| m_bufferManager.Release(partState->GetDownloadBuffer()); | ||
| partState->SetDownloadBuffer(nullptr); | ||
| } | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if(handle->ShouldContinue()) | ||
| { | ||
| Aws::IOStream* bufferStream = partState->GetDownloadPartStream(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2328,6 +2328,40 @@ TEST_P(TransferTests, TransferManager_TestRelativePrefix) | |
| } | ||
| } | ||
|
|
||
| TEST_P(TransferTests, TransferManager_ContentRangeVerificationTest) | ||
| { | ||
| const Aws::String RandomFileName = Aws::Utils::UUID::RandomUUID(); | ||
| Aws::String testFileName = MakeFilePath(RandomFileName.c_str()); | ||
| ScopedTestFile testFile(testFileName, MEDIUM_TEST_SIZE, testString); | ||
|
|
||
| TransferManagerConfiguration transferManagerConfig(m_executor.get()); | ||
| transferManagerConfig.s3Client = m_s3Clients[GetParam()]; | ||
| auto transferManager = TransferManager::Create(transferManagerConfig); | ||
|
|
||
| std::shared_ptr<TransferHandle> uploadPtr = transferManager->UploadFile(testFileName, GetTestBucketName(), RandomFileName, "text/plain", Aws::Map<Aws::String, Aws::String>()); | ||
| uploadPtr->WaitUntilFinished(); | ||
| ASSERT_EQ(TransferStatus::COMPLETED, uploadPtr->GetStatus()); | ||
| ASSERT_TRUE(WaitForObjectToPropagate(GetTestBucketName(), RandomFileName.c_str())); | ||
|
|
||
| auto downloadFileName = MakeDownloadFileName(RandomFileName); | ||
| auto createStreamFn = [=](){ | ||
| #ifdef _MSC_VER | ||
| return Aws::New<Aws::FStream>(ALLOCATION_TAG, Aws::Utils::StringUtils::ToWString(downloadFileName.c_str()).c_str(), std::ios_base::out | std::ios_base::in | std::ios_base::binary | std::ios_base::trunc); | ||
| #else | ||
| return Aws::New<Aws::FStream>(ALLOCATION_TAG, downloadFileName.c_str(), std::ios_base::out | std::ios_base::in | std::ios_base::binary | std::ios_base::trunc); | ||
| #endif | ||
| }; | ||
|
|
||
| uint64_t offset = 1024; | ||
| uint64_t partSize = 2048; | ||
| std::shared_ptr<TransferHandle> downloadPtr = transferManager->DownloadFile(GetTestBucketName(), RandomFileName, offset, partSize, createStreamFn); | ||
|
|
||
| downloadPtr->WaitUntilFinished(); | ||
| ASSERT_EQ(TransferStatus::COMPLETED, downloadPtr->GetStatus()); | ||
| ASSERT_EQ(partSize, downloadPtr->GetBytesTotalSize()); | ||
| ASSERT_EQ(partSize, downloadPtr->GetBytesTransferred()); | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should there also be a test with a false content range to test for failures? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll look into creating a mock S3 response with an incorrect ContentRange to test the failure path There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 i think even as going as far to have a "transfer manager unit test" might be good. you can largely copy the S3 unit tests to create the skeleton of it. |
||
| INSTANTIATE_TEST_SUITE_P(Https, TransferTests, testing::Values(TestType::Https)); | ||
| INSTANTIATE_TEST_SUITE_P(Http, TransferTests, testing::Values(TestType::Http)); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
else if(request.RangeHasBeenSet()) { ... }is preferred toelse { if { ... }}