fix(ses): answer invalid v1 bulk destination entries on their own (#6742)

SendBulkTemplatedEmail rejected the whole call for an entry with more
than 50 recipients, for template data that is not a JSON object, and
for a tag with an empty name. AWS answers each of these with a 200 and
that entry's InvalidParameterValue ("Recipient count exceeds 50.",
"The template data is invalid", "The tag name must be specified."),
while the other entries still send. DefaultTemplateData that is not a
JSON object, or a DefaultTags tag with an empty name, fails every
entry. Within one entry an address syntax error wins over an empty tag
name, which wins over invalid template data, which wins over the
recipient count.

A DefaultTags or ReplacementTags tag without a Name or a Value is a
model violation, and a skipped tag index is a tag with neither, listed
in the 400 ValidationError after the top-level members. A missing or
empty DefaultTemplateData is a 400 InvalidParameterValue, "The default
template data does not exist".

The handler marks such an entry with its message, and the send answers
it in the per-entry loop after the address syntax. The recipient limit
stays in the service: per entry for v1, whole call for v2 SendBulkEmail,
which was not probed.
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-10-27 11:43:25 -04:00
1 parent e8ba4b27e9
commit afdd4b46ec
7 files changed
+341 -55

No files matched your search

@@ -773,8 +773,9 @@ public class SesQueryHandler {
String templateName = getParam(params, "Template");
String templateArn = getParam(params, "TemplateArn");
BulkDestinationIndexes destinations = bulkDestinationIndexes(params);
// The model validation answers ahead of every other check of the request, listing its
// violations in the model's member order (probe-confirmed).
// The model validation answers ahead of every other check of the request. It lists the
// top-level members before the nested ones ('template' ahead of 'defaultTags.1.member.name'),
// each group in the model's member order (probe-confirmed).
List<String> violations = new ArrayList<>();
if (templateName == null && templateArn == null) {
violations.add(SmithyValidationErrors.notNullWithoutValue("template"));
@@ -782,12 +783,15 @@ public class SesQueryHandler {
if (!params.containsKey("Destinations") && !hasParamWithPrefix(params, "Destinations.")) {
violations.add(SmithyValidationErrors.notNullWithoutValue("destinations"));
}
addMessageTagViolations(params, "DefaultTags", "defaultTags", violations);
// A skipped index, like an entry without a Destination, is a null destination
// (probe-confirmed). An empty address list still sends its bare key.
for (int i = 1; i <= Math.min(destinations.last(), MAX_VALIDATED_DESTINATION_INDEX); i++) {
if (!destinations.withDestination().contains(i)) {
violations.add(SmithyValidationErrors.notNullWithoutValue("destinations." + i + ".member.destination"));
}
addMessageTagViolations(params, "Destinations.member." + i + ".ReplacementTags",
"destinations." + i + ".member.replacementTags", violations);
}
if (!violations.isEmpty()) {
throw SmithyValidationErrors.validationError(violations);
@@ -815,22 +819,40 @@ public class SesQueryHandler {
// AWS counts the destinations before it looks the template up (probe-confirmed). The list
// runs to its highest index, so counting that index first also bounds the scan below.
SesService.requireBulkDestinationCount(lastIndex);
// Probe-confirmed for an absent and an empty value; where it falls among the checks above
// is unprobed.
if (defaultDataRaw == null || defaultDataRaw.isEmpty()) {
throw new AwsException("InvalidParameterValue", "The default template data does not exist", 400);
}
JsonNode defaultTemplateData = templateDataObject(defaultDataRaw);
List<MessageTag> defaultEmailTags = readMessageTags(params, "DefaultTags");
List<BulkEmailEntry> entries = new ArrayList<>();
for (int i = 1; i <= lastIndex; i++) {
String destPrefix = "Destinations.member." + i;
List<String> to = extractMembers(params, destPrefix + ".Destination.ToAddresses");
List<String> cc = extractMembers(params, destPrefix + ".Destination.CcAddresses");
List<String> bcc = extractMembers(params, destPrefix + ".Destination.BccAddresses");
String replacementRaw = getParam(params, destPrefix + ".ReplacementTemplateData");
List<MessageTag> replacementTags = extractMessageTags(params, destPrefix + ".ReplacementTags");
JsonNode replacementData = templateDataObject(getParam(params, destPrefix + ".ReplacementTemplateData"));
List<MessageTag> replacementTags = readMessageTags(params, destPrefix + ".ReplacementTags");
// Each of these fails its own entry, with a 200 for the call; invalid default data or a
// blank default tag name fails every entry. A blank tag name wins over invalid data and
// an address syntax error over both (probe-confirmed).
String rejection = null;
if (hasBlankName(defaultEmailTags) || hasBlankName(replacementTags)) {
rejection = "The tag name must be specified.";
} else if (defaultTemplateData == null || replacementData == null) {
rejection = "The template data is invalid";
}
entries.add(new BulkEmailEntry(to, cc, bcc,
parseTemplateData(replacementRaw), replacementTags, List.of()));
replacementData == null ? objectMapper.createObjectNode() : replacementData,
replacementTags, List.of(), rejection));
}
if (defaultTemplateData == null) {
defaultTemplateData = objectMapper.createObjectNode();
}
JsonNode defaultTemplateData = parseTemplateData(defaultDataRaw);
String configurationSetName = getParam(params, "ConfigurationSetName");
String returnPath = getParam(params, "ReturnPath");
List<MessageTag> defaultEmailTags = extractMessageTags(params, "DefaultTags");
List<BulkEmailEntryResult> results = sesService.sendBulkEmail(SendBulkEmailRequest.builder()
.source(source)
.sourceArn(getParam(params, "SourceArn"))
@@ -840,6 +862,7 @@ public class SesQueryHandler {
.configurationSetName(configurationSetName)
.defaultEmailTags(defaultEmailTags)
.region(region)
.recipientLimitPerEntry(true)
.defaultContent(new EmailContent.Template(resolvedName, defaultTemplateData, List.of()))
.entries(entries)
.build());
@@ -1581,8 +1604,52 @@ public class SesQueryHandler {
return node;
}
// null for data that is not a JSON object, which a bulk send answers per entry.
private JsonNode templateDataObject(String raw) {
if (raw == null || raw.isBlank()) {
return objectMapper.createObjectNode();
}
try {
JsonNode node = objectMapper.readTree(raw);
return node.isObject() ? node : null;
} catch (JsonProcessingException expected) {
// Malformed JSON is the same per-entry failure as a non-object value.
return null;
}
}
// --- Helpers ---
// A skipped index is a tag with neither member, so it lists both (probe-confirmed).
private static void addMessageTagViolations(MultivaluedMap<String, String> params, String prefix,
String path, List<String> violations) {
int last = Math.min(lastMemberIndex(params, prefix + ".member."), MAX_VALIDATED_TAG_INDEX);
for (int i = 1; i <= last; i++) {
if (params.getFirst(prefix + ".member." + i + ".Name") == null) {
violations.add(SmithyValidationErrors.notNullWithoutValue(path + "." + i + ".member.name"));
}
if (params.getFirst(prefix + ".member." + i + ".Value") == null) {
violations.add(SmithyValidationErrors.notNullWithoutValue(path + "." + i + ".member.value"));
}
}
}
private static int lastMemberIndex(MultivaluedMap<String, String> params, String prefix) {
int last = 0;
for (String key : params.keySet()) {
if (key.startsWith(prefix)) {
int dot = key.indexOf('.', prefix.length());
last = Math.max(last, memberIndex(
dot < 0 ? key.substring(prefix.length()) : key.substring(prefix.length(), dot)));
}
}
return last;
}
private static boolean hasBlankName(List<MessageTag> tags) {
return tags.stream().anyMatch(tag -> tag.name() == null || tag.name().isBlank());
}
private static boolean hasParamWithPrefix(MultivaluedMap<String, String> params, String prefix) {
return params.keySet().stream().anyMatch(key -> key.startsWith(prefix));
}
@@ -1591,6 +1658,9 @@ public class SesQueryHandler {
// destination count. Where AWS stops listing null destinations has not been probed.
private static final int MAX_VALIDATED_DESTINATION_INDEX = 1000;
// Bounds the violation list in the same way; a tag past it is read only up to the first gap.
private static final int MAX_VALIDATED_TAG_INDEX = 1000;
/** The highest {@code Destinations.member.N}, and the Ns that carry a {@code Destination}. */
private record BulkDestinationIndexes(int last, Set<Integer> withDestination) {}
@@ -1639,18 +1709,24 @@ public class SesQueryHandler {
* no members are present.
*/
private List<MessageTag> extractMessageTags(MultivaluedMap<String, String> params, String prefix) {
List<MessageTag> tags = readMessageTags(params, prefix);
if (hasBlankName(tags)) {
throw new AwsException("InvalidParameterValue",
"The tag name must be specified.", 400);
}
return tags;
}
private List<MessageTag> readMessageTags(MultivaluedMap<String, String> params, String prefix) {
List<MessageTag> tags = new ArrayList<>();
for (int i = 1; ; i++) {
String name = getParam(params, prefix + ".member." + i + ".Name");
String value = getParam(params, prefix + ".member." + i + ".Value");
if (name == null && value == null) break;
if (name == null || name.isBlank()) {
throw new AwsException("InvalidParameterValue",
"The tag name must be specified.", 400);
if (name == null && value == null) {
return tags;
}
tags.add(new MessageTag(name, value));
}
return tags;
}
private String getParam(MultivaluedMap<String, String> params, String name) {
@@ -1700,14 +1700,14 @@ public class SesService {
? requireSendingTenant(request.tenantName(), request.region(), null, () -> null)
: null;
configSetService.validateForSending(request.configurationSetName(), request.region());
for (BulkEmailEntry entry : request.entries()) {
int recipientCount = sizeOf(entry.toAddresses())
+ sizeOf(entry.ccAddresses())
+ sizeOf(entry.bccAddresses());
if (recipientCount > MAX_RECIPIENTS_PER_DESTINATION) {
throw new AwsException("MessageRejected",
"Recipient count (" + recipientCount + ") in a destination exceeds the maximum of "
+ MAX_RECIPIENTS_PER_DESTINATION + ".", 400);
if (!request.recipientLimitPerEntry()) {
for (BulkEmailEntry entry : request.entries()) {
int recipientCount = recipientCount(entry);
if (recipientCount > MAX_RECIPIENTS_PER_DESTINATION) {
throw new AwsException("MessageRejected",
"Recipient count (" + recipientCount + ") in a destination exceeds the maximum of "
+ MAX_RECIPIENTS_PER_DESTINATION + ".", 400);
}
}
}
@@ -1734,7 +1734,15 @@ public class SesService {
List<BulkEmailEntryResult> results = new ArrayList<>(request.entries().size());
for (BulkEmailEntry entry : request.entries()) {
try {
// An address syntax error wins over the entry's other failures (probe-confirmed on v1).
SesAddressSyntax.requireBulkDestination(entry, request.replyToAddresses());
if (entry.rejection() != null) {
throw new AwsException("InvalidParameterValue", entry.rejection(), 400);
}
if (request.recipientLimitPerEntry() && recipientCount(entry) > MAX_RECIPIENTS_PER_DESTINATION) {
throw new AwsException("InvalidParameterValue",
"Recipient count exceeds " + MAX_RECIPIENTS_PER_DESTINATION + ".", 400);
}
// An entry with no recipient beside entries with one fails on its own (probe-confirmed
// on v1 and v2).
if (!anyBulkRecipient(List.of(entry))) {
@@ -1802,6 +1810,10 @@ public class SesService {
return emailTags == null ? List.of() : emailTags;
}
private static int recipientCount(BulkEmailEntry entry) {
return sizeOf(entry.toAddresses()) + sizeOf(entry.ccAddresses()) + sizeOf(entry.bccAddresses());
}
private static int sizeOf(List<?> list) {
return list == null ? 0 : list.size();
}
@@ -5,6 +5,10 @@ import io.quarkus.runtime.annotations.RegisterForReflection;
import java.util.List;
/**
* One destination of a bulk send. A non-null {@code rejection} is the {@code InvalidParameterValue}
* message the caller already found for this entry; the send answers the entry with it alone.
*/
@RegisterForReflection
public record BulkEmailEntry(
List<String> toAddresses,
@@ -12,5 +16,14 @@ public record BulkEmailEntry(
List<String> bccAddresses,
JsonNode replacementTemplateData,
List<MessageTag> replacementEmailTags,
List<MessageHeader> replacementHeaders
) {}
List<MessageHeader> replacementHeaders,
String rejection
) {
public BulkEmailEntry(List<String> toAddresses, List<String> ccAddresses, List<String> bccAddresses,
JsonNode replacementTemplateData, List<MessageTag> replacementEmailTags,
List<MessageHeader> replacementHeaders) {
this(toAddresses, ccAddresses, bccAddresses, replacementTemplateData, replacementEmailTags,
replacementHeaders, null);
}
}
@@ -9,7 +9,9 @@ import java.util.Objects;
* default content is a stored {@link EmailContent.Template}, which the send resolves only after its
* sender checks, or an {@link EmailContent.InlineTemplate}; its data and headers are the defaults
* each entry's replacements merge over. {@code sourceArn} and {@code returnPathArn} are the
* sending-authorization identity ARNs of the sender and the return path.
* sending-authorization identity ARNs of the sender and the return path. With
* {@code recipientLimitPerEntry} an entry over the recipient limit fails on its own, as on v1
* (probe-confirmed); without it the whole call is rejected.
*/
public record SendBulkEmailRequest(
String source,
@@ -21,6 +23,7 @@ public record SendBulkEmailRequest(
List<MessageTag> defaultEmailTags,
String tenantName,
String region,
boolean recipientLimitPerEntry,
EmailContent.TemplateContent defaultContent,
List<BulkEmailEntry> entries) {
@@ -49,6 +52,7 @@ public record SendBulkEmailRequest(
private List<MessageTag> defaultEmailTags = List.of();
private String tenantName;
private String region;
private boolean recipientLimitPerEntry;
private EmailContent.TemplateContent defaultContent;
private List<BulkEmailEntry> entries = List.of();
@@ -64,12 +68,14 @@ public record SendBulkEmailRequest(
public Builder defaultEmailTags(List<MessageTag> defaultEmailTags) { this.defaultEmailTags = defaultEmailTags; return this; }
public Builder tenantName(String tenantName) { this.tenantName = tenantName; return this; }
public Builder region(String region) { this.region = region; return this; }
public Builder recipientLimitPerEntry(boolean recipientLimitPerEntry) { this.recipientLimitPerEntry = recipientLimitPerEntry; return this; }
public Builder defaultContent(EmailContent.TemplateContent defaultContent) { this.defaultContent = defaultContent; return this; }
public Builder entries(List<BulkEmailEntry> entries) { this.entries = entries; return this; }
public SendBulkEmailRequest build() {
return new SendBulkEmailRequest(source, sourceArn, replyToAddresses, returnPath, returnPathArn,
configurationSetName, defaultEmailTags, tenantName, region, defaultContent, entries);
configurationSetName, defaultEmailTags, tenantName, region, recipientLimitPerEntry, defaultContent,
entries);
}
}
}