Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions moto/s3/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,22 @@ def __init__(self, failed_condition: str):
self.condition = failed_condition


class BadDigest(S3ClientError):
code = "BadDigest"

def __init__(self) -> None:
super().__init__(
"The Content-MD5 you specified did not match what we received."
)


class InvalidDigest(S3ClientError):
code = "InvalidDigest"

def __init__(self) -> None:
super().__init__("The Content-MD5 you specified is not valid.")


class InvalidRange(S3ClientError):
code = "InvalidRange"

Expand Down
20 changes: 20 additions & 0 deletions moto/s3/responses.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import base64
import binascii
import hashlib
import io
import re
import urllib.parse
Expand Down Expand Up @@ -32,6 +35,7 @@
from ..core.exceptions import SignatureDoesNotMatchError
from .exceptions import (
AccessForbidden,
BadDigest,
BucketAccessDeniedError,
BucketAlreadyExists,
BucketAlreadyOwnedByYou,
Expand All @@ -41,6 +45,7 @@
IllegalLocationConstraintException,
IncompatibleLocationConstraintException,
InvalidContinuationToken,
InvalidDigest,
InvalidLocationConstraintException,
InvalidMaxPartArgument,
InvalidMaxPartNumberArgument,
Expand Down Expand Up @@ -2295,6 +2300,7 @@ def put_object(self) -> TYPE_RESPONSE:
):
raise PreconditionFailed("If-None-Match")

self._check_content_md5()
checksum_algorithm, checksum_value = self._get_checksum(response_headers)

bucket = self.backend.get_bucket(self.bucket_name)
Expand Down Expand Up @@ -2335,6 +2341,19 @@ def put_object(self) -> TYPE_RESPONSE:
response_headers.pop("content-length", None)
return 200, response_headers, ""

def _check_content_md5(self) -> None:
content_md5 = self.headers.get("Content-MD5")
if content_md5 is None:
return
try:
expected = base64.b64decode(content_md5, validate=True)
except binascii.Error:
raise InvalidDigest()
if len(expected) != 16:
raise InvalidDigest()
if hashlib.md5(self.body or b"").digest() != expected:
raise BadDigest()

def _get_checksum(self, response_headers: dict[str, Any]) -> tuple[str, str | None]:
checksum_algorithm = self.headers.get("x-amz-sdk-checksum-algorithm", "")
checksum_header = f"x-amz-checksum-{checksum_algorithm.lower()}"
Expand Down Expand Up @@ -2566,6 +2585,7 @@ def upload_part(self) -> TYPE_RESPONSE:

if part_number > 10000:
raise InvalidMaxPartNumberArgument(part_number)
self._check_content_md5()
key = self.backend.upload_part(
self.bucket_name, upload_id, part_number, self.body
)
Expand Down
37 changes: 36 additions & 1 deletion tests/test_s3/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from moto.s3.responses import DEFAULT_REGION_NAME
from tests import DEFAULT_ACCOUNT_ID, aws_verified

from . import s3_aws_verified
from . import generate_content_md5, s3_aws_verified


class MyModel:
Expand Down Expand Up @@ -3706,3 +3706,38 @@ def test_list_buckets_with_prefix_and_bucket_region():
resp = client.list_buckets(Prefix=prefix, BucketRegion="us-east-2")

assert [b["Name"] for b in resp["Buckets"]] == [us_east_2_prefixed_bucket]


@mock_aws
def test_put_object_and_upload_part_check_content_md5():
client = boto3.client("s3", region_name=DEFAULT_REGION_NAME)
client.create_bucket(Bucket="bucket")
body = b"abc"
wrong = generate_content_md5(b"xyz")

upload_id = client.create_multipart_upload(Bucket="bucket", Key="mp")["UploadId"]
calls = [
lambda md5: client.put_object(
Bucket="bucket", Key="k", Body=body, ContentMD5=md5
),
lambda md5: client.upload_part(
Bucket="bucket",
Key="mp",
UploadId=upload_id,
PartNumber=1,
Body=body,
ContentMD5=md5,
),
]
for call in calls:
with pytest.raises(ClientError) as exc:
call(wrong)
assert exc.value.response["Error"]["Code"] == "BadDigest"
assert exc.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400

with pytest.raises(ClientError) as exc:
call("not-base64")
assert exc.value.response["Error"]["Code"] == "InvalidDigest"

call(generate_content_md5(body))
assert client.get_object(Bucket="bucket", Key="k")["Body"].read() == body