diff --git a/src/main/java/io/floci/aws/services/dynamodb/DynamoDbAttributeValueValidator.java b/src/main/java/io/floci/aws/services/dynamodb/DynamoDbAttributeValueValidator.java index 019de59ebd..4d5d91954d 100644 --- a/src/main/java/io/floci/aws/services/dynamodb/DynamoDbAttributeValueValidator.java +++ b/src/main/java/io/floci/aws/services/dynamodb/DynamoDbAttributeValueValidator.java @@ -179,7 +179,16 @@ final class DynamoDbAttributeValueValidator { // Batch and transact writes report the limit without the validation envelope. static void requireNestingWithinLimit(JsonNode attributes, boolean inValidationEnvelope) { - if (!nestingWithinLimit(attributes)) { + requireNestingWithin(attributes, MAX_NESTING_LEVELS, inValidationEnvelope); + } + + // DynamoDB lets an expression or condition value nest one level deeper than an item attribute. + static void requireValueNestingWithinLimit(JsonNode values, boolean inValidationEnvelope) { + requireNestingWithin(values, MAX_NESTING_LEVELS + 1, inValidationEnvelope); + } + + private static void requireNestingWithin(JsonNode attributes, int maxLevels, boolean inValidationEnvelope) { + if (!nestingWithin(attributes, maxLevels)) { throw validationEx((inValidationEnvelope ? "1 validation error detected: " : "") + "Nesting Levels have exceeded supported limits: " + "Attributes in the item have nested levels beyond supported limit"); @@ -194,16 +203,16 @@ final class DynamoDbAttributeValueValidator { } } - static boolean valueNestingWithinLimit(JsonNode value) { - return depthOf(value) <= MAX_NESTING_LEVELS; + static boolean nestingWithinLimit(JsonNode attributes) { + return nestingWithin(attributes, MAX_NESTING_LEVELS); } - static boolean nestingWithinLimit(JsonNode attributes) { + private static boolean nestingWithin(JsonNode attributes, int maxLevels) { if (attributes == null || !attributes.isObject()) { return true; } for (JsonNode value : attributes) { - if (!valueNestingWithinLimit(value)) { + if (maxLevels < depthOf(value)) { return false; } } 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 a7494da077..5b367dd8cc 100644 --- a/src/main/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandler.java +++ b/src/main/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandler.java @@ -847,14 +847,14 @@ public class NativeDynamoDbJsonHandler { rejectMixedParameters(request, List.of("Expected", "ConditionalOperator"), List.of("ConditionExpression")); requireNonEmptyMap(exprAttrNames, "ExpressionAttributeNames"); requireNonEmptyMap(exprAttrValues, "ExpressionAttributeValues"); - DynamoDbAttributeValueValidator.requireNestingWithinLimit(exprAttrValues, false); + DynamoDbAttributeValueValidator.requireValueNestingWithinLimit(exprAttrValues, false); requireReadableValues(exprAttrValues, ""); if (conditionExpression == null) { requireAbsentWithoutExpression(exprAttrNames, "ExpressionAttributeNames"); requireAbsentWithoutExpression(exprAttrValues, "ExpressionAttributeValues"); } requireNonEmptyExpression(conditionExpression, "ConditionExpression"); - DynamoDbAttributeValueValidator.requireNestingWithinLimit(legacyValues(request.get("Expected")), false); + DynamoDbAttributeValueValidator.requireValueNestingWithinLimit(legacyValues(request.get("Expected")), false); DynamoDbExpressionSize.checkWrite(conditionExpression, "ConditionExpression"); ExpressionEvaluator.validateSyntax(conditionExpression, "ConditionExpression"); ExpressionEvaluator.validateSemantics(conditionExpression, "ConditionExpression", @@ -1009,11 +1009,11 @@ public class NativeDynamoDbJsonHandler { requireReadableValues(key, VALIDATION_ENVELOPE); DynamoDbExpressionSize.checkWrite(updateExpression, "UpdateExpression"); DynamoDbExpressionSize.checkWrite(conditionExpression, "ConditionExpression"); - DynamoDbAttributeValueValidator.requireNestingWithinLimit(exprAttrValues); + DynamoDbAttributeValueValidator.requireValueNestingWithinLimit(exprAttrValues, true); ObjectNode attributeUpdateValues = legacyValues(attributeUpdates); DynamoDbAttributeValueValidator.requireNestingWithinLimit(attributeUpdateValues); - DynamoDbAttributeValueValidator.requireNestingWithinLimit( - legacyValues(request.has("Expected") ? request.get("Expected") : null)); + DynamoDbAttributeValueValidator.requireValueNestingWithinLimit( + legacyValues(request.has("Expected") ? request.get("Expected") : null), true); JsonNode updateData = attributeUpdates.isMissingNode() ? null : attributeUpdates; JsonNode expectedUpd = request.has("Expected") ? request.get("Expected") : null; @@ -1390,9 +1390,9 @@ public class NativeDynamoDbJsonHandler { DynamoDbExpressionSize.checkRead(keyConditionExpr, "KeyConditionExpression"); DynamoDbExpressionSize.checkRead(filterExpr, "FilterExpression"); DynamoDbExpressionSize.checkRead(projectionExpression, "ProjectionExpression"); - DynamoDbAttributeValueValidator.requireNestingWithinLimit(exprAttrValues); - DynamoDbAttributeValueValidator.requireNestingWithinLimit(legacyValues(keyConditions), false); - DynamoDbAttributeValueValidator.requireNestingWithinLimit(legacyValues(queryFilter), false); + DynamoDbAttributeValueValidator.requireValueNestingWithinLimit(exprAttrValues, false); + DynamoDbAttributeValueValidator.requireValueNestingWithinLimit(legacyValues(keyConditions), false); + DynamoDbAttributeValueValidator.requireValueNestingWithinLimit(legacyValues(queryFilter), false); ExpressionEvaluator.validateSyntax(keyConditionExpr, "KeyConditionExpression"); ExpressionEvaluator.validateSyntax(filterExpr, "FilterExpression"); // DynamoDB reports filter errors first, then key-condition errors, then projection errors. @@ -1548,7 +1548,8 @@ public class NativeDynamoDbJsonHandler { DynamoDbExpressionSize.checkReadWithSize(filterExpr, "FilterExpression"); DynamoDbExpressionSize.checkRead(projectionExpressionScan, "ProjectionExpression"); - DynamoDbAttributeValueValidator.requireNestingWithinLimit(exprAttrValues); + DynamoDbAttributeValueValidator.requireValueNestingWithinLimit(exprAttrValues, false); + DynamoDbAttributeValueValidator.requireValueNestingWithinLimit(legacyValues(scanFilter), false); ExpressionEvaluator.validateSyntax(filterExpr, "FilterExpression"); // DynamoDB reports filter errors before projection errors. ExpressionEvaluator.validateSemantics(filterExpr, "FilterExpression", exprAttrNames, exprAttrValues); @@ -3097,7 +3098,7 @@ public class NativeDynamoDbJsonHandler { addPartiQLReturnValuesError(op, action.path("ReturnValuesOnConditionCheckFailure"), onFailureErrors); throwValidationErrors(onFailureErrors); // AWS checks the depth of every value, so even an unused or condition-only value fails the request. - DynamoDbAttributeValueValidator.requireNestingWithinLimit(op.get("ExpressionAttributeValues"), false); + DynamoDbAttributeValueValidator.requireValueNestingWithinLimit(op.get("ExpressionAttributeValues"), false); requireValidTransactExpressions(op, false); } if ("Update".equals(action.type()) && op.has("UpdateExpression")) { diff --git a/src/test/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandlerTest.java b/src/test/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandlerTest.java index 96d6858da5..e417973b16 100644 --- a/src/test/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandlerTest.java +++ b/src/test/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandlerTest.java @@ -2752,14 +2752,14 @@ class NativeDynamoDbJsonHandlerTest { } @Test - void updateItemRejectsAnExpressionAttributeValueWithALeafAtLevel33() { + void updateItemRejectsAnExpressionAttributeValueWithALeafAtLevel34() { createUsersTable("eu-west-1"); ObjectNode request = updateUserRequest(); request.put("UpdateExpression", "SET touched = :t"); request.put("ConditionExpression", "#d = :deep"); request.set("ExpressionAttributeNames", mapper.createObjectNode().put("#d", "data")); ObjectNode values = singleValue(":t"); - values.set(":deep", nestedMaps(32)); + values.set(":deep", nestedMaps(33)); request.set("ExpressionAttributeValues", values); AwsException ex = assertThrows(AwsException.class, @@ -2931,12 +2931,12 @@ class NativeDynamoDbJsonHandlerTest { } @Test - void transactWriteItemsRejectsAnUpdateValueWithALeafAtLevel33() { + void transactWriteItemsRejectsAnUpdateValueWithALeafAtLevel34() { createUsersTable("eu-west-1"); ObjectNode update = mapper.createObjectNode(); update.set("Key", item("userId", "u1")); update.put("UpdateExpression", "SET deep = :deep"); - update.set("ExpressionAttributeValues", mapper.createObjectNode().set(":deep", nestedMaps(32))); + update.set("ExpressionAttributeValues", mapper.createObjectNode().set(":deep", nestedMaps(33))); AwsException ex = assertThrows(AwsException.class, () -> handler.handle("TransactWriteItems", transactWrite("Update", update), "eu-west-1")); @@ -2946,13 +2946,63 @@ class NativeDynamoDbJsonHandlerTest { assertNull(service.getItem("Users", item("userId", "u1"), "eu-west-1")); } + @ParameterizedTest + @CsvSource(delimiter = '|', textBlock = """ + UpdateItem | {"TableName":"Users","Key":{"userId":{"S":"u1"}},"UpdateExpression":"SET t = :t","ConditionExpression":"#d = :deep","ExpressionAttributeNames":{"#d":"d"},"ExpressionAttributeValues":{":t":{"S":"y"},":deep":"DEEP"}} + PutItem | {"TableName":"Users","Item":{"userId":{"S":"u1"}},"ConditionExpression":"#d = :deep","ExpressionAttributeNames":{"#d":"d"},"ExpressionAttributeValues":{":deep":"DEEP"}} + DeleteItem | {"TableName":"Users","Key":{"userId":{"S":"u1"}},"ConditionExpression":"#d = :deep","ExpressionAttributeNames":{"#d":"d"},"ExpressionAttributeValues":{":deep":"DEEP"}} + PutItem | {"TableName":"Users","Item":{"userId":{"S":"u1"}},"Expected":{"d":{"Value":"DEEP"}}} + """) + void aConditionValueWithALeafAtLevel33IsEvaluated(String operation, String request) throws Exception { + createUsersTable("eu-west-1"); + service.putItem("Users", item("userId", "u1"), "eu-west-1"); + JsonNode body = mapper.readTree(request.replace("\"DEEP\"", mapper.writeValueAsString(nestedMaps(32)))); + + AwsException ex = assertThrows(AwsException.class, () -> handler.handle(operation, body, "eu-west-1")); + assertEquals("ConditionalCheckFailedException", ex.getErrorCode()); + } + + @ParameterizedTest + @CsvSource(delimiter = '|', textBlock = """ + Scan | {"TableName":"Users","FilterExpression":"#d = :deep","ExpressionAttributeNames":{"#d":"d"},"ExpressionAttributeValues":{":deep":"DEEP"}} + Scan | {"TableName":"Users","ScanFilter":{"d":{"ComparisonOperator":"EQ","AttributeValueList":["DEEP"]}}} + Query | {"TableName":"Users","KeyConditionExpression":"userId = :u","FilterExpression":"#d = :deep","ExpressionAttributeNames":{"#d":"d"},"ExpressionAttributeValues":{":u":{"S":"u1"},":deep":"DEEP"}} + """) + void aReadFiltersOnAValueWithALeafAtLevel33AndRefusesOneAtLevel34Bare(String operation, String request) + throws Exception { + createUsersTable("eu-west-1"); + JsonNode accepted = mapper.readTree(request.replace("\"DEEP\"", mapper.writeValueAsString(nestedMaps(32)))); + JsonNode refused = mapper.readTree(request.replace("\"DEEP\"", mapper.writeValueAsString(nestedMaps(33)))); + + assertEquals(200, handler.handle(operation, accepted, "eu-west-1").getStatus()); + AwsException ex = assertThrows(AwsException.class, () -> handler.handle(operation, refused, "eu-west-1")); + assertEquals("Nesting Levels have exceeded supported limits: " + + "Attributes in the item have nested levels beyond supported limit", ex.getMessage()); + } + @Test - void transactWriteItemsRejectsAConditionCheckValueWithALeafAtLevel33BeforeTheTableLookup() { + void anUpdateThatWritesAValueWithALeafAtLevel33FailsOnTheFinishedItem() throws Exception { + createUsersTable("eu-west-1"); + ObjectNode request = updateUserRequest(); + request.put("UpdateExpression", "SET deep = :deep"); + request.set("ExpressionAttributeValues", mapper.createObjectNode().set(":deep", nestedMaps(32))); + ObjectNode update = request.deepCopy(); + update.remove("TableName"); + + AwsException ex = assertThrows(AwsException.class, () -> handler.handle("UpdateItem", request, "eu-west-1")); + assertEquals("Nesting Levels have exceeded supported limits", ex.getMessage()); + assertEquals("Nesting Levels have exceeded supported limits", + onlyValidationErrorMessage(handler.handle("TransactWriteItems", transactWrite("Update", update), "eu-west-1"))); + assertNull(service.getItem("Users", item("userId", "u1"), "eu-west-1")); + } + + @Test + void transactWriteItemsRejectsAConditionCheckValueWithALeafAtLevel34BeforeTheTableLookup() { ObjectNode check = mapper.createObjectNode(); check.set("Key", item("userId", "u1")); check.put("ConditionExpression", "#d = :deep"); check.set("ExpressionAttributeNames", mapper.createObjectNode().put("#d", "data")); - check.set("ExpressionAttributeValues", mapper.createObjectNode().set(":deep", nestedMaps(32))); + check.set("ExpressionAttributeValues", mapper.createObjectNode().set(":deep", nestedMaps(33))); AwsException ex = assertThrows(AwsException.class, () -> handler.handle("TransactWriteItems", transactWrite("ConditionCheck", check), "eu-west-1")); @@ -2962,11 +3012,11 @@ class NativeDynamoDbJsonHandlerTest { } @Test - void transactWriteItemsRejectsAnUnusedValueWithALeafAtLevel33() { + void transactWriteItemsRejectsAnUnusedValueWithALeafAtLevel34() { createUsersTable("eu-west-1"); ObjectNode delete = mapper.createObjectNode(); delete.set("Key", item("userId", "u1")); - delete.set("ExpressionAttributeValues", mapper.createObjectNode().set(":deep", nestedMaps(32))); + delete.set("ExpressionAttributeValues", mapper.createObjectNode().set(":deep", nestedMaps(33))); AwsException ex = assertThrows(AwsException.class, () -> handler.handle("TransactWriteItems", transactWrite("Delete", delete), "eu-west-1")); @@ -2981,7 +3031,7 @@ class NativeDynamoDbJsonHandlerTest { ObjectNode deepCheck = (ObjectNode) deep.get("ConditionCheck"); deepCheck.put("ConditionExpression", "#d = :deep"); deepCheck.set("ExpressionAttributeNames", mapper.createObjectNode().put("#d", "data")); - deepCheck.set("ExpressionAttributeValues", mapper.createObjectNode().set(":deep", nestedMaps(32))); + deepCheck.set("ExpressionAttributeValues", mapper.createObjectNode().set(":deep", nestedMaps(33))); ObjectNode broken = transactMember("ConditionCheck", "Users", "Key", item("userId", "u2")); ((ObjectNode) broken.get("ConditionCheck")).put("ConditionExpression", "attribute_exists("); @@ -3600,11 +3650,11 @@ class NativeDynamoDbJsonHandlerTest { } @Test - void putItemRejectsAnExpectedValueWithALeafAtLevel33() { + void putItemRejectsAnExpectedValueWithALeafAtLevel34() { createUsersTable("eu-west-1"); ObjectNode request = putRequest(attributeValue("S", "x")); request.set("Expected", mapper.createObjectNode().set("data", - mapper.createObjectNode().set("Value", nestedMaps(32)))); + mapper.createObjectNode().set("Value", nestedMaps(33)))); AwsException ex = assertThrows(AwsException.class, () -> handler.handle("PutItem", request, "eu-west-1")); @@ -3612,7 +3662,7 @@ class NativeDynamoDbJsonHandlerTest { } @Test - void queryRejectsAQueryFilterValueWithALeafAtLevel33WithoutTheEnvelope() { + void queryRejectsAQueryFilterValueWithALeafAtLevel34WithoutTheEnvelope() { createUsersTable("eu-west-1"); ObjectNode request = mapper.createObjectNode(); request.put("TableName", "Users"); @@ -3622,7 +3672,7 @@ class NativeDynamoDbJsonHandlerTest { request.set("KeyConditions", mapper.createObjectNode().set("userId", keyCondition)); ObjectNode filter = mapper.createObjectNode(); filter.put("ComparisonOperator", "EQ"); - filter.set("AttributeValueList", mapper.createArrayNode().add(nestedMaps(32))); + filter.set("AttributeValueList", mapper.createArrayNode().add(nestedMaps(33))); request.set("QueryFilter", mapper.createObjectNode().set("data", filter)); AwsException ex = assertThrows(AwsException.class,