diff --git a/docs/services/ses.md b/docs/services/ses.md index d8968e7b0f..82fbe58e07 100644 --- a/docs/services/ses.md +++ b/docs/services/ses.md @@ -519,7 +519,7 @@ A JSON `null` member counts as absent. A `null` address list element passes this The other operations check their members one at a time, and some of them still answer a wrong JSON type with `BadRequestException`. The list operations' map `Filter` answers as in the table. Their integer `PageSize`, like `CreateExportJob`'s `MaxResults`, answers a string with `STRING_VALUE can not be converted to an Integer` (`TRUE_VALUE` / `FALSE_VALUE` for a boolean, and as in the table for an object or an array), while a number is read as an int first, as on AWS: `1.5` is accepted, and a value above the int range is checked against the page-size bounds as `2147483647`. -A body that is not a JSON object is a `SerializationException`, before any member is read: `Start of list found where not expected` for an array, and no message for a string, a number or `null`. `CreateEmailIdentity`, `CreateEmailTemplate`, `UpdateEmailTemplate`, `CreateCustomVerificationEmailTemplate`, `UpdateCustomVerificationEmailTemplate` and `TagResource` do not check this yet and read such a body as one without members. +An empty body is read as an object without members. A body that is not a JSON object is a `SerializationException`, before any member is read: `Start of list found where not expected` for an array, and no message for a string, a number, `null`, a whitespace-only body, malformed JSON or anything but whitespace after the object (`{} x`, `{}{}`). `CreateContactList`, `CreateContact`, `CreateDedicatedIpPool`, `CreateEmailIdentityPolicy`, `UpdateEmailIdentityPolicy`, `PutEmailIdentityMailFromAttributes`, `CreateImportJob`, `PutSuppressedDestination`, `TestRenderEmailTemplate` and `TagResource` answer an empty body with `BadRequestException` `Request body is required.` instead of reporting the missing members. The body shapes are vendored in `src/main/resources/aws/sesv2-request-shapes.json`, generated by `tools/aws/regen_sesv2_shapes.py` from the botocore release pinned in `tools/aws/requirements.txt`. diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesAccountController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesAccountController.java index 0dbb0477b1..a6bdabeb55 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesAccountController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesAccountController.java @@ -1,6 +1,5 @@ package io.github.hectorvent.floci.services.ses; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -30,7 +29,6 @@ import static io.github.hectorvent.floci.services.ses.SesV2Json.coerceBoolean; import static io.github.hectorvent.floci.services.ses.SesV2Json.coerceBooleanOrFalse; import static io.github.hectorvent.floci.services.ses.SesV2Json.readOptionBody; import static io.github.hectorvent.floci.services.ses.SesV2Json.remapV1Exception; -import static io.github.hectorvent.floci.services.ses.SesV2Json.requireJsonObject; import static io.github.hectorvent.floci.services.ses.SesV2Json.stringMemberOrAbsent; /** @@ -164,10 +162,7 @@ public class SesAccountController { public Response putAccountDetails(@Context HttpHeaders headers, String body) { String region = regionResolver.resolveRegion(headers); try { - JsonNode request = (body == null || body.isBlank()) - ? objectMapper.createObjectNode() - : objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); // Parse every member first (rejecting wrong JSON types as a serialization error, the way // AWS does before validation), then validate the parsed values together so all constraint @@ -205,10 +200,6 @@ public class SesAccountController { return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - // AWS reports a malformed JSON body as a SerializationException, the same error type used - // for wrong-typed members above. - throw new AwsException("SerializationException", null, 400); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesContactController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesContactController.java index 733faab16b..467e3569c7 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesContactController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesContactController.java @@ -1,6 +1,5 @@ package io.github.hectorvent.floci.services.ses; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -38,8 +37,8 @@ import static io.github.hectorvent.floci.services.ses.SesV2Json.coerceBooleanOrF import static io.github.hectorvent.floci.services.ses.SesV2Json.intMemberOrAbsent; import static io.github.hectorvent.floci.services.ses.SesV2Json.parseTagsArray; import static io.github.hectorvent.floci.services.ses.SesV2Json.putTimestamp; +import static io.github.hectorvent.floci.services.ses.SesV2Json.readOptionBody; import static io.github.hectorvent.floci.services.ses.SesV2Json.remapV1Exception; -import static io.github.hectorvent.floci.services.ses.SesV2Json.requireJsonObject; /** * SES V2 contact-list and contact endpoints ({@code /v2/email/contact-lists}). Every operation @@ -71,11 +70,10 @@ public class SesContactController { public Response createContactList(@Context HttpHeaders headers, String body) { String region = regionResolver.resolveRegion(headers); try { - if (body == null || body.isBlank()) { + if (body == null || body.isEmpty()) { throw new AwsException("BadRequestException", "Request body is required.", 400); } - JsonNode request = objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); SesV2RequestShapes.check("CreateContactList", request); // Read leniently; the service surfaces a missing ContactListName as the AWS Smithy // validation error rather than a custom "required" message. @@ -88,8 +86,6 @@ public class SesContactController { return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("BadRequestException", e.getMessage(), 400); } } @@ -129,10 +125,7 @@ public class SesContactController { String body) { String region = regionResolver.resolveRegion(headers); try { - JsonNode request = (body == null || body.isBlank()) - ? objectMapper.createObjectNode() - : objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); SesV2RequestShapes.check("UpdateContactList", request); JsonNode topicsNode = request.path("Topics"); // Treat absent or explicit-null Topics as "not provided" (keep existing), consistent @@ -148,8 +141,6 @@ public class SesContactController { return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("BadRequestException", e.getMessage(), 400); } } @@ -211,11 +202,10 @@ public class SesContactController { @PathParam("contactListName") String contactListName, String body) { String region = regionResolver.resolveRegion(headers); try { - if (body == null || body.isBlank()) { + if (body == null || body.isEmpty()) { throw new AwsException("BadRequestException", "Request body is required.", 400); } - JsonNode request = objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); SesV2RequestShapes.check("CreateContact", request); String emailAddress = request.path("EmailAddress").asText(null); List prefs = parseTopicPreferences(request.path("TopicPreferences")); @@ -227,8 +217,6 @@ public class SesContactController { return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("BadRequestException", e.getMessage(), 400); } } @@ -238,10 +226,7 @@ public class SesContactController { @PathParam("contactListName") String contactListName, String body) { String region = regionResolver.resolveRegion(headers); try { - JsonNode request = (body == null || body.isBlank()) - ? objectMapper.createObjectNode() - : objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); SesV2RequestShapes.check("ListContacts", request); Integer pageSize = intMemberOrAbsent(request, "PageSize"); String nextToken = request.path("NextToken").asText(null); @@ -256,8 +241,6 @@ public class SesContactController { return Response.ok(result).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("BadRequestException", e.getMessage(), 400); } } @@ -294,10 +277,7 @@ public class SesContactController { @PathParam("emailAddress") String emailAddress, String body) { String region = regionResolver.resolveRegion(headers); try { - JsonNode request = (body == null || body.isBlank()) - ? objectMapper.createObjectNode() - : objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); SesV2RequestShapes.check("UpdateContact", request); JsonNode prefsNode = request.path("TopicPreferences"); boolean prefsPresent = !prefsNode.isMissingNode() && !prefsNode.isNull(); @@ -310,8 +290,6 @@ public class SesContactController { return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("BadRequestException", e.getMessage(), 400); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesCvetController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesCvetController.java index 461b042d41..db7315f3df 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesCvetController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesCvetController.java @@ -1,6 +1,5 @@ package io.github.hectorvent.floci.services.ses; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -26,8 +25,8 @@ import jakarta.ws.rs.core.Response; import org.jboss.logging.Logger; import static io.github.hectorvent.floci.services.ses.SesV2Json.parseTagsArray; +import static io.github.hectorvent.floci.services.ses.SesV2Json.readOptionBody; import static io.github.hectorvent.floci.services.ses.SesV2Json.remapV1Exception; -import static io.github.hectorvent.floci.services.ses.SesV2Json.requireJsonObject; /** * SES V2 custom verification email template endpoints @@ -63,7 +62,7 @@ public class SesCvetController { public Response createCustomVerificationEmailTemplate(@Context HttpHeaders headers, String body) { String region = regionResolver.resolveRegion(headers); try { - JsonNode request = objectMapper.readTree(body); + JsonNode request = readOptionBody(objectMapper, body); CustomVerificationEmailTemplate t = parseCvet(request); // Tags exist only on the create request; UpdateCustomVerificationEmailTemplate has no // Tags member and preserves the stored ones. @@ -72,8 +71,6 @@ public class SesCvetController { return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("BadRequestException", e.getMessage(), 400); } } @@ -115,14 +112,12 @@ public class SesCvetController { @PathParam("templateName") String templateName, String body) { String region = regionResolver.resolveRegion(headers); try { - CustomVerificationEmailTemplate t = parseCvet(objectMapper.readTree(body)); + CustomVerificationEmailTemplate t = parseCvet(readOptionBody(objectMapper, body)); t.setTemplateName(templateName); sesService.updateCustomVerificationEmailTemplate(t, region); return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("BadRequestException", e.getMessage(), 400); } } @@ -140,7 +135,6 @@ public class SesCvetController { } private CustomVerificationEmailTemplate parseCvet(JsonNode request) { - requireJsonObject(request); CustomVerificationEmailTemplate t = new CustomVerificationEmailTemplate(); t.setTemplateName(request.path("TemplateName").asText(null)); t.setFromEmailAddress(request.path("FromEmailAddress").asText(null)); diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesDedicatedIpController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesDedicatedIpController.java index 9beb0b90fd..b72cb41843 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesDedicatedIpController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesDedicatedIpController.java @@ -1,6 +1,5 @@ package io.github.hectorvent.floci.services.ses; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -33,7 +32,6 @@ import static io.github.hectorvent.floci.services.ses.SesV2Json.parseTagsArray; import static io.github.hectorvent.floci.services.ses.SesV2Json.readOptionBody; import static io.github.hectorvent.floci.services.ses.SesV2Json.readRequiredStringField; import static io.github.hectorvent.floci.services.ses.SesV2Json.remapV1Exception; -import static io.github.hectorvent.floci.services.ses.SesV2Json.requireJsonObject; /** * SES V2 dedicated IP pool and dedicated IP endpoints ({@code /v2/email/dedicated-ip-pools} and @@ -65,11 +63,10 @@ public class SesDedicatedIpController { public Response createDedicatedIpPool(@Context HttpHeaders headers, String body) { String region = regionResolver.resolveRegion(headers); try { - if (body == null || body.isBlank()) { + if (body == null || body.isEmpty()) { throw new AwsException("BadRequestException", "Request body is required.", 400); } - JsonNode request = objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); String poolName = readRequiredStringField(request, "PoolName"); JsonNode scalingNode = request.path("ScalingMode"); String scalingMode; @@ -87,8 +84,6 @@ public class SesDedicatedIpController { return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("BadRequestException", e.getMessage(), 400); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesIdentityController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesIdentityController.java index 75f50b2c08..2081285af2 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesIdentityController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesIdentityController.java @@ -1,6 +1,5 @@ package io.github.hectorvent.floci.services.ses; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -37,7 +36,6 @@ import static io.github.hectorvent.floci.services.ses.SesV2Json.parseTagsArray; import static io.github.hectorvent.floci.services.ses.SesV2Json.putTimestamp; import static io.github.hectorvent.floci.services.ses.SesV2Json.readOptionBody; import static io.github.hectorvent.floci.services.ses.SesV2Json.remapV1Exception; -import static io.github.hectorvent.floci.services.ses.SesV2Json.requireJsonObject; import static io.github.hectorvent.floci.services.ses.SesV2Json.requireObjectOrAbsent; import static io.github.hectorvent.floci.services.ses.SesV2Json.stringMapMemberOrAbsent; import static io.github.hectorvent.floci.services.ses.SesV2Json.stringMemberOrAbsent; @@ -85,7 +83,7 @@ public class SesIdentityController { public Response createEmailIdentity(@Context HttpHeaders headers, String body) { String region = regionResolver.resolveRegion(headers); try { - JsonNode request = objectMapper.readTree(body); + JsonNode request = readOptionBody(objectMapper, body); JsonNode emailIdentityNode = request.path("EmailIdentity"); if (!emailIdentityNode.isMissingNode() && !emailIdentityNode.isNull() && !emailIdentityNode.isTextual()) { @@ -232,8 +230,6 @@ public class SesIdentityController { return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("BadRequestException", e.getMessage(), 400); } } @@ -249,8 +245,6 @@ public class SesIdentityController { return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("BadRequestException", e.getMessage(), 400); } } @@ -276,12 +270,11 @@ public class SesIdentityController { return Response.ok(objectMapper.createObjectNode()).build(); } - private String readPolicyBody(String body) throws JsonProcessingException { - if (body == null || body.isBlank()) { + private String readPolicyBody(String body) { + if (body == null || body.isEmpty()) { throw new AwsException("BadRequestException", "Request body is required.", 400); } - JsonNode request = objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); JsonNode policyNode = request.path("Policy"); if (policyNode.isMissingNode() || policyNode.isNull()) { // Verified against AWS: a missing/null required member is a Smithy validation error. @@ -322,9 +315,7 @@ public class SesIdentityController { String body) { String region = regionResolver.resolveRegion(headers); try { - JsonNode request = (body == null || body.isBlank()) - ? objectMapper.createObjectNode() : objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); String origin = request.path("SigningAttributesOrigin").asText(null); if (!"AWS_SES".equals(origin) && !"EXTERNAL".equals(origin)) { throw new AwsException("BadRequestException", @@ -366,11 +357,10 @@ public class SesIdentityController { String body) { String region = regionResolver.resolveRegion(headers); try { - if (body == null || body.isBlank()) { + if (body == null || body.isEmpty()) { throw new AwsException("BadRequestException", "Request body is required.", 400); } - JsonNode request = objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); JsonNode mailFromDomainNode = request.path("MailFromDomain"); if (mailFromDomainNode.isMissingNode()) { throw new AwsException("BadRequestException", @@ -397,8 +387,6 @@ public class SesIdentityController { return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("BadRequestException", e.getMessage(), 400); } } @@ -428,30 +416,21 @@ public class SesIdentityController { String body) { String region = regionResolver.resolveRegion(headers); try { + // An omitted or explicit-null ConfigurationSetName clears the association, as does an + // empty body. String configurationSetName = null; - if (body != null && !body.isEmpty()) { - // Only a truly empty body is "no body". A non-empty body must be a JSON object; a - // whitespace-only or otherwise unparseable body is a serialization error (verified - // against real AWS: whitespace-only returns SerializationException and does not - // clear). Within a valid object, an omitted or explicit-null ConfigurationSetName - // clears the association (as does an empty body / {}). - JsonNode request = objectMapper.readTree(body); - requireJsonObject(request); - JsonNode node = request.path("ConfigurationSetName"); - if (!node.isMissingNode() && !node.isNull()) { - if (!node.isTextual()) { - throw new AwsException("BadRequestException", - "ConfigurationSetName must be a JSON string.", 400); - } - configurationSetName = node.asText(); + JsonNode node = readOptionBody(objectMapper, body).path("ConfigurationSetName"); + if (!node.isMissingNode() && !node.isNull()) { + if (!node.isTextual()) { + throw new AwsException("BadRequestException", + "ConfigurationSetName must be a JSON string.", 400); } + configurationSetName = node.asText(); } sesService.setEmailIdentityConfigurationSet(emailIdentity, configurationSetName, region); return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("SerializationException", null, 400); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesImportJobController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesImportJobController.java index 38541c62b1..2aabd6cb2c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesImportJobController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesImportJobController.java @@ -1,10 +1,7 @@ package io.github.hectorvent.floci.services.ses; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsException; @@ -26,9 +23,9 @@ import org.jboss.logging.Logger; import static io.github.hectorvent.floci.services.ses.SesV2Json.intMemberOrAbsent; import static io.github.hectorvent.floci.services.ses.SesV2Json.putTimestamp; +import static io.github.hectorvent.floci.services.ses.SesV2Json.readOptionBody; import static io.github.hectorvent.floci.services.ses.SesV2Json.readRequiredStringField; import static io.github.hectorvent.floci.services.ses.SesV2Json.remapV1Exception; -import static io.github.hectorvent.floci.services.ses.SesV2Json.requireJsonObject; import static io.github.hectorvent.floci.services.ses.SesV2Json.stringMemberOrAbsent; /** @@ -46,9 +43,6 @@ public class SesImportJobController { private final SesImportJobService importJobService; private final RegionResolver regionResolver; private final ObjectMapper objectMapper; - // FAIL_ON_TRAILING_TOKENS so "{...} garbage" is rejected instead of creating a job from the - // leading object, as the JSON protocol controllers do. - private final ObjectReader strictReader; @Inject public SesImportJobController(SesImportJobService importJobService, RegionResolver regionResolver, @@ -56,7 +50,6 @@ public class SesImportJobController { this.importJobService = importJobService; this.regionResolver = regionResolver; this.objectMapper = objectMapper; - this.strictReader = objectMapper.reader().with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS); } @POST @@ -64,11 +57,10 @@ public class SesImportJobController { public Response createImportJob(@Context HttpHeaders headers, String body) { String region = regionResolver.resolveRegion(headers); try { - if (body == null || body.isBlank()) { + if (body == null || body.isEmpty()) { throw new AwsException("BadRequestException", "Request body is required.", 400); } - JsonNode request = strictReader.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); SesV2RequestShapes.check("CreateImportJob", request); JsonNode source = request.path("ImportDataSource"); if (source.isMissingNode() || source.isNull()) { @@ -104,8 +96,6 @@ public class SesImportJobController { return Response.ok(result).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("BadRequestException", e.getMessage(), 400); } } @@ -131,10 +121,7 @@ public class SesImportJobController { public Response listImportJobs(@Context HttpHeaders headers, String body) { String region = regionResolver.resolveRegion(headers); try { - JsonNode request = (body == null || body.isBlank()) - ? objectMapper.createObjectNode() - : strictReader.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); String destinationType = stringMemberOrAbsent(request, "ImportDestinationType"); Integer pageSize = intMemberOrAbsent(request, "PageSize"); String nextToken = stringMemberOrAbsent(request, "NextToken"); @@ -149,8 +136,6 @@ public class SesImportJobController { return Response.ok(result).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("BadRequestException", e.getMessage(), 400); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesSendController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesSendController.java index f92100e618..93a48bf504 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesSendController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesSendController.java @@ -1,6 +1,5 @@ package io.github.hectorvent.floci.services.ses; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -41,8 +40,8 @@ import java.util.Base64; import java.util.List; import java.util.Map; +import static io.github.hectorvent.floci.services.ses.SesV2Json.readOptionBody; import static io.github.hectorvent.floci.services.ses.SesV2Json.remapV1Exception; -import static io.github.hectorvent.floci.services.ses.SesV2Json.requireJsonObject; /** * REST JSON controller for the three SES V2 send endpoints: @@ -93,8 +92,7 @@ public class SesSendController { "Account sending is disabled.", 400); } - JsonNode request = objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); SesV2RequestShapes.check("SendEmail", request); validateTrackingOverrides(request); @@ -246,8 +244,7 @@ public class SesSendController { "Account sending is disabled.", 400); } - JsonNode request = objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); SesV2RequestShapes.check("SendBulkEmail", request); validateTrackingOverrides(request); String fromEmailAddress = request.path("FromEmailAddress").asText(null); @@ -393,8 +390,7 @@ public class SesSendController { throw new AwsException("SendingPausedException", "Account sending is disabled.", 400); } - JsonNode request = objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); SesV2RequestShapes.check("SendCustomVerificationEmail", request); templateName = request.path("TemplateName").asText(null); String messageId = sesService.sendCustomVerificationEmail( @@ -411,8 +407,6 @@ public class SesSendController { "Custom verification email template <" + templateName + "> does not exist", 404); } throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("BadRequestException", e.getMessage(), 400); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesSuppressionController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesSuppressionController.java index b84bd6bc2b..ae10587075 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesSuppressionController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesSuppressionController.java @@ -1,6 +1,5 @@ package io.github.hectorvent.floci.services.ses; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -28,9 +27,9 @@ import org.jboss.logging.Logger; import java.util.List; import static io.github.hectorvent.floci.services.ses.SesV2Json.putTimestamp; +import static io.github.hectorvent.floci.services.ses.SesV2Json.readOptionBody; import static io.github.hectorvent.floci.services.ses.SesV2Json.readRequiredStringField; import static io.github.hectorvent.floci.services.ses.SesV2Json.remapV1Exception; -import static io.github.hectorvent.floci.services.ses.SesV2Json.requireJsonObject; import static io.github.hectorvent.floci.services.ses.SesV2Json.stringMemberOrAbsent; /** @@ -64,11 +63,10 @@ public class SesSuppressionController { public Response putSuppressedDestination(@Context HttpHeaders headers, String body) { String region = regionResolver.resolveRegion(headers); try { - if (body == null || body.isBlank()) { + if (body == null || body.isEmpty()) { throw new AwsException("BadRequestException", "Request body is required.", 400); } - JsonNode request = objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); String emailAddress = readRequiredStringField(request, "EmailAddress"); String reason = readRequiredStringField(request, "Reason"); String tenantName = stringMemberOrAbsent(request, "TenantName"); @@ -77,8 +75,6 @@ public class SesSuppressionController { return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("BadRequestException", e.getMessage(), 400); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesTagController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesTagController.java index f0562c031d..8813fed67e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesTagController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesTagController.java @@ -1,6 +1,5 @@ package io.github.hectorvent.floci.services.ses; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -26,6 +25,7 @@ import org.jboss.logging.Logger; import java.util.List; import static io.github.hectorvent.floci.services.ses.SesV2Json.parseTagsArray; +import static io.github.hectorvent.floci.services.ses.SesV2Json.readOptionBody; import static io.github.hectorvent.floci.services.ses.SesV2Json.remapV1Exception; /** @@ -59,10 +59,10 @@ public class SesTagController { public Response tagResource(@Context HttpHeaders headers, String body) { String region = regionResolver.resolveRegion(headers); try { - if (body == null || body.isBlank()) { + if (body == null || body.isEmpty()) { throw new AwsException("BadRequestException", "Request body is required.", 400); } - JsonNode request = objectMapper.readTree(body); + JsonNode request = readOptionBody(objectMapper, body); String arn = request.path("ResourceArn").asText(null); List tags = parseTagsArray(request.path("Tags")); if (tags == null) { @@ -73,8 +73,6 @@ public class SesTagController { return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("BadRequestException", e.getMessage(), 400); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesTemplateController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesTemplateController.java index ff0757d329..5fe3d9505e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesTemplateController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesTemplateController.java @@ -1,6 +1,5 @@ package io.github.hectorvent.floci.services.ses; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -30,8 +29,8 @@ import java.util.List; import static io.github.hectorvent.floci.services.ses.SesV2Json.parseTagsArray; import static io.github.hectorvent.floci.services.ses.SesV2Json.putTimestamp; +import static io.github.hectorvent.floci.services.ses.SesV2Json.readOptionBody; import static io.github.hectorvent.floci.services.ses.SesV2Json.remapV1Exception; -import static io.github.hectorvent.floci.services.ses.SesV2Json.requireJsonObject; /** * SES V2 email-template endpoints ({@code /v2/email/templates}). Talks to @@ -65,7 +64,7 @@ public class SesTemplateController { public Response createEmailTemplate(@Context HttpHeaders headers, String body) { String region = regionResolver.resolveRegion(headers); try { - JsonNode request = objectMapper.readTree(body); + JsonNode request = readOptionBody(objectMapper, body); String templateName = request.path("TemplateName").asText(null); if (templateName == null || templateName.isBlank()) { throw new AwsException("BadRequestException", "TemplateName is required.", 400); @@ -80,8 +79,6 @@ public class SesTemplateController { return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("BadRequestException", e.getMessage(), 400); } } @@ -125,15 +122,13 @@ public class SesTemplateController { String body) { String region = regionResolver.resolveRegion(headers); try { - JsonNode request = objectMapper.readTree(body); + JsonNode request = readOptionBody(objectMapper, body); EmailTemplate template = parseTemplateContent(templateName, request.path("TemplateContent")); templateService.updateTemplate(template, region); LOG.infov("SES V2 UpdateEmailTemplate: {0}", templateName); return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("BadRequestException", e.getMessage(), 400); } } @@ -158,11 +153,10 @@ public class SesTemplateController { String body) { String region = regionResolver.resolveRegion(headers); try { - if (body == null || body.isBlank()) { + if (body == null || body.isEmpty()) { throw new AwsException("BadRequestException", "Request body is required.", 400); } - JsonNode request = objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); JsonNode templateDataNode = request.path("TemplateData"); if (!templateDataNode.isMissingNode() && !templateDataNode.isNull() && !templateDataNode.isTextual()) { @@ -177,8 +171,6 @@ public class SesTemplateController { return Response.ok(result).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("BadRequestException", e.getMessage(), 400); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesTenantController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesTenantController.java index e6edcc3e2a..cde3305a94 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesTenantController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesTenantController.java @@ -1,6 +1,5 @@ package io.github.hectorvent.floci.services.ses; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -29,8 +28,8 @@ import static io.github.hectorvent.floci.services.ses.SesV2Json.filterValues; import static io.github.hectorvent.floci.services.ses.SesV2Json.intMemberOrAbsent; import static io.github.hectorvent.floci.services.ses.SesV2Json.parseTagsArray; import static io.github.hectorvent.floci.services.ses.SesV2Json.putTimestamp; +import static io.github.hectorvent.floci.services.ses.SesV2Json.readOptionBody; import static io.github.hectorvent.floci.services.ses.SesV2Json.remapV1Exception; -import static io.github.hectorvent.floci.services.ses.SesV2Json.requireJsonObject; import static io.github.hectorvent.floci.services.ses.SesV2Json.stringArrayOrAbsent; import static io.github.hectorvent.floci.services.ses.SesV2Json.stringMapMemberOrAbsent; import static io.github.hectorvent.floci.services.ses.SesV2Json.stringMemberOrAbsent; @@ -72,10 +71,7 @@ public class SesTenantController { public Response createTenant(@Context HttpHeaders headers, String body) { String region = regionResolver.resolveRegion(headers); try { - JsonNode request = (body == null || body.isBlank()) - ? objectMapper.createObjectNode() - : objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); String tenantName = stringMemberOrAbsent(request, "TenantName"); List tags = parseTagsArray(request.path("Tags")); List suppressedReasons = null; @@ -94,8 +90,6 @@ public class SesTenantController { return Response.ok(tenantJson(tenant)).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("SerializationException", null, 400); } } @@ -104,10 +98,7 @@ public class SesTenantController { public Response getTenant(@Context HttpHeaders headers, String body) { String region = regionResolver.resolveRegion(headers); try { - JsonNode request = (body == null || body.isBlank()) - ? objectMapper.createObjectNode() - : objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); String tenantName = stringMemberOrAbsent(request, "TenantName"); Tenant tenant = tenantService.getTenant(tenantName, region); ObjectNode result = objectMapper.createObjectNode(); @@ -115,8 +106,6 @@ public class SesTenantController { return Response.ok(result).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("SerializationException", null, 400); } } @@ -125,10 +114,7 @@ public class SesTenantController { public Response listTenants(@Context HttpHeaders headers, String body) { String region = regionResolver.resolveRegion(headers); try { - JsonNode request = (body == null || body.isBlank()) - ? objectMapper.createObjectNode() - : objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); Map filter = stringMapMemberOrAbsent(request, "Filter"); Integer pageSize = intMemberOrAbsent(request, "PageSize"); String nextToken = stringMemberOrAbsent(request, "NextToken"); @@ -148,8 +134,6 @@ public class SesTenantController { return Response.ok(result).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("SerializationException", null, 400); } } @@ -158,17 +142,12 @@ public class SesTenantController { public Response deleteTenant(@Context HttpHeaders headers, String body) { String region = regionResolver.resolveRegion(headers); try { - JsonNode request = (body == null || body.isBlank()) - ? objectMapper.createObjectNode() - : objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); String tenantName = stringMemberOrAbsent(request, "TenantName"); sesService.deleteTenant(tenantName, region); return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("SerializationException", null, 400); } } @@ -180,10 +159,7 @@ public class SesTenantController { public Response createTenantResourceAssociation(@Context HttpHeaders headers, String body) { String region = regionResolver.resolveRegion(headers); try { - JsonNode request = (body == null || body.isBlank()) - ? objectMapper.createObjectNode() - : objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); String tenantName = stringMemberOrAbsent(request, "TenantName"); String resourceArn = stringMemberOrAbsent(request, "ResourceArn"); sesService.createTenantResourceAssociation(tenantName, resourceArn, @@ -191,8 +167,6 @@ public class SesTenantController { return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("SerializationException", null, 400); } } @@ -201,10 +175,7 @@ public class SesTenantController { public Response deleteTenantResourceAssociation(@Context HttpHeaders headers, String body) { String region = regionResolver.resolveRegion(headers); try { - JsonNode request = (body == null || body.isBlank()) - ? objectMapper.createObjectNode() - : objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); String tenantName = stringMemberOrAbsent(request, "TenantName"); String resourceArn = stringMemberOrAbsent(request, "ResourceArn"); sesService.deleteTenantResourceAssociation(tenantName, resourceArn, @@ -212,8 +183,6 @@ public class SesTenantController { return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("SerializationException", null, 400); } } @@ -222,10 +191,7 @@ public class SesTenantController { public Response listTenantResources(@Context HttpHeaders headers, String body) { String region = regionResolver.resolveRegion(headers); try { - JsonNode request = (body == null || body.isBlank()) - ? objectMapper.createObjectNode() - : objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); String tenantName = stringMemberOrAbsent(request, "TenantName"); Map filter = stringMapMemberOrAbsent(request, "Filter"); String resourceTypeFilter = filter == null ? null : filter.get("RESOURCE_TYPE"); @@ -245,8 +211,6 @@ public class SesTenantController { return Response.ok(result).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("SerializationException", null, 400); } } @@ -255,10 +219,7 @@ public class SesTenantController { public Response listResourceTenants(@Context HttpHeaders headers, String body) { String region = regionResolver.resolveRegion(headers); try { - JsonNode request = (body == null || body.isBlank()) - ? objectMapper.createObjectNode() - : objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); String resourceArn = stringMemberOrAbsent(request, "ResourceArn"); Integer pageSize = intMemberOrAbsent(request, "PageSize"); String nextToken = stringMemberOrAbsent(request, "NextToken"); @@ -279,8 +240,6 @@ public class SesTenantController { return Response.ok(result).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("SerializationException", null, 400); } } @@ -291,10 +250,7 @@ public class SesTenantController { public Response putTenantSuppressionAttributes(@Context HttpHeaders headers, String body) { String region = regionResolver.resolveRegion(headers); try { - JsonNode request = (body == null || body.isBlank()) - ? objectMapper.createObjectNode() - : objectMapper.readTree(body); - requireJsonObject(request); + JsonNode request = readOptionBody(objectMapper, body); String tenantName = stringMemberOrAbsent(request, "TenantName"); List suppressedReasons = stringArrayOrAbsent(request, "SuppressedReasons"); String suppressionScope = stringMemberOrAbsent(request, "SuppressionScope"); @@ -303,8 +259,6 @@ public class SesTenantController { return Response.ok(objectMapper.createObjectNode()).build(); } catch (AwsException e) { throw remapV1Exception(e); - } catch (JsonProcessingException e) { - throw new AwsException("SerializationException", null, 400); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesV2Json.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesV2Json.java index 53ca71aaaf..3a19a2ebbc 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesV2Json.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesV2Json.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.ses; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; @@ -113,17 +114,23 @@ final class SesV2Json { return !node.isMissingNode() && !node.isNull() && !node.isTextual(); } - /** Parse an option PUT body into a JSON object, treating an empty body as {}. */ + /** + * Parse a request body into a JSON object. Only an empty body reads as {}: AWS answers a + * whitespace-only body, malformed JSON and a trailing token after the object (even a second + * value) with a {@code SerializationException} without a message. + */ static JsonNode readOptionBody(ObjectMapper objectMapper, String body) { - try { - JsonNode request = (body == null || body.isBlank()) - ? objectMapper.createObjectNode() - : objectMapper.readTree(body); - requireJsonObject(request); - return request; - } catch (JsonProcessingException e) { - throw new AwsException("BadRequestException", e.getMessage(), 400); + if (body == null || body.isEmpty()) { + return objectMapper.createObjectNode(); } + JsonNode request; + try { + request = objectMapper.reader().with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS).readTree(body); + } catch (JsonProcessingException e) { + throw new AwsException(SERIALIZATION_EXCEPTION, null, 400); + } + requireJsonObject(request); + return request; } /** Read an optional string member, rejecting a non-string value the way the AWS deserialization layer does. */ diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesImportJobV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesImportJobV2IntegrationTest.java index 5dc56850d3..340dee5160 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesImportJobV2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesImportJobV2IntegrationTest.java @@ -187,11 +187,14 @@ class SesImportJobV2IntegrationTest { "ImportDestination":{"SuppressionListDestination":{"SuppressionListImportAction":"PUT"}}} garbage """.formatted(BUCKET); - for (String body : new String[] {badFormat, bothDestinations, noDataSource, trailingTokens}) { + for (String body : new String[] {badFormat, bothDestinations, noDataSource}) { given().contentType("application/json").header("Authorization", AUTH).body(body) .when().post("/v2/email/import-jobs").then().statusCode(400) .body("__type", equalTo("BadRequestException")); } + given().contentType("application/json").header("Authorization", AUTH).body(trailingTokens) + .when().post("/v2/email/import-jobs").then().statusCode(400) + .body("__type", equalTo("SerializationException")); given().contentType("application/json").header("Authorization", AUTH) .when().post("/v2/email/import-jobs").then().statusCode(400) .body("__type", equalTo("BadRequestException")); diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesNonObjectBodyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesNonObjectBodyIntegrationTest.java new file mode 100644 index 0000000000..4decfcb39b --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesNonObjectBodyIntegrationTest.java @@ -0,0 +1,58 @@ +package io.github.hectorvent.floci.services.ses; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.List; +import java.util.stream.Stream; + +import static io.github.hectorvent.floci.services.ses.SesV2SerializationErrors.assertError; +import static io.github.hectorvent.floci.services.ses.SesV2SerializationErrors.assertSerializationException; + +/** + * The create and update operations that read their body member by member refuse a body that is + * not a JSON object before any member is read, as AWS's deserializer does, while a blank body is + * still read as one without members. + */ +@QuarkusTest +class SesNonObjectBodyIntegrationTest { + + private static final List ENDPOINTS = List.of( + Arguments.of("POST", "/v2/email/identities"), + Arguments.of("POST", "/v2/email/templates"), + Arguments.of("PUT", "/v2/email/templates/non-object-ghost"), + Arguments.of("POST", "/v2/email/custom-verification-email-templates"), + Arguments.of("PUT", "/v2/email/custom-verification-email-templates/non-object-ghost"), + Arguments.of("POST", "/v2/email/tags")); + + static Stream nonObjectBodyCases() { + return ENDPOINTS.stream().flatMap(endpoint -> SesV2SerializationErrors.nonObjectBodies() + .map(body -> Arguments.of(endpoint.get()[0], endpoint.get()[1], body.get()[0], body.get()[1]))); + } + + static Stream blankBodyCases() { + return Stream.of( + Arguments.of("POST", "/v2/email/identities", "EmailIdentity is required."), + Arguments.of("POST", "/v2/email/templates", "TemplateName is required."), + Arguments.of("PUT", "/v2/email/templates/non-object-ghost", + "Template must have at least a subject, text, or html part."), + Arguments.of("POST", "/v2/email/custom-verification-email-templates", "TemplateName is required."), + Arguments.of("PUT", "/v2/email/custom-verification-email-templates/non-object-ghost", + "FromEmailAddress is required."), + Arguments.of("POST", "/v2/email/tags", "Request body is required.")); + } + + @ParameterizedTest(name = "{0} {1} {2}") + @MethodSource("nonObjectBodyCases") + void nonObjectBody_isASerializationException(String method, String path, String body, String message) { + assertSerializationException(method, path, body, message); + } + + @ParameterizedTest(name = "{0} {1}") + @MethodSource("blankBodyCases") + void blankBody_isReadAsOneWithoutMembers(String method, String path, String message) { + assertError(method, path, "", "BadRequestException", message); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesV2BodyReadIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesV2BodyReadIntegrationTest.java new file mode 100644 index 0000000000..8c2bfeed3a --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesV2BodyReadIntegrationTest.java @@ -0,0 +1,101 @@ +package io.github.hectorvent.floci.services.ses; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.List; +import java.util.stream.Stream; + +import static io.github.hectorvent.floci.services.ses.SesV2SerializationErrors.AUTH; +import static io.github.hectorvent.floci.services.ses.SesV2SerializationErrors.assertSerializationException; +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; + +/** + * How every SES v2 operation reads its request body, as probed on AWS (us-west-2, 2026-10-24) with + * ListTenants, CreateConfigurationSet and CreateEmailTemplate: only an empty body is read as one + * without members, while a whitespace-only body, malformed JSON and a token after the object are a + * SerializationException without a message. The endpoints cover each controller that reads a body. + */ +@QuarkusTest +class SesV2BodyReadIntegrationTest { + + private static final String IDENTITY = "/v2/email/identities/body-read-ghost.example.com"; + + private static final List ENDPOINTS = List.of( + Arguments.of("POST", "/v2/email/tenants/list"), + Arguments.of("POST", "/v2/email/tenants"), + Arguments.of("POST", "/v2/email/configuration-sets"), + Arguments.of("POST", "/v2/email/templates"), + Arguments.of("POST", "/v2/email/templates/body-read-ghost/render"), + Arguments.of("POST", "/v2/email/contact-lists"), + Arguments.of("POST", "/v2/email/contact-lists/body-read-ghost/contacts/list"), + Arguments.of("POST", "/v2/email/account/details"), + Arguments.of("PUT", IDENTITY + "/dkim/signing"), + Arguments.of("PUT", IDENTITY + "/configuration-set"), + Arguments.of("PUT", IDENTITY + "/mail-from"), + Arguments.of("POST", IDENTITY + "/policies/body-read-policy"), + Arguments.of("POST", "/v2/email/import-jobs"), + Arguments.of("POST", "/v2/email/import-jobs/list"), + Arguments.of("POST", "/v2/email/outbound-emails"), + Arguments.of("POST", "/v2/email/dedicated-ip-pools"), + Arguments.of("PUT", "/v2/email/suppression/addresses"), + Arguments.of("POST", "/v2/email/tags")); + + static Stream malformedBodyCases() { + return ENDPOINTS.stream().flatMap(endpoint -> Stream.of("{", "{\"a\":}", "{} x", "{}{}") + .map(body -> Arguments.of(endpoint.get()[0], endpoint.get()[1], body))); + } + + static Stream endpoints() { + return ENDPOINTS.stream(); + } + + @ParameterizedTest(name = "{0} {1} {2}") + @MethodSource("malformedBodyCases") + void malformedBody_isASerializationExceptionWithoutMessage(String method, String path, String body) { + assertSerializationException(method, path, body, null); + } + + @ParameterizedTest(name = "{0} {1}") + @MethodSource("endpoints") + void whitespaceOnlyBody_isASerializationExceptionWithoutMessage(String method, String path) { + assertSerializationException(method, path, " ", null); + } + + @Test + void emptyBody_isReadAsOneWithoutMembers() { + listTenants(""); + // Reaches the missing-name validation rather than failing to read the body. + given() + .contentType("application/json") + .header("Authorization", AUTH) + .body("") + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(400) + .body("__type", equalTo("BadRequestException")); + } + + @Test + void whitespaceAfterTheObject_isAccepted() { + listTenants("{} "); + } + + private static void listTenants(String body) { + given() + .contentType("application/json") + .header("Authorization", AUTH) + .body(body) + .when() + .post("/v2/email/tenants/list") + .then() + .statusCode(200) + .body("Tenants", notNullValue()); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesV2JsonTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesV2JsonTest.java index fb176860d4..429acf3377 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesV2JsonTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesV2JsonTest.java @@ -116,16 +116,18 @@ class SesV2JsonTest { } @Test - void readOptionBody_blankIsEmptyObject_badShapesAre400() { + void readOptionBody_onlyEmptyIsEmptyObject_unreadableBodiesAreSerializationExceptions() { assertTrue(SesV2Json.readOptionBody(MAPPER, null).isObject()); - assertTrue(SesV2Json.readOptionBody(MAPPER, " ").isObject()); + assertTrue(SesV2Json.readOptionBody(MAPPER, "").isObject()); + assertTrue(SesV2Json.readOptionBody(MAPPER, " {} \n").isObject()); assertEquals("REQUIRE", SesV2Json.readOptionBody(MAPPER, "{\"HttpsPolicy\":\"REQUIRE\"}") .path("HttpsPolicy").asText()); assertEquals("Start of list found where not expected", assertAws("SerializationException", 400, () -> SesV2Json.readOptionBody(MAPPER, "[1]")).getMessage()); - assertNull(assertAws("SerializationException", 400, - () -> SesV2Json.readOptionBody(MAPPER, "null")).getMessage()); - assertAws("BadRequestException", 400, () -> SesV2Json.readOptionBody(MAPPER, "{not json")); + for (String body : List.of("null", " ", "{not json", "{", "{\"a\":}", "{} x", "{}{}")) { + assertNull(assertAws("SerializationException", 400, + () -> SesV2Json.readOptionBody(MAPPER, body)).getMessage(), body); + } } @Test