mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(ses): coerce a v2 boolean string only when it names a boolean (#4887)
SES v2 accepts a JSON string for a boolean member only when it reads, case-insensitively, as true/yes/y/t/1 or false/no/n/f/0; any other string is a SerializationException. Floci turned every string into true, so UnsubscribeAll "false" stored true. Affects UnsubscribeAll, SendingEnabled and AutoWarmupEnabled.
This commit is contained in:
1 parent
ae216f22a0
commit
5b6c53d0df
9 files changed
+73
-26
No files matched your search
@@ -226,7 +226,7 @@ public class SesAccountController {
|
||||
JsonNode enabledNode = request.path("AutoWarmupEnabled");
|
||||
// AutoWarmupEnabled has a default of false: the SDK omits it when false, so a missing
|
||||
// member is treated as false rather than rejected. A present value goes through the
|
||||
// shared SES v2 boolean coercion (string→true, null/number/container→SerializationException).
|
||||
// shared SES v2 boolean coercion.
|
||||
boolean enabled = enabledNode.isMissingNode() ? false : coerceBoolean(enabledNode);
|
||||
accountService.setDedicatedIpAutoWarmup(region, enabled);
|
||||
LOG.infov("SES V2 PutAccountDedicatedIpWarmupAttributes: {0}", enabled);
|
||||
|
||||
+2
-2
@@ -238,8 +238,8 @@ public class SesConfigurationSetController {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
try {
|
||||
// Reuse the AWS-aligned SendingEnabled deserialization shared with CreateConfigurationSet:
|
||||
// absent -> false, string -> true, null/number -> SerializationException. An empty body
|
||||
// / {} therefore disables sending (200). Verified against real AWS.
|
||||
// absent -> false, otherwise SesV2Json.coerceBoolean. An empty body / {} therefore
|
||||
// disables sending (200). Verified against real AWS.
|
||||
boolean enabled = parseSendingEnabled(
|
||||
readOptionBody(objectMapper, body).path("SendingEnabled"));
|
||||
configSetService.setSendingEnabled(name, enabled, region);
|
||||
|
||||
@@ -315,8 +315,7 @@ public class SesContactController {
|
||||
}
|
||||
|
||||
// UnsubscribeAll is a Boolean; AWS coerces it the same way as any other SES v2 boolean
|
||||
// (see parseSendingEnabled): a JSON string coerces to true, a number/null/array/object is a
|
||||
// SerializationException. Absent leaves it unset.
|
||||
// (see coerceBoolean). Absent leaves it unset.
|
||||
private static Boolean parseUnsubscribeAll(JsonNode request) {
|
||||
if (!request.has("UnsubscribeAll")) {
|
||||
return null;
|
||||
|
||||
@@ -10,6 +10,7 @@ import io.github.hectorvent.floci.services.ses.model.Tag;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Helpers shared by the SES v2 REST JSON controllers: body and member shape checks that answer
|
||||
@@ -119,14 +120,20 @@ final class SesV2Json {
|
||||
return node.asText();
|
||||
}
|
||||
|
||||
// AWS-verified Jackson coercion for a SES v2 boolean field: a JSON string coerces to true,
|
||||
// while a number/null/array/object is a SerializationException.
|
||||
// AWS-verified coercion for a SES v2 boolean field: a JSON string coerces only when it names a
|
||||
// boolean (case-insensitive, untrimmed); any other string, a number, null, an array or an object
|
||||
// is a SerializationException.
|
||||
static boolean coerceBoolean(JsonNode node) {
|
||||
if (node.isBoolean()) {
|
||||
return node.booleanValue();
|
||||
}
|
||||
if (node.isTextual()) {
|
||||
return true;
|
||||
return switch (node.textValue().toLowerCase(Locale.ROOT)) {
|
||||
case "true", "yes", "y", "t", "1" -> true;
|
||||
case "false", "no", "n", "f", "0" -> false;
|
||||
default -> throw new AwsException("SerializationException",
|
||||
"STRING_VALUE can not be converted to an Boolean", 400);
|
||||
};
|
||||
}
|
||||
if (node.isNull()) {
|
||||
throw new AwsException("SerializationException", null, 400);
|
||||
@@ -241,9 +248,8 @@ final class SesV2Json {
|
||||
/**
|
||||
* Reproduces the AWS deserialization behavior for {@code SendingEnabled}
|
||||
* (verified against real AWS SES V2 on 2026-06-13): a missing member
|
||||
* defaults to {@code false}, any string coerces to {@code true}, and
|
||||
* explicit {@code null} or non-boolean scalars fail with
|
||||
* {@code SerializationException}.
|
||||
* defaults to {@code false}, and a present value goes through
|
||||
* {@link #coerceBoolean(JsonNode)}.
|
||||
*/
|
||||
static boolean parseSendingEnabled(JsonNode enabledNode) {
|
||||
if (enabledNode.isMissingNode()) {
|
||||
|
||||
+1
-1
@@ -193,7 +193,7 @@ class SesConfigurationSetSendingOptionsV2IntegrationTest {
|
||||
@Test
|
||||
@Order(13)
|
||||
void putSendingOptions_stringSendingEnabled_coercesToTrue() {
|
||||
// Real AWS coerces any string to true (200), matching CreateConfigurationSet. Verify the
|
||||
// Real AWS coerces "yes" to true (200), matching CreateConfigurationSet. Verify the
|
||||
// persisted value, not just the status — CS was disabled by @Order(12).
|
||||
given()
|
||||
.contentType("application/json")
|
||||
|
||||
+1
-1
@@ -458,7 +458,7 @@ class SesConfigurationSetV2IntegrationTest {
|
||||
@Test
|
||||
@Order(19)
|
||||
void createConfigurationSet_stringSendingEnabled_coercesToTrue() {
|
||||
// AWS accepts any string for SendingEnabled and stores true
|
||||
// AWS accepts "yes" for SendingEnabled and stores true
|
||||
// (verified against real AWS SES V2 on 2026-06-13).
|
||||
given()
|
||||
.contentType("application/json")
|
||||
|
||||
+18
-6
@@ -263,9 +263,9 @@ class SesContactV2IntegrationTest {
|
||||
.body("__type", equalTo("BadRequestException"));
|
||||
}
|
||||
|
||||
// Type coercion — verified against real AWS (Jackson-backed), consistent with parseSendingEnabled:
|
||||
// a JSON string coerces to the Boolean UnsubscribeAll (any string -> true), but a number is a
|
||||
// SerializationException; AttributesData (String) rejects non-string types the same way.
|
||||
// Type coercion, verified against real AWS: a JSON string coerces to the Boolean UnsubscribeAll
|
||||
// only when it names a boolean, and a number is a SerializationException; AttributesData
|
||||
// (String) rejects non-string types the same way.
|
||||
|
||||
@Test
|
||||
@Order(21)
|
||||
@@ -296,13 +296,25 @@ class SesContactV2IntegrationTest {
|
||||
|
||||
@Test
|
||||
@Order(24)
|
||||
void createContact_unsubscribeAllString_coercesToTrue() {
|
||||
// AWS coerces ANY JSON string to true for the Boolean field (unlike a number, which is rejected).
|
||||
post(CONTACTS, "{\"EmailAddress\":\"type-d@example.com\",\"UnsubscribeAll\":\"no\"}")
|
||||
void createContact_unsubscribeAllString_coercesByText() {
|
||||
post(CONTACTS, "{\"EmailAddress\":\"type-d@example.com\",\"UnsubscribeAll\":\"yes\"}")
|
||||
.then().statusCode(200);
|
||||
given().header("Authorization", SES_AUTH)
|
||||
.when().get(CONTACTS + "/type-d@example.com")
|
||||
.then().statusCode(200).body("UnsubscribeAll", equalTo(true));
|
||||
|
||||
given().contentType("application/json").header("Authorization", SES_AUTH)
|
||||
.body("{\"UnsubscribeAll\":\"no\"}")
|
||||
.when().put(CONTACTS + "/type-d@example.com")
|
||||
.then().statusCode(200);
|
||||
given().header("Authorization", SES_AUTH)
|
||||
.when().get(CONTACTS + "/type-d@example.com")
|
||||
.then().statusCode(200).body("UnsubscribeAll", equalTo(false));
|
||||
|
||||
post(CONTACTS, "{\"EmailAddress\":\"type-e@example.com\",\"UnsubscribeAll\":\"abc\"}")
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("SerializationException"))
|
||||
.body("message", equalTo("STRING_VALUE can not be converted to an Boolean"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+18
-3
@@ -195,17 +195,32 @@ class SesDedicatedIpV2IntegrationTest {
|
||||
|
||||
@Test
|
||||
@Order(15)
|
||||
void putAccountAutoWarmup_stringCoercesToTrue() {
|
||||
// AWS's Jackson layer coerces a JSON string to true for SES v2 booleans.
|
||||
void putAccountAutoWarmup_stringCoercesByText() {
|
||||
// SES v2 coerces a JSON string only when it names a boolean.
|
||||
given().contentType("application/json").header("Authorization", AUTH_HEADER)
|
||||
.body("{\"AutoWarmupEnabled\": \"nonsense\"}")
|
||||
.body("{\"AutoWarmupEnabled\": \"no\"}")
|
||||
.when().put("/v2/email/account/dedicated-ips/warmup")
|
||||
.then().statusCode(200);
|
||||
given().header("Authorization", AUTH_HEADER)
|
||||
.when().get("/v2/email/account")
|
||||
.then().statusCode(200)
|
||||
.body("DedicatedIpAutoWarmupEnabled", equalTo(false));
|
||||
|
||||
given().contentType("application/json").header("Authorization", AUTH_HEADER)
|
||||
.body("{\"AutoWarmupEnabled\": \"yes\"}")
|
||||
.when().put("/v2/email/account/dedicated-ips/warmup")
|
||||
.then().statusCode(200);
|
||||
given().header("Authorization", AUTH_HEADER)
|
||||
.when().get("/v2/email/account")
|
||||
.then().statusCode(200)
|
||||
.body("DedicatedIpAutoWarmupEnabled", equalTo(true));
|
||||
|
||||
given().contentType("application/json").header("Authorization", AUTH_HEADER)
|
||||
.body("{\"AutoWarmupEnabled\": \"nonsense\"}")
|
||||
.when().put("/v2/email/account/dedicated-ips/warmup")
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("SerializationException"))
|
||||
.body("message", equalTo("STRING_VALUE can not be converted to an Boolean"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -130,13 +130,11 @@ class SesV2JsonTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void coerceBoolean_matchesAwsJacksonCoercion() {
|
||||
void coerceBoolean_matchesAwsCoercion() {
|
||||
JsonNode node = json(
|
||||
"{\"t\":true,\"f\":false,\"s\":\"no\",\"z\":null,\"n\":0,\"a\":[],\"o\":{}}");
|
||||
"{\"t\":true,\"f\":false,\"z\":null,\"n\":0,\"a\":[],\"o\":{}}");
|
||||
assertTrue(SesV2Json.coerceBoolean(node.path("t")));
|
||||
assertFalse(SesV2Json.coerceBoolean(node.path("f")));
|
||||
// A JSON string coerces to true regardless of its text (probe-confirmed).
|
||||
assertTrue(SesV2Json.coerceBoolean(node.path("s")));
|
||||
assertNull(assertAws("SerializationException", 400,
|
||||
() -> SesV2Json.coerceBoolean(node.path("z"))).getMessage());
|
||||
assertEquals("NUMBER_VALUE can not be converted to a Boolean",
|
||||
@@ -150,6 +148,22 @@ class SesV2JsonTest {
|
||||
() -> SesV2Json.coerceBoolean(node.path("o"))).getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void coerceBoolean_coercesOnlyStringsThatNameABoolean() {
|
||||
// Probe-confirmed: the match is case-insensitive and exact, so padding is not trimmed.
|
||||
for (String text : List.of("true", "TRUE", "Yes", "y", "T", "1")) {
|
||||
assertTrue(SesV2Json.coerceBoolean(json("\"" + text + "\"")), text);
|
||||
}
|
||||
for (String text : List.of("false", "FALSE", "No", "n", "F", "0")) {
|
||||
assertFalse(SesV2Json.coerceBoolean(json("\"" + text + "\"")), text);
|
||||
}
|
||||
for (String text : List.of("abc", "", "on", "off", " true", "2")) {
|
||||
assertEquals("STRING_VALUE can not be converted to an Boolean",
|
||||
assertAws("SerializationException", 400,
|
||||
() -> SesV2Json.coerceBoolean(json("\"" + text + "\""))).getMessage(), text);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void unexpectedStartError_distinguishesListFromStructure() {
|
||||
assertEquals("Start of list found where not expected",
|
||||
@@ -229,6 +243,7 @@ class SesV2JsonTest {
|
||||
assertFalse(SesV2Json.parseSendingEnabled(json("{}").path("SendingEnabled")));
|
||||
assertTrue(SesV2Json.parseSendingEnabled(json("true")));
|
||||
assertTrue(SesV2Json.parseSendingEnabled(json("\"yes\"")));
|
||||
assertFalse(SesV2Json.parseSendingEnabled(json("\"no\"")));
|
||||
assertAws("SerializationException", 400, () -> SesV2Json.parseSendingEnabled(json("null")));
|
||||
assertAws("SerializationException", 400, () -> SesV2Json.parseSendingEnabled(json("0")));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user