mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(ses): answer SES v2 wire-type errors with AWS's messages (#6330)
* fix(ses): answer SES v2 wire-type errors with AWS's messages A scalar for a map member is "Expected map or null", a string, boolean, object or list for an integer member gets the Integer conversion message, and a number is narrowed to an int the way AWS reads it (1.5 is accepted, a value above the int range saturates before the range check). A body that is not a JSON object is a SerializationException instead of a BadRequest. * test(ses): give the new SES v2 tests their own order and share the non-object bodies
This commit is contained in:
1 parent
b806f76f1b
commit
eea87ca998
26 files changed
+324
-89
No files matched your search
@@ -10,6 +10,7 @@ import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.intMemberOrAbsent;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.stringArrayOrAbsent;
|
||||
|
||||
/**
|
||||
@@ -46,9 +47,9 @@ final class SesExportFilters {
|
||||
}
|
||||
// The service validated the range at create time; this stays defensive so a stored job
|
||||
// written by an older build cannot slice with a negative bound.
|
||||
JsonNode maxResults = insights.path("MaxResults");
|
||||
if (maxResults.isInt() && maxResults.intValue() > 0 && kept.size() > maxResults.intValue()) {
|
||||
return kept.subList(0, maxResults.intValue());
|
||||
Integer maxResults = intMemberOrAbsent(insights, "MaxResults");
|
||||
if (maxResults != null && maxResults > 0 && kept.size() > maxResults) {
|
||||
return kept.subList(0, maxResults);
|
||||
}
|
||||
return kept;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
@@ -546,10 +547,11 @@ public class SesExportJobService implements Resettable {
|
||||
|
||||
/**
|
||||
* The model bounds {@code MaxResults} at 1 to 10000. An unchecked negative value reached
|
||||
* {@code subList} inside the filter and surfaced as a 500.
|
||||
* {@code subList} inside the filter and surfaced as a 500. The value is stored as the int it
|
||||
* was checked as, so a job created with 1.5 is described and filtered with 1.
|
||||
*/
|
||||
private static void requireMaxResults(JsonNode insights) {
|
||||
// intMemberOrAbsent answers SerializationException for a value that is not an integer, so
|
||||
// intMemberOrAbsent answers SerializationException for a value that is not a number, so
|
||||
// a wrong type is not reported as a value out of range.
|
||||
Integer maxResults = intMemberOrAbsent(insights, "MaxResults");
|
||||
if (maxResults == null) {
|
||||
@@ -562,6 +564,9 @@ public class SesExportJobService implements Resettable {
|
||||
+ "satisfy constraint: Member must be between 1 and " + MAX_RESULTS
|
||||
+ ", inclusive");
|
||||
}
|
||||
if (insights instanceof ObjectNode node) {
|
||||
node.put("MaxResults", maxResults);
|
||||
}
|
||||
}
|
||||
|
||||
private static Instant requireDate(JsonNode source, String field) {
|
||||
|
||||
@@ -436,9 +436,7 @@ public class SesIdentityController {
|
||||
// clear). Within a valid object, an omitted or explicit-null ConfigurationSetName
|
||||
// clears the association (as does an empty body / {}).
|
||||
JsonNode request = objectMapper.readTree(body);
|
||||
if (request == null || !request.isObject()) {
|
||||
throw new AwsException("SerializationException", null, 400);
|
||||
}
|
||||
requireJsonObject(request);
|
||||
JsonNode node = request.path("ConfigurationSetName");
|
||||
if (!node.isMissingNode() && !node.isNull()) {
|
||||
if (!node.isTextual()) {
|
||||
|
||||
@@ -227,14 +227,8 @@ public class SesTenantController {
|
||||
: objectMapper.readTree(body);
|
||||
requireJsonObject(request);
|
||||
String tenantName = stringMemberOrAbsent(request, "TenantName");
|
||||
String resourceTypeFilter = null;
|
||||
JsonNode filter = request.path("Filter");
|
||||
if (!filter.isMissingNode() && !filter.isNull()) {
|
||||
if (!filter.isObject()) {
|
||||
throw new AwsException("SerializationException", null, 400);
|
||||
}
|
||||
resourceTypeFilter = stringMemberOrAbsent(filter, "RESOURCE_TYPE");
|
||||
}
|
||||
Map<String, String> filter = stringMapMemberOrAbsent(request, "Filter");
|
||||
String resourceTypeFilter = filter == null ? null : filter.get("RESOURCE_TYPE");
|
||||
Integer pageSize = intMemberOrAbsent(request, "PageSize");
|
||||
String nextToken = stringMemberOrAbsent(request, "NextToken");
|
||||
PaginatedResult<TenantResourceAssociation> page = tenantService.listTenantResources(
|
||||
|
||||
@@ -27,7 +27,9 @@ final class SesV2Json {
|
||||
/** The deserializer's wording, which {@link SesV2RequestShapes}' walk of the body shapes shares. */
|
||||
static final String SERIALIZATION_EXCEPTION = "SerializationException";
|
||||
static final String CONVERSION = "%s can not be converted to a %s";
|
||||
static final String INTEGER_CONVERSION = "%s can not be converted to an Integer";
|
||||
static final String EXPECTED_NULL = "Expected null";
|
||||
static final String EXPECTED_MAP = "Expected map or null";
|
||||
static final String EXPECTED_LIST = "Expected list or null";
|
||||
static final String LIST_NOT_EXPECTED = "Start of list found where not expected";
|
||||
static final String OBJECT_NOT_EXPECTED = "Start of structure or map found where not expected.";
|
||||
@@ -52,11 +54,15 @@ final class SesV2Json {
|
||||
};
|
||||
}
|
||||
|
||||
/** A request body that is not a JSON object: AWS names a list and answers anything else without a message. */
|
||||
static void requireJsonObject(JsonNode root) {
|
||||
if (root == null || !root.isObject()) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"Request body must be a JSON object.", 400);
|
||||
if (root != null && root.isObject()) {
|
||||
return;
|
||||
}
|
||||
if (root != null && root.isArray()) {
|
||||
throw unexpectedStartError(root);
|
||||
}
|
||||
throw new AwsException(SERIALIZATION_EXCEPTION, null, 400);
|
||||
}
|
||||
|
||||
static JsonNode requireObjectOrAbsent(JsonNode parent, String fieldName) {
|
||||
@@ -152,21 +158,6 @@ final class SesV2Json {
|
||||
throw scalarTypeError(node, "Boolean");
|
||||
}
|
||||
|
||||
/**
|
||||
* A structure member as the AWS deserializer reads it: absent or null is returned as is, an
|
||||
* array is "Start of list found where not expected" and any other non-object "Expected null".
|
||||
*/
|
||||
private static JsonNode structureMemberOrAbsent(JsonNode parent, String field) {
|
||||
JsonNode node = parent.path(field);
|
||||
if (node.isMissingNode() || node.isNull() || node.isObject()) {
|
||||
return node;
|
||||
}
|
||||
if (node.isArray()) {
|
||||
throw unexpectedStartError(node);
|
||||
}
|
||||
throw expectedNullError();
|
||||
}
|
||||
|
||||
/** A string member with the AWS deserializer's message for each wrong JSON type. */
|
||||
private static String typedStringMemberOrAbsent(JsonNode parent, String field) {
|
||||
JsonNode node = parent.path(field);
|
||||
@@ -185,10 +176,16 @@ final class SesV2Json {
|
||||
* JSON type gets the deserializer's message for it.
|
||||
*/
|
||||
static Map<String, String> stringMapMemberOrAbsent(JsonNode parent, String field) {
|
||||
JsonNode node = structureMemberOrAbsent(parent, field);
|
||||
JsonNode node = parent.path(field);
|
||||
if (node.isMissingNode() || node.isNull()) {
|
||||
return null;
|
||||
}
|
||||
if (node.isArray()) {
|
||||
throw unexpectedStartError(node);
|
||||
}
|
||||
if (!node.isObject()) {
|
||||
throw expectedMapError();
|
||||
}
|
||||
Map<String, String> values = new LinkedHashMap<>();
|
||||
for (Map.Entry<String, JsonNode> entry : node.properties()) {
|
||||
values.put(entry.getKey(), typedStringMemberOrAbsent(node, entry.getKey()));
|
||||
@@ -240,11 +237,16 @@ final class SesV2Json {
|
||||
return unexpectedStartError(node);
|
||||
}
|
||||
|
||||
/** The deserializer's message for a structure or map member given a scalar. */
|
||||
/** The deserializer's message for a structure member given a scalar. */
|
||||
static AwsException expectedNullError() {
|
||||
return new AwsException(SERIALIZATION_EXCEPTION, EXPECTED_NULL, 400);
|
||||
}
|
||||
|
||||
/** The deserializer's message for a map member given a scalar. */
|
||||
static AwsException expectedMapError() {
|
||||
return new AwsException(SERIALIZATION_EXCEPTION, EXPECTED_MAP, 400);
|
||||
}
|
||||
|
||||
/** The deserializer's message for a list member given a scalar. */
|
||||
static AwsException expectedListError() {
|
||||
return new AwsException(SERIALIZATION_EXCEPTION, EXPECTED_LIST, 400);
|
||||
@@ -291,18 +293,26 @@ final class SesV2Json {
|
||||
return values;
|
||||
}
|
||||
|
||||
// Integer variant of stringMemberOrAbsent: absent/null returns null, non-integral JSON is
|
||||
// rejected rather than coerced, and so is an integral value outside the int range (intValue
|
||||
// would silently truncate it).
|
||||
// Integer variant of stringMemberOrAbsent: absent/null returns null and a string, a boolean, an
|
||||
// object or a list gets the deserializer's message for it. A number is narrowed to an int: AWS
|
||||
// accepts 1.5 and range-checks 4294967296 as 2147483647, which a double-to-int cast reproduces
|
||||
// where intValue would truncate 4294967296 to 0.
|
||||
static Integer intMemberOrAbsent(JsonNode parent, String field) {
|
||||
JsonNode n = parent.path(field);
|
||||
if (n.isMissingNode() || n.isNull()) {
|
||||
return null;
|
||||
}
|
||||
if (!n.isIntegralNumber() || !n.canConvertToInt()) {
|
||||
throw new AwsException(SERIALIZATION_EXCEPTION, null, 400);
|
||||
if (n.isTextual()) {
|
||||
throw new AwsException(SERIALIZATION_EXCEPTION, INTEGER_CONVERSION.formatted("STRING_VALUE"), 400);
|
||||
}
|
||||
return n.intValue();
|
||||
if (n.isBoolean()) {
|
||||
throw new AwsException(SERIALIZATION_EXCEPTION,
|
||||
INTEGER_CONVERSION.formatted(n.booleanValue() ? "TRUE_VALUE" : "FALSE_VALUE"), 400);
|
||||
}
|
||||
if (n.isContainerNode()) {
|
||||
throw unexpectedStartError(n);
|
||||
}
|
||||
return (int) n.doubleValue();
|
||||
}
|
||||
|
||||
static String readRequiredStringField(JsonNode request, String fieldName) {
|
||||
|
||||
@@ -20,10 +20,11 @@ import java.util.Set;
|
||||
* a list operation's map {@code Filter} keep the {@link SesV2Json} member helpers.
|
||||
*
|
||||
* <p>AWS reads the body in order and reports the first bad member, without naming it. A map
|
||||
* container answers like a structure, as the list operations' {@code Filter} already does. Only the
|
||||
* string, blob, list and structure messages have been probed, so a number, boolean or timestamp
|
||||
* member is still checked by its controller. The probes sent integers only: a non-integral number
|
||||
* is reported as {@code NUMBER_VALUE} too, although AWS may name it differently.
|
||||
* container answers like a structure, except that a scalar for it is "Expected map or null". Only
|
||||
* the string, blob, list, structure and map messages have been probed, so a number, boolean or
|
||||
* timestamp member is still checked by its controller. The probes sent integers only: a
|
||||
* non-integral number is reported as {@code NUMBER_VALUE} too, although AWS may name it
|
||||
* differently.
|
||||
*/
|
||||
final class SesV2RequestShapes {
|
||||
|
||||
@@ -31,15 +32,13 @@ final class SesV2RequestShapes {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(SesV2RequestShapes.class);
|
||||
|
||||
private static final ByPosition EXPECTED_NULL = ByPosition.same(SesV2Json.EXPECTED_NULL);
|
||||
|
||||
private static final RequestShapeCheck.Messages MESSAGES = new RequestShapeCheck.Messages(
|
||||
SesV2Json.SERIALIZATION_EXCEPTION,
|
||||
SesV2Json.OBJECT_NOT_EXPECTED,
|
||||
SesV2Json.LIST_NOT_EXPECTED,
|
||||
EXPECTED_NULL,
|
||||
ByPosition.same(SesV2Json.EXPECTED_NULL),
|
||||
ByPosition.same(SesV2Json.EXPECTED_LIST),
|
||||
EXPECTED_NULL,
|
||||
ByPosition.same(SesV2Json.EXPECTED_MAP),
|
||||
SesV2Json.CONVERSION,
|
||||
false,
|
||||
null,
|
||||
|
||||
Reference in new issue
Block a user