From 6f57af3edc449c8b7ef798e86a0cc777474b5346 Mon Sep 17 00:00:00 2001 From: RyotaroKoshio <59724197+kabosulike@users.noreply.github.com> Date: Thu, 14 May 2026 21:11:33 +0900 Subject: [PATCH] fix(s3): strip charset parameter from XML response Content-Type (#809) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(s3): strip charset parameter from XML response Content-Type Real AWS S3 returns `Content-Type: application/xml` for every XML response, but Quarkus REST automatically appends `;charset=UTF-8` to text-like media types declared via `@Produces(APPLICATION_XML)`. Some SDKs treat the extra parameter as a wire-format deviation. A `ContainerRequestFilter` registers a Vert.x `addHeadersEndHandler` that strips the charset just before HTTP headers hit the wire. The hook is necessary because for `Response.ok(entity).build()` (the common pattern in S3Controller), Quarkus REST's optimised write path bypasses both `ContainerResponseFilter` and `WriterInterceptor` — they see `getMediaType() == null`. Test: S3ContentTypeCharsetFilterTest covers ListBuckets (200) and NoSuchBucket (404). * fix(s3): address review feedback on charset filter - Trim class-level Javadoc to match sibling-filter convention. - Add @ApplicationScoped so the filter is a single shared CDI bean, matching the S3VirtualHostFilter pattern. - Tighten the semicolon-index guard from semi <= 0 to semi < 0 (a semicolon at index 0 is not a valid MediaType). - Add jsonResponseContentTypeUntouched regression test against /_floci/health to guard against future widening of the XML guard. --- .../s3/S3ContentTypeCharsetFilter.java | 38 +++++++++++++++ .../s3/S3ContentTypeCharsetFilterTest.java | 46 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/main/java/io/github/hectorvent/floci/services/s3/S3ContentTypeCharsetFilter.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/s3/S3ContentTypeCharsetFilterTest.java diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3ContentTypeCharsetFilter.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3ContentTypeCharsetFilter.java new file mode 100644 index 0000000000..3913422401 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3ContentTypeCharsetFilter.java @@ -0,0 +1,38 @@ +package io.github.hectorvent.floci.services.s3; + +import io.quarkus.vertx.http.runtime.CurrentVertxRequest; +import io.vertx.ext.web.RoutingContext; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import jakarta.ws.rs.container.ContainerRequestContext; +import jakarta.ws.rs.container.ContainerRequestFilter; +import jakarta.ws.rs.ext.Provider; + +/** + * Strips the {@code charset} parameter from {@code application/xml} and + * {@code text/xml} response Content-Type headers so they match the real AWS + * XML protocol wire format. + */ +@Provider +@ApplicationScoped +public class S3ContentTypeCharsetFilter implements ContainerRequestFilter { + + @Inject + CurrentVertxRequest currentVertxRequest; + + @Override + public void filter(ContainerRequestContext requestContext) { + RoutingContext rc = currentVertxRequest.getCurrent(); + if (rc == null) return; + rc.addHeadersEndHandler(v -> { + String ct = rc.response().headers().get("Content-Type"); + if (ct == null) return; + int semi = ct.indexOf(';'); + if (semi < 0) return; + String base = ct.substring(0, semi).trim(); + if ("application/xml".equalsIgnoreCase(base) || "text/xml".equalsIgnoreCase(base)) { + rc.response().headers().set("Content-Type", base); + } + }); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ContentTypeCharsetFilterTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ContentTypeCharsetFilterTest.java new file mode 100644 index 0000000000..16c295b398 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ContentTypeCharsetFilterTest.java @@ -0,0 +1,46 @@ +package io.github.hectorvent.floci.services.s3; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.startsWith; + +/** + * Verifies S3 XML responses match the real AWS wire format + * ({@code Content-Type: application/xml}, no {@code charset} parameter). + */ +@QuarkusTest +class S3ContentTypeCharsetFilterTest { + + @Test + void listBucketsContentTypeHasNoCharset() { + given() + .when() + .get("/") + .then() + .statusCode(200) + .header("Content-Type", equalTo("application/xml")); + } + + @Test + void noSuchBucketErrorContentTypeHasNoCharset() { + given() + .when() + .get("/charset-test-missing-bucket-xyz") + .then() + .statusCode(404) + .header("Content-Type", equalTo("application/xml")); + } + + @Test + void jsonResponseContentTypeUntouched() { + given() + .when() + .get("/_floci/health") + .then() + .statusCode(200) + .header("Content-Type", startsWith("application/json")); + } +}