diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluator.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluator.java index 61e9fa2234..6d5f82a7a9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluator.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluator.java @@ -395,25 +395,41 @@ final class ExpressionEvaluator { */ static void validateExpression(String expression, String exprType, JsonNode exprAttrNames, JsonNode exprAttrValues) { - if (expression == null || expression.isBlank()) return; - List tokens; - Expr expr; + validateSyntax(expression, exprType); + validateSemantics(expression, exprType, exprAttrNames, exprAttrValues); + } + + /** + * The tokenize/parse/redundant-parentheses half of {@link #validateExpression}, split out so + * callers can run an undefined-#name/:value check between this and {@link #validateSemantics}: + * DynamoDB reports a syntax or redundant-parentheses error before an undefined placeholder, + * but an undefined placeholder before a semantic error like {@code contains(x, x)}. + */ + static void validateSyntax(String expression, String exprType) { + if (expression == null || expression.isBlank()) { + return; + } try { - tokens = tokenize(expression.trim()); + List tokens = tokenize(expression.trim()); checkRedundantParentheses(tokens, exprType); - expr = parse(expression); + parse(expression); } catch (IllegalArgumentException e) { String detail = e.getMessage(); - + if (detail.startsWith("token:")) { - throw new AwsException("ValidationException", + throw new AwsException("ValidationException", "Invalid " + exprType + ": Syntax error; " + detail, 400); } else { - throw new AwsException("ValidationException", + throw new AwsException("ValidationException", "Invalid " + exprType + ": Syntax error", 400); } } - validateSemantics(expr, exprType, exprAttrNames, exprAttrValues); + } + + /** The semantic half of {@link #validateExpression}; see {@link #validateSyntax}. */ + static void validateSemantics(String expression, String exprType, + JsonNode exprAttrNames, JsonNode exprAttrValues) { + validateSemantics(parse(expression), exprType, exprAttrNames, exprAttrValues); } // A pair of parentheses is redundant when its entire content is itself a single 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 b8e693aebb..95f6234942 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 @@ -566,7 +566,9 @@ public class NativeDynamoDbJsonHandler { rejectExprAttrsWithoutExpression(exprAttrNames, exprAttrValues, conditionExpression != null, "ConditionExpression is null"); - ExpressionEvaluator.validateExpression(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues); + ExpressionEvaluator.validateSyntax(conditionExpression, "ConditionExpression"); + requireDefinedTokens(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues); + ExpressionEvaluator.validateSemantics(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues); // Reject ExpressionAttributeNames/Values entries not referenced by ConditionExpression (AWS parity, #2893) checkUnusedEan(exprAttrNames, extractHashTokens(conditionExpression)); @@ -695,7 +697,9 @@ public class NativeDynamoDbJsonHandler { rejectExprAttrsWithoutExpression(exprAttrNames, exprAttrValues, conditionExpression != null, "ConditionExpression is null"); - ExpressionEvaluator.validateExpression(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues); + ExpressionEvaluator.validateSyntax(conditionExpression, "ConditionExpression"); + requireDefinedTokens(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues); + ExpressionEvaluator.validateSemantics(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues); // Reject ExpressionAttributeNames/Values entries not referenced by ConditionExpression (AWS parity, #2893) checkUnusedEan(exprAttrNames, extractHashTokens(conditionExpression)); @@ -777,7 +781,7 @@ public class NativeDynamoDbJsonHandler { updateExpression != null || conditionExpression != null, "UpdateExpression is null, ConditionExpression is null"); - ExpressionEvaluator.validateExpression(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues); + ExpressionEvaluator.validateSyntax(conditionExpression, "ConditionExpression"); if (conditionExpression != null && expectedUpd != null) { throw new AwsException("ValidationException", @@ -803,18 +807,19 @@ public class NativeDynamoDbJsonHandler { "Invalid UpdateExpression: Syntax error; token: \"" + token + "\", near: \"" + near + "\"", 400); } } - Set colonTokens = extractColonTokens(updateExpression); - for (String token : colonTokens) { - if (exprAttrValues == null || !exprAttrValues.has(token)) { - throw new AwsException("ValidationException", - "Invalid UpdateExpression: An expression attribute value used in expression is not defined; attribute value: " + token, 400); - } - } + // Undefined tokens, then ConditionExpression's semantic errors, must both be caught + // before the unused-EAN/EAV checks below. + requireDefinedTokens(updateExpression, "UpdateExpression", exprAttrNames, exprAttrValues); + requireDefinedTokens(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues); + ExpressionEvaluator.validateSemantics(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues); Set hashTokens = extractHashTokens(updateExpression, conditionExpression); checkUnusedEan(exprAttrNames, hashTokens); Set colonTokensAll = extractColonTokens(updateExpression, conditionExpression); checkUnusedEav(exprAttrValues, colonTokensAll); dynamoDbService.requireAddOrDeleteOperandTypes(updateExpression, exprAttrValues, true); + } else { + requireDefinedTokens(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues); + ExpressionEvaluator.validateSemantics(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues); } if (expectedUpd != null) { @@ -1072,9 +1077,16 @@ public class NativeDynamoDbJsonHandler { DynamoDbNumberUtils.requireStorable(exprAttrValues, false); DynamoDbAttributeValueValidator.requireNestingWithinLimit(legacyValues(keyConditions), false); DynamoDbAttributeValueValidator.requireNestingWithinLimit(legacyValues(queryFilter), false); - ExpressionEvaluator.validateExpression(keyConditionExpr, "KeyConditionExpression", exprAttrNames, exprAttrValues); - ExpressionEvaluator.validateExpression(filterExpr, "FilterExpression", exprAttrNames, exprAttrValues); + ExpressionEvaluator.validateSyntax(keyConditionExpr, "KeyConditionExpression"); + ExpressionEvaluator.validateSyntax(filterExpr, "FilterExpression"); + // DynamoDB reports filter errors before projection errors, and projection before key-condition. + requireDefinedTokens(filterExpr, "FilterExpression", exprAttrNames, exprAttrValues); + ExpressionEvaluator.validateSemantics(filterExpr, "FilterExpression", exprAttrNames, exprAttrValues); ProjectionEvaluator.validateExpression(projectionExpression, exprAttrNames); + // Must run before DynamoDbAccessPathValidator below: an unresolved #name there falls + // through to "Query condition missed key schema element" instead of the real cause. + requireDefinedTokens(keyConditionExpr, "KeyConditionExpression", exprAttrNames, exprAttrValues); + ExpressionEvaluator.validateSemantics(keyConditionExpr, "KeyConditionExpression", exprAttrNames, exprAttrValues); if (select != null && !VALID_SELECT.contains(select)) { throw new AwsException("ValidationException", @@ -1097,15 +1109,6 @@ public class NativeDynamoDbJsonHandler { projectionExpression, attributesToGet, exprAttrNames); rejectConsistentReadOnGsi(request, queryAccessPath); - // Check undefined #tokens in FilterExpression - if (filterExpr != null) { - for (String token : extractHashTokens(filterExpr)) { - if (exprAttrNames == null || !exprAttrNames.has(token)) { - throw new AwsException("ValidationException", - "Invalid FilterExpression: An expression attribute name used in the document path is not defined; attribute name: " + token, 400); - } - } - } // Reject ExpressionAttributeNames/Values entries not referenced by any expression // (AWS parity, #2893). Unconditional: a legacy KeyConditions request that still // carries EAN/EAV must be rejected, and either map can only be referenced from @@ -1244,7 +1247,10 @@ public class NativeDynamoDbJsonHandler { DynamoDbExpressionSize.checkRead(projectionExpressionScan, "ProjectionExpression"); DynamoDbAttributeValueValidator.requireNestingWithinLimit(exprAttrValues); DynamoDbNumberUtils.requireStorable(exprAttrValues, false); - ExpressionEvaluator.validateExpression(filterExpr, "FilterExpression", exprAttrNames, exprAttrValues); + ExpressionEvaluator.validateSyntax(filterExpr, "FilterExpression"); + // DynamoDB reports filter errors before projection errors. + requireDefinedTokens(filterExpr, "FilterExpression", exprAttrNames, exprAttrValues); + ExpressionEvaluator.validateSemantics(filterExpr, "FilterExpression", exprAttrNames, exprAttrValues); ProjectionEvaluator.validateExpression(projectionExpressionScan, exprAttrNames); // Reject ExpressionAttributeNames/Values entries not referenced by any expression (AWS parity, #2893) @@ -1858,14 +1864,22 @@ public class NativeDynamoDbJsonHandler { for (JsonNode op : txItem) { String conditionExpression = op.has("ConditionExpression") ? op.get("ConditionExpression").asText() : null; + String updateExpression = op.has("UpdateExpression") + ? op.get("UpdateExpression").asText() : null; + JsonNode opExprAttrNames = op.get("ExpressionAttributeNames"); + JsonNode opExprAttrValues = op.get("ExpressionAttributeValues"); DynamoDbExpressionSize.checkRead(op.path("UpdateExpression").textValue(), "UpdateExpression"); DynamoDbExpressionSize.checkReadWithSize(conditionExpression, "ConditionExpression"); - ExpressionEvaluator.validateExpression(conditionExpression, "ConditionExpression", - op.get("ExpressionAttributeNames"), - op.get("ExpressionAttributeValues")); + ExpressionEvaluator.validateSyntax(conditionExpression, "ConditionExpression"); + // Undefined tokens must fail here as a ValidationException, never a + // TransactionCanceledException with a false ConditionalCheckFailed reason. + requireDefinedTokens(updateExpression, "UpdateExpression", opExprAttrNames, opExprAttrValues); + requireDefinedTokens(conditionExpression, "ConditionExpression", opExprAttrNames, opExprAttrValues); + ExpressionEvaluator.validateSemantics(conditionExpression, "ConditionExpression", + opExprAttrNames, opExprAttrValues); DynamoDbAttributeValueValidator.requireNestingWithinLimit(op.get("Item"), false); dynamoDbService.requireAddOrDeleteOperandTypes(op.path("UpdateExpression").textValue(), - op.get("ExpressionAttributeValues"), false); + opExprAttrValues, false); } } @@ -2314,32 +2328,83 @@ public class NativeDynamoDbJsonHandler { // ── Expression token utilities ── - private static void extractPrefixedTokens(String expr, char prefix, Set out) { - if (expr == null) return; + // A #name or :value placeholder found while scanning an expression left to right. + private record ExprToken(char prefix, String text) {} + + // Scans an expression once, left to right, for #name and :value placeholders, in the order + // they occur in the text. Both extractHashTokens/extractColonTokens (membership checks) and + // requireDefinedTokens (first-undefined-token-wins) build on this single scan. + private static List scanExprTokens(String expr) { + List tokens = new ArrayList<>(); + if (expr == null) { + return tokens; + } int i = 0; while (i < expr.length()) { - if (expr.charAt(i) == prefix) { - int start = i++; - while (i < expr.length() && (Character.isLetterOrDigit(expr.charAt(i)) || expr.charAt(i) == '_')) i++; - if (i > start + 1) out.add(expr.substring(start, i)); - } else { + char c = expr.charAt(i); + if (c != '#' && c != ':') { + i++; + continue; + } + int start = i++; + while (i < expr.length() && (Character.isLetterOrDigit(expr.charAt(i)) || expr.charAt(i) == '_')) { i++; } + if (i > start + 1) { + tokens.add(new ExprToken(c, expr.substring(start, i))); + } } + return tokens; } private static Set extractHashTokens(String... expressions) { Set tokens = new LinkedHashSet<>(); - for (String expr : expressions) extractPrefixedTokens(expr, '#', tokens); + for (String expr : expressions) { + for (ExprToken t : scanExprTokens(expr)) { + if (t.prefix() == '#') { + tokens.add(t.text()); + } + } + } return tokens; } private static Set extractColonTokens(String... expressions) { Set tokens = new LinkedHashSet<>(); - for (String expr : expressions) extractPrefixedTokens(expr, ':', tokens); + for (String expr : expressions) { + for (ExprToken t : scanExprTokens(expr)) { + if (t.prefix() == ':') { + tokens.add(t.text()); + } + } + } return tokens; } + /** + * Rejects the first #name or :value token, in textual order, that neither + * ExpressionAttributeNames nor ExpressionAttributeValues defines. This matches DynamoDB for + * condition, key-condition and filter expressions, and for a single-action update expression; + * with several undefined tokens spread across an UpdateExpression's actions/clauses, DynamoDB + * may name a different one, though always with this same error type and message form. + */ + private static void requireDefinedTokens(String expression, String exprType, + JsonNode exprAttrNames, JsonNode exprAttrValues) { + for (ExprToken token : scanExprTokens(expression)) { + if (token.prefix() == '#') { + if (exprAttrNames == null || !exprAttrNames.has(token.text())) { + throw new AwsException("ValidationException", + "Invalid " + exprType + ": An expression attribute name used in the document path " + + "is not defined; attribute name: " + token.text(), 400); + } + } else if (exprAttrValues == null || !exprAttrValues.has(token.text())) { + throw new AwsException("ValidationException", + "Invalid " + exprType + ": An expression attribute value used in expression is not " + + "defined; attribute value: " + token.text(), 400); + } + } + } + /** * Rejects ExpressionAttributeNames/Values supplied when the request carries no expression to * reference them. AWS answers this sub-case with "<param> can only be specified when using diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java index 1686298fc8..8811bb78dc 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java @@ -4805,6 +4805,851 @@ given() deleteTable(tableName); } + // A filter's semantic error is reported before an undefined #name in the projection. + @Test + void filterSemanticErrorWinsOverUndefinedProjectionName() { + String tableName = "FilterVsProjectionTable"; + + given() + .header("X-Amz-Target", "DynamoDB_20120810.CreateTable") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "KeySchema": [ + {"AttributeName": "pk", "KeyType": "HASH"}, + {"AttributeName": "sk", "KeyType": "RANGE"} + ], + "AttributeDefinitions": [ + {"AttributeName": "pk", "AttributeType": "S"}, + {"AttributeName": "sk", "AttributeType": "S"} + ], + "BillingMode": "PAY_PER_REQUEST" + } + """.formatted(tableName)) + .when().post("/") + .then().statusCode(200); + + given() + .header("X-Amz-Target", "DynamoDB_20120810.Query") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "KeyConditionExpression": "pk = :pk", + "FilterExpression": "contains(a, a)", + "ProjectionExpression": "#p", + "ExpressionAttributeValues": {":pk": {"S": "a"}} + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid FilterExpression: The first operand must be distinct " + + "from the remaining operands for this operator or function; operator: contains, " + + "first operand: [a]")); + + given() + .header("X-Amz-Target", "DynamoDB_20120810.Scan") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "FilterExpression": "contains(a, a)", + "ProjectionExpression": "#p" + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid FilterExpression: The first operand must be distinct " + + "from the remaining operands for this operator or function; operator: contains, " + + "first operand: [a]")); + + deleteTable(tableName); + } + + // An undefined #name/:value in ConditionExpression must reject, not false-fail the condition. + @Test + void undefinedTokensInConditionExpressionAreRejected() { + String tableName = "ConditionExprTokenTable"; + + given() + .header("X-Amz-Target", "DynamoDB_20120810.CreateTable") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "KeySchema": [ + {"AttributeName": "pk", "KeyType": "HASH"}, + {"AttributeName": "sk", "KeyType": "RANGE"} + ], + "AttributeDefinitions": [ + {"AttributeName": "pk", "AttributeType": "S"}, + {"AttributeName": "sk", "AttributeType": "S"} + ], + "BillingMode": "PAY_PER_REQUEST" + } + """.formatted(tableName)) + .when().post("/") + .then().statusCode(200); + + // PutItem: ConditionExpression references an undefined :value. + given() + .header("X-Amz-Target", "DynamoDB_20120810.PutItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Item": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "ConditionExpression": "sk = :v" + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid ConditionExpression: An expression attribute value " + + "used in expression is not defined; attribute value: :v")); + + // PutItem: ConditionExpression references an undefined #name. + given() + .header("X-Amz-Target", "DynamoDB_20120810.PutItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Item": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "ConditionExpression": "#n = sk" + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid ConditionExpression: An expression attribute name " + + "used in the document path is not defined; attribute name: #n")); + + // Both undefined; the textually-first token (:v) is reported, not names before values. + given() + .header("X-Amz-Target", "DynamoDB_20120810.PutItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Item": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "ConditionExpression": ":v = #n" + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid ConditionExpression: An expression attribute value " + + "used in expression is not defined; attribute value: :v")); + + // The undefined name must be reported ahead of contains()'s distinct-operand semantic error. + given() + .header("X-Amz-Target", "DynamoDB_20120810.PutItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Item": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "ConditionExpression": "contains(#missing, #missing)" + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid ConditionExpression: An expression attribute name " + + "used in the document path is not defined; attribute name: #missing")); + + // DeleteItem: ConditionExpression references an undefined :value. + given() + .header("X-Amz-Target", "DynamoDB_20120810.DeleteItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Key": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "ConditionExpression": "sk = :v" + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid ConditionExpression: An expression attribute value " + + "used in expression is not defined; attribute value: :v")); + + // DeleteItem: ConditionExpression references an undefined #name. + given() + .header("X-Amz-Target", "DynamoDB_20120810.DeleteItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Key": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "ConditionExpression": "#n = sk" + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid ConditionExpression: An expression attribute name " + + "used in the document path is not defined; attribute name: #n")); + + // UpdateItem: valid UpdateExpression, but ConditionExpression is undefined. + given() + .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Key": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "UpdateExpression": "SET payload = :d", + "ExpressionAttributeValues": {":d": {"S": "x"}}, + "ConditionExpression": "sk = :v" + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid ConditionExpression: An expression attribute value " + + "used in expression is not defined; attribute value: :v")); + + // UpdateItem: ConditionExpression references an undefined #name. + given() + .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Key": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "UpdateExpression": "SET payload = :d", + "ExpressionAttributeValues": {":d": {"S": "x"}}, + "ConditionExpression": "#n = sk" + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid ConditionExpression: An expression attribute name " + + "used in the document path is not defined; attribute name: #n")); + + // Undefined :v must win over the unused-EAV check for the stray :z. + given() + .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Key": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "UpdateExpression": "SET payload = :d", + "ConditionExpression": "sk = :v", + "ExpressionAttributeValues": {":d": {"S": "x"}, ":z": {"S": "unused"}} + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid ConditionExpression: An expression attribute value " + + "used in expression is not defined; attribute value: :v")); + + // contains()'s distinct-operand error must win over the unused-EAN check for the stray #z. + given() + .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Key": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "UpdateExpression": "SET payload = :d", + "ConditionExpression": "contains(#m, #m)", + "ExpressionAttributeNames": {"#m": "sk", "#z": "unused"}, + "ExpressionAttributeValues": {":d": {"S": "x"}} + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid ConditionExpression: The first operand must be distinct " + + "from the remaining operands for this operator or function; operator: contains, " + + "first operand: [sk]")); + + // Edge case: nested path #a.#b, only #a defined. + given() + .header("X-Amz-Target", "DynamoDB_20120810.PutItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Item": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "ConditionExpression": "attribute_not_exists(#a.#b)", + "ExpressionAttributeNames": {"#a": "payload"} + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid ConditionExpression: An expression attribute name " + + "used in the document path is not defined; attribute name: #b")); + + // Edge case: the same undefined token used twice must still report once, not crash. + given() + .header("X-Amz-Target", "DynamoDB_20120810.PutItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Item": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "ConditionExpression": "sk = :v OR pk = :v" + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid ConditionExpression: An expression attribute value " + + "used in expression is not defined; attribute value: :v")); + + // Control: a different token than the one supplied is missing and must be named. + given() + .header("X-Amz-Target", "DynamoDB_20120810.PutItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Item": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "ConditionExpression": "#n = :v", + "ExpressionAttributeNames": {"#n": "sk"}, + "ExpressionAttributeValues": {":other": {"S": "x"}} + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid ConditionExpression: An expression attribute value " + + "used in expression is not defined; attribute value: :v")); + + // Control: a fully-defined ConditionExpression is still accepted; seed the item first. + given() + .header("X-Amz-Target", "DynamoDB_20120810.PutItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Item": {"pk": {"S": "a"}, "sk": {"S": "b"}} + } + """.formatted(tableName)) + .when().post("/") + .then().statusCode(200); + + given() + .header("X-Amz-Target", "DynamoDB_20120810.PutItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Item": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "ConditionExpression": "#n = :v", + "ExpressionAttributeNames": {"#n": "sk"}, + "ExpressionAttributeValues": {":v": {"S": "b"}} + } + """.formatted(tableName)) + .when().post("/") + .then().statusCode(200); + + deleteTable(tableName); + } + + // Query/Scan must reject an undefined #name/:value, never silently treat it as "no match". + @Test + void undefinedTokensInKeyConditionAndFilterExpressionsAreRejected() { + String tableName = "QueryScanTokenTable"; + + given() + .header("X-Amz-Target", "DynamoDB_20120810.CreateTable") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "KeySchema": [ + {"AttributeName": "pk", "KeyType": "HASH"}, + {"AttributeName": "sk", "KeyType": "RANGE"} + ], + "AttributeDefinitions": [ + {"AttributeName": "pk", "AttributeType": "S"}, + {"AttributeName": "sk", "AttributeType": "S"} + ], + "BillingMode": "PAY_PER_REQUEST" + } + """.formatted(tableName)) + .when().post("/") + .then().statusCode(200); + + // Must be the real cause, not "Query condition missed key schema element". + given() + .header("X-Amz-Target", "DynamoDB_20120810.Query") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "KeyConditionExpression": "#n = :pk", + "ExpressionAttributeValues": {":pk": {"S": "a"}} + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid KeyConditionExpression: An expression attribute name " + + "used in the document path is not defined; attribute name: #n")); + + // Must not silently evaluate as "no match" and return an empty page. + given() + .header("X-Amz-Target", "DynamoDB_20120810.Query") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "KeyConditionExpression": "pk = :pk", + "FilterExpression": "payload = :d", + "ExpressionAttributeValues": {":pk": {"S": "a"}} + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid FilterExpression: An expression attribute value " + + "used in expression is not defined; attribute value: :d")); + + // Scan: FilterExpression references an undefined :value. + given() + .header("X-Amz-Target", "DynamoDB_20120810.Scan") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "FilterExpression": "payload = :d" + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid FilterExpression: An expression attribute value " + + "used in expression is not defined; attribute value: :d")); + + // Scan: FilterExpression references an undefined #name. + given() + .header("X-Amz-Target", "DynamoDB_20120810.Scan") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "FilterExpression": "#n = :d", + "ExpressionAttributeValues": {":d": {"S": "x"}} + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid FilterExpression: An expression attribute name " + + "used in the document path is not defined; attribute name: #n")); + + // Edge case: :p is a function argument, begins_with(#n, :p), not a bare operand. + given() + .header("X-Amz-Target", "DynamoDB_20120810.Scan") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "FilterExpression": "begins_with(#n, :p)", + "ExpressionAttributeNames": {"#n": "payload"} + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid FilterExpression: An expression attribute value " + + "used in expression is not defined; attribute value: :p")); + + deleteTable(tableName); + } + + // UpdateExpression must reject an undefined #name the same way it already rejects :value. + @Test + void undefinedTokensInUpdateExpressionAreRejected() { + String tableName = "UpdateExprTokenTable"; + + given() + .header("X-Amz-Target", "DynamoDB_20120810.CreateTable") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "KeySchema": [ + {"AttributeName": "pk", "KeyType": "HASH"}, + {"AttributeName": "sk", "KeyType": "RANGE"} + ], + "AttributeDefinitions": [ + {"AttributeName": "pk", "AttributeType": "S"}, + {"AttributeName": "sk", "AttributeType": "S"} + ], + "BillingMode": "PAY_PER_REQUEST" + } + """.formatted(tableName)) + .when().post("/") + .then().statusCode(200); + + // The UpdateExpression itself references an undefined :value. + given() + .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Key": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "UpdateExpression": "SET notes = :d" + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid UpdateExpression: An expression attribute value " + + "used in expression is not defined; attribute value: :d")); + + // Previously this silently stored an attribute literally named "#n". + given() + .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Key": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "UpdateExpression": "SET #n = :d", + "ExpressionAttributeValues": {":d": {"S": "x"}} + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid UpdateExpression: An expression attribute name " + + "used in the document path is not defined; attribute name: #n")); + + // Both undefined; #n comes first in the text and is reported. + given() + .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Key": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "UpdateExpression": "SET #n = :d" + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid UpdateExpression: An expression attribute name " + + "used in the document path is not defined; attribute name: #n")); + + // :d is inside list_append's args but still comes before #n in the text. + given() + .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Key": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "UpdateExpression": "SET notes = list_append(:d, #n)" + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid UpdateExpression: An expression attribute value " + + "used in expression is not defined; attribute value: :d")); + + // Control: a fully-defined UpdateExpression #name is still accepted (no regression). + given() + .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Key": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "UpdateExpression": "SET #n = :d", + "ExpressionAttributeNames": {"#n": "notes"}, + "ExpressionAttributeValues": {":d": {"S": "x"}} + } + """.formatted(tableName)) + .when().post("/") + .then().statusCode(200); + + deleteTable(tableName); + } + + // An undefined token must reject, never fall through to a false ConditionalCheckFailed. + @Test + void undefinedTokensInTransactWriteItemsAreRejected() { + String tableName = "TransactTokenTable"; + + given() + .header("X-Amz-Target", "DynamoDB_20120810.CreateTable") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "KeySchema": [ + {"AttributeName": "pk", "KeyType": "HASH"}, + {"AttributeName": "sk", "KeyType": "RANGE"} + ], + "AttributeDefinitions": [ + {"AttributeName": "pk", "AttributeType": "S"}, + {"AttributeName": "sk", "AttributeType": "S"} + ], + "BillingMode": "PAY_PER_REQUEST" + } + """.formatted(tableName)) + .when().post("/") + .then().statusCode(200); + + // ConditionCheck's ConditionExpression references an undefined :value. + given() + .header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TransactItems": [{ + "ConditionCheck": { + "TableName": "%s", + "Key": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "ConditionExpression": "sk = :v" + } + }] + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid ConditionExpression: An expression attribute value " + + "used in expression is not defined; attribute value: :v")); + + // ConditionCheck's ConditionExpression references an undefined #name. + given() + .header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TransactItems": [{ + "ConditionCheck": { + "TableName": "%s", + "Key": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "ConditionExpression": "#n = sk" + } + }] + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid ConditionExpression: An expression attribute name " + + "used in the document path is not defined; attribute name: #n")); + + // Put's ConditionExpression references an undefined :value. + given() + .header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TransactItems": [{ + "Put": { + "TableName": "%s", + "Item": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "ConditionExpression": "sk = :v" + } + }] + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid ConditionExpression: An expression attribute value " + + "used in expression is not defined; attribute value: :v")); + + // Delete's ConditionExpression references an undefined #name. + given() + .header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TransactItems": [{ + "Delete": { + "TableName": "%s", + "Key": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "ConditionExpression": "#n = sk" + } + }] + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid ConditionExpression: An expression attribute name " + + "used in the document path is not defined; attribute name: #n")); + + // Update's ConditionExpression is undefined despite an otherwise-valid UpdateExpression. + given() + .header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TransactItems": [{ + "Update": { + "TableName": "%s", + "Key": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "UpdateExpression": "SET notes = :d", + "ExpressionAttributeValues": {":d": {"S": "x"}}, + "ConditionExpression": "sk = :v" + } + }] + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid ConditionExpression: An expression attribute value " + + "used in expression is not defined; attribute value: :v")); + + // Previously this silently no-op'd instead of erroring. + given() + .header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TransactItems": [{ + "Update": { + "TableName": "%s", + "Key": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "UpdateExpression": "SET notes = :d" + } + }] + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid UpdateExpression: An expression attribute value " + + "used in expression is not defined; attribute value: :d")); + + // Previously this silently stored an attribute literally named "#n". + given() + .header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TransactItems": [{ + "Update": { + "TableName": "%s", + "Key": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "UpdateExpression": "SET #n = :d", + "ExpressionAttributeValues": {":d": {"S": "x"}} + } + }] + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid UpdateExpression: An expression attribute name " + + "used in the document path is not defined; attribute name: #n")); + + // Both undefined; UpdateExpression's own token is reported first. + given() + .header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TransactItems": [{ + "Update": { + "TableName": "%s", + "Key": {"pk": {"S": "a"}, "sk": {"S": "b"}}, + "UpdateExpression": "SET notes = :d", + "ConditionExpression": "sk = :v" + } + }] + } + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", equalTo("Invalid UpdateExpression: An expression attribute value " + + "used in expression is not defined; attribute value: :d")); + + // Control: a fully-defined transaction is still accepted; seed the item first. + given() + .header("X-Amz-Target", "DynamoDB_20120810.PutItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Item": {"pk": {"S": "txn-control"}, "sk": {"S": "b"}} + } + """.formatted(tableName)) + .when().post("/") + .then().statusCode(200); + + given() + .header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TransactItems": [{ + "Update": { + "TableName": "%s", + "Key": {"pk": {"S": "txn-control"}, "sk": {"S": "b"}}, + "UpdateExpression": "SET #n = :d", + "ExpressionAttributeNames": {"#n": "notes"}, + "ConditionExpression": "sk = :v", + "ExpressionAttributeValues": {":v": {"S": "b"}, ":d": {"S": "x"}} + } + }] + } + """.formatted(tableName)) + .when().post("/") + .then().statusCode(200); + + deleteTable(tableName); + } + private void deleteTable(String tableName) { given() .header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")