mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ses): validate template names the way SES does (#8996)
* fix(ses): validate template names the way SES does
This commit is contained in:
1 parent
573cb9e9b7
commit
a8cb033a98
6 files changed
+362
-15
No files matched your search
@@ -0,0 +1,227 @@
|
||||
package io.floci.aws.services.ses;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
/**
|
||||
* The template name rules, as probed on us-west-2: blank, then longer than 64 characters, then a
|
||||
* character outside ASCII letters, digits, '_' and '-'. Create, update, get, delete and test-render
|
||||
* all answer them, on both APIs, before the name is looked up; the other lookups do not apply them.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class SesTemplateNameIntegrationTest {
|
||||
|
||||
private static final String V1_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/email/aws4_request";
|
||||
private static final String V2_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/ses/aws4_request";
|
||||
private static final String BLANK = "The template name must be specified.";
|
||||
private static final String TOO_LONG = "Template name cannot exceed 64 characters.";
|
||||
|
||||
private static String notAllowed(String name) {
|
||||
return "Template name <" + name + ">: only alphanumeric ASCII characters, '_', and '-' are allowed.";
|
||||
}
|
||||
|
||||
static Stream<Arguments> refusedNames() {
|
||||
return Stream.of(
|
||||
Arguments.of(" ", BLANK),
|
||||
Arguments.of("a".repeat(65), TOO_LONG),
|
||||
Arguments.of("name-probe ", notAllowed("name-probe ")),
|
||||
Arguments.of(" name-probe", notAllowed(" name-probe")),
|
||||
Arguments.of("name probe", notAllowed("name probe")),
|
||||
Arguments.of("name.probe", notAllowed("name.probe")),
|
||||
Arguments.of("name:probe", notAllowed("name:probe")),
|
||||
Arguments.of("name-probé", notAllowed("name-probé")));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("refusedNames")
|
||||
void v1_everyOperationRefusesTheName(String name, String message) {
|
||||
for (String action : new String[] {"CreateTemplate", "UpdateTemplate"}) {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded; charset=UTF-8")
|
||||
.header("Authorization", V1_AUTH)
|
||||
.formParam("Action", action)
|
||||
.formParam("Template.TemplateName", name)
|
||||
.formParam("Template.SubjectPart", "S")
|
||||
.formParam("Template.TextPart", "t")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"))
|
||||
.body("ErrorResponse.Error.Message", equalTo(message));
|
||||
}
|
||||
for (String action : new String[] {"GetTemplate", "DeleteTemplate", "TestRenderTemplate"}) {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded; charset=UTF-8")
|
||||
.header("Authorization", V1_AUTH)
|
||||
.formParam("Action", action)
|
||||
.formParam("TemplateName", name)
|
||||
.formParam("TemplateData", "{}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"))
|
||||
.body("ErrorResponse.Error.Message", equalTo(message));
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("refusedNames")
|
||||
void v2_everyOperationRefusesTheName(String name, String message) {
|
||||
given()
|
||||
.contentType("application/json; charset=UTF-8")
|
||||
.header("Authorization", V2_AUTH)
|
||||
.body("{\"TemplateName\": \"" + name + "\", \"TemplateContent\": {\"Subject\": \"S\", \"Text\": \"t\"}}")
|
||||
.when()
|
||||
.post("/v2/email/templates")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo(message));
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", V2_AUTH)
|
||||
.body("{\"TemplateContent\": {\"Subject\": \"S\", \"Text\": \"t\"}}")
|
||||
.when()
|
||||
.put("/v2/email/templates/{name}", name)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo(message));
|
||||
|
||||
given()
|
||||
.header("Authorization", V2_AUTH)
|
||||
.when()
|
||||
.get("/v2/email/templates/{name}", name)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo(message));
|
||||
|
||||
given()
|
||||
.header("Authorization", V2_AUTH)
|
||||
.when()
|
||||
.delete("/v2/email/templates/{name}", name)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo(message));
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", V2_AUTH)
|
||||
.body("{\"TemplateData\": \"{}\"}")
|
||||
.when()
|
||||
.post("/v2/email/templates/{name}/render", name)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo(message));
|
||||
}
|
||||
|
||||
// probed: the name is checked before the subject
|
||||
@Test
|
||||
void theNameIsCheckedBeforeTheContent() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", V1_AUTH)
|
||||
.formParam("Action", "CreateTemplate")
|
||||
.formParam("Template.TemplateName", "name probe")
|
||||
.formParam("Template.TextPart", "t")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Message", equalTo(notAllowed("name probe")));
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", V2_AUTH)
|
||||
.body("{\"TemplateName\": \"name probe\", \"TemplateContent\": {\"Text\": \"t\"}}")
|
||||
.when()
|
||||
.post("/v2/email/templates")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(notAllowed("name probe")));
|
||||
}
|
||||
|
||||
// probed: v1 writes a control character of the name as 0x0009, v2 sends it as it came
|
||||
@Test
|
||||
void v1SpellsOutAControlCharacterOfTheName() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", V1_AUTH)
|
||||
.formParam("Action", "GetTemplate")
|
||||
.formParam("TemplateName", "name\tprobe\u0001")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Message", equalTo(notAllowed("name0x0009probe0x0001")));
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", V2_AUTH)
|
||||
.body("{\"TemplateName\": \"name\\tprobe\", \"TemplateContent\": {\"Subject\": \"S\", \"Text\": \"t\"}}")
|
||||
.when()
|
||||
.post("/v2/email/templates")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(notAllowed("name\tprobe")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void a64CharacterNameOfEveryAllowedKindIsAccepted() {
|
||||
String name = "Name_probe-0123456789" + "a".repeat(43);
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", V1_AUTH)
|
||||
.formParam("Action", "CreateTemplate")
|
||||
.formParam("Template.TemplateName", name)
|
||||
.formParam("Template.SubjectPart", "S")
|
||||
.formParam("Template.TextPart", "t")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("Authorization", V2_AUTH)
|
||||
.when()
|
||||
.get("/v2/email/templates/{name}", name)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("TemplateName", equalTo(name));
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", V1_AUTH)
|
||||
.formParam("Action", "GetTemplate")
|
||||
.formParam("TemplateName", name)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<TemplateName>" + name + "</TemplateName>"));
|
||||
|
||||
given()
|
||||
.header("Authorization", V2_AUTH)
|
||||
.when()
|
||||
.delete("/v2/email/templates/{name}", name)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
}
|
||||
@@ -11,9 +11,11 @@ import io.floci.aws.services.ses.model.Tag;
|
||||
import io.floci.aws.testing.MutableClock;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.time.Clock;
|
||||
@@ -150,9 +152,92 @@ class SesTemplateServiceTest {
|
||||
assertEquals(" ", service.getTemplate("blank-text", REGION).getTextPart());
|
||||
}
|
||||
|
||||
static Stream<Arguments> refusedNames() {
|
||||
String charset = ">: only alphanumeric ASCII characters, '_', and '-' are allowed.";
|
||||
return Stream.of(
|
||||
Arguments.of(null, "The template name must be specified."),
|
||||
Arguments.of("", "The template name must be specified."),
|
||||
Arguments.of(" ", "The template name must be specified."),
|
||||
Arguments.of("\t", "The template name must be specified."),
|
||||
Arguments.of("\u3000", "The template name must be specified."),
|
||||
Arguments.of("a".repeat(65), "Template name cannot exceed 64 characters."),
|
||||
// probed: the length is checked before the characters
|
||||
Arguments.of("a".repeat(64) + " ", "Template name cannot exceed 64 characters."),
|
||||
Arguments.of("welcome ", "Template name <welcome " + charset),
|
||||
Arguments.of(" welcome", "Template name < welcome" + charset),
|
||||
Arguments.of("wel come", "Template name <wel come" + charset),
|
||||
Arguments.of("wel.come", "Template name <wel.come" + charset),
|
||||
Arguments.of("wel:come", "Template name <wel:come" + charset),
|
||||
Arguments.of("wel/come", "Template name <wel/come" + charset),
|
||||
Arguments.of("welcome\n", "Template name <welcome\n" + charset),
|
||||
Arguments.of("welcom\u00e9", "Template name <welcom\u00e9" + charset),
|
||||
Arguments.of("welcome\uff11", "Template name <welcome\uff11" + charset),
|
||||
// probed: a no-break space is not blank, so it is a character that is not allowed
|
||||
Arguments.of("\u00a0", "Template name <\u00a0" + charset));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("refusedNames")
|
||||
void everyOperation_refusesTheName(String name, String message) {
|
||||
List<Executable> operations = List.of(
|
||||
() -> service.createTemplate(template(name), REGION, ACCOUNT),
|
||||
() -> service.updateTemplate(template(name), REGION),
|
||||
() -> service.getTemplate(name, REGION),
|
||||
() -> service.deleteTemplate(name, REGION),
|
||||
() -> service.renderTestTemplate(name, "{}", REGION, ACCOUNT));
|
||||
for (Executable operation : operations) {
|
||||
AwsException e = assertThrows(AwsException.class, operation);
|
||||
assertEquals("InvalidParameterValue", e.getErrorCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// probed: the name is checked before the subject
|
||||
@Test
|
||||
void create_rejectsBlankName() {
|
||||
assertThrows(AwsException.class, () -> service.createTemplate(template(" "), REGION, ACCOUNT));
|
||||
void create_refusesTheNameBeforeTheContent() {
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> service.createTemplate(new EmailTemplate("wel come", null, null, null), REGION, ACCOUNT));
|
||||
assertEquals("Template name <wel come>: only alphanumeric ASCII characters, '_', and '-' are allowed.",
|
||||
e.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"-", "_", "0123456789", "Welcome_Mail-2"})
|
||||
void create_acceptsLettersDigitsUnderscoreAndHyphen(String name) {
|
||||
service.createTemplate(template(name), REGION, ACCOUNT);
|
||||
assertEquals(name, service.getTemplate(name, REGION).getTemplateName());
|
||||
}
|
||||
|
||||
// The sends, the tag operations and the tenant association were not probed with such a name:
|
||||
// they keep answering that the template is missing.
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"wel.come", "wel come", " welcome"})
|
||||
void lookupsOutsideTheTemplateOperations_treatARefusedNameAsMissing(String name) {
|
||||
assertTrue(service.find(name, REGION).isEmpty());
|
||||
|
||||
AwsException tags = assertThrows(AwsException.class, () -> service.listTags(name, REGION));
|
||||
assertEquals("NotFoundException", tags.getErrorCode());
|
||||
assertEquals("No Template present with name: " + name, tags.getMessage());
|
||||
|
||||
EmailContent.Template content = new EmailContent.Template(name, MAPPER.createObjectNode(), List.of());
|
||||
AwsException send = assertThrows(AwsException.class, () -> service.inline(content, REGION));
|
||||
assertEquals("TemplateDoesNotExist", send.getErrorCode());
|
||||
assertEquals("Template " + name + " does not exist.", send.getMessage());
|
||||
}
|
||||
|
||||
// probed with an unknown name and bad data; the name rules come ahead of both
|
||||
@Test
|
||||
void renderTestTemplate_refusesTheNameBeforeTheData() {
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> service.renderTestTemplate("wel come", null, REGION, ACCOUNT));
|
||||
assertEquals("InvalidParameterValue", e.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void create_acceptsA64CharacterName() {
|
||||
String name = "a".repeat(64);
|
||||
service.createTemplate(template(name), REGION, ACCOUNT);
|
||||
assertEquals(name, service.getTemplate(name, REGION).getTemplateName());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -279,7 +279,7 @@ class SesTemplateV1IntegrationTest {
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>InvalidTemplate</Code>"));
|
||||
.body(containsString("<Code>InvalidParameterValue</Code>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user