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.
This commit is contained in:
Tyler Webb authored and GitHub committed 2026-10-16 19:46:44 -04:00
1 parent 3db18d58a5
commit ad57ac837f
2 files changed
+144 -2

No files matched your search

@@ -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);
@@ -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";