8000 implement Content-MD5 check for PutObject by bentsku · Pull Request #9064 · localstack/localstack · GitHub
[go: up one dir, main page]

Skip to content

implement Content-MD5 check for PutObject #9064

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

Merged
merged 2 commits into from
Sep 13, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
switch ContentMD5 check for default provider
  • Loading branch information
bentsku committed Sep 5, 2023
commit 64a28a221a56bb696f5c58f906399731869e7164
29 changes: 18 additions & 11 deletions localstack/services/s3/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
from localstack.utils.aws.arns import s3_bucket_name
from localstack.utils.collections import get_safe
from localstack.utils.patch import patch
from localstack.utils.strings import md5, short_uid
from localstack.utils.strings import short_uid
from localstack.utils.time import parse_timestamp
from localstack.utils.urls import localstack_host

Expand Down Expand Up @@ -519,16 +519,6 @@ def put_object(
if checksum_algorithm := request.get("ChecksumAlgorithm"):
verify_checksum(checksum_algorithm, context.request.data, request)

# TODO: handle ContentMD5 and ChecksumAlgorithm in a handler for all requests except requests with a streaming
# body. We can use the specs to verify which operations needs to have the checksum validated
if content_md5 := request.get("ContentMD5"):
calculated_md5 = etag_to_base_64_content_md5(md5(context.request.data))
if calculated_md5 != content_md5:
raise InvalidDigest(
"The Content-MD5 you specified was invalid.",
Content_MD5=content_md5,
)

moto_backend = get_moto_s3_backend(context)
moto_bucket = get_bucket_from_moto(moto_backend, bucket=request["Bucket"])

Expand All @@ -546,6 +536,23 @@ def put_object(
)
raise

# TODO: handle ContentMD5 and ChecksumAlgorithm in a handler for all requests except requests with a streaming
# body. We can use the specs to verify which operations needs to have the checksum validated
# verify content_md5
if content_md5 := request.get("ContentMD5"):
calculated_md5 = etag_to_base_64_content_md5(response["ETag"].strip('"'))
if calculated_md5 != content_md5:
moto_backend.delete_object(
bucket_name=request["Bucket"],
key_name=request["Key"],
version_id=response.get("VersionId"),
bypass=True,
)
raise InvalidDigest(
"The Content-MD5 you specified was invalid.",
Content_MD5=content_md5,
)

# moto interprets the Expires in query string for presigned URL as an Expires header and use it for the object
# we set it to the correctly parsed value in Request, else we remove it from moto metadata
# we are getting the last set key here so no need for versionId when getting the key
Expand Down
0