Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix S3 PutObject with MD5 and enabled ARN Partition Rewriting #9233

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 1 commit into from
Sep 26, 2023
Merged
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
8 changes: 8 additions & 0 deletions localstack/aws/handlers/partition_rewriter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import base64
import hashlib
import logging
import re
from re import Match
Expand Down Expand Up @@ -109,6 +111,12 @@ def modify_request(self, request: Request) -> Request:
dict(request.headers), self.DEFAULT_INBOUND_PARTITION
)

# if a Content-MD5 was given, we need to update it after a potential modification
if "Content-MD5" in forward_rewritten_headers:
md = hashlib.md5(forward_rewritten_body).digest()
content_md5 = base64.b64encode(md).decode("utf-8")
forward_rewritten_headers["Content-MD5"] = content_md5

# add header to signal request has already been rewritten
forward_rewritten_headers["LS-INTERNAL-REWRITE-HANDLER"] = "1"
# Create a new request with the updated data
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/test_partition_rewriter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import base64
import hashlib
import json
from unittest import mock
from urllib.parse import urlencode
Expand Down Expand Up @@ -117,6 +119,37 @@ def test_arn_partition_rewriting_urlencoded_body():
}


def test_arn_partition_rewriting_contentmd5():
rewrite_handler = ArnPartitionRewriteHandler()
data = {"some-data-with-arn": "arn:aws-us-gov:iam::000000000000:role/test-role"}
body = urlencode(data)
original_md5 = base64.b64encode(hashlib.md5(body.encode("utf-8")).digest()).decode("utf-8")
headers = {
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
"Content-MD5": original_md5,
}

request = Request(
method="POST",
path="/",
query_string="",
body=body,
headers=headers,
)
result = rewrite_handler.modify_request(request)
data = result.get_data()
assert result.method == "POST"
assert get_full_raw_path(result) == "/"
assert result.form.to_dict() == {
"some-data-with-arn": "arn:aws:iam::000000000000:role/test-role"
}
assert "Content-MD5" in result.headers
assert result.headers["Content-MD5"] != original_md5
assert result.headers["Content-MD5"] == base64.b64encode(hashlib.md5(data).digest()).decode(
"utf-8"
)


def test_arn_partition_rewriting_url_encoding(httpserver, monkeypatch):
path = "/query%3Aencoded%2Fpath/"

Expand Down