From ac8ae81909a150b7cb5a40508e66491785ca8881 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Fri, 19 Jun 2026 10:57:37 -0500 Subject: [PATCH] fix(dynamodb): validate ExclusiveStartKey against the key schema (#1443) Query and Scan now reject an ExclusiveStartKey whose attribute set does not exactly match the paged key schema (table primary key, plus index keys when an IndexName is given), with the distinct messages each operation returns. Closes 5 conformance failures. --- .../dynamodb/DynamoDbJsonHandler.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) 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 fcd3835126..3085eedac8 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 @@ -645,6 +645,31 @@ 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. + private void validateExclusiveStartKey(JsonNode exclusiveStartKey, TableDefinition table, + String indexName, 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()))); + } + 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); + } + } + private static final Set VALID_SELECT = Set.of( "ALL_ATTRIBUTES", "ALL_PROJECTED_ATTRIBUTES", "SPECIFIC_ATTRIBUTES", "COUNT"); @@ -755,6 +780,11 @@ public class DynamoDbJsonHandler { checkUnusedEan(exprAttrNames, hashTokens); } + if (exclusiveStartKey != null) { + validateExclusiveStartKey(exclusiveStartKey, + dynamoDbService.describeTable(tableName, region), indexName, false); + } + DynamoDbService.QueryResult result = dynamoDbService.query(tableName, keyConditions, exprAttrValues, keyConditionExpr, filterExpr, limit, scanIndexForward, indexName, exclusiveStartKey, exprAttrNames, region); @@ -851,6 +881,11 @@ public class DynamoDbJsonHandler { String projectionExpressionScan = request.has("ProjectionExpression") ? request.get("ProjectionExpression").asText() : null; + if (exclusiveStartKey != null) { + validateExclusiveStartKey(exclusiveStartKey, + dynamoDbService.describeTable(tableName, region), indexNameScan, true); + } + DynamoDbService.ScanResult result = dynamoDbService.scan( tableName, filterExpr, exprAttrNames, exprAttrValues, scanFilter, limit, exclusiveStartKey, region);