diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java index 11baefd1a9..ce549b0da6 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java @@ -2569,6 +2569,9 @@ public class NativeDynamoDbJsonHandler { required.add(SmithyValidationErrors.lengthTooShort(action.path(member), "", 1)); } } + if (write) { + addPartiQLReturnValuesError(action.op(), action.path("ReturnValuesOnConditionCheckFailure"), required); + } } boolean older = transactDepth(transactItems, ops) >= OLDER_VALIDATOR_DEPTH; if (!missing && !older) { @@ -2579,8 +2582,9 @@ public class NativeDynamoDbJsonHandler { required.addFirst("Value '" + transactItems + "' at 'transactItems' failed to satisfy constraint: " + "Member must have length less than or equal to " + MAX_TRANSACT_ITEMS); } - // The older validator stops at a missing member, before the operation and name checks. - if (!required.isEmpty()) { + // The older validator stops at a missing member or an unknown ReturnValuesOnConditionCheckFailure, + // before the operation and name checks. The newer one leaves that enum to the per-action checks. + if ((missing || older) && !required.isEmpty()) { required.addAll(memberErrors); throwValidationErrors(required); } @@ -2633,26 +2637,45 @@ public class NativeDynamoDbJsonHandler { throwValidationErrors(validationErrors); } - // DynamoDB checks an action's ConditionExpression in full before its UpdateExpression. + // DynamoDB checks an action's ConditionExpression in full before its UpdateExpression. In a request too deep + // for the newer validator, it checks the expressions before the action's item or key. private void requireValidTransactAction(TransactAction action, boolean older) { JsonNode op = action.op(); - String conditionExpression = textOrNull(op, "ConditionExpression"); - String updateExpression = textOrNull(op, "UpdateExpression"); - JsonNode exprAttrNames = op.get("ExpressionAttributeNames"); - JsonNode exprAttrValues = op.get("ExpressionAttributeValues"); - DynamoDbAttributeValueValidator.requireNestingWithinLimit(op.get("Item"), false); - DynamoDbAttributeValueValidator.requireNestingWithinLimit(op.get("Key"), false); - requireReadableValues(op.get("Put".equals(action.type()) ? "Item" : "Key"), ""); + if (older) { + requireValidTransactExpressions(op, true); + requireTransactItemOrKey(action); + return; + } + requireTransactItemOrKey(action); List onFailureErrors = new ArrayList<>(); 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(exprAttrValues, false); + DynamoDbAttributeValueValidator.requireNestingWithinLimit(op.get("ExpressionAttributeValues"), false); + requireValidTransactExpressions(op, false); + } + + private static void requireTransactItemOrKey(TransactAction action) { + JsonNode op = action.op(); + DynamoDbAttributeValueValidator.requireNestingWithinLimit(op.get("Item"), false); + DynamoDbAttributeValueValidator.requireNestingWithinLimit(op.get("Key"), false); + requireReadableValues(op.get("Put".equals(action.type()) ? "Item" : "Key"), ""); + } + + private void requireValidTransactExpressions(JsonNode op, boolean older) { + String conditionExpression = textOrNull(op, "ConditionExpression"); + String updateExpression = textOrNull(op, "UpdateExpression"); + JsonNode exprAttrNames = op.get("ExpressionAttributeNames"); + JsonNode exprAttrValues = op.get("ExpressionAttributeValues"); // An action without an expression ignores its ExpressionAttributeNames and ExpressionAttributeValues. if (conditionExpression == null && updateExpression == null) { return; } - requireReadableValues(exprAttrValues, ""); + if (older) { + requireNamedExpressionValues(exprAttrValues); + } else { + requireReadableValues(exprAttrValues, ""); + } requireNonEmptyExpression(conditionExpression, "ConditionExpression"); // Only a request too deep for the newer validator reports the condition's size. @@ -2677,6 +2700,23 @@ public class NativeDynamoDbJsonHandler { checkUnusedEav(exprAttrValues, extractColonTokens(conditionExpression, updateExpression)); } + // The older validator names the ExpressionAttributeValues key it refuses. + private void requireNamedExpressionValues(JsonNode exprAttrValues) { + if (exprAttrValues == null) { + return; + } + for (Map.Entry entry : exprAttrValues.properties()) { + ObjectNode value = objectMapper.createObjectNode().set(entry.getKey(), entry.getValue()); + try { + DynamoDbAttributeValueValidator.requireNestingWithinLimit(value, false); + requireReadableValues(value, ""); + } catch (AwsException e) { + throw new AwsException("ValidationException", "ExpressionAttributeValues contains invalid value: " + + e.getMessage() + " for key " + entry.getKey(), 400); + } + } + } + private static void requireNonEmptyExpression(String expression, String exprType) { if (expression != null && expression.isEmpty()) { throw new AwsException("ValidationException", diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandlerTest.java index 7fa3675544..9c479955d8 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandlerTest.java @@ -2351,6 +2351,7 @@ 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] + 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 { AwsException ex = assertThrows(AwsException.class, @@ -2379,6 +2380,27 @@ class NativeDynamoDbJsonHandlerTest { assertEquals(message, ex.getMessage()); } + @ParameterizedTest + @CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """ + [{"Delete":{"TableName":"Missing","Key":{"k":"DEEP"},"ConditionExpression":""}}] | Invalid ConditionExpression: The expression can not be empty; + [{"Put":{"TableName":"Missing","Item":{"d":"DEEP"},"ConditionExpression":"a = :zz"}}] | Invalid ConditionExpression: An expression attribute value used in expression is not defined; attribute value: :zz + [{"Delete":{"TableName":"Missing","Key":{"k":"DEEP"},"ConditionExpression":"attribute_exists(a)","ExpressionAttributeValues":{":v":{"S":"x"}}}}] | Value provided in ExpressionAttributeValues unused in expressions: keys: {:v} + [{"ConditionCheck":{"TableName":"Missing","Key":{},"ConditionExpression":"d = :deep","ExpressionAttributeValues":{":deep":"DEEP"}}}] | ExpressionAttributeValues contains invalid value: Nesting Levels have exceeded supported limits: Attributes in the item have nested levels beyond supported limit for key :deep + [{"Put":{"TableName":"Missing","Item":{"d":"DEEP"},"ConditionExpression":"a = :n","ExpressionAttributeValues":{":n":{"N":"abc"}}}}] | ExpressionAttributeValues contains invalid value: The parameter cannot be converted to a numeric value: abc for key :n + [{"Put":{"TableName":"Missing","Item":{},"ExpressionAttributeValues":{":deep":"DEEP"}}}] | Requested resource not found + [{"Put":{"TableName":"bad!name","Item":{"d":"DEEP"},"ReturnValuesOnConditionCheckFailure":"BOGUS"}}] | 1 validation error detected: Value 'BOGUS' at 'transactItems.1.member.put.returnValuesOnConditionCheckFailure' failed to satisfy constraint: Member must satisfy enum value set: [ALL_OLD, NONE] + """) + void aTransactionTooDeepForTheNewerValidatorChecksItsExpressionsBeforeItsItem(String items, String message) + throws Exception { + ObjectNode request = mapper.createObjectNode(); + request.set("TransactItems", mapper.readTree(items.replace("\"DEEP\"", + mapper.writeValueAsString(nestedMaps(61))))); + + AwsException ex = assertThrows(AwsException.class, + () -> handler.handle("TransactWriteItems", request, "eu-west-1")); + assertEquals(message, ex.getMessage()); + } + @ParameterizedTest @CsvSource(delimiter = '|', textBlock = """ TransactWriteItems | {"TransactItems":[]} | 1 validation error detected: Value at 'TransactItems' failed to satisfy constraint: Member must have length greater than or equal to 1