fix(ses): validate template names the way SES does (#8996)

* fix(ses): validate template names the way SES does
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-11-19 11:43:00 -04:00
1 parent 573cb9e9b7
commit a8cb033a98
6 files changed
+362 -15

No files matched your search

@@ -185,10 +185,31 @@ public class SesQueryHandler {
"Operation " + action + " is not supported by SES.", AwsNamespaces.SES, 400);
};
} catch (AwsException e) {
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.SES, e.getHttpStatus());
return AwsQueryResponse.error(e.getErrorCode(), spellOutControlCharacters(e.getMessage()),
AwsNamespaces.SES, e.getHttpStatus());
}
}
/**
* SES writes a control character a message quotes as {@code 0x0009}, which also keeps the
* characters XML 1.0 forbids out of the error document.
*/
static String spellOutControlCharacters(String message) {
if (message == null || message.chars().noneMatch(c -> c < ' ')) {
return message;
}
StringBuilder out = new StringBuilder(message.length() + 8);
for (int i = 0; i < message.length(); i++) {
char c = message.charAt(i);
if (c < ' ') {
out.append(String.format(Locale.ROOT, "0x%04x", (int) c));
} else {
out.append(c);
}
}
return out.toString();
}
private Response handleVerifyEmailIdentity(MultivaluedMap<String, String> params, String region) {
String emailAddress = getParam(params, "EmailAddress");
identityService.verifyEmailIdentity(emailAddress, region);
@@ -658,9 +679,6 @@ public class SesQueryHandler {
private Response handleTestRenderTemplate(MultivaluedMap<String, String> params, String region) {
String templateName = getParam(params, "TemplateName");
if (templateName == null || templateName.isBlank()) {
throw new AwsException("InvalidParameterValue", "TemplateName is required.", 400);
}
String templateDataRaw = getParam(params, "TemplateData");
String rendered = templateService.renderTestTemplate(templateName, templateDataRaw, region,
regionResolver.getAccountId());
@@ -66,7 +66,7 @@ public class SesTemplateController {
try {
JsonNode request = readOptionBody(objectMapper, body);
String templateName = request.path("TemplateName").asText(null);
if (templateName == null || templateName.isBlank()) {
if (templateName == null || templateName.isEmpty()) {
throw new AwsException("BadRequestException", "TemplateName is required.", 400);
}
EmailTemplate template = parseTemplateContent(templateName, request.path("TemplateContent"));
@@ -60,6 +60,8 @@ 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.
@@ -102,13 +104,18 @@ public class SesTemplateService implements SesTaggable {
}
public EmailTemplate getTemplate(String templateName, String region) {
validateTemplateName(templateName);
return stored(templateName, region);
}
private EmailTemplate stored(String templateName, String region) {
return templateStore.get(templateKey(region, templateName))
.orElseThrow(() -> new AwsException("TemplateDoesNotExist",
"Template " + templateName + " does not exist.", 400));
}
EmailContent.InlineTemplate inline(EmailContent.Template template, String region) {
EmailTemplate stored = getTemplate(template.templateName(), region);
EmailTemplate stored = stored(template.templateName(), region);
return new EmailContent.InlineTemplate(stored.getSubject(), stored.getTextPart(),
stored.getHtmlPart(), template.templateData(), template.headers());
}
@@ -131,6 +138,7 @@ public class SesTemplateService implements SesTaggable {
}
public void deleteTemplate(String templateName, String region) {
validateTemplateName(templateName);
String key = templateKey(region, templateName);
if (templateStore.get(key).isEmpty()) {
throw new AwsException("TemplateDoesNotExist",
@@ -203,19 +211,27 @@ 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
* 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.
*/
private static void validateTemplateName(String templateName) {
if (templateName == null || templateName.isBlank()) {
throw new AwsException("InvalidTemplate", "TemplateName is required.", 400);
throw new AwsException("InvalidParameterValue", "The template name must be specified.", 400);
}
if (Character.isWhitespace(templateName.charAt(0))
|| Character.isWhitespace(templateName.charAt(templateName.length() - 1))) {
throw new AwsException("InvalidTemplate",
"TemplateName must not contain leading or trailing whitespace.", 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);
}
}
private static String templateKey(String region, String templateName) {
validateTemplateName(templateName);
return "template::" + region + "::" + templateName;
}
@@ -278,6 +294,7 @@ public class SesTemplateService implements SesTaggable {
public String renderTestTemplate(String templateName, String templateDataRaw, String region,
String accountId) {
validateTemplateName(templateName);
// The rendering data is read before the template is looked up: both wrong answers the data error.
JsonNode templateData = parseRenderingData(objectMapper, templateDataRaw);
// TestRenderTemplate words a missing template its own way, unlike the template CRUD.