diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DynamoDbAccessPathValidationTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DynamoDbAccessPathValidationTest.java index d7c94bacac..e205b3b1b9 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DynamoDbAccessPathValidationTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DynamoDbAccessPathValidationTest.java @@ -184,6 +184,23 @@ class DynamoDbAccessPathValidationTest { .expressionAttributeValues(Map.of(":status", value("open"))))); } + @Test + void queryAndScanRejectWrongExclusiveStartKeyTypes() { + Map invalidStartKey = Map.of( + "pk", AttributeValue.builder().n("1").build(), + "sk", value("s1")); + + assertValidationException(() -> ddb.query(request -> request + .tableName(TABLE) + .keyConditionExpression("pk = :pk") + .expressionAttributeValues(Map.of(":pk", value("p1"))) + .exclusiveStartKey(invalidStartKey))); + + assertValidationException(() -> ddb.scan(request -> request + .tableName(TABLE) + .exclusiveStartKey(invalidStartKey))); + } + private static void assertValidationException(org.assertj.core.api.ThrowableAssert.ThrowingCallable call) { assertThatThrownBy(call) .isInstanceOf(DynamoDbException.class) diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java index 08b5169caf..93d1447074 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java @@ -718,31 +718,67 @@ public class DynamoDbJsonHandler { return changedAttributes; } - // DynamoDB rejects an ExclusiveStartKey whose attribute set does not exactly match the - // key schema being paged: the table's primary key, plus the index's key attributes when - // an IndexName is supplied. Query and Scan report different messages for the same fault. + // DynamoDB requires an ExclusiveStartKey to exactly match the paged key schema's + // attribute names and scalar types. Query and Scan report different messages for faults. private void validateExclusiveStartKey(JsonNode exclusiveStartKey, TableDefinition table, - String indexName, boolean isScan) { + DynamoDbAccessPath accessPath, boolean isScan) { if (exclusiveStartKey == null || exclusiveStartKey.isNull()) return; Set expected = new HashSet<>(); expected.add(table.getPartitionKeyName()); String sortKey = table.getSortKeyName(); if (sortKey != null) expected.add(sortKey); - if (indexName != null) { - table.findGsi(indexName).ifPresent(g -> - g.getKeySchema().forEach(k -> expected.add(k.getAttributeName()))); - table.findLsi(indexName).ifPresent(l -> - l.getKeySchema().forEach(k -> expected.add(k.getAttributeName()))); + if (accessPath.isIndex()) { + expected.addAll(accessPath.keyAttributeNames()); } Set actual = new HashSet<>(); exclusiveStartKey.fieldNames().forEachRemaining(actual::add); - if (!actual.equals(expected)) { - throw new AwsException("ValidationException", isScan - ? "The provided starting key is invalid: The provided key element does not match the schema" - : "The provided starting key is invalid", 400); + if (!actual.equals(expected) || hasInvalidKeyValue(exclusiveStartKey, table, expected)) { + throw invalidStartingKey(isScan); } } + private boolean hasInvalidKeyValue(JsonNode exclusiveStartKey, TableDefinition table, + Set expected) { + if (table.getAttributeDefinitions() == null) { + return true; + } + for (String attribute : expected) { + String expectedType = table.getAttributeDefinitions().stream() + .filter(definition -> attribute.equals(definition.getAttributeName())) + .map(AttributeDefinition::getAttributeType) + .findFirst() + .orElse(null); + if (expectedType == null || hasInvalidScalarValue(exclusiveStartKey.get(attribute), expectedType)) { + return true; + } + } + return false; + } + + private boolean hasInvalidScalarValue(JsonNode value, String expectedType) { + if (value == null || !value.isObject() || value.size() != 1 || !value.has(expectedType)) { + return true; + } + JsonNode payload = value.get(expectedType); + if (payload == null || !payload.isTextual() || payload.textValue().isEmpty()) { + return true; + } + if ("N".equals(expectedType)) { + DynamoDbNumberUtils.validateAndNormalize(payload.textValue()); + } + if ("B".equals(expectedType)) { + return !payload.textValue().matches( + "(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?"); + } + return false; + } + + private AwsException invalidStartingKey(boolean isScan) { + return new AwsException("ValidationException", isScan + ? "The provided starting key is invalid: The provided key element does not match the schema" + : "The provided starting key is invalid", 400); + } + private static final Set VALID_SELECT = Set.of( "ALL_ATTRIBUTES", "ALL_PROJECTED_ATTRIBUTES", "SPECIFIC_ATTRIBUTES", "COUNT"); @@ -861,7 +897,7 @@ public class DynamoDbJsonHandler { } if (exclusiveStartKey != null) { - validateExclusiveStartKey(exclusiveStartKey, queryTable, indexName, false); + validateExclusiveStartKey(exclusiveStartKey, queryTable, queryAccessPath, false); } DynamoDbService.QueryResult result = dynamoDbService.query(tableName, keyConditions, @@ -983,7 +1019,7 @@ public class DynamoDbJsonHandler { projectionExpressionScan, attributesToGetScan, exprAttrNames); if (exclusiveStartKey != null) { - validateExclusiveStartKey(exclusiveStartKey, scanTable, indexNameScan, true); + validateExclusiveStartKey(exclusiveStartKey, scanTable, scanAccessPath, true); } DynamoDbService.ScanResult result = dynamoDbService.scan( diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathIntegrationTest.java index fcc2e04e14..7faf13dbbf 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathIntegrationTest.java @@ -247,6 +247,108 @@ class DynamoDbAccessPathIntegrationTest { .body("message", equalTo("Consistent reads are not supported on global secondary indexes")); } + @Test + @Order(9) + void queryAndScanRejectWrongExclusiveStartKeyTypes() { + request("DynamoDB_20120810.Query", """ + { + "TableName":"%s", + "KeyConditionExpression":"pk = :pk", + "ExpressionAttributeValues":{":pk":{"S":"p1"}}, + "ExclusiveStartKey":{"pk":{"N":"1"},"sk":{"S":"s1"}} + } + """.formatted(TABLE)) + .statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo("The provided starting key is invalid")); + + request("DynamoDB_20120810.Scan", """ + { + "TableName":"%s", + "ExclusiveStartKey":{"pk":{"S":"p1"},"sk":{"BOOL":true}} + } + """.formatted(TABLE)) + .statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo("The provided starting key is invalid: " + + "The provided key element does not match the schema")); + + request("DynamoDB_20120810.Query", """ + { + "TableName":"%s", + "KeyConditionExpression":"pk = :pk", + "ExpressionAttributeValues":{":pk":{"S":"p1"}}, + "ExclusiveStartKey":{"pk":{"S":null},"sk":{"S":"s1"}} + } + """.formatted(TABLE)) + .statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo("The provided starting key is invalid")); + + request("DynamoDB_20120810.Scan", """ + { + "TableName":"%s", + "ExclusiveStartKey":{"pk":{"S":123},"sk":{"S":"s1"}} + } + """.formatted(TABLE)) + .statusCode(400) + .body("__type", equalTo("ValidationException")); + } + + @Test + @Order(10) + void queryRejectsMalformedNumericExclusiveStartKey() { + String numericTable = TABLE + "-numeric"; + request("DynamoDB_20120810.CreateTable", """ + { + "TableName":"%s", + "AttributeDefinitions":[{"AttributeName":"pk","AttributeType":"N"}], + "KeySchema":[{"AttributeName":"pk","KeyType":"HASH"}], + "BillingMode":"PAY_PER_REQUEST" + } + """.formatted(numericTable)) + .statusCode(200); + + request("DynamoDB_20120810.Query", """ + { + "TableName":"%s", + "KeyConditionExpression":"pk = :pk", + "ExpressionAttributeValues":{":pk":{"N":"1"}}, + "ExclusiveStartKey":{"pk":{"N":"not-a-number"}} + } + """.formatted(numericTable)) + .statusCode(400) + .body("__type", equalTo("ValidationException")); + + request("DynamoDB_20120810.DeleteTable", """ + {"TableName":"%s"} + """.formatted(numericTable)) + .statusCode(200); + } + + @Test + @Order(11) + void queryRejectsWrongIndexExclusiveStartKeyType() { + request("DynamoDB_20120810.Query", """ + { + "TableName":"%s", + "IndexName":"status-index", + "KeyConditionExpression":"#status = :status", + "ExpressionAttributeNames":{"#status":"status"}, + "ExpressionAttributeValues":{":status":{"S":"open"}}, + "ExclusiveStartKey":{ + "pk":{"S":"p1"}, + "sk":{"S":"s1"}, + "status":{"S":"open"}, + "createdAt":{"N":"1"} + } + } + """.formatted(TABLE)) + .statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo("The provided starting key is invalid")); + } + @Test @Order(99) void deleteTable() {