From d44f29cc155fd8e64d44495bbbf75fef24963567 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TATSUNO=20=E2=80=9CTaz=E2=80=9D=20Yasuhiro?= Date: Sun, 29 Nov 2026 03:47:13 +0900 Subject: [PATCH] fix(core): answer a disabled service's CBOR error in the request's media type (#9662) A CBOR request to a disabled service got its ServiceNotAvailableException as application/cbor, even when it was sent as application/x-amz-cbor-1.1. The error now goes through ProtocolErrorResponses.cbor and the shared CBOR writer, so it answers in the media type the request sent, as Floci's other CBOR errors do. Its body and headers are unchanged. --- .../aws/core/common/ServiceEnabledFilter.java | 21 +++---------------- .../ServiceEnablementIntegrationTest.java | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/floci/aws/core/common/ServiceEnabledFilter.java b/src/main/java/io/floci/aws/core/common/ServiceEnabledFilter.java index cac79771f8..3b5c8bed73 100644 --- a/src/main/java/io/floci/aws/core/common/ServiceEnabledFilter.java +++ b/src/main/java/io/floci/aws/core/common/ServiceEnabledFilter.java @@ -1,7 +1,5 @@ package io.floci.aws.core.common; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.cbor.CBORFactory; import jakarta.inject.Inject; import jakarta.ws.rs.container.ContainerRequestContext; import jakarta.ws.rs.container.ContainerRequestFilter; @@ -16,8 +14,6 @@ import java.util.Optional; @Provider public class ServiceEnabledFilter implements ContainerRequestFilter { - private static final ObjectMapper CBOR_MAPPER = new ObjectMapper(new CBORFactory()); - @Context ResourceInfo resourceInfo; @@ -37,7 +33,7 @@ public class ServiceEnabledFilter implements ContainerRequestFilter { return; } if (!serviceConfigAccess.isEnabled(request.serviceKey())) { - ctx.abortWith(disabledResponse(request)); + ctx.abortWith(disabledResponse(ctx, request)); } } @@ -78,22 +74,11 @@ public class ServiceEnabledFilter implements ContainerRequestFilter { return resourceInfo != null ? resourceInfo.getResourceClass() : null; } - private Response disabledResponse(ResolvedRequest request) { + private Response disabledResponse(ContainerRequestContext ctx, ResolvedRequest request) { String message = "Service " + request.serviceKey() + " is not enabled."; if (request.protocol() == ServiceProtocol.CBOR) { - try { - byte[] errBytes = CBOR_MAPPER.writeValueAsBytes( - new AwsErrorResponse("ServiceNotAvailableException", message)); - return Response.status(400) - .header("smithy-protocol", "rpc-v2-cbor") - .header("x-amzn-query-error", "ServiceNotAvailableException;Sender") - .type("application/cbor") - .entity(errBytes) - .build(); - } catch (Exception ignored) { - return Response.status(400).build(); - } + return ProtocolErrorResponses.cbor(ctx, "ServiceNotAvailableException", message, 400); } if (request.protocol() == ServiceProtocol.JSON || request.protocol() == ServiceProtocol.REST_JSON) { diff --git a/src/test/java/io/floci/aws/core/common/ServiceEnablementIntegrationTest.java b/src/test/java/io/floci/aws/core/common/ServiceEnablementIntegrationTest.java index c14ac15fbe..be9b44d12f 100644 --- a/src/test/java/io/floci/aws/core/common/ServiceEnablementIntegrationTest.java +++ b/src/test/java/io/floci/aws/core/common/ServiceEnablementIntegrationTest.java @@ -151,6 +151,27 @@ class ServiceEnablementIntegrationTest { assertEquals("Service dynamodb is not enabled.", body.get("message").asText()); } + @Test + void anAmzCborRequestGetsItsErrorInTheMediaTypeItSent() throws Exception { + JsonNode body = cborBody( + given() + .contentType("application/x-amz-cbor-1.1") + .accept("application/x-amz-cbor-1.1") + .header("X-Amz-Target", "DynamoDB_20120810.ListTables") + .body(CBOR_MAPPER.writeValueAsBytes(Map.of())) + .when() + .post("/") + .then() + .statusCode(400) + .contentType("application/x-amz-cbor-1.1") + .header("x-amzn-query-error", "ServiceNotAvailableException;Sender") + .extract().asByteArray() + ); + + assertEquals("ServiceNotAvailableException", body.get("__type").asText()); + assertEquals("Service dynamodb is not enabled.", body.get("message").asText()); + } + @Test void dynamodbSmithyCborRequestsReturnCborErrors() throws Exception { JsonNode body = cborBody(