fix(ses): check the TemplateArn of a v1 templated send as SES does (#9131)

SendTemplatedEmail and SendBulkTemplatedEmail now check TemplateArn with the
parser the v2 sends use: partition, service, region and resource type each
answer their own "Invalid template ARN" message, a value without "arn:" and an
empty template name are rejected, and the account is not checked. The v1-only
parser that looked for ":template/" is removed, and the name/ARN alignment
check moves into SesTemplateService for both protocols; the v2 controller's
remap keeps its BadRequestException.

Both v1 sends require Template whatever TemplateArn holds, as the model does,
and answer "Template cannot be empty" ahead of the ARN checks;
SendBulkTemplatedEmail treats a whitespace-only Template as empty. The Java and
Python compatibility tests build the template ARN in the client's partition and
region.
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-11-21 09:37:57 -04:00
1 parent 9b19f17fe7
commit 2b9ed9cd55
9 files changed
+288 -57

No files matched your search

@@ -648,13 +648,10 @@ public class SesQueryHandler {
String templateArn = getParam(params, "TemplateArn");
String templateDataRaw = getParam(params, "TemplateData");
boolean hasName = templateName != null && !templateName.isBlank();
boolean hasArn = templateArn != null && !templateArn.isBlank();
if (!hasName && !hasArn) {
throw new AwsException("InvalidParameterValue",
"Template or TemplateArn is required.", 400);
if (templateName == null) {
throw SmithyValidationErrors.validationError(SmithyValidationErrors.notNullWithoutValue("template"));
}
String resolvedName = hasName ? templateName : SesTemplateService.templateNameFromArn(templateArn);
String resolvedName = sendTemplateName(templateName.isEmpty(), templateName, templateArn, region);
JsonNode templateData = parseTemplateData(templateDataRaw);
String configurationSetName = getParam(params, "ConfigurationSetName");
@@ -786,7 +783,7 @@ public class SesQueryHandler {
// 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) {
if (templateName == null) {
violations.add(SmithyValidationErrors.notNullWithoutValue("template"));
}
if (!params.containsKey("Destinations") && !hasParamWithPrefix(params, "Destinations.")) {
@@ -812,13 +809,7 @@ public class SesQueryHandler {
List<String> replyToAddresses = extractMembers(params, "ReplyToAddresses");
String defaultDataRaw = getParam(params, "DefaultTemplateData");
boolean hasName = templateName != null && !templateName.isBlank();
boolean hasArn = templateArn != null && !templateArn.isBlank();
if (!hasName && !hasArn) {
throw new AwsException("InvalidParameterValue",
"Template or TemplateArn is required.", 400);
}
String resolvedName = hasName ? templateName : SesTemplateService.templateNameFromArn(templateArn);
String resolvedName = sendTemplateName(templateName.isBlank(), templateName, templateArn, region);
int lastIndex = destinations.last();
if (lastIndex == 0) {
@@ -1738,6 +1729,19 @@ public class SesQueryHandler {
}
}
// The templated sends require Template whatever TemplateArn holds, word an empty one ahead of
// the ARN checks, and take a TemplateArn only beside the Template it names (probe-confirmed).
// SendBulkTemplatedEmail counts a whitespace-only Template as empty; SendTemplatedEmail does not.
private static String sendTemplateName(boolean empty, String templateName, String templateArn, String region) {
if (empty) {
throw new AwsException("InvalidParameterValue", "Template cannot be empty", 400);
}
if (templateArn != null && !templateArn.isBlank()) {
SesTemplateService.requireNameAlignsWithArn(templateName, templateArn, region);
}
return templateName;
}
private String getParam(MultivaluedMap<String, String> params, String name) {
return params.getFirst(name);
}
@@ -451,16 +451,12 @@ public class SesSendController {
}
String storedName(String region) {
return hasName() ? name : SesTemplateService.sendTemplateNameFromArn(arn, region);
return hasName() ? name : SesTemplateService.templateNameFromArn(arn, region);
}
// AWS accepts a TemplateName beside a TemplateArn when both name the same template, and
// reports a malformed ARN first (probe-confirmed on SendEmail and SendBulkEmail).
void requireNameAlignsWithArn(String region) {
if (hasName() && hasArn()
&& !name.equals(SesTemplateService.sendTemplateNameFromArn(arn, region))) {
throw new AwsException("BadRequestException", "The SES resource '" + name
+ "' does not align with the given ARN '" + arn + "'.", 400);
if (hasName() && hasArn()) {
SesTemplateService.requireNameAlignsWithArn(name, arn, region);
}
}
}
@@ -237,34 +237,11 @@ public class SesTemplateService implements SesTaggable {
}
/**
* Extracts the template name from an SES template ARN of the form
* {@code arn:aws:ses:<region>:<account>:template/<name>}. Region and
* account segments are not validated; only the {@code template/<name>}
* suffix is required.
* The template a send's {@code TemplateArn} names, on v1 and v2 alike. SES checks the ARN's
* partition, service, region and resource type, each with a message of its own, and not its
* account (probe-confirmed). The order among several mismatches at once is not probed.
*/
public static String templateNameFromArn(String arn) {
if (arn == null || arn.isBlank()) {
throw new AwsException("InvalidParameterValue", "TemplateArn is required.", 400);
}
int marker = arn.indexOf(":template/");
if (!AwsArnUtils.looksLikeArn(arn) || marker < 0) {
throw new AwsException("InvalidParameterValue",
"TemplateArn is not a valid SES template ARN: " + arn, 400);
}
String name = arn.substring(marker + ":template/".length());
if (name.isEmpty()) {
throw new AwsException("InvalidParameterValue",
"TemplateArn is missing a template name: " + arn, 400);
}
return name;
}
/**
* The template a v2 send's {@code TemplateArn} names. SES checks the ARN's partition, service,
* region and resource type, each with a message of its own, and not its account
* (probe-confirmed). The order among several mismatches at once is not probed.
*/
public static String sendTemplateNameFromArn(String arn, String region) {
public static String templateNameFromArn(String arn, String region) {
if (!AwsArnUtils.looksLikeArn(arn)) {
throw new AwsException("InvalidParameterValue", "ARNs must start with 'arn:': " + arn, 400);
}
@@ -295,6 +272,17 @@ public class SesTemplateService implements SesTaggable {
return name;
}
/**
* A send may name its template beside a {@code TemplateArn} only when both name the same
* template; a malformed ARN is reported first (probe-confirmed on both protocols).
*/
public static void requireNameAlignsWithArn(String name, String arn, String region) {
if (!name.equals(templateNameFromArn(arn, region))) {
throw new AwsException("InvalidParameterValue", "The SES resource '" + name
+ "' does not align with the given ARN '" + arn + "'.", 400);
}
}
private static AwsException invalidTemplateArn(String arn, String reason) {
return new AwsException("InvalidParameterValue", "Invalid template ARN <" + arn + ">. " + reason, 400);
}