From ad57ac837fc77e64860a88aaa90a8b487f4a8110 Mon Sep 17 00:00:00 2001 From: Tyler Webb <85189637+T-Webb1@users.noreply.github.com> Date: Fri, 16 Oct 2026 16:46:44 -0700 Subject: [PATCH] fix(dynamodb): compare numeric key attributes by value in UpdateItem (#5837) * fix(dynamodb): compare numeric key attributes by value in UpdateItem UpdateItem and the TransactWriteItems Update path rejected a request whose key carried an equivalent but differently written number, such as a sort key of {"N":"1700000000.0"} against a stored {"N":"1700000000"}, with "Cannot update attribute timestamp. This attribute is part of the key". Stored numbers are normalized on write, and the key-modification check compared AttributeValue JSON nodes structurally. The check now uses attributeValuesEqual, which requires matching types and compares N by numeric value, as DynamoDB does. The item lookup already treated these keys as the same item. * fix(dynamodb): reject key attribute values with more than one type member in UpdateItem Comparing key attributes with attributeValuesEqual matches on the first type member the two values share, so an UpdateExpression that set a key attribute to a value carrying an extra type member passed the key check and the malformed key was stored. The request key is already validated for shape and schema type when the item key is built, so checking that the updated value has exactly one type member is enough for the comparison to imply a matching schema type. --- .../services/dynamodb/DynamoDbService.java | 10 +- .../dynamodb/DynamoDbServiceTest.java | 136 ++++++++++++++++++ 2 files changed, 144 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 4149153b96..7c7d0c1651 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 @@ -2082,7 +2082,10 @@ public class DynamoDbService { String pkName = table.getPartitionKeyName(); JsonNode origPk = key.get(pkName); JsonNode newPk = item.get(pkName); - if (origPk != null && newPk != null && !origPk.equals(newPk)) { + if (newPk != null) { + validateAttributeValueShape(newPk); + } + if (origPk != null && newPk != null && !attributeValuesEqual(origPk, newPk)) { throw new AwsException("ValidationException", "One or more parameter values were invalid: Cannot update attribute " + pkName + ". This attribute is part of the key", 400); @@ -2091,7 +2094,10 @@ public class DynamoDbService { if (skName != null) { JsonNode origSk = key.get(skName); JsonNode newSk = item.get(skName); - if (origSk != null && newSk != null && !origSk.equals(newSk)) { + if (newSk != null) { + validateAttributeValueShape(newSk); + } + if (origSk != null && newSk != null && !attributeValuesEqual(origSk, newSk)) { throw new AwsException("ValidationException", "One or more parameter values were invalid: Cannot update attribute " + skName + ". This attribute is part of the key", 400); 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 844e8f7714..5e19bcd51d 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 @@ -2349,6 +2349,142 @@ class DynamoDbServiceTest { assertEquals("owner-2", item2.get("owner").get("S").asText()); } + private void createEventsTableWithNumericSortKey(String region) { + service.createTable("Events", + List.of( + new KeySchemaElement("deviceId", "HASH"), + new KeySchemaElement("timestamp", "RANGE")), + List.of( + new AttributeDefinition("deviceId", "S"), + new AttributeDefinition("timestamp", "N")), + 5L, 5L, region); + } + + @Test + void updateItem_numericSortKeyWithEquivalentRepresentation_updatesStoredItem() throws Exception { + String region = "eu-west-1"; + createEventsTableWithNumericSortKey(region); + service.putItem("Events", mapper.readTree(""" + {"deviceId":{"S":"d1"},"timestamp":{"N":"1700000000"},"status":{"S":"before"}} + """), region); + + JsonNode key = mapper.readTree(""" + {"deviceId":{"S":"d1"},"timestamp":{"N":"1700000000.0"}} + """); + ObjectNode exprValues = mapper.createObjectNode(); + exprValues.set(":status", attributeValue("S", "after")); + ObjectNode exprNames = mapper.createObjectNode(); + exprNames.put("#status", "status"); + + assertDoesNotThrow(() -> service.updateItem("Events", key, null, + "SET #status = :status", exprNames, exprValues, null, region)); + + JsonNode stored = service.getItem("Events", mapper.readTree(""" + {"deviceId":{"S":"d1"},"timestamp":{"N":"1700000000"}} + """), region); + assertEquals("after", stored.path("status").path("S").asText()); + } + + @Test + void transactWriteUpdate_numericSortKeyWithEquivalentRepresentation_updatesStoredItem() throws Exception { + String region = "eu-west-1"; + createEventsTableWithNumericSortKey(region); + service.putItem("Events", mapper.readTree(""" + {"deviceId":{"S":"d1"},"timestamp":{"N":"1700000000"},"status":{"S":"before"}} + """), region); + JsonNode update = mapper.readTree(""" + {"Update":{"TableName":"Events", + "Key":{"deviceId":{"S":"d1"},"timestamp":{"N":"1700000000.0"}}, + "UpdateExpression":"SET #status = :status", + "ExpressionAttributeNames":{"#status":"status"}, + "ExpressionAttributeValues":{":status":{"S":"after"}}}} + """); + + assertDoesNotThrow(() -> service.transactWriteItems(List.of(update), region)); + + JsonNode stored = service.getItem("Events", mapper.readTree(""" + {"deviceId":{"S":"d1"},"timestamp":{"N":"1700000000"}} + """), region); + assertEquals("after", stored.path("status").path("S").asText()); + } + + @Test + void updateItem_numericSortKeyChangedToDifferentValue_throwsValidationException() throws Exception { + String region = "eu-west-1"; + createEventsTableWithNumericSortKey(region); + service.putItem("Events", mapper.readTree(""" + {"deviceId":{"S":"d1"},"timestamp":{"N":"1700000000"}} + """), region); + + JsonNode key = mapper.readTree(""" + {"deviceId":{"S":"d1"},"timestamp":{"N":"1700000000.0"}} + """); + ObjectNode exprValues = mapper.createObjectNode(); + exprValues.set(":ts", attributeValue("N", "1700000001")); + ObjectNode exprNames = mapper.createObjectNode(); + exprNames.put("#ts", "timestamp"); + + AwsException ex = assertThrows(AwsException.class, () -> + service.updateItem("Events", key, null, + "SET #ts = :ts", exprNames, exprValues, null, region)); + assertEquals("ValidationException", ex.getErrorCode()); + assertTrue(ex.getMessage().contains("Cannot update attribute timestamp")); + } + + @Test + void updateItem_sortKeySetToValueWithExtraTypeMember_throwsValidationException() throws Exception { + String region = "eu-west-1"; + createEventsTableWithNumericSortKey(region); + service.putItem("Events", mapper.readTree(""" + {"deviceId":{"S":"d1"},"timestamp":{"N":"1700000000"}} + """), region); + + JsonNode key = mapper.readTree(""" + {"deviceId":{"S":"d1"},"timestamp":{"N":"1700000000"}} + """); + ObjectNode exprValues = (ObjectNode) mapper.readTree(""" + {":ts":{"N":"1700000000","S":"extra"}} + """); + ObjectNode exprNames = mapper.createObjectNode(); + exprNames.put("#ts", "timestamp"); + + AwsException ex = assertThrows(AwsException.class, () -> + service.updateItem("Events", key, null, + "SET #ts = :ts", exprNames, exprValues, null, region)); + assertEquals("ValidationException", ex.getErrorCode()); + assertTrue(ex.getMessage().contains("more than one datatypes")); + + JsonNode stored = service.getItem("Events", key, region); + assertFalse(stored.path("timestamp").has("S")); + } + + @Test + void updateItem_partitionKeySetToValueWithExtraTypeMember_throwsValidationException() throws Exception { + String region = "eu-west-1"; + createEventsTableWithNumericSortKey(region); + service.putItem("Events", mapper.readTree(""" + {"deviceId":{"S":"d1"},"timestamp":{"N":"1700000000"}} + """), region); + + JsonNode key = mapper.readTree(""" + {"deviceId":{"S":"d1"},"timestamp":{"N":"1700000000"}} + """); + ObjectNode exprValues = (ObjectNode) mapper.readTree(""" + {":id":{"S":"d1","N":"5"}} + """); + ObjectNode exprNames = mapper.createObjectNode(); + exprNames.put("#id", "deviceId"); + + AwsException ex = assertThrows(AwsException.class, () -> + service.updateItem("Events", key, null, + "SET #id = :id", exprNames, exprValues, null, region)); + assertEquals("ValidationException", ex.getErrorCode()); + assertTrue(ex.getMessage().contains("more than one datatypes")); + + JsonNode stored = service.getItem("Events", key, region); + assertFalse(stored.path("deviceId").has("N")); + } + @Test void updateItemMissingSortKeyThrowsValidationException() { String region = "eu-west-1";