Skip to content

S3: check the Content-MD5 of put_object and upload_part - #10508

Open
jayhemnani9910 wants to merge 1 commit into
getmoto:masterfrom
jayhemnani9910:content-md5-check
Open

jayhemnani9910 wants to merge 1 commit into
getmoto:masterfrom
jayhemnani9910:content-md5-check

Conversation

@jayhemnani9910

Copy link
Copy Markdown

A Content-MD5 that doesn't match the body is accepted and the object is stored:

s3.put_object(Bucket=b, Key="k", Body=b"abc", ContentMD5=base64.b64encode(hashlib.md5(b"xyz").digest()).decode())
# moto 5.2.5: 200; S3: 400 BadDigest
s3.put_object(Bucket=b, Key="k", Body=b"abc", ContentMD5="not-base64")
# moto 5.2.5: 200; S3: 400 InvalidDigest

Code that relies on Content-MD5 to catch a corrupted upload can't test that path against moto. put_object and upload_part now decode the header and compare it with the MD5 of the body: a value that isn't base64 for 16 bytes raises InvalidDigest, a mismatch raises BadDigest, both 400. Requests without the header are unchanged, so boto3's default CRC32 checksums and upload_fileobj are not affected.

test_put_object_and_upload_part_check_content_md5 in tests/test_s3/test_s3.py fails without the change. pytest tests/test_s3 passes (564 passed), ruff 0.14.10 clean. Against moto_server from this branch: the two calls above return BadDigest and InvalidDigest, the correct MD5 still stores the object, and upload_fileobj of 9 MiB and a plain put_object still work.

A Content-MD5 that did not match the body was accepted and the object
stored. S3 rejects it with BadDigest, and a value that is not a base64
MD5 digest with InvalidDigest.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant