From 247c62cd19150b78be7dccfc0443ef1639b20e3d Mon Sep 17 00:00:00 2001 From: Christine Nguyen <73955570+ctnnguyen@users.noreply.github.com> Date: Sat, 18 Apr 2026 09:14:10 -0700 Subject: [PATCH] fix(dynamodb): validate sort key in buildItemKey to prevent item collisions (#506) --- .../services/dynamodb/DynamoDbService.java | 6 +- .../dynamodb/DynamoDbIntegrationTest.java | 77 +++++++++++++++++++ .../dynamodb/DynamoDbServiceTest.java | 77 +++++++++++++++++++ 3 files changed, 158 insertions(+), 2 deletions(-) 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 2a07a11c5c..83dbbed475 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 @@ -1622,9 +1622,11 @@ public class DynamoDbService { String skName = table.getSortKeyName(); if (skName != null) { JsonNode skAttr = item.get(skName); - if (skAttr != null) { - return pk + "#" + extractScalarValue(skAttr); + if (skAttr == null) { + throw new AwsException("ValidationException", + "One of the required keys was not given a value", 400); } + return pk + "#" + extractScalarValue(skAttr); } return pk; } 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 46a3484cc8..08d38b6b8a 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 @@ -1605,6 +1605,83 @@ given() assertEquals(Long.toString(crc32Of(errorResponse.asByteArray())), errorCrc); } + @Test + void updateItemWithSamePartitionKeyButDifferentSortKeyCreatesSeparateItems() { + // Reproduces GitHub issue #498: UpdateItem on a table with a sort key + // overwrites the existing row instead of creating a new one when the + // partition key matches but the sort key differs. + String tableName = "CoordinationTable"; + + given() + .header("X-Amz-Target", "DynamoDB_20120810.CreateTable") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "KeySchema": [ + {"AttributeName": "GroupKey", "KeyType": "HASH"}, + {"AttributeName": "Id", "KeyType": "RANGE"} + ], + "AttributeDefinitions": [ + {"AttributeName": "GroupKey", "AttributeType": "S"}, + {"AttributeName": "Id", "AttributeType": "S"} + ], + "ProvisionedThroughput": {"ReadCapacityUnits": 1, "WriteCapacityUnits": 1} + } + """.formatted(tableName)) + .when().post("/") + .then().statusCode(200); + + given() + .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Key": {"GroupKey": {"S": "leader"}, "Id": {"S": "app1"}}, + "UpdateExpression": "SET Owner = :1", + "ExpressionAttributeValues": {":1": {"S": "owner-app1"}} + } + """.formatted(tableName)) + .when().post("/") + .then().statusCode(200); + + given() + .header("X-Amz-Target", "DynamoDB_20120810.UpdateItem") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "%s", + "Key": {"GroupKey": {"S": "leader"}, "Id": {"S": "app2"}}, + "UpdateExpression": "SET Owner = :1", + "ExpressionAttributeValues": {":1": {"S": "owner-app2"}} + } + """.formatted(tableName)) + .when().post("/") + .then().statusCode(200); + + given() + .header("X-Amz-Target", "DynamoDB_20120810.Scan") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + {"TableName": "%s"} + """.formatted(tableName)) + .when().post("/") + .then() + .statusCode(200) + .body("Count", equalTo(2)) + .body("ScannedCount", equalTo(2)); + + given() + .header("X-Amz-Target", "DynamoDB_20120810.DeleteTable") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + {"TableName": "%s"} + """.formatted(tableName)) + .when().post("/") + .then().statusCode(200); + } + private static long crc32Of(byte[] bytes) { CRC32 crc = new CRC32(); crc.update(bytes); 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 3d68e40f4e..4b7a485565 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 @@ -1215,6 +1215,83 @@ class DynamoDbServiceTest { return key; } + @Test + void updateItemWithDifferentSortKeysCreatesSeparateItems() { + createOrdersTable(); + + ObjectNode key1 = item("customerId", "c1", "orderId", "app1"); + ObjectNode exprValues = mapper.createObjectNode(); + exprValues.set(":owner", attributeValue("S", "owner-1")); + + service.updateItem("Orders", key1, null, + "SET owner = :owner", null, exprValues, null); + + ObjectNode key2 = item("customerId", "c1", "orderId", "app2"); + exprValues = mapper.createObjectNode(); + exprValues.set(":owner", attributeValue("S", "owner-2")); + + service.updateItem("Orders", key2, null, + "SET owner = :owner", null, exprValues, null); + + DynamoDbService.ScanResult scanResult = service.scan("Orders", null, null, null, null, null, null); + assertEquals(2, scanResult.items().size(), + "two items with same partition key but different sort keys must be stored separately"); + + JsonNode item1 = service.getItem("Orders", key1); + assertNotNull(item1); + assertEquals("owner-1", item1.get("owner").get("S").asText()); + + JsonNode item2 = service.getItem("Orders", key2); + assertNotNull(item2); + assertEquals("owner-2", item2.get("owner").get("S").asText()); + } + + @Test + void updateItemMissingSortKeyThrowsValidationException() { + createOrdersTable(); + + ObjectNode keyMissingSk = item("customerId", "c1"); + ObjectNode exprValues = mapper.createObjectNode(); + exprValues.set(":val", attributeValue("S", "test")); + + AwsException ex = assertThrows(AwsException.class, () -> + service.updateItem("Orders", keyMissingSk, null, + "SET name = :val", null, exprValues, null)); + assertEquals("ValidationException", ex.getErrorCode()); + } + + @Test + void getItemMissingSortKeyThrowsValidationException() { + createOrdersTable(); + service.putItem("Orders", item("customerId", "c1", "orderId", "o1", "total", "100")); + + ObjectNode keyMissingSk = item("customerId", "c1"); + AwsException ex = assertThrows(AwsException.class, () -> + service.getItem("Orders", keyMissingSk)); + assertEquals("ValidationException", ex.getErrorCode()); + } + + @Test + void deleteItemMissingSortKeyThrowsValidationException() { + createOrdersTable(); + service.putItem("Orders", item("customerId", "c1", "orderId", "o1", "total", "100")); + + ObjectNode keyMissingSk = item("customerId", "c1"); + AwsException ex = assertThrows(AwsException.class, () -> + service.deleteItem("Orders", keyMissingSk)); + assertEquals("ValidationException", ex.getErrorCode()); + } + + @Test + void putItemMissingSortKeyThrowsValidationException() { + createOrdersTable(); + + ObjectNode itemMissingSk = item("customerId", "c1", "total", "100"); + AwsException ex = assertThrows(AwsException.class, () -> + service.putItem("Orders", itemMissingSk)); + assertEquals("ValidationException", ex.getErrorCode()); + } + @Test void updateExpressionAcceptsNewlineBetweenSetAndAdd() { // "SET ... \n ADD ..." — previously both clauses were silently dropped: