mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ses): read every SES v2 request body by the same rule (#6385)
Every SES v2 operation reads its body through readOptionBody, now also CreateEmailIdentity, CreateEmailTemplate, UpdateEmailTemplate, CreateCustomVerificationEmailTemplate, UpdateCustomVerificationEmailTemplate and TagResource, which read a body that is not a JSON object as one without members. Only an empty body is read as one without members; a whitespace-only body, malformed JSON and anything but whitespace after the object are a SerializationException without a message, as AWS answers them. The hand-rolled copies in the tenant, contact, account, identity, import job, send, dedicated IP, suppression and template controllers, which answered malformed JSON with a BadRequestException carrying Jackson's message, are gone, and so is the import job controller's own strict reader. The operations that answer an empty body with "Request body is required." no longer give that answer for a whitespace-only one.
This commit is contained in:
1 parent
42f5019231
commit
0acb8cd44c
17 files changed
+245
-218
No files matched your search
+4
-1
@@ -187,11 +187,14 @@ class SesImportJobV2IntegrationTest {
|
||||
"ImportDestination":{"SuppressionListDestination":{"SuppressionListImportAction":"PUT"}}} garbage
|
||||
""".formatted(BUCKET);
|
||||
|
||||
for (String body : new String[] {badFormat, bothDestinations, noDataSource, trailingTokens}) {
|
||||
for (String body : new String[] {badFormat, bothDestinations, noDataSource}) {
|
||||
given().contentType("application/json").header("Authorization", AUTH).body(body)
|
||||
.when().post("/v2/email/import-jobs").then().statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"));
|
||||
}
|
||||
given().contentType("application/json").header("Authorization", AUTH).body(trailingTokens)
|
||||
.when().post("/v2/email/import-jobs").then().statusCode(400)
|
||||
.body("__type", equalTo("SerializationException"));
|
||||
given().contentType("application/json").header("Authorization", AUTH)
|
||||
.when().post("/v2/email/import-jobs").then().statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"));
|
||||
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2SerializationErrors.assertError;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2SerializationErrors.assertSerializationException;
|
||||
|
||||
/**
|
||||
* The create and update operations that read their body member by member refuse a body that is
|
||||
* not a JSON object before any member is read, as AWS's deserializer does, while a blank body is
|
||||
* still read as one without members.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class SesNonObjectBodyIntegrationTest {
|
||||
|
||||
private static final List<Arguments> ENDPOINTS = List.of(
|
||||
Arguments.of("POST", "/v2/email/identities"),
|
||||
Arguments.of("POST", "/v2/email/templates"),
|
||||
Arguments.of("PUT", "/v2/email/templates/non-object-ghost"),
|
||||
Arguments.of("POST", "/v2/email/custom-verification-email-templates"),
|
||||
Arguments.of("PUT", "/v2/email/custom-verification-email-templates/non-object-ghost"),
|
||||
Arguments.of("POST", "/v2/email/tags"));
|
||||
|
||||
static Stream<Arguments> nonObjectBodyCases() {
|
||||
return ENDPOINTS.stream().flatMap(endpoint -> SesV2SerializationErrors.nonObjectBodies()
|
||||
.map(body -> Arguments.of(endpoint.get()[0], endpoint.get()[1], body.get()[0], body.get()[1])));
|
||||
}
|
||||
|
||||
static Stream<Arguments> blankBodyCases() {
|
||||
return Stream.of(
|
||||
Arguments.of("POST", "/v2/email/identities", "EmailIdentity is required."),
|
||||
Arguments.of("POST", "/v2/email/templates", "TemplateName is required."),
|
||||
Arguments.of("PUT", "/v2/email/templates/non-object-ghost",
|
||||
"Template must have at least a subject, text, or html part."),
|
||||
Arguments.of("POST", "/v2/email/custom-verification-email-templates", "TemplateName is required."),
|
||||
Arguments.of("PUT", "/v2/email/custom-verification-email-templates/non-object-ghost",
|
||||
"FromEmailAddress is required."),
|
||||
Arguments.of("POST", "/v2/email/tags", "Request body is required."));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0} {1} {2}")
|
||||
@MethodSource("nonObjectBodyCases")
|
||||
void nonObjectBody_isASerializationException(String method, String path, String body, String message) {
|
||||
assertSerializationException(method, path, body, message);
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0} {1}")
|
||||
@MethodSource("blankBodyCases")
|
||||
void blankBody_isReadAsOneWithoutMembers(String method, String path, String message) {
|
||||
assertError(method, path, "", "BadRequestException", message);
|
||||
}
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
package io.github.hectorvent.floci.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.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2SerializationErrors.AUTH;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2SerializationErrors.assertSerializationException;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
|
||||
/**
|
||||
* How every SES v2 operation reads its request body, as probed on AWS (us-west-2, 2026-10-24) with
|
||||
* ListTenants, CreateConfigurationSet and CreateEmailTemplate: only an empty body is read as one
|
||||
* without members, while a whitespace-only body, malformed JSON and a token after the object are a
|
||||
* SerializationException without a message. The endpoints cover each controller that reads a body.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class SesV2BodyReadIntegrationTest {
|
||||
|
||||
private static final String IDENTITY = "/v2/email/identities/body-read-ghost.example.com";
|
||||
|
||||
private static final List<Arguments> ENDPOINTS = List.of(
|
||||
Arguments.of("POST", "/v2/email/tenants/list"),
|
||||
Arguments.of("POST", "/v2/email/tenants"),
|
||||
Arguments.of("POST", "/v2/email/configuration-sets"),
|
||||
Arguments.of("POST", "/v2/email/templates"),
|
||||
Arguments.of("POST", "/v2/email/templates/body-read-ghost/render"),
|
||||
Arguments.of("POST", "/v2/email/contact-lists"),
|
||||
Arguments.of("POST", "/v2/email/contact-lists/body-read-ghost/contacts/list"),
|
||||
Arguments.of("POST", "/v2/email/account/details"),
|
||||
Arguments.of("PUT", IDENTITY + "/dkim/signing"),
|
||||
Arguments.of("PUT", IDENTITY + "/configuration-set"),
|
||||
Arguments.of("PUT", IDENTITY + "/mail-from"),
|
||||
Arguments.of("POST", IDENTITY + "/policies/body-read-policy"),
|
||||
Arguments.of("POST", "/v2/email/import-jobs"),
|
||||
Arguments.of("POST", "/v2/email/import-jobs/list"),
|
||||
Arguments.of("POST", "/v2/email/outbound-emails"),
|
||||
Arguments.of("POST", "/v2/email/dedicated-ip-pools"),
|
||||
Arguments.of("PUT", "/v2/email/suppression/addresses"),
|
||||
Arguments.of("POST", "/v2/email/tags"));
|
||||
|
||||
static Stream<Arguments> malformedBodyCases() {
|
||||
return ENDPOINTS.stream().flatMap(endpoint -> Stream.of("{", "{\"a\":}", "{} x", "{}{}")
|
||||
.map(body -> Arguments.of(endpoint.get()[0], endpoint.get()[1], body)));
|
||||
}
|
||||
|
||||
static Stream<Arguments> endpoints() {
|
||||
return ENDPOINTS.stream();
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0} {1} {2}")
|
||||
@MethodSource("malformedBodyCases")
|
||||
void malformedBody_isASerializationExceptionWithoutMessage(String method, String path, String body) {
|
||||
assertSerializationException(method, path, body, null);
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0} {1}")
|
||||
@MethodSource("endpoints")
|
||||
void whitespaceOnlyBody_isASerializationExceptionWithoutMessage(String method, String path) {
|
||||
assertSerializationException(method, path, " ", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyBody_isReadAsOneWithoutMembers() {
|
||||
listTenants("");
|
||||
// Reaches the missing-name validation rather than failing to read the body.
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH)
|
||||
.body("")
|
||||
.when()
|
||||
.post("/v2/email/configuration-sets")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whitespaceAfterTheObject_isAccepted() {
|
||||
listTenants("{} ");
|
||||
}
|
||||
|
||||
private static void listTenants(String body) {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH)
|
||||
.body(body)
|
||||
.when()
|
||||
.post("/v2/email/tenants/list")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Tenants", notNullValue());
|
||||
}
|
||||
}
|
||||
@@ -116,16 +116,18 @@ class SesV2JsonTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void readOptionBody_blankIsEmptyObject_badShapesAre400() {
|
||||
void readOptionBody_onlyEmptyIsEmptyObject_unreadableBodiesAreSerializationExceptions() {
|
||||
assertTrue(SesV2Json.readOptionBody(MAPPER, null).isObject());
|
||||
assertTrue(SesV2Json.readOptionBody(MAPPER, " ").isObject());
|
||||
assertTrue(SesV2Json.readOptionBody(MAPPER, "").isObject());
|
||||
assertTrue(SesV2Json.readOptionBody(MAPPER, " {} \n").isObject());
|
||||
assertEquals("REQUIRE", SesV2Json.readOptionBody(MAPPER, "{\"HttpsPolicy\":\"REQUIRE\"}")
|
||||
.path("HttpsPolicy").asText());
|
||||
assertEquals("Start of list found where not expected", assertAws("SerializationException", 400,
|
||||
() -> SesV2Json.readOptionBody(MAPPER, "[1]")).getMessage());
|
||||
assertNull(assertAws("SerializationException", 400,
|
||||
() -> SesV2Json.readOptionBody(MAPPER, "null")).getMessage());
|
||||
assertAws("BadRequestException", 400, () -> SesV2Json.readOptionBody(MAPPER, "{not json"));
|
||||
for (String body : List.of("null", " ", "{not json", "{", "{\"a\":}", "{} x", "{}{}")) {
|
||||
assertNull(assertAws("SerializationException", 400,
|
||||
() -> SesV2Json.readOptionBody(MAPPER, body)).getMessage(), body);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user