diff --git a/docs/services/ses.md b/docs/services/ses.md index 0ba103506d..03a819450b 100644 --- a/docs/services/ses.md +++ b/docs/services/ses.md @@ -290,6 +290,8 @@ For a **single-recipient** list-managed send, Floci injects a functional unsubsc Tag operations support these ARN forms: `arn:aws:ses:::configuration-set/`, `arn:aws:ses:::template/`, and `arn:aws:ses:::identity/`. Tags supplied to `CreateConfigurationSet`, `CreateEmailTemplate`, and `CreateEmailIdentity` are reachable through `ListTagsForResource`; `UpdateEmailTemplate` does not modify tags. Other resource types return `NotFoundException`. +The same AWS-compatible validation applies wherever tags are set (including `CreateContactList` and `TagResource`): at most 50 tags, unique keys, keys 1–128 / values 0–256 code points, characters limited to letters, numbers, Unicode whitespace and `_ . : / = + - @`, and the reserved `aws:` key prefix. `TagResource` applies the 50-tag limit to the merged (existing + incoming) set. Violations return `BadRequestException` with AWS's messages. + Identity, identity-policy, template, custom-verification-template, configuration-set, and sent-message state is shared between the v1 Query API and the v2 REST JSON API, so a template created with `CreateTemplate` resolves through `SendEmail` on v2 (and vice versa), a policy written with `PutIdentityPolicy` (v1) is returned by `GetEmailIdentityPolicies` (v2), a custom verification email template created with `CreateCustomVerificationEmailTemplate` (v1) is returned by `GetCustomVerificationEmailTemplate` (v2), a configuration set created with `CreateConfigurationSet` is visible to both `DescribeConfigurationSet` (v1) and `GetConfigurationSet` (v2), and every send appears in the same `GET /_aws/ses` inspection mailbox. DKIM follows AWS's domain-centric model. A **domain** identity carries DKIM tokens (generated at verification, stable across `VerifyDomainDkim` calls); its `DkimVerificationStatus` tracks **DNS record detection** — it transitions to `Success` when the expected `._domainkey.` CNAMEs are present in the Route53 emulation, not when DKIM is enabled. An **email** identity has no DKIM of its own: its `DkimAttributes` (`SigningEnabled`, `Status`, `Tokens`) are inherited from its parent domain identity when one is registered. The parent is the exact domain after the `@` (verified against AWS): a verified `example.com` covers `user@example.com` but not `user@mail.example.com` unless `mail.example.com` is itself a registered identity. `SetIdentityDkimEnabled` / `PutEmailIdentityDkimAttributes` only toggle the signing flag (they no longer force the verification status); `PutEmailIdentityDkimSigningAttributes` sets the signing origin (`AWS_SES` Easy DKIM — regenerating tokens when the key length changes — or `EXTERNAL` BYODKIM). diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesContactService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesContactService.java index e3d1de2c0d..c4f622e01c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesContactService.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesContactService.java @@ -77,6 +77,7 @@ public class SesContactService { public ContactList createContactList(String name, String description, List topics, List tags, String region) { validateContactListInput(name, description, topics); + SesTags.validate(tags); ContactList list = new ContactList(name); list.setDescription(description); list.setTopics(topics); diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesController.java index 924b22e82d..1d2542c18e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesController.java @@ -123,6 +123,9 @@ public class SesController { // without leaving a half-created identity behind, matching AWS and the ConfigurationSetName // pre-check below. List parsedTags = parseTagsArray(request.path("Tags")); + // Validate tags before creating the identity so an invalid set fails atomically + // instead of leaving the identity behind. + SesTags.validate(parsedTags); // Verified against AWS: a non-existent ConfigurationSetName fails the whole call // (NotFoundException) without creating the identity, so validate it before creating. diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java index 690e562903..13539a5540 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java @@ -1380,11 +1380,7 @@ public class SesService { "ConfigurationSetName is required.", 400); } String key = configSetKey(region, configSet.getName()); - if (configSet.getTags() != null) { - for (Tag tag : configSet.getTags()) { - SesTags.validate(tag); - } - } + SesTags.validate(configSet.getTags()); if (configSet.getSuppressionOptions() != null && configSet.getSuppressionOptions().getSuppressedReasons() != null) { for (String reason : configSet.getSuppressionOptions().getSuppressedReasons()) { @@ -2066,9 +2062,7 @@ public class SesService { "1 validation error detected: Value at 'tags' failed to satisfy constraint: " + "Member must have length greater than or equal to 1", 400); } - for (Tag t : newTags) { - SesTags.validate(t); - } + SesTags.validate(newTags); switch (ref.type()) { case "configuration-set" -> tagConfigurationSet(ref.name(), ref.region(), newTags); case "template" -> tagEmailTemplate(ref.name(), ref.region(), newTags); @@ -2161,6 +2155,7 @@ public class SesService { for (Tag t : incoming) { merged.put(t.key(), t.value()); } + SesTags.validateMergedCount(merged.size()); List out = new ArrayList<>(); merged.forEach((k, v) -> out.add(new Tag(k, v))); return out; @@ -2195,11 +2190,7 @@ public class SesService { } public void setIdentityTags(String identityValue, String region, List tags) { - if (tags != null) { - for (Tag tag : tags) { - SesTags.validate(tag); - } - } + SesTags.validate(tags); String key = identityKey(region, identityValue); Identity identity = identityStore.get(key) .orElseThrow(() -> new AwsException("NotFoundException", diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesTags.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesTags.java index 0e1700d096..ffc592f6ef 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesTags.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesTags.java @@ -3,30 +3,126 @@ package io.github.hectorvent.floci.services.ses; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.services.ses.model.Tag; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.regex.Pattern; + /** * Shared SES resource-tag validation. Lives outside the facade so the extracted domain services can * validate tags without depending back on {@link SesService}. + * + *

Rules and messages are probe-confirmed against real AWS SES v2 (2026-08-23): at most 50 tags, + * unique keys, key 1–128 / value 0–256 characters, both key and value restricted to letters, numbers, + * spaces, and {@code _ . : / = + - @}, and the {@code aws:} key prefix reserved for system tags. The + * evaluation order mirrors the observed AWS precedence: count, then character set (value before key), + * then the reserved prefix, then the aggregated Smithy length errors, then empty keys, then duplicate + * keys. All violations are {@code BadRequestException} / 400. */ final class SesTags { + private static final int MAX_TAGS = 50; + private static final int KEY_MAX = 128; + private static final int VALUE_MAX = 256; + // Letters, numbers, any Unicode whitespace/separator (\p{Z}, e.g. U+00A0 — probe-confirmed + // accepted, not just the ASCII space), and _ . : / = + - @ (AWS's allowed tag character set). + private static final Pattern TAG_CHARS = Pattern.compile("[\\p{L}\\p{N}\\p{Z}_.:/=+@-]*"); + private static final String CHARSET_MESSAGE = + "Tags can only contain letters, numbers, spaces, and the following special characters: " + + "_ . : / = + - @"; + private static final String MAX_TAGS_MESSAGE = + "Maximum of " + MAX_TAGS + " user tags are allowed per resource, consider reducing the " + + "number of tags in the request"; + // TagResource merges the incoming tags with the resource's existing tags before enforcing the + // 50-tag ceiling, and uses a slightly different message than the create path (probe-confirmed). + private static final String MAX_TAGS_MERGED_MESSAGE = MAX_TAGS_MESSAGE + + " or delete existing tags and retry"; + private static final String SYSTEM_TAG_MESSAGE = + "Caller is an end user and not allowed to mutate system tags"; + private SesTags() {} - static void validate(Tag tag) { - if (tag == null) { - throw new AwsException("InvalidParameterValue", "Tag must not be null.", 400); + /** + * Validate a tag collection the way AWS does: at most 50 tags with unique keys, each key/value + * within its length bound and limited to the allowed character set, with the {@code aws:} key + * prefix reserved. A {@code null}/empty list is a no-op. + */ + static void validate(List tags) { + if (tags == null || tags.isEmpty()) { + return; } - String key = tag.key(); - if (key == null || key.isEmpty()) { - throw new AwsException("InvalidParameterValue", "Tag Key is required.", 400); + if (tags.size() > MAX_TAGS) { + throw new AwsException("BadRequestException", MAX_TAGS_MESSAGE, 400); } - if (key.length() > 128) { - throw new AwsException("InvalidParameterValue", - "Tag Key must be 1-128 characters.", 400); + // Character set is checked before length, and value before key within a tag (probe-confirmed). + for (Tag tag : tags) { + if (tag == null || !charsetOk(tag.value()) || !charsetOk(tag.key())) { + throw new AwsException("BadRequestException", CHARSET_MESSAGE, 400); + } } - String value = tag.value(); - if (value != null && value.length() > 256) { - throw new AwsException("InvalidParameterValue", - "Tag Value must be 0-256 characters.", 400); + // The aws: key prefix (case-insensitive) is reserved for system tags; only the key is checked + // — a value starting with aws: is allowed. Checked after the character set (probe-confirmed). + for (Tag tag : tags) { + if (tag.key() != null && tag.key().regionMatches(true, 0, "aws:", 0, 4)) { + throw new AwsException("BadRequestException", SYSTEM_TAG_MESSAGE, 400); + } + } + // Length violations are reported together in the AWS Smithy form, value before key, with a + // 1-based tag index. Smithy length constraints count Unicode code points, not UTF-16 code + // units, so a supplementary-plane character counts as one. + List lengthErrors = new ArrayList<>(); + for (int i = 0; i < tags.size(); i++) { + Tag tag = tags.get(i); + if (tag.value() != null && codePointLength(tag.value()) > VALUE_MAX) { + lengthErrors.add(lengthConstraint(i + 1, "value", VALUE_MAX)); + } + if (tag.key() != null && codePointLength(tag.key()) > KEY_MAX) { + lengthErrors.add(lengthConstraint(i + 1, "key", KEY_MAX)); + } + } + if (!lengthErrors.isEmpty()) { + throw new AwsException("BadRequestException", + lengthErrors.size() + " validation error" + (lengthErrors.size() > 1 ? "s" : "") + + " detected: " + String.join("; ", lengthErrors), 400); + } + // An empty key is rejected with an empty body, matching AWS. + for (Tag tag : tags) { + if (tag.key() == null || tag.key().isEmpty()) { + throw new AwsException("BadRequestException", null, 400); + } + } + Set seen = new HashSet<>(); + for (Tag tag : tags) { + if (!seen.add(tag.key())) { + throw new AwsException("BadRequestException", + "Cannot provide multiple tags with the same key", 400); + } } } + + /** + * Enforce the 50-tag ceiling on the merged (existing + incoming) tag set that {@code TagResource} + * persists. AWS applies the limit to the merged result — updating an existing key is allowed, but + * a merge that would exceed 50 unique keys is rejected with the merge-specific message. + */ + static void validateMergedCount(int mergedCount) { + if (mergedCount > MAX_TAGS) { + throw new AwsException("BadRequestException", MAX_TAGS_MERGED_MESSAGE, 400); + } + } + + // A null value counts as a character-set violation (AWS requires a string value; empty is allowed). + private static boolean charsetOk(String s) { + return s != null && TAG_CHARS.matcher(s).matches(); + } + + private static int codePointLength(String s) { + return s.codePointCount(0, s.length()); + } + + private static String lengthConstraint(int index, String member, int max) { + return "Value at 'tags." + index + ".member." + member + "' failed to satisfy constraint: " + + "Member must have length less than or equal to " + max; + } } diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesTemplateService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesTemplateService.java index 3a43e325f3..27e681f6d8 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesTemplateService.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesTemplateService.java @@ -5,7 +5,6 @@ import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.services.ses.model.EmailTemplate; -import io.github.hectorvent.floci.services.ses.model.Tag; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import org.jboss.logging.Logger; @@ -47,11 +46,7 @@ public class SesTemplateService { public EmailTemplate createTemplate(EmailTemplate template, String region) { validateTemplate(template); - if (template.getTags() != null) { - for (Tag tag : template.getTags()) { - SesTags.validate(tag); - } - } + SesTags.validate(template.getTags()); String key = templateKey(region, template.getTemplateName()); if (templateStore.get(key).isPresent()) { throw new AwsException("AlreadyExists", diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetV2IntegrationTest.java index d1902ab741..d258834a85 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetV2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetV2IntegrationTest.java @@ -275,7 +275,7 @@ class SesConfigurationSetV2IntegrationTest { @Test @Order(12) - void createConfigurationSet_tagWithMissingValue_roundTripsAsAbsent() { + void createConfigurationSet_tagWithMissingValue_returns400() { given() .contentType("application/json") .header("Authorization", AUTH_HEADER) @@ -288,15 +288,10 @@ class SesConfigurationSetV2IntegrationTest { .when() .post("/v2/email/configuration-sets") .then() - .statusCode(200); - - given() - .header("Authorization", AUTH_HEADER) - .when() - .get("/v2/email/configuration-sets/v2-cs-tag-no-value") - .then() - .statusCode(200) - .body("Tags[0].Key", equalTo("env")); + .statusCode(400) + .body("__type", equalTo("BadRequestException")) + .body("message", equalTo("Tags can only contain letters, numbers, spaces, and the " + + "following special characters: _ . : / = + - @")); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesContactListV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesContactListV2IntegrationTest.java index 9608cfdd77..2efa7ff4b5 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesContactListV2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesContactListV2IntegrationTest.java @@ -429,6 +429,17 @@ class SesContactListV2IntegrationTest { + "Member must satisfy enum value set: [OPT_OUT, OPT_IN]")); } + @Test + @Order(25) + void createContactList_duplicateTagKeys_returns400() { + post("{\"ContactListName\":\"tagged-list\",\"Topics\":[]," + + "\"Tags\":[{\"Key\":\"dup\",\"Value\":\"1\"},{\"Key\":\"dup\",\"Value\":\"2\"}]}") + .then() + .statusCode(400) + .body("__type", equalTo("BadRequestException")) + .body("message", equalTo("Cannot provide multiple tags with the same key")); + } + private static io.restassured.response.Response post(String body) { return given() .contentType("application/json") diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesTagsTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesTagsTest.java new file mode 100644 index 0000000000..5edffae091 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesTagsTest.java @@ -0,0 +1,202 @@ +package io.github.hectorvent.floci.services.ses; + +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.services.ses.model.Tag; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * Unit test for the shared SES tag validation. Rules and messages are probe-confirmed against real AWS + * SES v2 (2026-08-23): max 50 tags, unique keys, key 1–128 / value 0–256 chars, and a restricted + * character set on both key and value. + */ +class SesTagsTest { + + private static Tag tag(String k, String v) { + return new Tag(k, v); + } + + @Test + void validList_and_emptyOrNull_ok() { + assertDoesNotThrow(() -> SesTags.validate((List) null)); + assertDoesNotThrow(() -> SesTags.validate(List.of())); + assertDoesNotThrow(() -> SesTags.validate(List.of(tag("env", "dev"), tag("owner", "alice")))); + // Empty value and the allowed special characters are accepted. + assertDoesNotThrow(() -> SesTags.validate(List.of(tag("k", "")))); + assertDoesNotThrow(() -> SesTags.validate(List.of(tag("k", "_ . : / = + - @ a1")))); + } + + @Test + void tooManyTags_reportsMaxFifty() { + List tags = new ArrayList<>(); + for (int i = 0; i < 51; i++) { + tags.add(tag("k" + i, "v")); + } + AwsException e = assertThrows(AwsException.class, () -> SesTags.validate(tags)); + assertEquals("BadRequestException", e.getErrorCode()); + assertEquals("Maximum of 50 user tags are allowed per resource, consider reducing the number " + + "of tags in the request", e.getMessage()); + } + + @Test + void fiftyTags_ok() { + List tags = new ArrayList<>(); + for (int i = 0; i < 50; i++) { + tags.add(tag("k" + i, "v")); + } + assertDoesNotThrow(() -> SesTags.validate(tags)); + } + + @Test + void duplicateKeys_rejected() { + AwsException e = assertThrows(AwsException.class, + () -> SesTags.validate(List.of(tag("dup", "1"), tag("dup", "2")))); + assertEquals("Cannot provide multiple tags with the same key", e.getMessage()); + } + + @Test + void nullValue_isCharsetViolation() { + AwsException e = assertThrows(AwsException.class, () -> SesTags.validate(List.of(tag("k", null)))); + assertEquals("Tags can only contain letters, numbers, spaces, and the following special " + + "characters: _ . : / = + - @", e.getMessage()); + } + + @Test + void invalidChar_inValueOrKey_isCharsetViolation() { + String msg = "Tags can only contain letters, numbers, spaces, and the following special " + + "characters: _ . : / = + - @"; + assertEquals(msg, assertThrows(AwsException.class, + () -> SesTags.validate(List.of(tag("k", "bad!value")))).getMessage()); + assertEquals(msg, assertThrows(AwsException.class, + () -> SesTags.validate(List.of(tag("bad!key", "v")))).getMessage()); + } + + @Test + void keyTooLong_smithyLengthMessage() { + AwsException e = assertThrows(AwsException.class, + () -> SesTags.validate(List.of(tag("k".repeat(129), "v")))); + assertEquals("1 validation error detected: Value at 'tags.1.member.key' failed to satisfy " + + "constraint: Member must have length less than or equal to 128", e.getMessage()); + } + + @Test + void valueTooLong_smithyLengthMessage() { + AwsException e = assertThrows(AwsException.class, + () -> SesTags.validate(List.of(tag("k", "v".repeat(257))))); + assertEquals("1 validation error detected: Value at 'tags.1.member.value' failed to satisfy " + + "constraint: Member must have length less than or equal to 256", e.getMessage()); + } + + @Test + void multipleLengthViolations_aggregatedValueThenKey() { + AwsException e = assertThrows(AwsException.class, + () -> SesTags.validate(List.of(tag("k".repeat(129), "v".repeat(257))))); + assertEquals("2 validation errors detected: Value at 'tags.1.member.value' failed to satisfy " + + "constraint: Member must have length less than or equal to 256; Value at " + + "'tags.1.member.key' failed to satisfy constraint: Member must have length less than " + + "or equal to 128", e.getMessage()); + } + + @Test + void charsetBeatsLength() { + // key too long AND value has an invalid char -> the character-set error wins (probe-confirmed). + AwsException e = assertThrows(AwsException.class, + () -> SesTags.validate(List.of(tag("k".repeat(129), "bad!")))); + assertEquals("Tags can only contain letters, numbers, spaces, and the following special " + + "characters: _ . : / = + - @", e.getMessage()); + } + + @Test + void maxCountBeatsDuplicate() { + List tags = new ArrayList<>(); + tags.add(tag("dup", "1")); + tags.add(tag("dup", "2")); + for (int i = 0; i < 49; i++) { + tags.add(tag("k" + i, "v")); + } + AwsException e = assertThrows(AwsException.class, () -> SesTags.validate(tags)); + assertEquals("Maximum of 50 user tags are allowed per resource, consider reducing the number " + + "of tags in the request", e.getMessage()); + } + + @Test + void emptyKey_rejectedWithNullMessage() { + AwsException e = assertThrows(AwsException.class, () -> SesTags.validate(List.of(tag("", "v")))); + assertEquals("BadRequestException", e.getErrorCode()); + assertNull(e.getMessage()); + } + + @Test + void awsPrefixKey_rejectedAsSystemTag() { + AwsException e = assertThrows(AwsException.class, + () -> SesTags.validate(List.of(tag("aws:foo", "v")))); + assertEquals("BadRequestException", e.getErrorCode()); + assertEquals("Caller is an end user and not allowed to mutate system tags", e.getMessage()); + } + + @Test + void awsPrefixKey_isCaseInsensitive() { + AwsException e = assertThrows(AwsException.class, + () -> SesTags.validate(List.of(tag("AWS:foo", "v")))); + assertEquals("Caller is an end user and not allowed to mutate system tags", e.getMessage()); + } + + @Test + void awsPrefix_onlyAppliesToKey_valueAndNoColonAreAllowed() { + // A value starting with aws: is allowed; only a key with the reserved prefix is rejected. + assertDoesNotThrow(() -> SesTags.validate(List.of(tag("k", "aws:foo")))); + // "aws" / "awsfoo" without the colon are ordinary keys. + assertDoesNotThrow(() -> SesTags.validate(List.of(tag("aws", "v")))); + assertDoesNotThrow(() -> SesTags.validate(List.of(tag("awsfoo", "v")))); + } + + @Test + void charsetBeatsAwsPrefix() { + // aws: reserved key AND an invalid char in the value -> the character-set error wins. + AwsException e = assertThrows(AwsException.class, + () -> SesTags.validate(List.of(tag("aws:foo", "bad!")))); + assertEquals("Tags can only contain letters, numbers, spaces, and the following special " + + "characters: _ . : / = + - @", e.getMessage()); + } + + @Test + void validateMergedCount_overFifty_usesMergeMessage() { + AwsException e = assertThrows(AwsException.class, () -> SesTags.validateMergedCount(51)); + assertEquals("BadRequestException", e.getErrorCode()); + assertEquals("Maximum of 50 user tags are allowed per resource, consider reducing the number " + + "of tags in the request or delete existing tags and retry", e.getMessage()); + } + + @Test + void validateMergedCount_fiftyOrFewer_ok() { + assertDoesNotThrow(() -> SesTags.validateMergedCount(50)); + assertDoesNotThrow(() -> SesTags.validateMergedCount(0)); + } + + @Test + void unicodeWhitespace_isAllowed() { + // U+00A0 (no-break space) is a Unicode separator (\p{Z}), not the ASCII space. AWS accepts it + // in both key and value (probe-confirmed), so the charset rule must allow it. + assertDoesNotThrow(() -> SesTags.validate(List.of(tag("a\u00A0b", "c\u00A0d")))); + } + + @Test + void length_countsUnicodeCodePointsNotUtf16Units() { + // U+1D400 (MATHEMATICAL BOLD CAPITAL A) is a letter (\p{L}, charset-allowed) that occupies one + // code point but two UTF-16 code units. Smithy length constraints count code points, so 128 of + // them is exactly the key limit (would be 256 UTF-16 units). + String cp = "\uD835\uDC00"; + assertDoesNotThrow(() -> SesTags.validate(List.of(tag(cp.repeat(128), "v")))); + AwsException e = assertThrows(AwsException.class, + () -> SesTags.validate(List.of(tag(cp.repeat(129), "v")))); + assertEquals("1 validation error detected: Value at 'tags.1.member.key' failed to satisfy " + + "constraint: Member must have length less than or equal to 128", e.getMessage()); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesTagsV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesTagsV2IntegrationTest.java index ab9df0b659..fce4297cc0 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesTagsV2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesTagsV2IntegrationTest.java @@ -530,4 +530,115 @@ class SesTagsV2IntegrationTest { .body("Tags[0].Key", equalTo("owner")) .body("Tags[0].Value", equalTo("alice")); } + + @Test + @Order(20) + void tagResource_mergeExceedingFifty_returns400() { + // AWS enforces the 50-tag ceiling on the MERGED (existing + incoming) set, and updating an + // existing key (net count unchanged) is allowed. Probe-confirmed against real AWS. + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + {"ConfigurationSetName": "tag-cs-merge"} + """) + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(200); + + String arn = "arn:aws:ses:us-east-1:000000000000:configuration-set/tag-cs-merge"; + + // Seed with the full 50 tags. + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body("{\"ResourceArn\": \"" + arn + "\", \"Tags\": " + tagArray(0, 50) + "}") + .when() + .post("/v2/email/tags") + .then() + .statusCode(200); + + // Updating an existing key keeps the count at 50 and is accepted. + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body("{\"ResourceArn\": \"" + arn + "\", \"Tags\": [{\"Key\": \"k0\", \"Value\": \"updated\"}]}") + .when() + .post("/v2/email/tags") + .then() + .statusCode(200); + + // Adding one new key would make 51 — rejected with the merge-specific message. + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body("{\"ResourceArn\": \"" + arn + "\", \"Tags\": [{\"Key\": \"extra\", \"Value\": \"v\"}]}") + .when() + .post("/v2/email/tags") + .then() + .statusCode(400) + .body("__type", equalTo("BadRequestException")) + .body("message", equalTo("Maximum of 50 user tags are allowed per resource, consider " + + "reducing the number of tags in the request or delete existing tags and retry")); + } + + @Test + @Order(21) + void tagResource_reservedAwsPrefixKey_returns400() { + String arn = "arn:aws:ses:us-east-1:000000000000:configuration-set/tag-cs-1"; + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body("{\"ResourceArn\": \"" + arn + "\", \"Tags\": [{\"Key\": \"aws:foo\", \"Value\": \"v\"}]}") + .when() + .post("/v2/email/tags") + .then() + .statusCode(400) + .body("__type", equalTo("BadRequestException")) + .body("message", equalTo("Caller is an end user and not allowed to mutate system tags")); + } + + @Test + @Order(22) + void createEmailIdentity_invalidInlineTags_isAtomic_identityNotPersisted() { + // A semantic tag error must fail CreateEmailIdentity before the identity is persisted, so a + // corrected retry does not hit AlreadyExistsException. Validation is now applied to the parsed + // tag list up front, before verifyEmailIdentity/verifyDomainIdentity. + String id = "atomic-tag-id@example.com"; + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + {"EmailIdentity": "%s", "Tags": [ + {"Key": "dup", "Value": "1"}, + {"Key": "dup", "Value": "2"} + ]} + """.formatted(id)) + .when() + .post("/v2/email/identities") + .then() + .statusCode(400) + .body("__type", equalTo("BadRequestException")) + .body("message", equalTo("Cannot provide multiple tags with the same key")); + + // The failed create must leave no identity behind. + given() + .header("Authorization", AUTH_HEADER) + .when() + .get("/v2/email/identities/" + id) + .then() + .statusCode(404); + } + + private static String tagArray(int from, int to) { + StringBuilder sb = new StringBuilder("["); + for (int i = from; i < to; i++) { + if (i > from) { + sb.append(","); + } + sb.append("{\"Key\": \"k").append(i).append("\", \"Value\": \"v\"}"); + } + return sb.append("]").toString(); + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesTemplateV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesTemplateV2IntegrationTest.java index 3d1402ad0b..391b180353 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesTemplateV2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesTemplateV2IntegrationTest.java @@ -633,6 +633,37 @@ class SesTemplateV2IntegrationTest { .body("Tags.find { it.Key == 'team' }.Value", equalTo("platform")); } + @Test + @Order(24) + void createTemplate_invalidTags_returns400_andNotPersisted() { + // Route-level coverage: CreateEmailTemplate must reject an invalid tag set (here duplicate + // keys) atomically, before the template is persisted. + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "TemplateName": "v2-badtags", + "TemplateContent": {"Subject": "S", "Text": "T"}, + "Tags": [{"Key": "dup", "Value": "1"}, {"Key": "dup", "Value": "2"}] + } + """) + .when() + .post("/v2/email/templates") + .then() + .statusCode(400) + .body("__type", equalTo("BadRequestException")) + .body("message", equalTo("Cannot provide multiple tags with the same key")); + + // The failed create must leave no template behind. + given() + .header("Authorization", AUTH_HEADER) + .when() + .get("/v2/email/templates/v2-badtags") + .then() + .statusCode(404); + } + @ParameterizedTest(name = "{0}") @MethodSource("malformedSendEmailBodies") @Order(50)