mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(ses): enforce full AWS tag validation across all SES resources (#2504)
* fix(ses): enforce full AWS tag validation across all SES resources The shared SES tag validator only checked a single tag's key/value length, so CreateConfigurationSet, CreateEmailTemplate, CreateEmailIdentity, CreateContactList, TagResource, and tenant tagging all silently accepted invalid tag sets. Add SesTags.validate(List<Tag>) matching probe-confirmed AWS SES v2 behavior: at most 50 tags, unique keys, key 1-128 / value 0-256 code points, both key and value restricted to letters, numbers, Unicode whitespace, and _ . : / = + - @, and the aws: key prefix (case-insensitive) reserved for system tags. Lengths are counted by Unicode code point (Smithy semantics), not UTF-16 code units. TagResource enforces the 50-tag limit on the merged (existing + incoming) set, allowing updates to existing keys. Messages and evaluation order mirror AWS; all violations are BadRequestException / 400. Route every SES tag entry point through the validator, and validate CreateEmailIdentity's tags before the identity is persisted so an invalid tag set fails atomically instead of leaving the identity behind. Update the integration tests that encoded the old non-AWS behavior (an omitted tag value now returns 400 like real AWS). * test(ses): cover CreateEmailTemplate invalid-tag rejection Route-level coverage that CreateEmailTemplate rejects an invalid tag set (duplicate keys) atomically without persisting the template. The service-level SesTags call was otherwise only exercised through SesTagsTest, so a regression removing it could pass unnoticed.
This commit is contained in:
1 parent
55edfd9412
commit
29f6e7ac6b
11 files changed
+480
-42
No files matched your search
@@ -77,6 +77,7 @@ public class SesContactService {
|
||||
public ContactList createContactList(String name, String description, List<Topic> topics,
|
||||
List<Tag> tags, String region) {
|
||||
validateContactListInput(name, description, topics);
|
||||
SesTags.validate(tags);
|
||||
ContactList list = new ContactList(name);
|
||||
list.setDescription(description);
|
||||
list.setTopics(topics);
|
||||
|
||||
@@ -123,6 +123,9 @@ public class SesController {
|
||||
// without leaving a half-created identity behind, matching AWS and the ConfigurationSetName
|
||||
// pre-check below.
|
||||
List<Tag> 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.
|
||||
|
||||
@@ -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<Tag> 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<Tag> 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",
|
||||
|
||||
@@ -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}.
|
||||
*
|
||||
* <p>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<Tag> 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<String> 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<String> 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;
|
||||
}
|
||||
}
|
||||
@@ -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",
|
||||
|
||||
Reference in new issue
Block a user