fix(dynamodb): check all PutItem and DeleteItem enums in one list with the table name (#7452)

This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-03 21:04:15 -04:00
1 parent 64a72bfc20
commit 91f0f5f768
4 files changed
+75 -32

No files matched your search

@@ -109,15 +109,23 @@ public final class DynamoDbTableNames {
}
private static void validateTableName(String tableName) {
String violation = nameViolation(tableName);
if (violation != null) {
throw SmithyValidationErrors.validationException(violation);
}
}
/** The violation for a short table name, or {@code null} for a valid one. A bad length hides a bad pattern. */
static String nameViolation(String tableName) {
String lengthViolation = SmithyValidationErrors.lengthOutOfRange("tableName", tableName, 3, 255);
if (lengthViolation != null) {
throw SmithyValidationErrors.validationException(lengthViolation);
return lengthViolation;
}
if (!TABLE_NAME_CHARS.matcher(tableName).matches()) {
throw SmithyValidationErrors.validationException(
"Value '" + tableName
+ "' at 'tableName' failed to satisfy constraint: Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+");
return "Value '" + tableName
+ "' at 'tableName' failed to satisfy constraint: Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+";
}
return null;
}
private static AwsException invalid(String message) {
@@ -565,7 +565,6 @@ public class NativeDynamoDbJsonHandler {
return Response.ok(response).build();
}
private static final Set<String> VALID_RETURN_VALUES_PUT = Set.of("NONE", "ALL_OLD");
private static final Set<String> VALID_RETURN_CONSUMED_CAPACITY = Set.of("INDEXES", "TOTAL", "NONE");
private static final Set<String> VALID_RETURN_ITEM_COLLECTION_METRICS = Set.of("SIZE", "NONE");
@@ -588,9 +587,37 @@ public class NativeDynamoDbJsonHandler {
+ "Member must satisfy enum value set: [SIZE, NONE]";
}
// DynamoDB names neither the member nor the value outside the enum, and spells UPDATED_NEW as UPDATE_NEW.
private static String itemWriteReturnValuesError(JsonNode request) {
JsonNode value = request.get("ReturnValues");
if (value == null || value.isNull() || VALID_RETURN_VALUES_ITEM_WRITE.contains(value.asText())) {
return null;
}
if (VALID_RETURN_VALUES_UPDATE.contains(value.asText())) {
return "ReturnValues can only be ALL_OLD or NONE";
}
return "Failed to satisfy constraint: Member must satisfy enum value set: "
+ "[ALL_OLD, UPDATED_OLD, ALL_NEW, UPDATE_NEW, NONE]";
}
// PutItem and DeleteItem list a bad short table name together with their bad enums.
private static void requireValidItemWriteMembers(JsonNode request, String tableName) {
List<String> validationErrors = new ArrayList<>();
if (tableName == null || tableName.isEmpty() || AwsArnUtils.looksLikeArn(tableName)) {
DynamoDbTableNames.resolve(tableName);
} else {
addIfPresent(validationErrors, DynamoDbTableNames.nameViolation(tableName));
}
addReturnConsumedCapacityError(request, validationErrors);
addIfPresent(validationErrors, itemWriteReturnValuesError(request));
addIfPresent(validationErrors, returnItemCollectionMetricsError(request));
addPartiQLReturnValuesError(request, "returnValuesOnConditionCheckFailure", validationErrors);
throwValidationErrors(validationErrors);
}
private Response handlePutItem(JsonNode request, String region) {
String tableName = request.has("TableName") ? request.path("TableName").asText() : null;
DynamoDbTableNames.resolve(tableName); // validate tableName before other params
requireValidItemWriteMembers(request, tableName);
JsonNode item = request.path("Item");
String returnValues = request.path("ReturnValues").asText("NONE");
String returnValuesOnConditionCheckFailure = request.path("ReturnValuesOnConditionCheckFailure").asText("NONE");
@@ -601,20 +628,6 @@ public class NativeDynamoDbJsonHandler {
JsonNode exprAttrValues = request.has("ExpressionAttributeValues")
? request.get("ExpressionAttributeValues") : null;
List<String> validationErrors = new ArrayList<>();
addReturnConsumedCapacityError(request, validationErrors);
String ricm = request.has("ReturnItemCollectionMetrics") ? request.get("ReturnItemCollectionMetrics").asText() : null;
if (ricm != null && !VALID_RETURN_ITEM_COLLECTION_METRICS.contains(ricm)) {
validationErrors.add("Value '" + ricm + "' at 'returnItemCollectionMetrics' failed to satisfy constraint: "
+ "Member must satisfy enum value set: [SIZE, NONE]");
}
if (!VALID_RETURN_VALUES_PUT.contains(returnValues)) {
validationErrors.add("Value '" + returnValues + "' at 'returnValues' failed to satisfy constraint: "
+ "Member must satisfy enum value set: [NONE, ALL_OLD]");
}
if (!validationErrors.isEmpty()) {
throw SmithyValidationErrors.validationException(validationErrors);
}
requireReadableValues(item, VALIDATION_ENVELOPE);
DynamoDbExpressionSize.checkWrite(conditionExpression, "ConditionExpression");
DynamoDbAttributeValueValidator.requireNestingWithinLimit(item);
@@ -704,13 +717,13 @@ public class NativeDynamoDbJsonHandler {
return Response.ok(response).build();
}
private static final Set<String> VALID_RETURN_VALUES_DELETE = Set.of("NONE", "ALL_OLD");
private static final Set<String> VALID_RETURN_VALUES_ITEM_WRITE = Set.of("NONE", "ALL_OLD");
private static final Set<String> VALID_RETURN_VALUES_UPDATE =
Set.of("NONE", "ALL_OLD", "ALL_NEW", "UPDATED_OLD", "UPDATED_NEW");
private Response handleDeleteItem(JsonNode request, String region) {
String tableName = request.path("TableName").asText();
DynamoDbTableNames.resolve(tableName); // validate tableName first
requireValidItemWriteMembers(request, tableName);
JsonNode key = request.path("Key");
String returnValues = request.path("ReturnValues").asText("NONE");
String returnValuesOnConditionCheckFailure = request.path("ReturnValuesOnConditionCheckFailure").asText("NONE");
@@ -721,16 +734,6 @@ public class NativeDynamoDbJsonHandler {
JsonNode exprAttrValues = request.has("ExpressionAttributeValues")
? request.get("ExpressionAttributeValues") : null;
// Validate ReturnValues + ReturnConsumedCapacity together
List<String> delValidationErrors = new ArrayList<>();
addReturnConsumedCapacityError(request, delValidationErrors);
if (!VALID_RETURN_VALUES_DELETE.contains(returnValues)) {
delValidationErrors.add("Value '" + returnValues + "' at 'returnValues' failed to satisfy constraint: "
+ "Member must satisfy enum value set: [NONE, ALL_OLD]");
}
if (!delValidationErrors.isEmpty()) {
throw SmithyValidationErrors.validationException(delValidationErrors);
}
requireReadableValues(key, VALIDATION_ENVELOPE);
DynamoDbExpressionSize.checkWrite(conditionExpression, "ConditionExpression");
DynamoDbAttributeValueValidator.requireNestingWithinLimit(exprAttrValues);
@@ -5865,6 +5865,23 @@ given()
deleteTable("EmptyNameTable");
}
@ParameterizedTest
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
DeleteItem | {"TableName": "Missing", "Key": {"pk": {"S": "a"}}, "ReturnValues": "ALL_NEW"} | 1 validation error detected: ReturnValues can only be ALL_OLD or NONE
PutItem | {"TableName": "bad!name", "Item": {"pk": {"S": "a"}}, "ReturnValues": "BOGUS", "ReturnConsumedCapacity": "BOGUS"} | 3 validation errors detected: Value 'bad!name' at 'tableName' failed to satisfy constraint: 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]; Failed to satisfy constraint: Member must satisfy enum value set: [ALL_OLD, UPDATED_OLD, ALL_NEW, UPDATE_NEW, NONE]
""")
void putAndDeleteItemReportTheTableNameAndEnumsTogether(String operation, String body, String message) {
given()
.header("X-Amz-Target", "DynamoDB_20120810." + operation)
.contentType(DYNAMODB_CONTENT_TYPE)
.body(body)
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo(message));
}
private void deleteTable(String tableName) {
given()
.header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")
@@ -2360,6 +2360,21 @@ class NativeDynamoDbJsonHandlerTest {
assertEquals(message, 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
DeleteItem | {"TableName":"Missing","Key":{},"ReturnValues":"BOGUS"} | 1 validation error detected: Failed to satisfy constraint: Member must satisfy enum value set: [ALL_OLD, UPDATED_OLD, ALL_NEW, UPDATE_NEW, NONE]
DeleteItem | {"TableName":"bad!name","Key":{},"ReturnValuesOnConditionCheckFailure":"BOGUS","ReturnItemCollectionMetrics":"BOGUS","ReturnValues":"UPDATED_NEW","ReturnConsumedCapacity":"BOGUS"} | 5 validation errors detected: Value 'bad!name' at 'tableName' failed to satisfy constraint: 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]; ReturnValues can only be ALL_OLD or NONE; Value 'BOGUS' at 'returnItemCollectionMetrics' failed to satisfy constraint: Member must satisfy enum value set: [SIZE, NONE]; Value 'BOGUS' at 'returnValuesOnConditionCheckFailure' failed to satisfy constraint: Member must satisfy enum value set: [ALL_OLD, NONE]
PutItem | {"TableName":"a!","Item":{},"ReturnValuesOnConditionCheckFailure":"ALL_NEW"} | 2 validation errors detected: Value 'a!' at 'tableName' failed to satisfy constraint: Member must have length greater than or equal to 3; Value 'ALL_NEW' at 'returnValuesOnConditionCheckFailure' failed to satisfy constraint: Member must satisfy enum value set: [ALL_OLD, NONE]
""")
void aPutOrDeleteReportsItsTableNameAndEnumErrorsTogether(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
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
61 | {"TransactItems":[{},{"Delete":{"TableName":"bad!name","Key":{}}}]} | Invalid Request: TransactWriteRequest should contain Delete or Put or Update request