diff --git a/docs/services/ses.md b/docs/services/ses.md index d6276f95db..0350168d80 100644 --- a/docs/services/ses.md +++ b/docs/services/ses.md @@ -503,7 +503,7 @@ S/MIME certificate associations are stored per region and never used: Floci does ### Request member types -The operations whose body nests a structure in a structure, or holds a list of structures, check the request body against the SES v2 service model before anything else is validated, as AWS's restJson1 deserializer does: the send operations (`SendEmail`, `SendBulkEmail`, `SendCustomVerificationEmail`), `CreateConfigurationSet`, `UpdateConfigurationSet`, `PutConfigurationSetSuppressionOptions`, `PutConfigurationSetVdmOptions`, `CreateConfigurationSetEventDestination` and `UpdateConfigurationSetEventDestination`. In those operations, the first body member of the wrong JSON type, in body order, is a `SerializationException` with AWS's deserializer message: +The operations whose body nests a structure in a structure, or holds a list of structures, check the request body against the SES v2 service model before anything else is validated, as AWS's restJson1 deserializer does: the send operations (`SendEmail`, `SendBulkEmail`, `SendCustomVerificationEmail`), `CreateConfigurationSet`, `UpdateConfigurationSet`, `PutConfigurationSetSuppressionOptions`, `PutConfigurationSetVdmOptions`, `CreateConfigurationSetEventDestination`, `UpdateConfigurationSetEventDestination`, `CreateContactList`, `UpdateContactList`, `CreateContact`, `UpdateContact` and `ListContacts`. In those operations, the first body member of the wrong JSON type, in body order, is a `SerializationException` with AWS's deserializer message: | Member type | Value sent | Message | |---|---|---| @@ -512,7 +512,7 @@ The operations whose body nests a structure in a structure, or holds a list of s | structure, map | array / any other non-object | `Start of list found where not expected` / `Expected null` | | list | object / any other non-array | `Start of structure or map found where not expected.` / `Expected list or null` | -A JSON `null` member counts as absent. A `null` address list element passes this check and fails later, as on AWS: `SendEmail` answers `Invalid email address null.` after its content and sender checks and before the tenant lookup, and `SendBulkEmail` reports it as the `INVALID_PARAMETER` status of the entry it belongs to (of every entry for a `null` Reply-To), or as `Destination cannot be empty` when no entry of the request has a non-null recipient. The account-level sending switch is still checked first, so a paused account answers `SendingPausedException` even for a malformed body. +A JSON `null` member counts as absent. A `null` address list element passes this check and fails later, as on AWS: `SendEmail` answers `Invalid email address null.` after its content and sender checks and before the tenant lookup, and `SendBulkEmail` reports it as the `INVALID_PARAMETER` status of the entry it belongs to (of every entry for a `null` Reply-To), or as `Destination cannot be empty` when no entry of the request has a non-null recipient. A `null` element of `Topics` (`CreateContactList`, `UpdateContactList`) or `TopicPreferences` (`CreateContact`, `UpdateContact`) also passes this check, but there AWS answers it with a `500 InternalFailure`, which Floci replaces with `BadRequestException` naming the element ("1 validation error detected: Value at 'topics.1.member.topicName' failed to satisfy constraint: Member must not be null"). The account-level sending switch is still checked first, so a paused account answers `SendingPausedException` even for a malformed body. The other operations check their members one at a time, and some of them still answer a wrong JSON type with `BadRequestException`. 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 380d64ae27..733faab16b 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 @@ -40,10 +40,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.remapV1Exception; import static io.github.hectorvent.floci.services.ses.SesV2Json.requireJsonObject; -import static io.github.hectorvent.floci.services.ses.SesV2Json.stringMemberOrAbsent; -import static io.github.hectorvent.floci.services.ses.SesV2Json.structureMemberOrAbsent; -import static io.github.hectorvent.floci.services.ses.SesV2Json.typedStringMemberOrAbsent; -import static io.github.hectorvent.floci.services.ses.SesV2Json.unexpectedStartError; /** * SES V2 contact-list and contact endpoints ({@code /v2/email/contact-lists}). Every operation @@ -80,6 +76,7 @@ public class SesContactController { } JsonNode request = objectMapper.readTree(body); requireJsonObject(request); + SesV2RequestShapes.check("CreateContactList", request); // Read leniently; the service surfaces a missing ContactListName as the AWS Smithy // validation error rather than a custom "required" message. String name = request.path("ContactListName").asText(null); @@ -136,6 +133,7 @@ public class SesContactController { ? objectMapper.createObjectNode() : objectMapper.readTree(body); requireJsonObject(request); + SesV2RequestShapes.check("UpdateContactList", request); JsonNode topicsNode = request.path("Topics"); // Treat absent or explicit-null Topics as "not provided" (keep existing), consistent // with Description; clearing is done via an explicit empty array []. @@ -170,9 +168,6 @@ public class SesContactController { if (topicsNode == null || topicsNode.isMissingNode() || topicsNode.isNull()) { return out; } - if (!topicsNode.isArray()) { - throw new AwsException("BadRequestException", "Topics must be an array.", 400); - } for (JsonNode t : topicsNode) { out.add(new Topic( t.path("TopicName").asText(null), @@ -221,10 +216,11 @@ public class SesContactController { } JsonNode request = objectMapper.readTree(body); requireJsonObject(request); + SesV2RequestShapes.check("CreateContact", request); String emailAddress = request.path("EmailAddress").asText(null); List prefs = parseTopicPreferences(request.path("TopicPreferences")); Boolean unsubscribeAll = parseUnsubscribeAll(request); - String attributesData = parseAttributesData(request); + String attributesData = request.path("AttributesData").asText(null); contactService.createContact(contactListName, emailAddress, prefs, unsubscribeAll, attributesData, region); LOG.infov("SES V2 CreateContact: {0} in {1}", emailAddress, contactListName); @@ -246,8 +242,9 @@ public class SesContactController { ? objectMapper.createObjectNode() : objectMapper.readTree(body); requireJsonObject(request); + SesV2RequestShapes.check("ListContacts", request); Integer pageSize = intMemberOrAbsent(request, "PageSize"); - String nextToken = stringMemberOrAbsent(request, "NextToken"); + String nextToken = request.path("NextToken").asText(null); SesContactService.ContactPage listed = contactService.listContacts(contactListName, region, SesListPaging.V2_LIST_CONTACTS, pageSize, nextToken, parseContactFilter(request)); ObjectNode result = objectMapper.createObjectNode(); @@ -265,17 +262,17 @@ public class SesContactController { } private static SesContactService.ContactFilter parseContactFilter(JsonNode request) { - JsonNode filterNode = structureMemberOrAbsent(request, "Filter"); + JsonNode filterNode = request.path("Filter"); if (filterNode.isMissingNode() || filterNode.isNull()) { return null; } - JsonNode topicNode = structureMemberOrAbsent(filterNode, "TopicFilter"); + JsonNode topicNode = filterNode.path("TopicFilter"); SesContactService.TopicFilter topicFilter = null; if (!topicNode.isMissingNode() && !topicNode.isNull()) { - topicFilter = new SesContactService.TopicFilter(typedStringMemberOrAbsent(topicNode, "TopicName"), + topicFilter = new SesContactService.TopicFilter(topicNode.path("TopicName").asText(null), coerceBooleanOrFalse(topicNode.path("UseDefaultIfPreferenceUnavailable"))); } - return new SesContactService.ContactFilter(typedStringMemberOrAbsent(filterNode, "FilteredStatus"), + return new SesContactService.ContactFilter(filterNode.path("FilteredStatus").asText(null), topicFilter); } @@ -301,11 +298,12 @@ public class SesContactController { ? objectMapper.createObjectNode() : objectMapper.readTree(body); requireJsonObject(request); + SesV2RequestShapes.check("UpdateContact", request); JsonNode prefsNode = request.path("TopicPreferences"); boolean prefsPresent = !prefsNode.isMissingNode() && !prefsNode.isNull(); List prefs = prefsPresent ? parseTopicPreferences(prefsNode) : null; Boolean unsubscribeAll = parseUnsubscribeAll(request); - String attributesData = parseAttributesData(request); + String attributesData = request.path("AttributesData").asText(null); contactService.updateContact(contactListName, emailAddress, prefs, prefsPresent, unsubscribeAll, attributesData, region); LOG.infov("SES V2 UpdateContact: {0} in {1}", emailAddress, contactListName); @@ -332,9 +330,6 @@ public class SesContactController { if (node == null || node.isMissingNode() || node.isNull()) { return null; } - if (!node.isArray()) { - throw new AwsException("BadRequestException", "TopicPreferences must be an array.", 400); - } List out = new ArrayList<>(); for (JsonNode p : node) { out.add(new TopicPreference( @@ -353,31 +348,6 @@ public class SesContactController { return coerceBoolean(request.path("UnsubscribeAll")); } - // AttributesData is a String; a non-string (number/boolean/array/object) is a - // SerializationException. Absent or explicit null leaves it unset. - private static String parseAttributesData(JsonNode request) { - if (!request.has("AttributesData")) { - return null; - } - JsonNode node = request.path("AttributesData"); - if (node.isNull()) { - return null; - } - if (node.isTextual()) { - return node.textValue(); - } - if (node.isNumber()) { - throw new AwsException("SerializationException", - "NUMBER_VALUE can not be converted to a String", 400); - } - if (node.isBoolean()) { - throw new AwsException("SerializationException", - (node.booleanValue() ? "TRUE_VALUE" : "FALSE_VALUE") - + " can not be converted to a String", 400); - } - throw unexpectedStartError(node); - } - private ObjectNode contactJson(Contact c, ContactList list, boolean full) { ObjectNode result = objectMapper.createObjectNode(); if (full) { diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesContactMemberTypeIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesContactMemberTypeIntegrationTest.java new file mode 100644 index 0000000000..8ee8a23a6c --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesContactMemberTypeIntegrationTest.java @@ -0,0 +1,160 @@ +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.stream.Stream; + +import static io.github.hectorvent.floci.services.ses.SesV2SerializationErrors.EXPECTED_LIST; +import static io.github.hectorvent.floci.services.ses.SesV2SerializationErrors.EXPECTED_NULL; +import static io.github.hectorvent.floci.services.ses.SesV2SerializationErrors.LIST; +import static io.github.hectorvent.floci.services.ses.SesV2SerializationErrors.NUMBER; +import static io.github.hectorvent.floci.services.ses.SesV2SerializationErrors.STRUCTURE; +import static io.github.hectorvent.floci.services.ses.SesV2SerializationErrors.assertError; +import static io.github.hectorvent.floci.services.ses.SesV2SerializationErrors.assertSerializationException; + +/** + * The contact list and contact operations whose body holds a list of structures, and ListContacts + * with its nested Filter, check it against the SES v2 shapes, so a member of the wrong JSON type is + * a SerializationException ahead of the name validation and the contact list lookup. The messages + * per JSON type are covered by SesV2RequestShapesTest; these cases show each operation runs the + * check, nested members included. + */ +@QuarkusTest +class SesContactMemberTypeIntegrationTest { + + private static final String CONTACT_LISTS = "/v2/email/contact-lists"; + private static final String MISSING_LIST = CONTACT_LISTS + "/v2-contact-member-type-missing"; + private static final String CONTACTS = MISSING_LIST + "/contacts"; + private static final String TOPIC = + "{\"TopicName\": \"news\", \"DisplayName\": \"News\", \"DefaultSubscriptionStatus\": \"OPT_IN\"}"; + private static final String PREFERENCE = "{\"TopicName\": \"news\", \"SubscriptionStatus\": \"OPT_OUT\"}"; + + static Stream createContactListCases() { + return Stream.of( + Arguments.of("numericName", "\"ContactListName\": 1", NUMBER), + Arguments.of("stringTopicsBeforeNameValidation", + "\"ContactListName\": \"\", \"Topics\": \"news\"", EXPECTED_LIST), + Arguments.of("objectTopics", "\"Topics\": {}", STRUCTURE), + Arguments.of("stringTopic", "\"Topics\": [\"news\"]", EXPECTED_NULL), + Arguments.of("arrayTopic", "\"Topics\": [[]]", LIST), + Arguments.of("numericTopicName", "\"Topics\": [{\"TopicName\": 1}]", NUMBER), + Arguments.of("booleanDefaultSubscriptionStatus", + "\"Topics\": [{\"TopicName\": \"news\", \"DefaultSubscriptionStatus\": true}]", + "TRUE_VALUE can not be converted to a String"), + Arguments.of("tagsBeforeTopicsInBodyOrder", "\"Tags\": \"x\", \"Topics\": {}", EXPECTED_LIST), + Arguments.of("numericDescription", "\"ContactListName\": \"x\", \"Description\": 1", NUMBER)); + } + + static Stream updateContactListCases() { + return Stream.of( + Arguments.of("stringTopics", "\"Topics\": \"news\"", EXPECTED_LIST), + Arguments.of("numericDisplayName", "\"Topics\": [{\"TopicName\": \"news\", \"DisplayName\": 1}]", + NUMBER), + Arguments.of("booleanDescription", "\"Description\": false", + "FALSE_VALUE can not be converted to a String")); + } + + static Stream createContactCases() { + return Stream.of( + Arguments.of("numericEmailAddress", "\"EmailAddress\": 1", NUMBER), + Arguments.of("stringTopicPreferences", + "\"EmailAddress\": \"a@example.com\", \"TopicPreferences\": \"news\"", EXPECTED_LIST), + Arguments.of("stringTopicPreference", "\"TopicPreferences\": [\"news\"]", EXPECTED_NULL), + Arguments.of("numericSubscriptionStatus", + "\"TopicPreferences\": [{\"TopicName\": \"news\", \"SubscriptionStatus\": 1}]", NUMBER), + Arguments.of("objectAttributesData", "\"AttributesData\": {}", STRUCTURE)); + } + + static Stream updateContactCases() { + return Stream.of( + Arguments.of("objectTopicPreferences", "\"TopicPreferences\": {}", STRUCTURE), + Arguments.of("arrayTopicName", "\"TopicPreferences\": [{\"TopicName\": []}]", LIST), + Arguments.of("arrayAttributesData", "\"AttributesData\": []", LIST)); + } + + static Stream listContactsCases() { + return Stream.of( + Arguments.of("numericNextToken", "\"NextToken\": 1", NUMBER), + Arguments.of("stringFilter", "\"Filter\": \"x\"", EXPECTED_NULL), + Arguments.of("arrayTopicFilter", "\"Filter\": {\"TopicFilter\": []}", LIST), + Arguments.of("numericTopicFilterTopicName", + "\"Filter\": {\"FilteredStatus\": \"OPT_IN\", \"TopicFilter\": {\"TopicName\": 1}}", NUMBER), + Arguments.of("filterTypeBeforePageSizeValidation", + "\"PageSize\": 0, \"Filter\": {\"TopicFilter\": \"x\"}", EXPECTED_NULL)); + } + + // A null element passes AWS's deserializer ([null, "x"] answers "Expected null" for "x"), and AWS + // then answers 500 InternalFailure (probed 2026-10-17). Floci keeps a 400 naming the element. + static Stream nullElementCases() { + return Stream.of( + Arguments.of("POST", CONTACT_LISTS, "{\"ContactListName\": \"x\", \"Topics\": [null]}", + nullTopicName("topics", 1)), + Arguments.of("PUT", MISSING_LIST, "{\"Topics\": [" + TOPIC + ", null]}", nullTopicName("topics", 2)), + Arguments.of("POST", CONTACTS, "{\"EmailAddress\": \"a@example.com\", \"TopicPreferences\": [null]}", + nullTopicName("topicPreferences", 1)), + Arguments.of("PUT", CONTACTS + "/a@example.com", "{\"TopicPreferences\": [" + PREFERENCE + ", null]}", + nullTopicName("topicPreferences", 2))); + } + + static Stream nullElementBeforeWrongTypeCases() { + return Stream.of( + Arguments.of("POST", CONTACT_LISTS, "{\"Topics\": [null, \"x\"]}"), + Arguments.of("PUT", MISSING_LIST, "{\"Topics\": [null, \"x\"]}"), + Arguments.of("POST", CONTACTS, "{\"TopicPreferences\": [null, \"x\"]}"), + Arguments.of("PUT", CONTACTS + "/a@example.com", "{\"TopicPreferences\": [null, \"x\"]}")); + } + + private static String nullTopicName(String member, int position) { + return "1 validation error detected: Value at '" + member + "." + position + + ".member.topicName' failed to satisfy constraint: Member must not be null"; + } + + private static void assertMembersRejected(String method, String path, String members, String message) { + assertSerializationException(method, path, "{" + members + "}", message); + } + + @ParameterizedTest(name = "{0}") + @MethodSource("createContactListCases") + void createContactList_wrongMemberType(String caseName, String members, String message) { + assertMembersRejected("POST", CONTACT_LISTS, members, message); + } + + @ParameterizedTest(name = "{0}") + @MethodSource("updateContactListCases") + void updateContactList_wrongMemberTypeBeforeLookup(String caseName, String members, String message) { + assertMembersRejected("PUT", MISSING_LIST, members, message); + } + + @ParameterizedTest(name = "{0}") + @MethodSource("createContactCases") + void createContact_wrongMemberTypeBeforeLookup(String caseName, String members, String message) { + assertMembersRejected("POST", CONTACTS, members, message); + } + + @ParameterizedTest(name = "{0}") + @MethodSource("updateContactCases") + void updateContact_wrongMemberTypeBeforeLookup(String caseName, String members, String message) { + assertMembersRejected("PUT", CONTACTS + "/a@example.com", members, message); + } + + @ParameterizedTest(name = "{0}") + @MethodSource("listContactsCases") + void listContacts_wrongMemberTypeBeforeLookup(String caseName, String members, String message) { + assertMembersRejected("POST", CONTACTS + "/list", members, message); + } + + @ParameterizedTest(name = "{0} {1}") + @MethodSource("nullElementCases") + void nullElement_isAValidationErrorNotAServerError(String method, String path, String body, String message) { + assertError(method, path, body, "BadRequestException", message); + } + + @ParameterizedTest(name = "{0} {1}") + @MethodSource("nullElementBeforeWrongTypeCases") + void nullElement_passesTheShapeCheck(String method, String path, String body) { + assertSerializationException(method, path, body, EXPECTED_NULL); + } +}