Skip to content

Commit

Permalink
Handle older S3 packages for operations that require MD5 or checksums (
Browse files Browse the repository at this point in the history
…#3642)

* fix: Handle older S3 packages for operations that support either MD5 or flexible checksums
* fix: Update HttpWebRequestMessage to handle MD5Stream (which does not support seeking)
  • Loading branch information
dscpinheiro authored Feb 7, 2025
1 parent 970f624 commit 9cd49b6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"core": {
"updateMinimum": true,
"type": "Patch",
"changeLogMessages": [
"Update data integrity component to handle older versions of the `AWSSDK.S3` package when operations require a `Content-MD5` header"
]
}
}
8 changes: 7 additions & 1 deletion sdk/src/Core/Amazon.Runtime/Internal/Util/ChecksumUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,13 @@ public static void SetChecksumData(IRequest request, string checksumAlgorithm, b
// S3 does not support).

// TODO: Similar to "SetRequestChecksum", this method should be removed in V4.
if (!string.IsNullOrEmpty(checksumAlgorithm))
if (fallbackToMD5)
{
// Reported in https://github.com/aws/aws-sdk-net/issues/3641
// Some operations will fail if the Content-MD5 header is not set, so we need to set it to maintain backwards compatibility.
SetChecksumData(request);
}
else if (!string.IsNullOrEmpty(checksumAlgorithm))
{
SetChecksumData(request, checksumAlgorithm, fallbackToMD5, isRequestChecksumRequired: true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,20 @@ public void WriteToRequestBody(HttpContent requestContent, Stream contentStream,
if ((isChunkedUploadWrapperStreamWithLength || isTrailingHeadersWrapperStreamWithLength || isCompressionWrapperStreamWithLength)
|| (chunkedUploadWrapperStream == null && trailingHeadersWrapperStream == null && compressionWrapperStream == null))
{
_request.Content.Headers.ContentLength = contentStream.Length - contentStream.Position;
long position;
try
{
position = contentStream.Position;
}
catch (NotSupportedException)
{
// If this method is invoked from an older service package, the content stream may not support getting the
// current position (for example, "MD5Stream" doesn't).
// In this case, we'll assume the position is 0 to maintain the previous behavior.
position = 0;
}

_request.Content.Headers.ContentLength = contentStream.Length - position;
}

WriteContentHeaders(contentHeaders);
Expand Down

0 comments on commit 9cd49b6

Please sign in to comment.