Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(app): combine repeated request header lines instead of keeping the last (#1388)

Andrew Gaul15e8f935cc33
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fb2119f7..7d4d7658 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,8 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
+### Fixed
+- **Request headers — a field repeated across lines is no longer reduced to its last line** — the ASGI header dict was built with plain assignment, so when a client sent the same field twice the earlier line was discarded. The AWS SDK for Java v2 uploads exactly that way, emitting `Content-Encoding: gzip` and `Content-Encoding: aws-chunked` as separate lines: MiniStack saw only `aws-chunked`, stripped it as the chunk-framing marker it is, and stored the object with no content encoding at all — silently losing `gzip` for every Java SDK v2 caller, S3Proxy's `aws-s3` backend among them. Repeated field lines now combine into one comma-joined value as RFC 9110 5.2 requires (`Cookie` rejoins with `"; "` per RFC 9113 8.2.3), so `aws-chunked` is stripped from the joined list and the caller's encoding survives. Sending the header once, in either order, was already correct and is unchanged.
### Added
- **SES v2 — email template CRUD and `SendEmail` with `Content.Template`** — the v2 `SendEmail` handler read only `Content.Simple` and `Content.Raw`, so a templated send was accepted with a `200` and a `MessageId` while the template was silently dropped, delivering an empty subject and body. `Content.Template` is now rendered through the same `{{placeholder}}` substitution v1 uses, from a stored template or from `TemplateContent` supplied inline, and a named template that doesn't exist fails the send with `NotFoundException` instead of delivering an empty email. `CreateEmailTemplate`, `GetEmailTemplate`, `UpdateEmailTemplate`, `DeleteEmailTemplate`, and `ListEmailTemplates` are served at `/v2/email/templates[/{TemplateName}]` over the shared v1 store, so a template created with `aws ses create-template` is sendable through `aws sesv2 send-email`. `ListEmailTemplates` paginates: `PageSize` accepts the documented 1–100 and defaults to 10, with an opaque `NextToken` returned only while further templates remain.
diff --git a/ministack/app.py b/ministack/app.py
index 905559db..b258cbec 100644
--- a/ministack/app.py
+++ b/ministack/app.py
@@ -1890,10 +1890,21 @@ async def app(scope, receive, send):
headers = {}
for name, value in scope.get("headers", []):
+ key = name.decode("latin-1").lower()
try:
- headers[name.decode("latin-1").lower()] = value.decode("utf-8")
+ decoded = value.decode("utf-8")
except UnicodeDecodeError:
- headers[name.decode("latin-1").lower()] = value.decode("latin-1")
+ decoded = value.decode("latin-1")
+ if key in headers:
+ # A field repeated across lines carries the same meaning as one
+ # comma-joined value (RFC 9110 5.2), and dropping either line
+ # loses data: the AWS SDK for Java v2 sends the caller's
+ # Content-Encoding and aws-chunked on separate lines, so keeping
+ # only the last one strips the caller's encoding along with the
+ # chunking marker. Cookie rejoins with "; " (RFC 9113 8.2.3).
+ headers[key] += ("; " if key == "cookie" else ", ") + decoded
+ else:
+ headers[key] = decoded
request_id = str(uuid.uuid4())
diff --git a/tests/test_s3.py b/tests/test_s3.py
index 0d5f7f39..a42c23a8 100644
--- a/tests/test_s3.py
+++ b/tests/test_s3.py
@@ -296,6 +296,58 @@ def test_s3_put_zero_byte_chunked(s3):
assert resp["Body"].read() == b""
assert resp["ContentLength"] == 0
+def test_s3_put_repeated_content_encoding_header(s3):
+ """Repeated Content-Encoding lines mean one comma-joined value.
+
+ The AWS SDK for Java v2 puts the caller's encoding and the aws-chunked
+ marker on separate header lines, so reading only one of them strips the
+ caller's encoding along with the marker.
+ """
+ import http.client
+ from urllib.parse import urlparse
+ bucket = "intg-s3-repeated-ce"
+ s3.create_bucket(Bucket=bucket)
+
+ payload = b"body-bytes"
+ fake_sig = b"abc123"
+ chunked = (
+ f"{len(payload):x}".encode() + b";chunk-signature=" + fake_sig + b"\r\n" +
+ payload + b"\r\n" +
+ b"0;chunk-signature=" + fake_sig + b"\r\n\r\n"
+ )
+ parsed = urlparse(ENDPOINT)
+
+ # (key, the Content-Encoding lines sent, what must be stored)
+ cases = [
+ ("caller-first", ["gzip", "aws-chunked"], "gzip"),
+ ("marker-first", ["aws-chunked", "gzip"], "gzip"),
+ ("two-encodings", ["deflate", "gzip", "aws-chunked"], "deflate, gzip"),
+ ("marker-only", ["aws-chunked"], None),
+ ]
+ for key, lines, expected in cases:
+ conn = http.client.HTTPConnection(parsed.hostname, parsed.port or 4566, timeout=10)
+ conn.putrequest("PUT", f"/{bucket}/{key}")
+ conn.putheader("Content-Length", str(len(chunked)))
+ conn.putheader("x-amz-content-sha256", "STREAMING-AWS4-HMAC-SHA256-PAYLOAD")
+ conn.putheader(
+ "Authorization",
+ "AWS4-HMAC-SHA256 Credential=test/20240101/us-east-1/s3/aws4_request,"
+ " SignedHeaders=host, Signature=fake",
+ )
+ for line in lines:
+ conn.putheader("Content-Encoding", line)
+ conn.endheaders()
+ conn.send(chunked)
+ resp = conn.getresponse()
+ resp.read()
+ assert resp.status == 200, f"{key}: PUT returned {resp.status}"
+ conn.close()
+
+ head = s3.head_object(Bucket=bucket, Key=key)
+ assert head.get("ContentEncoding") == expected, key
+ # The chunk framing is still stripped from the body.
+ assert s3.get_object(Bucket=bucket, Key=key)["Body"].read() == payload, key
+
def test_s3_head_object(s3):
s3.create_bucket(Bucket="intg-s3-headobj")
s3.put_object(