mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ses): check a template's tags before its name and content (#9041)
CreateEmailTemplate validated the tags last, after the template name and content. SES checks them first, as probed on us-west-2: the tag count, character set, lengths, an empty key and duplicate keys are all refused ahead of a blank name or a missing subject or part. The reserved aws: key prefix is the exception. SES refuses it after the name and the content, before it looks the name up, and words it "Invalid tags" on this operation.
This commit is contained in:
1 parent
f08cc57437
commit
c91740de85
5 files changed
+225
-10
No files matched your search
@@ -52,9 +52,45 @@ final class SesTags {
|
||||
* prefix reserved. A {@code null}/empty list is a no-op.
|
||||
*/
|
||||
static void validate(List<Tag> tags) {
|
||||
validate(tags, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link #validate} without the reserved {@code aws:} prefix, for an operation that refuses such
|
||||
* a key at another point and in its own words (see {@link #requireNoSystemKeys}).
|
||||
*/
|
||||
static void validateIgnoringSystemKeys(List<Tag> tags) {
|
||||
validate(tags, false);
|
||||
}
|
||||
|
||||
private static void validate(List<Tag> tags, boolean refuseSystemKeys) {
|
||||
if (tags == null || tags.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
requireCountAndCharset(tags);
|
||||
// The aws: key prefix is checked after the character set (probe-confirmed).
|
||||
if (refuseSystemKeys) {
|
||||
requireNoSystemKeys(tags, SYSTEM_TAG_MESSAGE);
|
||||
}
|
||||
requireLengthsAndDistinctKeys(tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Refuses a key with the reserved {@code aws:} prefix (case-insensitive). Only the key is checked,
|
||||
* a value starting with {@code aws:} is allowed.
|
||||
*/
|
||||
static void requireNoSystemKeys(List<Tag> tags, String message) {
|
||||
if (tags == null) {
|
||||
return;
|
||||
}
|
||||
for (Tag tag : tags) {
|
||||
if (tag != null && tag.key() != null && tag.key().regionMatches(true, 0, "aws:", 0, 4)) {
|
||||
throw new AwsException("BadRequestException", message, 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireCountAndCharset(List<Tag> tags) {
|
||||
if (tags.size() > MAX_TAGS) {
|
||||
throw new AwsException("BadRequestException", MAX_TAGS_MESSAGE, 400);
|
||||
}
|
||||
@@ -64,13 +100,9 @@ final class SesTags {
|
||||
throw new AwsException("BadRequestException", CHARSET_MESSAGE, 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireLengthsAndDistinctKeys(List<Tag> tags) {
|
||||
// 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.
|
||||
|
||||
@@ -47,7 +47,7 @@ import java.util.regex.Pattern;
|
||||
* {@link SecureRandom} (no static instance, so no native-image run-time-init registration).
|
||||
*
|
||||
* <p>Tag validation is a shared cross-resource concern, so {@code createTemplate} calls
|
||||
* {@link SesTags#validate} rather than depending back on the facade.
|
||||
* {@link SesTags} rather than depending back on the facade.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class SesTemplateService implements SesTaggable {
|
||||
@@ -85,9 +85,13 @@ public class SesTemplateService implements SesTaggable {
|
||||
}
|
||||
|
||||
public EmailTemplate createTemplate(EmailTemplate template, String region, String accountId) {
|
||||
requirePresent(template);
|
||||
// SES checks the tags before the template itself, except for a reserved key, which it
|
||||
// refuses after the template, before it looks the name up, and in words of its own.
|
||||
SesTags.validateIgnoringSystemKeys(template.getTags());
|
||||
validateTemplate(template);
|
||||
dropEmptyParts(template);
|
||||
SesTags.validate(template.getTags());
|
||||
SesTags.requireNoSystemKeys(template.getTags(), "Invalid tags");
|
||||
String key = templateKey(region, template.getTemplateName());
|
||||
if (templateStore.get(key).isPresent()) {
|
||||
throw new AwsException("AlreadyExists",
|
||||
@@ -179,10 +183,14 @@ public class SesTemplateService implements SesTaggable {
|
||||
templateStore.put(templateKey(region, template.getTemplateName()), template);
|
||||
}
|
||||
|
||||
private static void validateTemplate(EmailTemplate template) {
|
||||
private static void requirePresent(EmailTemplate template) {
|
||||
if (template == null) {
|
||||
throw new AwsException("InvalidTemplate", "Template is required.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateTemplate(EmailTemplate template) {
|
||||
requirePresent(template);
|
||||
validateTemplateName(template.getTemplateName());
|
||||
// SES asks for the subject first, then for a part, and a blank value counts as missing.
|
||||
if (template.getSubject() == null || template.getSubject().isBlank()) {
|
||||
|
||||
Reference in new issue
Block a user