diff --git a/src/main/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandler.java b/src/main/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandler.java index 7f0ac440ad..2a48ffff4c 100644 --- a/src/main/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandler.java +++ b/src/main/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandler.java @@ -818,6 +818,7 @@ public class NativeDynamoDbJsonHandler { requireReadableValues(item, VALIDATION_ENVELOPE); DynamoDbAttributeValueValidator.requireNestingWithinLimit(item); + item.forEach(DynamoDbAttributeValueValidator::validate); JsonNode expected = request.has("Expected") ? request.get("Expected") : null; String conditionalOperator = request.has("ConditionalOperator") @@ -3102,6 +3103,9 @@ public class NativeDynamoDbJsonHandler { DynamoDbAttributeValueValidator.requireNestingWithinLimit(op.get("Key"), false); requireReadableValues(values, ""); DynamoDbAttributeValueValidator.requireNonEmptyMemberNames(values); + if ("Put".equals(action.type())) { + values.forEach(DynamoDbAttributeValueValidator::validate); + } } private void requireValidTransactExpressions(JsonNode op, boolean older) { diff --git a/src/test/java/io/floci/aws/services/dynamodb/DynamoDbMalformedAttributeValueIntegrationTest.java b/src/test/java/io/floci/aws/services/dynamodb/DynamoDbMalformedAttributeValueIntegrationTest.java new file mode 100644 index 0000000000..f81bc1926f --- /dev/null +++ b/src/test/java/io/floci/aws/services/dynamodb/DynamoDbMalformedAttributeValueIntegrationTest.java @@ -0,0 +1,133 @@ +package io.floci.aws.services.dynamodb; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.floci.aws.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static io.restassured.RestAssured.given; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@QuarkusTest +class DynamoDbMalformedAttributeValueIntegrationTest { + + private static final ObjectMapper JSON = new ObjectMapper(); + + @BeforeAll + static void configureJson() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + void putItemRejectsMalformedNestedValueWithoutChangingStoredItems() throws Exception { + String table = createTable(); + try { + put(table, item("existing", true), 200); + + assertValidation(put(table, item("existing", false), 400)); + assertValidation(put(table, item("new", false), 400)); + + assertEquals(item("existing", true), get(table, "existing").path("Item")); + assertTrue(get(table, "new").path("Item").isMissingNode()); + } finally { + deleteTable(table); + } + } + + @Test + void transactWriteRejectsMalformedNestedValueWithoutApplyingOtherPuts() throws Exception { + String table = createTable(); + try { + ObjectNode request = JSON.createObjectNode(); + ArrayNode writes = request.putArray("TransactItems"); + writes.addObject().putObject("Put").put("TableName", table).set("Item", item("valid", true)); + writes.addObject().putObject("Put").put("TableName", table).set("Item", item("invalid", false)); + + assertValidation(call("TransactWriteItems", request, 400)); + assertTrue(get(table, "valid").path("Item").isMissingNode()); + assertTrue(get(table, "invalid").path("Item").isMissingNode()); + + writes.remove(1); + call("TransactWriteItems", request, 200); + assertEquals(item("valid", true), get(table, "valid").path("Item")); + } finally { + deleteTable(table); + } + } + + @Test + void batchWriteRejectsMalformedNestedValueWithoutApplyingOtherPuts() throws Exception { + String table = createTable(); + try { + ObjectNode request = JSON.createObjectNode(); + ArrayNode writes = request.putObject("RequestItems").putArray(table); + writes.addObject().putObject("PutRequest").set("Item", item("valid", true)); + writes.addObject().putObject("PutRequest").set("Item", item("invalid", false)); + + assertValidation(call("BatchWriteItem", request, 400)); + assertTrue(get(table, "valid").path("Item").isMissingNode()); + assertTrue(get(table, "invalid").path("Item").isMissingNode()); + + writes.remove(1); + call("BatchWriteItem", request, 200); + assertEquals(item("valid", true), get(table, "valid").path("Item")); + } finally { + deleteTable(table); + } + } + + private static String createTable() throws Exception { + String table = "malformed-" + UUID.randomUUID(); + ObjectNode request = JSON.createObjectNode().put("TableName", table).put("BillingMode", "PAY_PER_REQUEST"); + request.putArray("KeySchema").addObject().put("AttributeName", "pk").put("KeyType", "HASH"); + request.putArray("AttributeDefinitions").addObject().put("AttributeName", "pk").put("AttributeType", "S"); + call("CreateTable", request, 200); + return table; + } + + private static void deleteTable(String table) throws Exception { + call("DeleteTable", JSON.createObjectNode().put("TableName", table), 200); + } + + private static JsonNode put(String table, ObjectNode item, int status) throws Exception { + ObjectNode request = JSON.createObjectNode().put("TableName", table); + request.set("Item", item); + return call("PutItem", request, status); + } + + private static JsonNode get(String table, String key) throws Exception { + ObjectNode request = JSON.createObjectNode().put("TableName", table); + request.set("Key", JSON.createObjectNode().set("pk", JSON.createObjectNode().put("S", key))); + return call("GetItem", request, 200); + } + + private static ObjectNode item(String key, boolean valid) { + ObjectNode item = JSON.createObjectNode(); + item.putObject("pk").put("S", key); + ObjectNode map = item.putObject("m").putObject("M"); + map.putObject("S").put(valid ? "S" : "0", "v"); + if (valid) { + map.putObject("list").putArray("L").addObject().putObject("M").putObject("leaf").put("S", "v"); + } + return item; + } + + private static void assertValidation(JsonNode response) { + assertTrue(response.path("__type").asText().endsWith("ValidationException"), response.toString()); + } + + private static JsonNode call(String action, JsonNode request, int status) throws Exception { + String response = given().contentType("application/x-amz-json-1.0") + .header("X-Amz-Target", "DynamoDB_20120810." + action) + .body(request.toString()) + .post("/").then().statusCode(status).extract().asString(); + return JSON.readTree(response); + } +}