diff --git a/src/main/java/io/floci/aws/services/ses/SesConfigurationSetService.java b/src/main/java/io/floci/aws/services/ses/SesConfigurationSetService.java index d0eb88d18e..cb2f12fac3 100644 --- a/src/main/java/io/floci/aws/services/ses/SesConfigurationSetService.java +++ b/src/main/java/io/floci/aws/services/ses/SesConfigurationSetService.java @@ -55,12 +55,8 @@ public class SesConfigurationSetService implements SesTaggable { private static final Logger LOG = Logger.getLogger(SesConfigurationSetService.class); - private static final Pattern CONFIG_SET_NAME = Pattern.compile("^[A-Za-z0-9_-]+$"); - private static final int MAX_CONFIG_SET_NAME_LENGTH = 64; private static final Set VDM_FEATURE_STATES = Set.of("ENABLED", "DISABLED"); - private static final Pattern EVENT_DESTINATION_NAME_CHARS = Pattern.compile("^[A-Za-z0-9_-]+$"); - private static final int MAX_EVENT_DESTINATION_NAME_LENGTH = 64; private static final String INVALID_SNS_DESTINATION = "InvalidSNSDestination"; private static final String EVENT_BUS_RESOURCE_PREFIX = "event-bus/"; private static final String DEFAULT_EVENT_BUS_RESOURCE = EVENT_BUS_RESOURCE_PREFIX + "default"; @@ -404,7 +400,7 @@ public class SesConfigurationSetService implements SesTaggable { /** For guards that must not trip the key derivation's name validation (the tenant gate). */ static boolean isValidName(String name) { - return name != null && name.length() <= MAX_CONFIG_SET_NAME_LENGTH && CONFIG_SET_NAME.matcher(name).matches(); + return name != null && SesNames.isValid(name); } // ──────────────────────── Domain-pure option setters ──────────────────────── @@ -619,15 +615,8 @@ public class SesConfigurationSetService implements SesTaggable { if (name == null || name.isBlank()) { throw new AwsException("InvalidParameterValue", "EventDestinationName is required.", 400); } - if (!EVENT_DESTINATION_NAME_CHARS.matcher(name).matches()) { - throw new AwsException("InvalidParameterValue", - "Invalid event destination name <" + name + ">: only alphanumeric ASCII characters, " - + "'_', and '-' are allowed.", 400); - } - if (name.length() > MAX_EVENT_DESTINATION_NAME_LENGTH) { - throw new AwsException("InvalidParameterValue", - "Event destination name cannot exceed 64 characters.", 400); - } + SesNames.requireAllowedCharacters(name, "InvalidParameterValue", "Invalid event destination name"); + SesNames.requireMaxLength(name, "InvalidParameterValue", "Event destination name"); } static void validateEventDestination(EventDestination dest, String accountId, String region) { @@ -818,15 +807,8 @@ public class SesConfigurationSetService implements SesTaggable { throw new AwsException("InvalidParameterValue", "The configuration set name must be specified.", 400); } - if (name.length() > MAX_CONFIG_SET_NAME_LENGTH) { - throw new AwsException("InvalidParameterValue", - "Configuration set name cannot exceed " + MAX_CONFIG_SET_NAME_LENGTH + " characters.", 400); - } - if (!CONFIG_SET_NAME.matcher(name).matches()) { - throw new AwsException("InvalidParameterValue", - "Invalid configuration set name <" + name + ">: only alphanumeric ASCII characters, " - + "'_', and '-' are allowed.", 400); - } + SesNames.requireMaxLength(name, "InvalidParameterValue", "Configuration set name"); + SesNames.requireAllowedCharacters(name, "InvalidParameterValue", "Invalid configuration set name"); } private static String configSetKey(String region, String name) { diff --git a/src/main/java/io/floci/aws/services/ses/SesContactService.java b/src/main/java/io/floci/aws/services/ses/SesContactService.java index 42ffa24371..2d3a720a4e 100644 --- a/src/main/java/io/floci/aws/services/ses/SesContactService.java +++ b/src/main/java/io/floci/aws/services/ses/SesContactService.java @@ -49,7 +49,6 @@ public class SesContactService implements SesTaggable { private static final Logger LOG = Logger.getLogger(SesContactService.class); private static final Set SUBSCRIPTION_STATUSES = Set.of("OPT_IN", "OPT_OUT"); - private static final Pattern CONTACT_LIST_NAME_CHARS = Pattern.compile("^[A-Za-z0-9_-]{1,64}$"); private static final Pattern EMAIL_PATTERN = Pattern.compile("^[^@\\s]+@[^@\\s]+\\.[^@\\s]+$"); private static final int MAX_TOPICS_PER_LIST = 20; private static final int MAX_DISPLAY_NAME_LENGTH = 128; @@ -231,7 +230,7 @@ public class SesContactService implements SesTaggable { if (name.isBlank()) { throw new AwsException("BadRequestException", "ContactListName can't be blank.", 400); } - if (!CONTACT_LIST_NAME_CHARS.matcher(name).matches()) { + if (!SesNames.isValid(name)) { throw new AwsException("BadRequestException", "ContactListName can contain up to 64 characters. Only alphanumeric characters, " + "underscores(_) and hyphens(-) are allowed.", 400); @@ -277,7 +276,7 @@ public class SesContactService implements SesTaggable { if (name.isBlank()) { throw new AwsException("BadRequestException", "ContactListName can't be blank.", 400); } - if (!CONTACT_LIST_NAME_CHARS.matcher(name).matches()) { + if (!SesNames.isValid(name)) { throw new AwsException("BadRequestException", "ContactListName can contain up to 64 characters. Only alphanumeric characters, " + "underscores(_) and hyphens(-) are allowed.", 400); @@ -292,7 +291,7 @@ public class SesContactService implements SesTaggable { if (t.getTopicName().isBlank()) { throw new AwsException("BadRequestException", "TopicName can't be blank.", 400); } - if (!CONTACT_LIST_NAME_CHARS.matcher(t.getTopicName()).matches()) { + if (!SesNames.isValid(t.getTopicName())) { throw new AwsException("BadRequestException", "TopicName can contain up to 64 characters. Only alphanumeric characters, " + "underscores(_) and hyphens(-) are allowed.", 400); diff --git a/src/main/java/io/floci/aws/services/ses/SesMetricsController.java b/src/main/java/io/floci/aws/services/ses/SesMetricsController.java index 6f28ee40a5..0545e05052 100644 --- a/src/main/java/io/floci/aws/services/ses/SesMetricsController.java +++ b/src/main/java/io/floci/aws/services/ses/SesMetricsController.java @@ -30,7 +30,6 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.regex.Pattern; import static io.floci.aws.services.ses.SesV2Json.readOptionBody; @@ -54,7 +53,6 @@ public class SesMetricsController { private static final int MAX_QUERIES = 10; private static final int MAX_DIMENSIONS = 3; - private static final Pattern NAME_CHARSET = Pattern.compile("[A-Za-z0-9_-]+"); private static final Duration MAX_INTERVAL = Duration.ofDays(60); private static final Duration WINDOW_SLACK = Duration.ofDays(1); @@ -302,7 +300,7 @@ public class SesMetricsController { throw new AwsException("BadRequestException", "Invalid email identity provided.", 400); } String isp = dimensions.get("ISP"); - if (isp != null && !NAME_CHARSET.matcher(isp).matches()) { + if (isp != null && !SesNames.hasOnlyAllowedCharacters(isp)) { throw new AwsException("BadRequestException", "Invalid ISP name provided.", 400); } requireName(dimensions.get("CONFIGURATION_SET"), "configuration set name", @@ -317,10 +315,7 @@ public class SesMetricsController { if (value.isEmpty()) { throw new AwsException("BadRequestException", blankMessage, 400); } - if (!NAME_CHARSET.matcher(value).matches()) { - throw new AwsException("BadRequestException", "Invalid " + noun + " <" + value - + ">: only alphanumeric ASCII characters, '_', and '-' are allowed.", 400); - } + SesNames.requireAllowedCharacters(value, "BadRequestException", "Invalid " + noun); } /** diff --git a/src/main/java/io/floci/aws/services/ses/SesNames.java b/src/main/java/io/floci/aws/services/ses/SesNames.java new file mode 100644 index 0000000000..fac2dd66b5 --- /dev/null +++ b/src/main/java/io/floci/aws/services/ses/SesNames.java @@ -0,0 +1,45 @@ +package io.floci.aws.services.ses; + +import io.floci.aws.core.common.AwsException; + +import java.util.regex.Pattern; + +/** + * The rule SES applies to the names of templates, configuration sets, event destinations, tenants, + * contact lists, topics and policies: at most {@value #MAX_LENGTH} characters, all of them ASCII + * letters, digits, '_' or '-'. The wording, the error code and the order of the two checks differ + * per name, so each caller passes its own and keeps its own blank check. + */ +final class SesNames { + + static final int MAX_LENGTH = 64; + + private static final Pattern ALLOWED_CHARACTERS = Pattern.compile("[A-Za-z0-9_-]+"); + + private SesNames() { + } + + static boolean hasOnlyAllowedCharacters(String name) { + return ALLOWED_CHARACTERS.matcher(name).matches(); + } + + /** Both halves of the rule at once, for a caller that answers them with one message or none. */ + static boolean isValid(String name) { + return name.length() <= MAX_LENGTH && hasOnlyAllowedCharacters(name); + } + + /** Refuses a name over the limit with {@code " cannot exceed 64 characters."}. */ + static void requireMaxLength(String name, String errorCode, String subject) { + if (name.length() > MAX_LENGTH) { + throw new AwsException(errorCode, subject + " cannot exceed " + MAX_LENGTH + " characters.", 400); + } + } + + /** Refuses a name with any other character, quoting it after {@code subject}. */ + static void requireAllowedCharacters(String name, String errorCode, String subject) { + if (!hasOnlyAllowedCharacters(name)) { + throw new AwsException(errorCode, subject + " <" + name + + ">: only alphanumeric ASCII characters, '_', and '-' are allowed.", 400); + } + } +} diff --git a/src/main/java/io/floci/aws/services/ses/SesPolicyService.java b/src/main/java/io/floci/aws/services/ses/SesPolicyService.java index 192965b5f7..ccbfbac7e6 100644 --- a/src/main/java/io/floci/aws/services/ses/SesPolicyService.java +++ b/src/main/java/io/floci/aws/services/ses/SesPolicyService.java @@ -14,7 +14,6 @@ import org.jboss.logging.Logger; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.regex.Pattern; /** * Identity sending-authorization policies (the {@code policyStore}), extracted from @@ -33,7 +32,6 @@ public class SesPolicyService { private static final Logger LOG = Logger.getLogger(SesPolicyService.class); static final int MAX_POLICIES_PER_IDENTITY = 20; - private static final Pattern POLICY_NAME_CHARS = Pattern.compile("[A-Za-z0-9_-]+"); private final StorageBackend policyStore; // Serializes the per-identity policy count check-then-put so concurrent creates can't both pass. @@ -178,7 +176,7 @@ public class SesPolicyService { "Value at 'policyName' failed to satisfy constraint: " + "Member must have length less than or equal to 64"); } - if (!POLICY_NAME_CHARS.matcher(policyName).matches()) { + if (!SesNames.hasOnlyAllowedCharacters(policyName)) { throw new AwsException("InvalidParameterValue", "PolicyName is invalid. Policy names must only include alpha-numeric characters, " + "dashes, and underscores.", 400); diff --git a/src/main/java/io/floci/aws/services/ses/SesTemplateService.java b/src/main/java/io/floci/aws/services/ses/SesTemplateService.java index 4103dab1a3..45d08442ca 100644 --- a/src/main/java/io/floci/aws/services/ses/SesTemplateService.java +++ b/src/main/java/io/floci/aws/services/ses/SesTemplateService.java @@ -60,8 +60,6 @@ public class SesTemplateService implements SesTaggable { "\\{\\{\\{\\s*([\\w-]+)\\s*\\}\\}\\}|\\{\\{\\s*([\\w-]+)\\s*\\}\\}"); private static final DateTimeFormatter RENDER_DATE = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSX", Locale.ROOT).withZone(ZoneOffset.UTC); - private static final int MAX_TEMPLATE_NAME_LENGTH = 64; - private static final Pattern TEMPLATE_NAME = Pattern.compile("[A-Za-z0-9_-]+"); private static final Pattern SUBJECT_WHITESPACE_RUN = Pattern.compile("[ \\t\\r\\n]+"); static final String UNSUBSCRIBE_PLACEHOLDER = "{{amazonSESUnsubscribeUrl}}"; // What TestRenderTemplate shows in place of the list-management link a send would carry. @@ -213,7 +211,7 @@ public class SesTemplateService implements SesTaggable { /** * The name rules create, update, get, delete and test-render apply before anything else, in - * SES's order: blank, then longer than {@value #MAX_TEMPLATE_NAME_LENGTH}, then a character + * SES's order: blank, then longer than {@value SesNames#MAX_LENGTH}, then a character * outside ASCII letters, digits, '_' and '-'. The sends, the tag operations and the tenant * association read a template without them: what SES answers there for such a name is not known. */ @@ -221,14 +219,8 @@ public class SesTemplateService implements SesTaggable { if (templateName == null || templateName.isBlank()) { throw new AwsException("InvalidParameterValue", "The template name must be specified.", 400); } - if (templateName.length() > MAX_TEMPLATE_NAME_LENGTH) { - throw new AwsException("InvalidParameterValue", - "Template name cannot exceed " + MAX_TEMPLATE_NAME_LENGTH + " characters.", 400); - } - if (!TEMPLATE_NAME.matcher(templateName).matches()) { - throw new AwsException("InvalidParameterValue", "Template name <" + templateName - + ">: only alphanumeric ASCII characters, '_', and '-' are allowed.", 400); - } + SesNames.requireMaxLength(templateName, "InvalidParameterValue", "Template name"); + SesNames.requireAllowedCharacters(templateName, "InvalidParameterValue", "Template name"); } private static String templateKey(String region, String templateName) { diff --git a/src/main/java/io/floci/aws/services/ses/SesTenantService.java b/src/main/java/io/floci/aws/services/ses/SesTenantService.java index bff8a0301f..0bf8c6c4d2 100644 --- a/src/main/java/io/floci/aws/services/ses/SesTenantService.java +++ b/src/main/java/io/floci/aws/services/ses/SesTenantService.java @@ -31,7 +31,6 @@ import java.util.Set; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.UnaryOperator; -import java.util.regex.Pattern; /** * SES v2 tenants (multi-tenancy), owning the {@code tenantStore}. The domain owns id/ARN generation, @@ -53,9 +52,6 @@ public class SesTenantService implements SesTaggable { private static final Logger LOG = Logger.getLogger(SesTenantService.class); - private static final int TENANT_NAME_MAX = 64; - private static final Pattern TENANT_NAME_CHARS = Pattern.compile("[A-Za-z0-9_-]+"); - // The wire values for ResourceType — the ARN segment, not the SDK enum spelling. Real AWS both // returns these and requires them as Filter values; the SDK's EMAIL_IDENTITY-style enum constants // are rejected by the service (probe-confirmed 2026-08-28). @@ -692,15 +688,8 @@ public class SesTenantService implements SesTaggable { if (name.isBlank()) { throw new AwsException("BadRequestException", "TenantName cannot be empty", 400); } - if (name.length() > TENANT_NAME_MAX) { - throw new AwsException("BadRequestException", - "TenantName cannot exceed " + TENANT_NAME_MAX + " characters.", 400); - } - if (!TENANT_NAME_CHARS.matcher(name).matches()) { - throw new AwsException("BadRequestException", - "Invalid tenant name <" + name + ">: only alphanumeric ASCII characters, '_', and " - + "'-' are allowed.", 400); - } + SesNames.requireMaxLength(name, "BadRequestException", "TenantName"); + SesNames.requireAllowedCharacters(name, "BadRequestException", "Invalid tenant name"); } private static AwsException tenantNotFound(String tenantName) { diff --git a/src/test/java/io/floci/aws/services/ses/SesNamesTest.java b/src/test/java/io/floci/aws/services/ses/SesNamesTest.java new file mode 100644 index 0000000000..443478dea8 --- /dev/null +++ b/src/test/java/io/floci/aws/services/ses/SesNamesTest.java @@ -0,0 +1,54 @@ +package io.floci.aws.services.ses; + +import io.floci.aws.core.common.AwsException; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class SesNamesTest { + + @Test + void hasOnlyAllowedCharacters_acceptsAsciiLettersDigitsUnderscoreAndHyphen() { + assertTrue(SesNames.hasOnlyAllowedCharacters("Name_probe-09")); + assertFalse(SesNames.hasOnlyAllowedCharacters("name probe")); + assertFalse(SesNames.hasOnlyAllowedCharacters("name.probe")); + assertFalse(SesNames.hasOnlyAllowedCharacters("name-probé")); + assertFalse(SesNames.hasOnlyAllowedCharacters("")); + } + + @Test + void isValid_appliesTheLengthAndTheCharacters() { + assertTrue(SesNames.isValid("a".repeat(64))); + assertFalse(SesNames.isValid("a".repeat(65))); + assertFalse(SesNames.isValid("a b")); + assertFalse(SesNames.isValid("")); + } + + @Test + void requireMaxLength_refusesTheSixtyFifthCharacterWithTheCallersWording() { + assertDoesNotThrow(() -> SesNames.requireMaxLength("a".repeat(64), "BadRequestException", "TenantName")); + + AwsException e = assertThrows(AwsException.class, + () -> SesNames.requireMaxLength("a".repeat(65), "BadRequestException", "TenantName")); + assertEquals("BadRequestException", e.getErrorCode()); + assertEquals("TenantName cannot exceed 64 characters.", e.getMessage()); + assertEquals(400, e.getHttpStatus()); + } + + @Test + void requireAllowedCharacters_quotesTheNameAfterTheCallersWording() { + assertDoesNotThrow( + () -> SesNames.requireAllowedCharacters("a_b-1", "InvalidParameterValue", "Template name")); + + AwsException e = assertThrows(AwsException.class, + () -> SesNames.requireAllowedCharacters("a b", "InvalidParameterValue", "Template name")); + assertEquals("InvalidParameterValue", e.getErrorCode()); + assertEquals("Template name : only alphanumeric ASCII characters, '_', and '-' are allowed.", + e.getMessage()); + assertEquals(400, e.getHttpStatus()); + } +}