fix(dynamodb): check BatchGetItem and BatchWriteItem table names, list sizes and enums before the tables (#7650)

This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-06 09:37:11 -04:00
1 parent c1a88b8ab8
commit 07d75c8c8e
7 files changed
+202 -41

No files matched your search

@@ -1147,7 +1147,8 @@ class DynamoDbIntegrationTest {
.then()
.statusCode(400)
.body("__type", equalTo("ValidationException"))
.body("message", equalTo("The requestItems parameter is required for BatchWriteItem"));
.body("message", equalTo("1 validation error detected: Value at 'RequestItems' failed to satisfy "
+ "constraint: Member must have length greater than or equal to 1"));
}
@Test
@@ -5870,8 +5871,12 @@ given()
PutItem | {"TableName": "Missing", "Item": {"pk": {"S": "a"}}, "ConditionExpression": "attribute_exists(pk)", "ExpressionAttributeNames": {}, "ExpressionAttributeValues": {}} | 1 validation error detected: ExpressionAttributeNames must not be empty
DeleteItem | {"TableName": "Missing", "Key": {"pk": {"S": "a"}}, "ExpressionAttributeValues": {":v": {"S": "x"}}} | 1 validation error detected: ExpressionAttributeValues can only be specified when using expressions
DeleteItem | {"TableName": "Missing", "Key": {"pk": {"S": "a"}}, "ConditionExpression": ""} | 1 validation error detected: Invalid ConditionExpression: The expression can not be empty;
BatchGetItem | {"RequestItems": {"bad!name": {"Keys": [{"pk": {"S": "a"}}]}}, "ReturnConsumedCapacity": "BOGUS"} | 2 validation errors detected: Value '{"bad!name":{"Keys":[{"pk":{"S":"a"}}]}}' at 'requestItems' failed to satisfy constraint: Map keys must satisfy constraint: [Member must have length less than or equal to 255, Member must have length greater than or equal to 3, Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+]; Value 'BOGUS' at 'returnConsumedCapacity' failed to satisfy constraint: Member must satisfy enum value set: [INDEXES, TOTAL, NONE]
BatchGetItem | {"RequestItems": {"Missing": {"Keys": []}}} | 1 validation error detected: Value at 'RequestItems.Missing.member.Keys' failed to satisfy constraint: Member must have length greater than or equal to 1
BatchWriteItem | {"RequestItems": {"Missing": [{"PutRequest": {"Item": {"pk": {"S": "x"}}}}]}, "ReturnConsumedCapacity": "BOGUS", "ReturnItemCollectionMetrics": "BOGUS2"} | 2 validation errors detected: Value 'BOGUS' at 'returnConsumedCapacity' failed to satisfy constraint: Member must satisfy enum value set: [INDEXES, TOTAL, NONE]; Value 'BOGUS2' at 'returnItemCollectionMetrics' failed to satisfy constraint: Member must satisfy enum value set: [SIZE, NONE]
BatchWriteItem | {"RequestItems": {"bad!name": [{"PutRequest": {"Item": {"pk": {"S": "x"}}}}]}} | 1 validation error detected: Value '{"bad!name":[{"PutRequest":{"Item":{"pk":{"S":"x"}}}}]}' at 'requestItems' failed to satisfy constraint: Map keys must satisfy constraint: [Member must have length less than or equal to 255, Member must have length greater than or equal to 3, Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+]
""")
void putAndDeleteItemReportTheirNewerChecksInsideTheEnvelope(String operation, String body, String message) {
void itemAndBatchCallsReportTheirNewerChecksInsideTheEnvelope(String operation, String body, String message) {
given()
.header("X-Amz-Target", "DynamoDB_20120810." + operation)
.contentType(DYNAMODB_CONTENT_TYPE)
@@ -66,6 +66,21 @@ class DynamoDbTableNamesTest {
assertEquals(400, ex.getHttpStatus());
}
@ParameterizedTest
@CsvSource({
"arn:aws:dynamodb:eu-west-2:123456789012:table/abc, true",
"arn:aws:dynamodb:us-east-1:123456789012:table/abc, false",
"arn:aws-cn:dynamodb:eu-west-2:123456789012:table/abc, false",
"arn:aws:dynamodb:eu-west-2:12:table/abc, false",
"arn:aws:dynamodb:eu-west-2:123456789012:table/ab, false",
"arn:aws:dynamodb:eu-west-2:123456789012:table/abc/stream/x, false",
"arn:aws:dynamodb::123456789012:global-table/abc, false",
"arn:x, false"
})
void isTableArnNeedsATableInTheRequestRegion(String value, boolean expected) {
assertEquals(expected, DynamoDbTableNames.isTableArn(value, "eu-west-2"));
}
@Test
void resolveAcceptsShortNameWithoutRegion() {
DynamoDbTableNames.ResolvedTableRef ref = DynamoDbTableNames.resolveWithRegion("Orders", "us-east-1");
@@ -2351,15 +2351,34 @@ class NativeDynamoDbJsonHandlerTest {
TransactGetItems | {"TransactItems":[{"Get":{"TableName":""}},{"Put":{}}],"ReturnConsumedCapacity":"BOGUS"} | 4 validation errors detected: Value null at 'transactItems.1.member.get.key' failed to satisfy constraint: Member must not be null; Value '' at 'transactItems.1.member.get.tableName' failed to satisfy constraint: Member must have length greater than or equal to 1; Value null at 'transactItems.2.member.get' failed to satisfy constraint: Member must not be null; Value 'BOGUS' at 'returnConsumedCapacity' failed to satisfy constraint: Member must satisfy enum value set: [INDEXES, TOTAL, NONE]
TransactWriteItems | {"TransactItems":null,"ClientRequestToken":"0123456789012345678901234567890123456","ReturnConsumedCapacity":"BOGUS","ReturnItemCollectionMetrics":"BOGUS2"} | 4 validation errors detected: Value null at 'transactItems' failed to satisfy constraint: Member must not be null; Value '0123456789012345678901234567890123456' at 'clientRequestToken' failed to satisfy constraint: Member must have length less than or equal to 36; Value 'BOGUS' at 'returnConsumedCapacity' failed to satisfy constraint: Member must satisfy enum value set: [INDEXES, TOTAL, NONE]; Value 'BOGUS2' at 'returnItemCollectionMetrics' failed to satisfy constraint: Member must satisfy enum value set: [SIZE, NONE]
TransactGetItems | {"ReturnConsumedCapacity":"BOGUS"} | 2 validation errors detected: Value null at 'transactItems' failed to satisfy constraint: Member must not be null; Value 'BOGUS' at 'returnConsumedCapacity' failed to satisfy constraint: Member must satisfy enum value set: [INDEXES, TOTAL, NONE]
BatchGetItem | {"RequestItems":{"bad!name":{"ProjectionExpression":"pk","Foo":1,"AttributesToGet":null,"ConsistentRead":true,"Keys":[{"pk":{"S":"q\\"\\u001f"}}]}},"ReturnConsumedCapacity":"BOGUS"} | 2 validation errors detected: Value '{"bad!name":{"ConsistentRead":true,"Keys":[{"pk":{"S":"q\\"\\u001f"}}],"ProjectionExpression":"pk"}}' at 'requestItems' failed to satisfy constraint: Map keys must satisfy constraint: [Member must have length less than or equal to 255, Member must have length greater than or equal to 3, Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+]; Value 'BOGUS' at 'returnConsumedCapacity' failed to satisfy constraint: Member must satisfy enum value set: [INDEXES, TOTAL, NONE]
BatchGetItem | {"RequestItems":{"Missing":{"AttributesToGet":[],"Keys":[]}},"ReturnConsumedCapacity":"BOGUS"} | 2 validation errors detected: Value at 'RequestItems.Missing.member.AttributesToGet' failed to satisfy constraint: Member must have length greater than or equal to 1; Value at 'RequestItems.Missing.member.Keys' failed to satisfy constraint: Member must have length greater than or equal to 1
BatchGetItem | {"RequestItems":{"":{"Keys":[]}}} | 2 validation errors detected: Value at 'RequestItems.' failed to satisfy constraint: Member must have length greater than or equal to 1; Value at 'RequestItems..member.Keys' failed to satisfy constraint: Member must have length greater than or equal to 1
BatchGetItem | {"RequestItems":{"arn:aws:dynamodb:us-east-1:123456789012:table/abc":{"Keys":[{"pk":{"S":"a"}}]}}} | 1 validation error detected: Value '{"arn:aws:dynamodb:us-east-1:123456789012:table/abc":{"Keys":[{"pk":{"S":"a"}}]}}' at 'requestItems' failed to satisfy constraint: Map keys must satisfy constraint: [Valid resource name or resource arn must be given. Valid resource arn format is 'arn:<awsPartition>:<vendor>:<region>:<subscriber>:resourceType/resourceName', where Resource name must have length less than or equal to 255, Resource name must have length greater than or equal to 3, Resource name must satisfy regular expression pattern: [a-zA-Z0-9_.-]+]
BatchGetItem | {"RequestItems":{"a!b":{"Keys":[{"pk":{"S":"a"}}]},"arn:aws:dynamodb:eu-west-1:123456789012:table/a!b":{"Keys":[{"pk":{"S":"a"}}]}}} | 1 validation error detected: Value '{"a!b":{"Keys":[{"pk":{"S":"a"}}]},"arn:aws:dynamodb:eu-west-1:123456789012:table/a!b":{"Keys":[{"pk":{"S":"a"}}]}}' at 'requestItems' failed to satisfy constraint: Map keys must satisfy constraint: [Member must have length less than or equal to 255, Member must have length greater than or equal to 3, Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+]
BatchWriteItem | {"RequestItems":{"bad!name":[{"PutRequest":{"Item":{"pk":{"S":"x"}},"Foo":1},"DeleteRequest":{"Key":{"pk":{"S":"y"}}}},{}]},"ReturnConsumedCapacity":"BOGUS","ReturnItemCollectionMetrics":""} | 3 validation errors detected: Value '{"bad!name":[{"DeleteRequest":{"Key":{"pk":{"S":"y"}}},"PutRequest":{"Item":{"pk":{"S":"x"}}}},{}]}' at 'requestItems' failed to satisfy constraint: Map keys must satisfy constraint: [Member must have length less than or equal to 255, Member must have length greater than or equal to 3, Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+]; Value 'BOGUS' at 'returnConsumedCapacity' failed to satisfy constraint: Member must satisfy enum value set: [INDEXES, TOTAL, NONE]; Value '' at 'returnItemCollectionMetrics' failed to satisfy constraint: Member must satisfy enum value set: [SIZE, NONE]
BatchWriteItem | {"RequestItems":{"Missing":[]},"ReturnConsumedCapacity":"BOGUS"} | 1 validation error detected: Value at 'RequestItems.Missing.member' failed to satisfy constraint: Member must have length greater than or equal to 1
BatchWriteItem | {"RequestItems":{},"ReturnItemCollectionMetrics":"BOGUS2"} | 1 validation error detected: Value at 'RequestItems' failed to satisfy constraint: Member must have length greater than or equal to 1
TransactWriteItems | {"TransactItems":[{"Delete":{"TableName":"Tbl","Key":{},"ReturnValuesOnConditionCheckFailure":"BOGUS"}},{"Put":{"TableName":"Tbl"}}]} | 2 validation errors detected: Value 'BOGUS' at 'transactItems.1.member.delete.returnValuesOnConditionCheckFailure' failed to satisfy constraint: Member must satisfy enum value set: [ALL_OLD, NONE]; Value null at 'transactItems.2.member.put.item' failed to satisfy constraint: Member must not be null
""")
void aTransactionReportsItsMemberErrorsTogether(String operation, String request, String message) throws Exception {
void aTransactionOrBatchReportsItsMemberErrorsTogether(String operation, String request, String message)
throws Exception {
AwsException ex = assertThrows(AwsException.class,
() -> handler.handle(operation, mapper.readTree(request), "eu-west-1"));
assertEquals("ValidationException", ex.getErrorCode());
assertEquals(message, ex.getMessage());
}
@ParameterizedTest
@ValueSource(strings = {"BatchGetItem", "BatchWriteItem"})
void aBatchThatSkippedTheShapeCheckFindsNoTablesInAnArray(String operation) throws Exception {
JsonNode request = mapper.readTree("{\"RequestItems\":[{}]}");
AwsException ex = assertThrows(AwsException.class, () -> handler.handle(operation, request, "eu-west-1"));
assertEquals("1 validation error detected: Value at 'RequestItems' failed to satisfy constraint: "
+ "Member must have length greater than or equal to 1", ex.getMessage());
}
@ParameterizedTest
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
PutItem | {"TableName":"Missing","Item":{},"ReturnValues":"ALL_NEW"} | 1 validation error detected: ReturnValues can only be ALL_OLD or NONE
@@ -248,7 +248,10 @@ class DynamoDbEmptyProjectionIntegrationTest {
JsonNode error = response(send(root, action, protocol, request), protocol, 400);
assertTrue(error.path("__type").asText().contains("ValidationException"), error.toString());
assertTrue(error.path("message").asText().contains("greater than or equal to 1"), error.toString());
assertTrue(error.path("message").asText().contains("Value '[]' at 'attributesToGet'"), error.toString());
String member = "BatchGetItem".equals(action)
? "Value at 'RequestItems." + table + ".member.AttributesToGet'"
: "Value '[]' at 'attributesToGet'";
assertTrue(error.path("message").asText().contains(member), error.toString());
} finally {
response(send(root, "DeleteTable", "json", JSON.createObjectNode().put("TableName", table)), "json", 200);
}