From bdc13f894e07e42eeb73c70a08fd62ff10b54d31 Mon Sep 17 00:00:00 2001 From: Mateus Scheuer Macedo <5440428@alunouninter.com> Date: Wed, 22 Apr 2026 01:03:06 -0300 Subject: [PATCH] fix(s3): wrap S3 Control XML errors in ErrorResponse wrapper (#560) * fix(s3): wrap S3 Control XML errors in ErrorResponse wrapper (#557) * fix(s3): align S3 Control error request ids - reuse the XML body request id in S3 Control error response headers - strengthen regression tests to assert the top-level RequestId and header/body alignment Addresses Copilot review feedback on PR #560. --------- Co-authored-by: MestreY0d4-Uninter <241404605+MestreY0d4-Uninter@users.noreply.github.com> --- .../services/s3/S3ControlController.java | 21 ++++++-- ...S3ControlUrlEncodedArnIntegrationTest.java | 52 +++++++++++++------ 2 files changed, 54 insertions(+), 19 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3ControlController.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3ControlController.java index bd06a7aa83..b484a1d5b3 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3ControlController.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3ControlController.java @@ -35,6 +35,10 @@ import java.util.UUID; @Produces(MediaType.APPLICATION_XML) public class S3ControlController { + private static final String AMZ_REQUEST_ID = "x-amz-request-id"; + private static final String AMZN_REQUEST_ID = "x-amzn-RequestId"; + private static final String AMZ_ID_2 = "x-amz-id-2"; + private final S3Service s3Service; @Inject @@ -150,20 +154,31 @@ public class S3ControlController { /** * S3 Control is a REST-XML protocol, so error responses must also be XML. - * The global {@code AwsExceptionMapper} emits JSON, which trips up the - * Go SDK's XML error deserializer with "unexpected EOF". + * AWS S3 Control wraps errors in an {@code } envelope + * containing the inner {@code } block and a top-level {@code }. + * + *

References: AWS Go SDK v2 s3control error deserializer expects this wrapper; + * bare {@code } collapses to "UnknownError" at the SDK layer. + * See issue #557. */ private Response xmlErrorResponse(AwsException e) { + String requestId = UUID.randomUUID().toString(); String xml = new XmlBuilder() .raw("") + .start("ErrorResponse", AwsNamespaces.S3_CONTROL) .start("Error") .elem("Code", e.getErrorCode()) .elem("Message", e.getMessage()) - .elem("RequestId", UUID.randomUUID().toString()) + .elem("RequestId", requestId) .end("Error") + .elem("RequestId", requestId) + .end("ErrorResponse") .build(); return Response.status(e.getHttpStatus()) .type(MediaType.APPLICATION_XML) + .header(AMZ_REQUEST_ID, requestId) + .header(AMZN_REQUEST_ID, requestId) + .header(AMZ_ID_2, requestId) .entity(xml) .build(); } diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ControlUrlEncodedArnIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ControlUrlEncodedArnIntegrationTest.java index 0f6846023b..cbe5d5ed0f 100644 --- a/src/test/java/io/github/hectorvent/floci/services/s3/S3ControlUrlEncodedArnIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ControlUrlEncodedArnIntegrationTest.java @@ -1,5 +1,7 @@ package io.github.hectorvent.floci.services.s3; +import io.github.hectorvent.floci.core.common.XmlParser; +import io.restassured.response.Response; import io.quarkus.test.junit.QuarkusTest; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.MethodOrderer; @@ -7,9 +9,14 @@ import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; +import java.util.List; + import static io.restassured.RestAssured.given; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Integration tests for S3 Control API handling of URL-encoded ARN path @@ -33,6 +40,25 @@ class S3ControlUrlEncodedArnIntegrationTest { private static final String ENCODED_ARN = "arn%3Aaws%3As3%3A" + REGION + "%3A" + ACCOUNT + "%3Abucket%2F" + BUCKET; + private void assertS3ControlErrorResponse(Response response) { + String body = response.getBody().asString(); + List requestIds = XmlParser.extractAll(body, "RequestId"); + + assertEquals(400, response.statusCode()); + assertThat(response.getContentType(), containsString("xml")); + assertThat(body, containsString("")); + assertThat(body, containsString("")); + assertThat(body, containsString("InvalidRequest")); + assertTrue(body.contains(""), + "expected top-level RequestId sibling after the Error block"); + assertEquals(2, requestIds.size(), "expected inner and top-level RequestId elements"); + assertEquals(requestIds.get(0), requestIds.get(1), + "expected inner and top-level RequestId values to match"); + assertEquals(requestIds.get(0), response.getHeader("x-amz-request-id")); + assertEquals(requestIds.get(0), response.getHeader("x-amzn-RequestId")); + assertEquals(requestIds.get(0), response.getHeader("x-amz-id-2")); + } + @Test @Order(1) @DisplayName("setup: create bucket and tag it via the standard S3 tagging API") @@ -131,33 +157,27 @@ class S3ControlUrlEncodedArnIntegrationTest { @Test @Order(6) - @DisplayName("Malformed ARN returns valid S3-style XML error body (#435)") + @DisplayName("Malformed ARN returns S3 Control ErrorResponse wrapper (#435, #557)") void malformedArnReturnsXmlError() { // Path param must not contain a literal ':bucket/' segment after decoding. - given() + Response response = given() .header("x-amz-account-id", ACCOUNT) .when() - .get("/v20180820/tags/arn%3Aaws%3As3%3A%3A%3Abogus%2F" + BUCKET) - .then() - .statusCode(400) - .contentType(containsString("xml")) - .body(containsString("")) - .body(containsString("InvalidRequest")); + .get("/v20180820/tags/arn%3Aaws%3As3%3A%3A%3Abogus%2F" + BUCKET); + + assertS3ControlErrorResponse(response); } @Test @Order(7) - @DisplayName("Malformed percent-encoding returns XML error, not JSON 500 (#435)") + @DisplayName("Malformed percent-encoding returns S3 Control ErrorResponse wrapper (#435, #557)") void malformedPercentEncodingReturnsXmlError() { // %ZZ is not a valid percent-encoding sequence; URLDecoder throws IAE. - given() + Response response = given() .header("x-amz-account-id", ACCOUNT) .when() - .get("/v20180820/tags/arn%3Aaws%ZZbucket%2F" + BUCKET) - .then() - .statusCode(400) - .contentType(containsString("xml")) - .body(containsString("")) - .body(containsString("InvalidRequest")); + .get("/v20180820/tags/arn%3Aaws%ZZbucket%2F" + BUCKET); + + assertS3ControlErrorResponse(response); } }