fix(dynamodb): refuse an over-limit UpdateItem statement before the condition (#8076)

This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-12 07:27:37 -04:00
1 parent f8e5a883d8
commit 67814c5d21
5 files changed
+201 -12

No files matched your search

@@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.storage.InMemoryStorage;
import io.floci.aws.services.dynamodb.model.AttributeDefinition;
import io.floci.aws.services.dynamodb.model.ConditionalCheckFailedException;
import io.floci.aws.services.dynamodb.model.KeySchemaElement;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -209,6 +210,51 @@ class DynamoDbItemSizeServiceTest {
assertRefused(() -> update("b", "SET b = if_not_exists(b, :pad)", ceiling + 1, 0));
}
@ParameterizedTest
@CsvSource({
"'SET c = if_not_exists(c, :huge)', attribute_exists(nope)",
"'SET c = if_not_exists(c, :huge)', attribute_exists(c)",
"'SET c = if_not_exists(c, :huge)',",
"SET pk = :huge,"
})
void updateItemRefusesAnOverLimitStatementBeforeTheCondition(String expression, String condition) {
seed("a", "c", stringValue("x"));
assertRefused(() -> service.updateItem(TABLE, key("a"), null, expression, null,
values(":huge", stringValue("h".repeat(MAX_ITEM_SIZE + 100))), "NONE", condition, REGION, "NONE"));
assertEquals("pk".length() + "a".length() + "c".length() + "x".length(), storedBytes("a"));
}
@ParameterizedTest
@CsvSource(delimiter = '|', textBlock = """
SET a
SET a = :v, b
ADD a
SET ,
ADD ,
SET a = if_not_exists(b, :v
SET a = list_append(c, :v
SET a = if_not_exists(list_append(c, :v), :v
SET a, b = :v
""")
void updateItemLeavesAStatementItCannotSizeToTheUpdateParser(String expression) {
seed("a", "c", stringValue("x"));
AwsException refused = assertThrows(AwsException.class, () -> service.updateItem(TABLE, key("a"), null,
expression, null, values(":v", stringValue("v")), "NONE", REGION));
assertEquals("ValidationException", refused.getErrorCode());
}
@Test
void updateItemChecksTheConditionBeforeTheFinishedItem() {
seed("a", "b", stringValue(padding("a", MAX_ITEM_SIZE, 0)));
assertThrows(ConditionalCheckFailedException.class, () -> service.updateItem(TABLE, key("a"), null,
"SET d = :d", null, values(":d", stringValue("y")), "NONE", "attribute_exists(nope)", REGION, "NONE"));
assertRefused(() -> service.updateItem(TABLE, key("a"), null,
"SET d = :d", null, values(":d", stringValue("y")), "NONE", null, REGION, "NONE"));
}
@Test
void updateItemMeasuresALegacyAttributeUpdateByTheFinishedItem() {
updateAttribute("a", MAX_ITEM_SIZE);
@@ -1784,6 +1784,50 @@ class NativeDynamoDbJsonHandlerTest {
});
}
@ParameterizedTest
@CsvSource(delimiter = '|', textBlock = """
SET a, b = :v | a, b
SET a , b = :v | a , b
SET a,b = :v | a,b
set a, b = :v | a, b
SET a = :v, b, c = :v | b, c
SET a.b[1], c = :v | ], c
SET #n, b = :v | #n, b
SET , b = :v | SET , b
SET , | SET ,
SET a, | a,
SET a = :v, , b = :v | , , b
SET a, b = (:v | a, b
SET a = if_not_exists(b, :v), c, d = :v | c, d
REMOVE b SET a, c = :v | a, c
SET a, b = :v $ | a, b
""")
void updateItemRejectsACommaInASetTargetBeforeTheTableLookup(String expression, String near) {
ObjectNode request = updateUserRequest();
request.put("TableName", "Missing");
request.put("UpdateExpression", expression);
request.set("ExpressionAttributeValues", singleValue(":v"));
AwsException ex = assertThrows(AwsException.class,
() -> handler.handle("UpdateItem", request, "eu-west-1"));
assertEquals("ValidationException", ex.getErrorCode());
assertEquals("1 validation error detected: Invalid UpdateExpression: Syntax error; "
+ "token: \",\", near: \"" + near + "\"", ex.getMessage());
}
@ParameterizedTest
@ValueSource(strings = {"SET a = :v REMOVE b, c", "SET a = if_not_exists(b, :v), c = :v", "SET a = b + :v, c = :v"})
void updateItemReadsACommaOutsideASetTargetAsASeparator(String expression) {
ObjectNode request = updateUserRequest();
request.put("TableName", "Missing");
request.put("UpdateExpression", expression);
request.set("ExpressionAttributeValues", singleValue(":v"));
AwsException ex = assertThrows(AwsException.class,
() -> handler.handle("UpdateItem", request, "eu-west-1"));
assertEquals("ResourceNotFoundException", ex.getErrorCode(), ex.getMessage());
}
@Test
void updateItemAcceptsAnUpdateExpressionOfExactly4096Bytes() throws Exception {
createUsersTable("eu-west-1");
@@ -2015,6 +2059,21 @@ class NativeDynamoDbJsonHandlerTest {
assertEquals("Invalid UpdateExpression: Syntax error; token: \"<EOF>\", near: \":v\"", ex.getMessage());
}
@Test
void transactWriteItemsRejectsACommaInASetTarget() {
createUsersTable("eu-west-1");
ObjectNode update = mapper.createObjectNode();
update.set("Key", item("userId", "u1"));
update.put("UpdateExpression", "SET a, b = :v");
update.set("ExpressionAttributeValues", singleValue(":v"));
AwsException ex = assertThrows(AwsException.class,
() -> handler.handle("TransactWriteItems", transactWrite("Update", update), "eu-west-1"));
assertEquals("ValidationException", ex.getErrorCode());
assertEquals("Invalid UpdateExpression: Syntax error; token: \",\", near: \"a, b\"", ex.getMessage());
assertNull(service.getItem("Users", item("userId", "u1"), "eu-west-1"));
}
@Test
void transactWriteItemsRejectsAPutItemWithALeafAtLevel33() {
createUsersTable("eu-west-1");