From 8f606c3587d8eeb6151a2ed42b66b8c2cb963644 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TATSUNO=20=E2=80=9CTaz=E2=80=9D=20Yasuhiro?= Date: Fri, 25 Sep 2026 10:13:18 +0900 Subject: [PATCH] fix(dynamodb): match AWS error messages for item calls and projections (#4315) * fix(dynamodb): drop the table name from ResourceNotFound on item calls AWS answers a data-plane call on a missing table with the bare message "Requested resource not found". Floci named the table there, which only control-plane calls such as DescribeTable do. * fix(dynamodb): reject overlapping and undefined ProjectionExpression paths AWS rejects a read whose ProjectionExpression names the same document path twice, or names a path that covers another one, and it does so before the read, so a request that matches nothing still fails. An undefined #alias in a projection path is rejected the same way. Floci accepted both and only trimmed the item afterwards. The check runs on GetItem, Query, Scan, BatchGetItem and TransactGetItems. * test(dynamodb): pin the ResourceNotFound message every item call returns Covers the missing-table half next to the existing CREATING-table case, and records that DescribeTable still names the table. * fix(dynamodb): reject BatchGetItem mixing AttributesToGet and ProjectionExpression AWS weighs the two projection styles across the whole request. One table entry carrying both, or one entry naming AttributesToGet while another names ProjectionExpression, fails with the expression conflict message. Floci accepted both shapes. * fix(dynamodb): reject DeleteItem carrying both Expected and ConditionExpression PutItem and UpdateItem already rejected the mix. DeleteItem ran the legacy Expected block alongside the condition instead. * fix(dynamodb): answer an empty BatchGetItem RequestItems the way AWS does AWS moved BatchGetItem to the validation framework message for an empty RequestItems map. BatchWriteItem still answers with its own sentence, so only the read side changes. * fix(dynamodb): match AWS wording for a StreamViewType with streams disabled CreateTable answers "Table is being created with a stream disabled, UpdateViewType should not be specified". * fix(dynamodb): name the set type when a binary set has duplicates AWS answers a BS duplicate with "Input collection [, ]of type BS contains duplicates.", with no space before "of". String and number sets keep the plain wording. * refactor(dynamodb): read each projection path once per loop in validatePaths * refactor(dynamodb): compare projection path prefixes with subList * fix(dynamodb): reject overlapping and undefined ProjectionExpression paths on SearchVectors * refactor(dynamodb): declare the BatchWriteItem cost table with its type * test(dynamodb): pin the overlapping projection rejection on reads that match nothing --- .../dynamodb/DynamoDbJsonHandler.java | 78 +++++++---- .../services/dynamodb/DynamoDbService.java | 50 ++++--- .../dynamodb/DynamoDbVectorSearch.java | 2 +- .../dynamodb/ProjectionEvaluator.java | 77 +++++++++-- .../dynamodb/DynamoDbIntegrationTest.java | 129 ++++++++++++++++++ .../dynamodb/DynamoDbJsonHandlerTest.java | 21 ++- .../DynamoDbProjectionIntegrationTest.java | 58 ++++++++ .../dynamodb/DynamoDbServiceTest.java | 37 +++++ .../DynamoDbVectorSearchIntegrationTest.java | 15 ++ .../dynamodb/ProjectionEvaluatorTest.java | 62 +++++++++ 10 files changed, 473 insertions(+), 56 deletions(-) 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 e992d24299..6615675a19 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 @@ -233,8 +233,8 @@ public class DynamoDbJsonHandler { if (streamSpecCheck.isObject() && !streamSpecCheck.path("StreamEnabled").asBoolean(false) && streamSpecCheck.hasNonNull("StreamViewType")) { throw new AwsException("ValidationException", - "One or more parameter values were invalid: StreamViewType cannot be specified " - + "when StreamEnabled is false", 400); + "One or more parameter values were invalid: Table is being created with a stream " + + "disabled, UpdateViewType should not be specified", 400); } TableDefinition table = dynamoDbService.createTable(tableName, keySchema, attrDefs, @@ -600,7 +600,7 @@ public class DynamoDbJsonHandler { JsonNode oldItem = null; if ("ALL_OLD".equals(returnValues) || expected != null) { - dynamoDbService.describeTable(tableName, region); + dynamoDbService.requireTableForItemCall(tableName, region); oldItem = dynamoDbService.getItem(tableName, item, region); } @@ -646,8 +646,7 @@ public class DynamoDbJsonHandler { // Validate ProjectionExpression syntax and reserved words before item lookup if (projectionExpression != null) { DynamoDbExpressionSize.checkRead(projectionExpression, "ProjectionExpression"); - ProjectionEvaluator.validateSyntax(projectionExpression, "ProjectionExpression"); - DynamoDbReservedWords.check(projectionExpression, "ProjectionExpression"); + ProjectionEvaluator.validateExpression(projectionExpression, exprAttrNames); } JsonNode item = dynamoDbService.getItem(tableName, key, region); @@ -706,8 +705,14 @@ public class DynamoDbJsonHandler { DynamoDbExpressionSize.checkWrite(conditionExpression, "ConditionExpression"); DynamoDbAttributeValueValidator.requireNestingWithinLimit(exprAttrValues); DynamoDbNumberUtils.requireStorable(exprAttrValues, true); - DynamoDbAttributeValueValidator.requireNestingWithinLimit( - legacyValues(request.has("Expected") ? request.get("Expected") : null)); + JsonNode expectedDel = request.has("Expected") ? request.get("Expected") : null; + DynamoDbAttributeValueValidator.requireNestingWithinLimit(legacyValues(expectedDel)); + + if (conditionExpression != null && expectedDel != null) { + throw new AwsException("ValidationException", + "Can not use both expression and non-expression parameters in the same request: " + + "Non-expression parameters: {Expected} Expression parameters: {ConditionExpression}", 400); + } // EAN/EAV with no expression to reference them: AWS reports "can only be specified // when using expressions", not the "unused in expressions" wording (#2893). @@ -720,7 +725,6 @@ public class DynamoDbJsonHandler { checkUnusedEan(exprAttrNames, extractHashTokens(conditionExpression)); checkUnusedEav(exprAttrValues, extractColonTokens(conditionExpression)); - JsonNode expectedDel = request.has("Expected") ? request.get("Expected") : null; String condOpDel = request.has("ConditionalOperator") ? request.get("ConditionalOperator").asText() : "AND"; if (expectedDel != null) { @@ -971,7 +975,7 @@ public class DynamoDbJsonHandler { ? request.get("ReturnItemCollectionMetrics").asText() : null; if (!"SIZE".equals(ricm) || itemOrKey == null) return; try { - TableDefinition table = dynamoDbService.describeTable(tableName, region); + TableDefinition table = dynamoDbService.requireTableForItemCall(tableName, region); String pkName = table.getPartitionKeyName(); JsonNode pkValue = itemOrKey.get(pkName); if (pkValue == null) return; @@ -1094,7 +1098,7 @@ public class DynamoDbJsonHandler { DynamoDbAttributeValueValidator.requireNestingWithinLimit(legacyValues(queryFilter), false); ExpressionEvaluator.validateExpression(keyConditionExpr, "KeyConditionExpression", exprAttrNames, exprAttrValues); ExpressionEvaluator.validateExpression(filterExpr, "FilterExpression", exprAttrNames, exprAttrValues); - ProjectionEvaluator.validateExpression(projectionExpression); + ProjectionEvaluator.validateExpression(projectionExpression, exprAttrNames); if (select != null && !VALID_SELECT.contains(select)) { throw new AwsException("ValidationException", @@ -1109,7 +1113,7 @@ public class DynamoDbJsonHandler { "1 validation error detected: " + SELECT_NEEDS_PROJECTION, 400); } - TableDefinition queryTable = dynamoDbService.describeTable(tableName, region); + TableDefinition queryTable = dynamoDbService.requireTableForItemCall(tableName, region); DynamoDbAccessPath queryAccessPath = DynamoDbAccessPath.resolve(queryTable, indexName); DynamoDbAccessPathValidator.validateQuery(queryTable, queryAccessPath, keyConditions, keyConditionExpr, filterExpr, queryFilter, exprAttrNames, exprAttrValues); @@ -1265,7 +1269,7 @@ public class DynamoDbJsonHandler { DynamoDbAttributeValueValidator.requireNestingWithinLimit(exprAttrValues); DynamoDbNumberUtils.requireStorable(exprAttrValues, false); ExpressionEvaluator.validateExpression(filterExpr, "FilterExpression", exprAttrNames, exprAttrValues); - ProjectionEvaluator.validateExpression(projectionExpressionScan); + ProjectionEvaluator.validateExpression(projectionExpressionScan, exprAttrNames); // Reject ExpressionAttributeNames/Values entries not referenced by any expression (AWS parity, #2893) checkUnusedEan(exprAttrNames, extractHashTokens(filterExpr, projectionExpressionScan)); @@ -1287,7 +1291,7 @@ public class DynamoDbJsonHandler { throw new AwsException("ValidationException", SELECT_NEEDS_PROJECTION, 400); } - TableDefinition scanTable = dynamoDbService.describeTable(tableName, region); + TableDefinition scanTable = dynamoDbService.requireTableForItemCall(tableName, region); DynamoDbAccessPath scanAccessPath = DynamoDbAccessPath.resolve(scanTable, indexNameScan); DynamoDbAccessPathValidator.validateSelection(scanTable, scanAccessPath, select, projectionExpressionScan, attributesToGetScan, exprAttrNames); @@ -1481,7 +1485,7 @@ public class DynamoDbJsonHandler { } for (Map.Entry> entry : items.entrySet()) { - TableDefinition bwTable = dynamoDbService.describeTable(entry.getKey(), region); + TableDefinition bwTable = dynamoDbService.requireTableForItemCall(entry.getKey(), region); Set seen = new HashSet<>(); for (JsonNode writeReq : entry.getValue()) { JsonNode keyNode = writeReq.has("PutRequest") @@ -1508,7 +1512,7 @@ public class DynamoDbJsonHandler { if (!"NONE".equals(returnCCBatch)) { costs = new LinkedHashMap<>(); for (var entry : items.entrySet()) { - var costTable = dynamoDbService.describeTable(entry.getKey(), region); + TableDefinition costTable = dynamoDbService.requireTableForItemCall(entry.getKey(), region); var cost = DynamoDbWriteCapacity.Cost.zero(); for (var writeReq : entry.getValue()) { var newItem = writeReq.has("PutRequest") @@ -1541,11 +1545,17 @@ public class DynamoDbJsonHandler { private Response handleBatchGetItem(JsonNode request, String region) { JsonNode requestItems = request.get("RequestItems"); - if (requestItems == null || requestItems.isNull() || requestItems.isMissingNode() - || !requestItems.fields().hasNext()) { + if (requestItems == null || requestItems.isNull() || requestItems.isMissingNode()) { throw new AwsException("ValidationException", "The requestItems parameter is required for BatchGetItem", 400); } + // BatchGetItem answers an empty map from the validation framework, BatchWriteItem + // still answers with its own sentence. + if (!requestItems.fields().hasNext()) { + throw new AwsException("ValidationException", + "1 validation error detected: Value at 'RequestItems' failed to satisfy constraint: " + + "Member must have length greater than or equal to 1", 400); + } Map items = new HashMap<>(); Iterator> tables = requestItems.fields(); while (tables.hasNext()) { @@ -1553,11 +1563,27 @@ public class DynamoDbJsonHandler { items.put(entry.getKey(), entry.getValue()); } + // AWS weighs the two projection styles across the whole request, so one table + // entry naming AttributesToGet and another naming ProjectionExpression conflict. + boolean usesAttributesToGet = false; + boolean usesProjectionExpression = false; + for (JsonNode tableRequest : items.values()) { + usesAttributesToGet |= tableRequest.hasNonNull("AttributesToGet"); + usesProjectionExpression |= tableRequest.hasNonNull("ProjectionExpression"); + } + if (usesAttributesToGet && usesProjectionExpression) { + throw new AwsException("ValidationException", + "Can not use both expression and non-expression parameters in the same request: " + + "Non-expression parameters: {AttributesToGet} Expression parameters: {ProjectionExpression}", 400); + } + // Per-table check: max 100 keys for (Map.Entry entry : items.entrySet()) { var projection = entry.getValue().path("ProjectionExpression"); if (projection.isTextual()) { DynamoDbExpressionSize.checkRead(projection.asText(), "ProjectionExpression"); + ProjectionEvaluator.validateExpression(projection.asText(), + entry.getValue().get("ExpressionAttributeNames")); } JsonNode keysNode = entry.getValue().has("Keys") ? entry.getValue().get("Keys") : null; if (keysNode != null && keysNode.size() > 100) { @@ -1569,7 +1595,7 @@ public class DynamoDbJsonHandler { } for (Map.Entry entry : items.entrySet()) { - TableDefinition bgTable = dynamoDbService.describeTable(entry.getKey(), region); + TableDefinition bgTable = dynamoDbService.requireTableForItemCall(entry.getKey(), region); JsonNode keys = entry.getValue().get("Keys"); if (keys == null || !keys.isArray()) continue; Set seen = new HashSet<>(); @@ -2007,7 +2033,7 @@ public class DynamoDbJsonHandler { if (op == null) continue; String opTable = op.path("TableName").asText(); TableDefinition txTable = tableCache.computeIfAbsent(opTable, - tn -> dynamoDbService.describeTable(tn, region)); + tn -> dynamoDbService.requireTableForItemCall(tn, region)); JsonNode keyNode = op.has("Item") ? op.get("Item") : op.get("Key"); if (keyNode == null) continue; String key = region + "::" + opTable + "::" + dynamoDbService.buildItemKey(txTable, keyNode); @@ -2111,7 +2137,7 @@ public class DynamoDbJsonHandler { if (get == null) continue; String opTable = get.path("TableName").asText(); TableDefinition txTable = tableCache.computeIfAbsent(opTable, - tn -> dynamoDbService.describeTable(tn, region)); + tn -> dynamoDbService.requireTableForItemCall(tn, region)); JsonNode keyNode = get.get("Key"); if (keyNode == null) continue; try { @@ -2128,13 +2154,13 @@ public class DynamoDbJsonHandler { } } - // Validate ProjectionExpression syntax in each Get action before executing + // Validate the ProjectionExpression in each Get action before executing for (JsonNode txItem : transactItemsNode) { JsonNode get = txItem.has("Get") ? txItem.get("Get") : null; if (get == null) continue; String pe = get.has("ProjectionExpression") ? get.get("ProjectionExpression").asText() : null; if (pe != null) { - ProjectionEvaluator.validateSyntax(pe, "ProjectionExpression"); + ProjectionEvaluator.validateExpression(pe, get.get("ExpressionAttributeNames")); } } @@ -2448,8 +2474,10 @@ public class DynamoDbJsonHandler { "One or more parameter values were invalid: Binary sets should not be empty", 400); Set seen = new HashSet<>(); for (JsonNode e : bs) { + // A binary set names its type, and AWS leaves out the space before "of". if (!seen.add(e.asText())) throw new AwsException("ValidationException", - "One or more parameter values were invalid: Input collection " + formatSetForError(bs) + " contains duplicates.", 400); + "One or more parameter values were invalid: Input collection " + formatSetForError(bs) + + "of type BS contains duplicates.", 400); } } else if (attr.has("M")) { attr.get("M").fields().forEachRemaining(e -> checkAttrSets(e.getValue())); @@ -2579,7 +2607,7 @@ public class DynamoDbJsonHandler { String region, JsonNode oldItem, JsonNode newItem) { String returnCC = request.path("ReturnConsumedCapacity").asText("NONE"); if ("NONE".equals(returnCC)) return; - TableDefinition table = dynamoDbService.describeTable(tableName, region); + TableDefinition table = dynamoDbService.requireTableForItemCall(tableName, region); var cost = DynamoDbWriteCapacity.forWrite(table, oldItem, newItem != null ? DynamoDbNumberUtils.normalizeNumbersInItem(newItem) : null); response.set("ConsumedCapacity", @@ -3291,7 +3319,7 @@ public class DynamoDbJsonHandler { if (select.index() != null) { throw batchSelectNeedsKey(); } - TableDefinition table = dynamoDbService.describeTable(select.table(), region); + TableDefinition table = dynamoDbService.requireTableForItemCall(select.table(), region); DynamoDbPartiQLHandler.requireReadsWithinLimit(table, select.where()); if (!DynamoDbPartiQLHandler.namesOnlyTheKey(table, select.where())) { throw batchSelectNeedsKey(); diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java index 1462f35155..0b9cde59e0 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java @@ -884,7 +884,7 @@ public class DynamoDbService implements ResourceProvider { JsonNode exprAttrNames, JsonNode exprAttrValues, String region, String returnValuesOnConditionCheckFailure) { var canonicalTableName = canonicalTableName(region, tableName); - requireActiveTable(regionKey(region, canonicalTableName), canonicalTableName); + requireActiveTable(regionKey(region, canonicalTableName)); return putItemInternal(tableName, item, conditionExpression, exprAttrNames, exprAttrValues, region, returnValuesOnConditionCheckFailure, true); } @@ -918,7 +918,7 @@ public class DynamoDbService implements ResourceProvider { String canonicalTableName = canonicalTableName(region, tableName); String storageKey = regionKey(region, canonicalTableName); TableDefinition table = tableStore.get(storageKey) - .orElseThrow(() -> resourceNotFoundException(canonicalTableName)); + .orElseThrow(DynamoDbService::itemCallResourceNotFound); // Validate and normalize all number attributes before storage final JsonNode normalizedItem = DynamoDbNumberUtils.normalizeNumbersInItem(item); @@ -968,7 +968,7 @@ public class DynamoDbService implements ResourceProvider { public JsonNode getItem(String tableName, JsonNode key, String region) { String canonicalTableName = canonicalTableName(region, tableName); String storageKey = regionKey(region, canonicalTableName); - var table = requireActiveTable(storageKey, canonicalTableName); + TableDefinition table = requireActiveTable(storageKey); String itemKey = buildItemKey(table, key, true); var items = currentItems(storageKey, false); @@ -1025,7 +1025,7 @@ public class DynamoDbService implements ResourceProvider { Map> stagedItems) { String canonicalTableName = canonicalTableName(region, tableName); String storageKey = regionKey(region, canonicalTableName); - var table = requireActiveTable(storageKey, canonicalTableName); + TableDefinition table = requireActiveTable(storageKey); String itemKey = buildItemKey(table, key, true); @@ -1136,7 +1136,7 @@ public class DynamoDbService implements ResourceProvider { UpdateSizeRule sizeRule) { String canonicalTableName = canonicalTableName(region, tableName); String storageKey = regionKey(region, canonicalTableName); - var table = requireActiveTable(storageKey, canonicalTableName); + TableDefinition table = requireActiveTable(storageKey); String itemKey = buildItemKey(table, key, true); @@ -1318,7 +1318,7 @@ public class DynamoDbService implements ResourceProvider { JsonNode exclusiveStartKey, JsonNode exprAttrNames, String region) { String canonicalTableName = canonicalTableName(region, tableName); String storageKey = regionKey(region, canonicalTableName); - var table = requireActiveTable(storageKey, canonicalTableName); + TableDefinition table = requireActiveTable(storageKey); DynamoDbAccessPath accessPath = DynamoDbAccessPath.resolve(table, indexName); String partitionKeyValuePlaceholder = DynamoDbAccessPathValidator.validateQuery( @@ -1487,7 +1487,7 @@ public class DynamoDbService implements ResourceProvider { DynamoDbReservedWords.check(filterExpression, "FilterExpression"); String canonicalTableName = canonicalTableName(region, tableName); String storageKey = regionKey(region, canonicalTableName); - var table = requireActiveTable(storageKey, canonicalTableName); + TableDefinition table = requireActiveTable(storageKey); DynamoDbAccessPath accessPath = DynamoDbAccessPath.resolve(table, indexName); @@ -1616,7 +1616,7 @@ public class DynamoDbService implements ResourceProvider { for (Map.Entry> entry : requestItems.entrySet()) { String tableName = canonicalTableName(region, entry.getKey()); String storageKey = regionKey(region, tableName); - var table = requireActiveTable(storageKey, tableName); + TableDefinition table = requireActiveTable(storageKey); Set seenKeys = new HashSet<>(); for (JsonNode writeRequest : entry.getValue()) { String itemKey; @@ -1914,7 +1914,7 @@ public class DynamoDbService implements ResourceProvider { cost = DynamoDbTransactCapacity.conditionCheck(oldItem); } else { cost = DynamoDbTransactCapacity.write( - requireActiveTable(participant.storageKey(), participant.tableName()), oldItem, newItem); + requireActiveTable(participant.storageKey()), oldItem, newItem); } byTable.merge(participant.tableName(), cost, DynamoDbWriteCapacity.Cost::plus); } @@ -1950,7 +1950,7 @@ public class DynamoDbService implements ResourceProvider { } String storageKey = regionKey(region, tableName); - var table = requireActiveTable(storageKey, tableName); + TableDefinition table = requireActiveTable(storageKey); String itemKey = buildItemKey(table, keyOrItem); return new TransactParticipant(storageKey, itemKey, tableName); } @@ -1990,7 +1990,7 @@ public class DynamoDbService implements ResourceProvider { return; } String tableName = canonicalTableName(region, target.path("TableName").asText()); - TableDefinition table = requireActiveTable(regionKey(region, tableName), tableName); + TableDefinition table = requireActiveTable(regionKey(region, tableName)); List keyNames = table.getSortKeyName() == null ? List.of(table.getPartitionKeyName()) : List.of(table.getPartitionKeyName(), table.getSortKeyName()); @@ -2059,7 +2059,7 @@ public class DynamoDbService implements ResourceProvider { JsonNode exprAttrValues = target.has("ExpressionAttributeValues") ? target.get("ExpressionAttributeValues") : null; String storageKey = regionKey(region, canonicalTableName); - var table = requireActiveTable(storageKey, canonicalTableName); + TableDefinition table = requireActiveTable(storageKey); String itemKey = buildItemKey(table, key); var tableItems = itemsFor(storageKey, stagedItems, false); @@ -2106,7 +2106,7 @@ public class DynamoDbService implements ResourceProvider { JsonNode put = transactItem.get("Put"); String tableName = canonicalTableName(region, put.path("TableName").asText()); String storageKey = regionKey(region, tableName); - var table = requireActiveTable(storageKey, tableName); + TableDefinition table = requireActiveTable(storageKey); JsonNode item = put.get("Item"); if (item == null) { throw new AwsException("ValidationException", "Item is required for Put", 400); @@ -2120,7 +2120,7 @@ public class DynamoDbService implements ResourceProvider { JsonNode del = transactItem.get("Delete"); String tableName = canonicalTableName(region, del.path("TableName").asText()); String storageKey = regionKey(region, tableName); - var table = requireActiveTable(storageKey, tableName); + TableDefinition table = requireActiveTable(storageKey); JsonNode key = del.get("Key"); if (key == null) { throw new AwsException("ValidationException", "Key is required for Delete", 400); @@ -2130,7 +2130,7 @@ public class DynamoDbService implements ResourceProvider { JsonNode upd = transactItem.get("Update"); String tableName = canonicalTableName(region, upd.path("TableName").asText()); String storageKey = regionKey(region, tableName); - var table = requireActiveTable(storageKey, tableName); + TableDefinition table = requireActiveTable(storageKey); JsonNode key = upd.get("Key"); if (key == null) { throw new AwsException("ValidationException", "Key is required for Update", 400); @@ -4441,16 +4441,26 @@ public class DynamoDbService implements ResourceProvider { "Requested resource not found: Table: " + tableName + " not found", 400); } - /** Item calls treat a CREATING table as absent. AWS answers them without the table name. */ - private TableDefinition requireActiveTable(String storageKey, String canonicalTableName) { - var table = tableStore.get(storageKey) - .orElseThrow(() -> resourceNotFoundException(canonicalTableName)); + /** Item calls never name the table, and they treat a CREATING table as absent. */ + private static AwsException itemCallResourceNotFound() { + return new AwsException("ResourceNotFoundException", "Requested resource not found", 400); + } + + private TableDefinition requireActiveTable(String storageKey) { + TableDefinition table = tableStore.get(storageKey) + .orElseThrow(DynamoDbService::itemCallResourceNotFound); if ("CREATING".equals(table.getTableStatus())) { - throw new AwsException("ResourceNotFoundException", "Requested resource not found", 400); + throw itemCallResourceNotFound(); } return table; } + /** Resolves a table for an item call, so a miss reports the message AWS uses there. */ + public TableDefinition requireTableForItemCall(String tableName, String region) { + String canonicalTableName = canonicalTableName(region, tableName); + return requireActiveTable(regionKey(region, canonicalTableName)); + } + public record UpdateResult(JsonNode newItem, JsonNode oldItem, List touched) {} // One path the update expression acted on, with the value there before and after. diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbVectorSearch.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbVectorSearch.java index 9e3fa9b7e1..19b89e1de8 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbVectorSearch.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbVectorSearch.java @@ -39,7 +39,7 @@ final class DynamoDbVectorSearch { float[] query = parseSearchVector(searchVector, index); ExpressionEvaluator.Expr condition = parseSearchCondition(index, searchConditionExpression, exprAttrNames, exprAttrValues); - ProjectionEvaluator.validateExpression(projectionExpression); + ProjectionEvaluator.validateExpression(projectionExpression, exprAttrNames); String hashAttribute = index.getHashAttributeName(); String distanceFunction = index.getDistanceFunction(); diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/ProjectionEvaluator.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/ProjectionEvaluator.java index a32e9b0557..5f214828d2 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/ProjectionEvaluator.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/ProjectionEvaluator.java @@ -36,7 +36,7 @@ final class ProjectionEvaluator { if (item == null || projectionExpression == null || projectionExpression.isBlank()) { return (ObjectNode) item; } - validateExpression(projectionExpression); + validateExpression(projectionExpression, exprAttrNames); PathTrie root = new PathTrie(); for (String rawPath : splitProjectionPaths(projectionExpression)) { List segments = resolvePath(rawPath.trim(), exprAttrNames); @@ -88,9 +88,57 @@ final class ProjectionEvaluator { "Invalid " + expressionType + ": Syntax error; token: \"" + token + "\", near: \"" + near + "\"", 400); } - static void validateExpression(String expression) { + static void validateExpression(String expression, JsonNode exprAttrNames) { validateSyntax(expression, "ProjectionExpression"); DynamoDbReservedWords.check(expression, "ProjectionExpression"); + validatePaths(expression, exprAttrNames); + } + + /** + * Rejects an undefined #alias, and any two paths where one covers the other. + * DynamoDB applies both before the read, so a request that matches nothing still fails. + */ + static void validatePaths(String expression, JsonNode exprAttrNames) { + if (expression == null || expression.isBlank()) { + return; + } + List> paths = new ArrayList<>(); + for (String rawPath : splitProjectionPaths(expression)) { + List segments = resolvePath(rawPath.trim(), exprAttrNames, true); + if (!segments.isEmpty()) { + paths.add(segments); + } + } + for (int i = 0; i < paths.size(); i++) { + List first = paths.get(i); + for (int j = i + 1; j < paths.size(); j++) { + List second = paths.get(j); + if (covers(first, second) || covers(second, first)) { + throw new AwsException("ValidationException", + "Invalid ProjectionExpression: Two document paths overlap with each other; " + + "must remove or rewrite one of these paths; path one: " + render(first) + + ", path two: " + render(second), 400); + } + } + } + } + + private static boolean covers(List outer, List inner) { + return outer.size() <= inner.size() && inner.subList(0, outer.size()).equals(outer); + } + + // DynamoDB prints a document path as its elements inside brackets, with a list + // index carrying brackets of its own: a.b is [a, b] and l[0] is [l, [0]]. + private static String render(List segments) { + StringBuilder rendered = new StringBuilder("["); + for (int i = 0; i < segments.size(); i++) { + if (i > 0) { + rendered.append(", "); + } + PathSegment segment = segments.get(i); + rendered.append(segment.isIndex() ? "[" + segment.index() + "]" : segment.name()); + } + return rendered.append(']').toString(); } // ── Path splitting ── @@ -133,6 +181,11 @@ final class ProjectionEvaluator { } private static List resolvePath(String path, JsonNode exprAttrNames) { + return resolvePath(path, exprAttrNames, false); + } + + private static List resolvePath(String path, JsonNode exprAttrNames, + boolean requireDefinedNames) { List segments = new ArrayList<>(); // Tokenize on dots, preserving [n] bracket indices String[] parts = path.split("\\."); @@ -142,7 +195,7 @@ final class ProjectionEvaluator { if (bracketIdx >= 0) { String name = part.substring(0, bracketIdx); if (!name.isEmpty()) { - segments.add(PathSegment.name(resolveSegment(name, exprAttrNames))); + segments.add(PathSegment.name(resolveSegment(name, exprAttrNames, requireDefinedNames))); } // Parse each [n] suffix String rest = part.substring(bracketIdx); @@ -156,7 +209,7 @@ final class ProjectionEvaluator { i = close + 1; } } else { - segments.add(PathSegment.name(resolveSegment(part, exprAttrNames))); + segments.add(PathSegment.name(resolveSegment(part, exprAttrNames, requireDefinedNames))); } } return segments; @@ -189,10 +242,18 @@ final class ProjectionEvaluator { } } - private static String resolveSegment(String seg, JsonNode exprAttrNames) { - if (seg.startsWith("#") && exprAttrNames != null) { - JsonNode resolved = exprAttrNames.get(seg); - return resolved != null ? resolved.asText() : seg; + private static String resolveSegment(String seg, JsonNode exprAttrNames, boolean requireDefinedNames) { + if (!seg.startsWith("#")) { + return seg; + } + JsonNode resolved = exprAttrNames != null ? exprAttrNames.get(seg) : null; + if (resolved != null) { + return resolved.asText(); + } + if (requireDefinedNames) { + throw new AwsException("ValidationException", + "Invalid ProjectionExpression: An expression attribute name used in the document path " + + "is not defined; attribute name: " + seg, 400); } return seg; } 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 19f5476a54..969b214146 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 @@ -856,6 +856,135 @@ class DynamoDbIntegrationTest { + "Non-expression parameters: {AttributesToGet} Expression parameters: {ProjectionExpression}")); } + @Test + @Order(10) + void batchGetItemWithEmptyRequestItemsFails() { + given() + .header("X-Amz-Target", "DynamoDB_20120810.BatchGetItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + {"RequestItems": {}} + """) + .when() + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo("1 validation error detected: Value at 'RequestItems' failed to satisfy " + + "constraint: Member must have length greater than or equal to 1")); + } + + @Test + @Order(10) + void batchWriteItemWithEmptyRequestItemsFails() { + given() + .header("X-Amz-Target", "DynamoDB_20120810.BatchWriteItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + {"RequestItems": {}} + """) + .when() + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo("The requestItems parameter is required for BatchWriteItem")); + } + + @Test + @Order(10) + void deleteItemWithExpectedAndConditionExpressionFails() { + given() + .header("X-Amz-Target", "DynamoDB_20120810.DeleteItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "TestTable", + "Key": {"pk": {"S": "user-1"}, "sk": {"S": "profile"}}, + "Expected": {"pk": {"Exists": false}}, + "ConditionExpression": "attribute_not_exists(pk)" + } + """) + .when() + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo("Can not use both expression and non-expression parameters in the same request: " + + "Non-expression parameters: {Expected} Expression parameters: {ConditionExpression}")); + } + + @Test + @Order(10) + void batchGetItemWithProjectionExpressionAndAttributesToGetFails() { + given() + .header("X-Amz-Target", "DynamoDB_20120810.BatchGetItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "RequestItems": { + "TestTable": { + "Keys": [{"pk": {"S": "user-1"}, "sk": {"S": "profile"}}], + "ProjectionExpression": "pk", + "AttributesToGet": ["pk"] + } + } + } + """) + .when() + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo("Can not use both expression and non-expression parameters in the same request: " + + "Non-expression parameters: {AttributesToGet} Expression parameters: {ProjectionExpression}")); + } + + @Test + @Order(10) + void batchGetItemMixingProjectionStylesAcrossTablesFails() { + given() + .header("X-Amz-Target", "DynamoDB_20120810.CreateTable") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "BatchGetMixTable", + "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}], + "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}], + "BillingMode": "PAY_PER_REQUEST" + } + """) + .when() + .post("/") + .then() + .statusCode(200); + + given() + .header("X-Amz-Target", "DynamoDB_20120810.BatchGetItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "RequestItems": { + "TestTable": { + "Keys": [{"pk": {"S": "user-1"}, "sk": {"S": "profile"}}], + "ProjectionExpression": "pk" + }, + "BatchGetMixTable": { + "Keys": [{"pk": {"S": "user-1"}}], + "AttributesToGet": ["pk"] + } + } + } + """) + .when() + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo("Can not use both expression and non-expression parameters in the same request: " + + "Non-expression parameters: {AttributesToGet} Expression parameters: {ProjectionExpression}")); + } + @Test @Order(10) void queryWithQueryFilterAndKeyConditionExpressionFails() { diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandlerTest.java index 12375a8371..83f32015f7 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandlerTest.java @@ -1034,8 +1034,8 @@ class DynamoDbJsonHandlerTest { "StreamSpecification": {"StreamEnabled": false, "StreamViewType": "NEW_AND_OLD_IMAGES"} } """)); - assertEquals("One or more parameter values were invalid: " - + "StreamViewType cannot be specified when StreamEnabled is false", ex.getMessage()); + assertEquals("One or more parameter values were invalid: Table is being created with a stream " + + "disabled, UpdateViewType should not be specified", ex.getMessage()); } @Test @@ -1350,6 +1350,23 @@ class DynamoDbJsonHandlerTest { assertEquals(200, response.getStatus()); } + @Test + void putItemNamesTheSetTypeWhenABinarySetHasDuplicates() { + createUsersTable("eu-west-1"); + ObjectNode request = mapper.createObjectNode(); + request.put("TableName", "Users"); + ObjectNode item = item("userId", "u1"); + ObjectNode binarySet = mapper.createObjectNode(); + binarySet.putArray("BS").add("").add(""); + item.set("bad", binarySet); + request.set("Item", item); + + AwsException ex = assertThrows(AwsException.class, + () -> handler.handle("PutItem", request, "eu-west-1")); + assertEquals("One or more parameter values were invalid: " + + "Input collection [, ]of type BS contains duplicates.", ex.getMessage()); + } + @Test void putItemRejectsAConditionExpressionOver4096Bytes() { createUsersTable("eu-west-1"); diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbProjectionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbProjectionIntegrationTest.java index 059e0e6884..e4ba5d2129 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbProjectionIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbProjectionIntegrationTest.java @@ -12,6 +12,8 @@ import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; +import java.util.Map; + import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.*; import static org.junit.jupiter.api.Assertions.*; @@ -247,6 +249,62 @@ class DynamoDbProjectionIntegrationTest { .body("Items[0].data.M.settings.M.'[beta]'", nullValue()); } + @Test + @Order(9) + void getItemRejectsOverlappingPathsBeforeReadingTheItem() { + given() + .header("X-Amz-Target", "DynamoDB_20120810.GetItem") + .contentType(CT) + .body(""" + { + "TableName": "%s", + "Key": {"pk": {"S": "no-such-item"}}, + "ProjectionExpression": "a, a.b" + } + """.formatted(TABLE)) + .when() + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo("Invalid ProjectionExpression: Two document paths overlap with each other; " + + "must remove or rewrite one of these paths; path one: [a], path two: [a, b]")); + } + + @Test + @Order(11) + void otherReadsRejectOverlappingPathsWhenNothingMatches() { + String missingKey = "{\"pk\": {\"S\": \"no-such-item\"}}"; + Map requests = Map.of( + "Query", """ + {"TableName": "%s", "KeyConditionExpression": "pk = :p", + "ExpressionAttributeValues": {":p": {"S": "no-such-item"}}, "ProjectionExpression": "a, a.b"} + """.formatted(TABLE), + "Scan", """ + {"TableName": "%s", "FilterExpression": "pk = :p", + "ExpressionAttributeValues": {":p": {"S": "no-such-item"}}, "ProjectionExpression": "a, a.b"} + """.formatted(TABLE), + "BatchGetItem", """ + {"RequestItems": {"%s": {"Keys": [%s], "ProjectionExpression": "a, a.b"}}} + """.formatted(TABLE, missingKey), + "TransactGetItems", """ + {"TransactItems": [ + {"Get": {"TableName": "%1$s", "Key": {"pk": {"S": "other"}}}}, + {"Get": {"TableName": "%1$s", "Key": %2$s, "ProjectionExpression": "a, a.b"}}]} + """.formatted(TABLE, missingKey)); + requests.forEach((action, body) -> given() + .header("X-Amz-Target", "DynamoDB_20120810." + action) + .contentType(CT) + .body(body) + .when() + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo("Invalid ProjectionExpression: Two document paths overlap with each other; " + + "must remove or rewrite one of these paths; path one: [a], path two: [a, b]"))); + } + @AfterAll static void cleanup() { given() diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java index d2613c30dd..590dc62225 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java @@ -20,6 +20,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -4121,6 +4122,42 @@ class DynamoDbServiceTest { return s3; } + /** DescribeTable names the missing table, an item call does not. */ + @Test + void itemCalls_missingTable_returnResourceNotFoundWithoutTableName() { + DynamoDbService svc = serviceWithS3(mock(S3Service.class), new InMemoryStorage<>()); + String region = "us-east-1"; + ObjectNode key = item("userId", "u1"); + ObjectNode keys = mapper.createObjectNode(); + keys.set("Keys", mapper.createArrayNode().add(key)); + ObjectNode putRequest = mapper.createObjectNode(); + putRequest.putObject("PutRequest").set("Item", key); + ObjectNode transactPut = mapper.createObjectNode(); + transactPut.putObject("Put").put("TableName", "Users").set("Item", key); + ObjectNode transactGet = mapper.createObjectNode(); + transactGet.putObject("Get").put("TableName", "Users").set("Key", key); + + List itemCalls = List.of( + () -> svc.getItem("Users", key, region), + () -> svc.putItem("Users", key, null, null, null, region, "NONE"), + () -> svc.updateItem("Users", key, null, "SET x = :v", null, item("v", "1"), "NONE", region), + () -> svc.deleteItem("Users", key, region), + () -> svc.query("Users", null, item("pk", "u1"), "userId = :pk", null, null, region), + () -> svc.scan("Users", null, null, null, null, null, null, null, region), + () -> svc.batchGetItem(Map.of("Users", keys), region), + () -> svc.batchWriteItem(Map.of("Users", List.of(putRequest)), region), + () -> svc.transactWriteItems(List.of(transactPut), region, null, null), + () -> svc.transactGetItems(List.of(transactGet), region)); + for (Executable call : itemCalls) { + AwsException e = assertThrows(AwsException.class, call); + assertEquals("ResourceNotFoundException", e.getErrorCode()); + assertEquals("Requested resource not found", e.getMessage()); + } + + AwsException describe = assertThrows(AwsException.class, () -> svc.describeTable("Users", region)); + assertEquals("Requested resource not found: Table: Users not found", describe.getMessage()); + } + /** Checked against real DynamoDB: every item call on a CREATING table fails this way, DescribeTable still works. */ @Test void itemCalls_creatingTable_returnResourceNotFoundWithoutTableName() { diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbVectorSearchIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbVectorSearchIntegrationTest.java index acd52794df..581eb111b6 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbVectorSearchIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbVectorSearchIntegrationTest.java @@ -978,4 +978,19 @@ class DynamoDbVectorSearchIntegrationTest { + "constraint: Member must have length greater than or equal to 1"); } + @Test + @Order(44) + void searchVectorsRejectsOverlappingAndUndefinedProjectionPaths() { + expectValidation("SearchVectors", search(DOCS, "cosine", 1, """ + , "ProjectionExpression": "#x, label", "ExpressionAttributeNames": {"#x": "label"} + """), + "Invalid ProjectionExpression: Two document paths overlap with each other; " + + "must remove or rewrite one of these paths; path one: [label], path two: [label]"); + expectValidation("SearchVectors", search(DOCS, "cosine", 1, """ + , "ProjectionExpression": "#undef" + """), + "Invalid ProjectionExpression: An expression attribute name used in the document " + + "path is not defined; attribute name: #undef"); + } + } diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/ProjectionEvaluatorTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/ProjectionEvaluatorTest.java index 30257bba1f..d7a68f4993 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/ProjectionEvaluatorTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/ProjectionEvaluatorTest.java @@ -285,6 +285,68 @@ class ProjectionEvaluatorTest { assertEquals(Set.of("[alpha]", "list"), attributes); } + @Test + void rejectsDuplicatePathsAsAnOverlap() { + AwsException ex = assertThrows(AwsException.class, + () -> ProjectionEvaluator.validateExpression("a, a", null)); + assertEquals("ValidationException", ex.getErrorCode()); + assertEquals("Invalid ProjectionExpression: Two document paths overlap with each other; " + + "must remove or rewrite one of these paths; path one: [a], path two: [a]", + ex.getMessage()); + } + + @Test + void rejectsTwoAliasesResolvingToOneAttribute() { + AwsException ex = assertThrows(AwsException.class, + () -> ProjectionEvaluator.validateExpression("#a, #b", readValue(""" + {"#a": "a", "#b": "a"} + """))); + assertEquals("Invalid ProjectionExpression: Two document paths overlap with each other; " + + "must remove or rewrite one of these paths; path one: [a], path two: [a]", + ex.getMessage()); + } + + @Test + void rejectsParentAndChildPaths() { + AwsException ex = assertThrows(AwsException.class, + () -> ProjectionEvaluator.validateExpression("a, a.b.c", null)); + assertEquals("Invalid ProjectionExpression: Two document paths overlap with each other; " + + "must remove or rewrite one of these paths; path one: [a], path two: [a, b, c]", + ex.getMessage()); + } + + @Test + void reportsOverlappingPathsInRequestOrder() { + AwsException ex = assertThrows(AwsException.class, + () -> ProjectionEvaluator.validateExpression("a.b, a", null)); + assertEquals("Invalid ProjectionExpression: Two document paths overlap with each other; " + + "must remove or rewrite one of these paths; path one: [a, b], path two: [a]", + ex.getMessage()); + } + + @Test + void rejectsAListAndOneOfItsElements() { + AwsException ex = assertThrows(AwsException.class, + () -> ProjectionEvaluator.validateExpression("l, l[0]", null)); + assertEquals("Invalid ProjectionExpression: Two document paths overlap with each other; " + + "must remove or rewrite one of these paths; path one: [l], path two: [l, [0]]", + ex.getMessage()); + } + + @Test + void keepsDistinctSiblingPaths() { + assertDoesNotThrow(() -> ProjectionEvaluator.validateExpression("a.b, a.c, l[0], l[1]", null)); + } + + @Test + void rejectsAnUndefinedExpressionAttributeName() { + AwsException ex = assertThrows(AwsException.class, + () -> ProjectionEvaluator.validateExpression("#undef", null)); + assertEquals("ValidationException", ex.getErrorCode()); + assertEquals("Invalid ProjectionExpression: An expression attribute name used in the document path " + + "is not defined; attribute name: #undef", ex.getMessage()); + } + private static ObjectNode readValue(String json) { try { return (ObjectNode) mapper.readTree(json);