mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(dynamodb): reject malformed nested attribute values before writes (#8121)
This commit is contained in:
1 parent
021ce5e59b
commit
e1cd3e3015
2 files changed
+137
No files matched your search
@@ -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) {
|
||||
|
||||
+133
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user