mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): reject a request member of the wrong JSON type (#4636)
* chore(dynamodb): vendor the input shapes of botocore's DynamoDB model tools/aws/regen_dynamodb_shapes.py keeps each operation's input shape and the type of every shape it reaches, from the pinned botocore release. make aws-data-sync regenerates it and make aws-data-check keeps it in sync. * fix(dynamodb): reject a request member of the wrong JSON type DynamoDB answers a member of the wrong JSON type with a SerializationException before it runs the operation, such as "NUMBER_VALUE cannot be converted to String" for a number BillingMode or IndexName. The message names the JSON token and the Java type, or says "Unexpected field type", "Unexpected value type in payload", "Start of structure or map found where not expected" or "UnknownError" by where the value sits. The first bad member in body order is reported. A JSON null counts as absent and an unknown member is ignored. Floci now checks every DynamoDB JSON request against the vendored input shapes. A number in an ExclusiveStartKey S value is one of these errors on AWS, so that test now expects the SerializationException. * fix(dynamodb): keep provenance out of the shapes freshness check The shapes file now carries its provenance in a _source block, like the other vendored AWS data, and --check compares it without that block. A run against --botocore-data or a local checkout no longer reports the file as stale when only the botocore label differs.
This commit is contained in:
1 parent
33617e340d
commit
1465251e0c
10 files changed
+2207
-5
No files matched your search
+2
-1
@@ -363,7 +363,8 @@ class DynamoDbAccessPathIntegrationTest {
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"));
|
||||
.body("__type", equalTo("SerializationException"))
|
||||
.body("message", equalTo("NUMBER_VALUE cannot be converted to String"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+37
@@ -693,6 +693,43 @@ class DynamoDbIntegrationTest {
|
||||
.body("message", equalTo(message));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aNonStringMemberFailsToDeserialize() {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "NonStringBillingModeTable",
|
||||
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
|
||||
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
|
||||
"BillingMode": 5
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("SerializationException"))
|
||||
.body("message", equalTo("NUMBER_VALUE cannot be converted to String"));
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.UpdateTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "NoSuchNonStringTable",
|
||||
"GlobalSecondaryIndexUpdates": [{"Delete": {"IndexName": true}}]
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("SerializationException"))
|
||||
.body("message", equalTo("TRUE_VALUE cannot be converted to String"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createTableWithGsiAndLsi() {
|
||||
given()
|
||||
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
package io.github.hectorvent.floci.services.dynamodb;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class DynamoDbRequestShapesTest {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
void stringMemberNamesTheJsonTokenItGot() {
|
||||
assertRejected("Scan", "{\"TableName\":\"t\",\"IndexName\":5}", "NUMBER_VALUE cannot be converted to String");
|
||||
assertRejected("Scan", "{\"TableName\":\"t\",\"IndexName\":5.5}", "DECIMAL_VALUE cannot be converted to String");
|
||||
assertRejected("Scan", "{\"TableName\":\"t\",\"IndexName\":1e3}", "DECIMAL_VALUE cannot be converted to String");
|
||||
assertRejected("Scan", "{\"TableName\":\"t\",\"IndexName\":true}", "TRUE_VALUE cannot be converted to String");
|
||||
assertRejected("Scan", "{\"TableName\":\"t\",\"IndexName\":false}", "FALSE_VALUE cannot be converted to String");
|
||||
assertRejected("Scan", "{\"TableName\":\"t\",\"IndexName\":{}}", "Start of structure or map found where not expected");
|
||||
assertRejected("Scan", "{\"TableName\":\"t\",\"IndexName\":[]}", "UnknownError");
|
||||
}
|
||||
|
||||
@Test
|
||||
void numericMembersAcceptAnyNumberAndNameTheirJavaType() {
|
||||
assertAccepted("Scan", "{\"TableName\":\"t\",\"Limit\":5.5}");
|
||||
assertAccepted("Scan", "{\"TableName\":\"t\",\"Limit\":99999999999}");
|
||||
assertRejected("Scan", "{\"TableName\":\"t\",\"Limit\":\"5\"}", "STRING_VALUE cannot be converted to Integer");
|
||||
assertRejected("Scan", "{\"TableName\":\"t\",\"Limit\":false}", "FALSE_VALUE cannot be converted to Integer");
|
||||
assertRejected("UpdateTable",
|
||||
"{\"TableName\":\"t\",\"ProvisionedThroughput\":{\"ReadCapacityUnits\":\"5\",\"WriteCapacityUnits\":5}}",
|
||||
"STRING_VALUE cannot be converted to Long");
|
||||
assertRejected("UpdateTableReplicaAutoScaling", "{\"TableName\":\"t\",\"ProvisionedWriteCapacityAutoScalingUpdate\":"
|
||||
+ "{\"ScalingPolicyUpdate\":{\"TargetTrackingScalingPolicyConfiguration\":{\"TargetValue\":\"50\"}}}}",
|
||||
"STRING_VALUE cannot be converted to Double");
|
||||
assertAccepted("ListBackups", "{\"TimeRangeLowerBound\":1577836800}");
|
||||
assertRejected("ListBackups", "{\"TimeRangeLowerBound\":\"2020-01-01T00:00:00Z\"}",
|
||||
"STRING_VALUE cannot be converted to Date");
|
||||
}
|
||||
|
||||
@Test
|
||||
void booleanMemberAcceptsItsWordsAsStrings() {
|
||||
assertAccepted("Scan", "{\"TableName\":\"t\",\"ConsistentRead\":\"false\"}");
|
||||
assertAccepted("Scan", "{\"TableName\":\"t\",\"ConsistentRead\":\"TRUE\"}");
|
||||
assertRejected("Scan", "{\"TableName\":\"t\",\"ConsistentRead\":\"abc\"}", "Unexpected token received from parser");
|
||||
assertRejected("Scan", "{\"TableName\":\"t\",\"ConsistentRead\":0}", "NUMBER_VALUE cannot be converted to Boolean");
|
||||
}
|
||||
|
||||
@Test
|
||||
void blobMemberTakesOnlyAString() {
|
||||
assertRejected("PutItem", "{\"TableName\":\"t\",\"Item\":{\"a\":{\"B\":5}}}",
|
||||
"only base-64-encoded strings are convertible to bytes");
|
||||
assertRejected("PutItem", "{\"TableName\":\"t\",\"Item\":{\"a\":{\"BS\":[true]}}}",
|
||||
"only base-64-encoded strings are convertible to bytes");
|
||||
}
|
||||
|
||||
@Test
|
||||
void containerMessageDependsOnWhereTheValueSits() {
|
||||
assertRejected("UpdateTable", "{\"TableName\":\"t\",\"ProvisionedThroughput\":\"a\"}", "Unexpected field type");
|
||||
assertRejected("UpdateTable", "{\"TableName\":\"t\",\"ProvisionedThroughput\":[]}", "UnknownError");
|
||||
assertRejected("Scan", "{\"TableName\":\"t\",\"AttributesToGet\":\"a\"}", "Unexpected field type");
|
||||
assertRejected("Scan", "{\"TableName\":\"t\",\"AttributesToGet\":{}}", "Start of structure or map found where not expected");
|
||||
assertRejected("Scan", "{\"TableName\":\"t\",\"ExpressionAttributeNames\":5}", "Unexpected field type");
|
||||
assertRejected("PutItem", "{\"TableName\":\"t\",\"Item\":{\"a\":5}}", "Unexpected value type in payload");
|
||||
assertRejected("UpdateTable", "{\"TableName\":\"t\",\"GlobalSecondaryIndexUpdates\":[\"a\"]}",
|
||||
"Unexpected value type in payload");
|
||||
assertRejected("BatchWriteItem", "{\"RequestItems\":{\"t\":5}}", "UnknownError");
|
||||
assertRejected("BatchGetItem", "{\"RequestItems\":{\"t\":{\"Keys\":[5]}}}", "UnknownError");
|
||||
}
|
||||
|
||||
@Test
|
||||
void nestedMembersAreCheckedToo() {
|
||||
assertRejected("UpdateTable", "{\"TableName\":\"t\",\"GlobalSecondaryIndexUpdates\":[{\"Delete\":{\"IndexName\":5}}]}",
|
||||
"NUMBER_VALUE cannot be converted to String");
|
||||
assertRejected("CreateTable", "{\"TableName\":\"t\",\"BillingMode\":5}", "NUMBER_VALUE cannot be converted to String");
|
||||
assertRejected("Scan", "{\"TableName\":\"t\",\"ExclusiveStartKey\":{\"pk\":{\"S\":123}}}",
|
||||
"NUMBER_VALUE cannot be converted to String");
|
||||
}
|
||||
|
||||
@Test
|
||||
void firstBadMemberInBodyOrderWins() {
|
||||
assertRejected("Scan", "{\"TableName\":\"t\",\"Limit\":\"5\",\"IndexName\":5}", "STRING_VALUE cannot be converted to Integer");
|
||||
assertRejected("Scan", "{\"TableName\":\"t\",\"IndexName\":5,\"Limit\":\"5\"}", "NUMBER_VALUE cannot be converted to String");
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullAndUnknownMembersAreLeftToTheOperation() {
|
||||
assertAccepted("Scan", "{\"TableName\":\"t\",\"IndexName\":null,\"Limit\":null,\"Unknown\":5}");
|
||||
assertAccepted("Scan", "{\"TableName\":\"t\",\"AttributesToGet\":[null]}");
|
||||
assertAccepted("NoSuchOperation", "{\"TableName\":5}");
|
||||
}
|
||||
|
||||
private static void assertRejected(String action, String body, String message) {
|
||||
AwsException error = assertThrows(AwsException.class, () -> DynamoDbRequestShapes.check(action, json(body)));
|
||||
assertEquals("SerializationException", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
assertEquals(message, error.getMessage());
|
||||
}
|
||||
|
||||
private static void assertAccepted(String action, String body) {
|
||||
assertDoesNotThrow(() -> DynamoDbRequestShapes.check(action, json(body)));
|
||||
}
|
||||
|
||||
private static JsonNode json(String body) throws Exception {
|
||||
return MAPPER.readTree(body);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user