fix(dynamodb): parse the whole UpdateExpression before reading names and values (#8190)

This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-13 00:12:26 -04:00
1 parent 1686f29e04
commit 38085d1d75
7 files changed
+346 -127

No files matched your search

@@ -4012,9 +4012,8 @@ class DynamoDbServiceTest {
}
@Test
void updateItemSetDoubledOuterParensStillApplies() {
void updateItemRefusesDoubledOuterParens() {
String region = "eu-west-1";
// Defence-in-depth: even multiply-wrapped expressions should parse the same as bare.
createUsersTable(region);
ObjectNode existing = mapper.createObjectNode();
@@ -4031,12 +4030,13 @@ class DynamoDbServiceTest {
ObjectNode exprNames = mapper.createObjectNode();
exprNames.put("#cnt", "counter");
service.updateItem("Users", key, null,
AwsException ex = assertThrows(AwsException.class, () -> service.updateItem("Users", key, null,
"SET #cnt = ((#cnt - :v))",
exprNames, exprValues, null, region);
exprNames, exprValues, null, region));
assertEquals("Invalid UpdateExpression: The expression has redundant parentheses;", ex.getMessage());
JsonNode stored = service.getItem("Users", key, region);
assertEquals("7", stored.get("counter").get("N").asText());
assertEquals("10", stored.get("counter").get("N").asText());
}
@Test
@@ -877,6 +877,8 @@ class ExpressionEvaluatorTest {
a = :v \uD83D\uDE00 | \uD83D\uDE00 | :v \uD83D\uDE00
`a\u3000= :v` | `\u3000` | `a\u3000=`
a = :v AND $ = = b | $ | AND $ =
a[01] = :v | 1 | 01]
a[007] = :v | 0 | 007
""")
void syntaxErrorsNameTheTokenAndTheTextAroundIt(String expression, String token, String near) {
AwsException e = assertThrows(AwsException.class,
@@ -891,6 +893,12 @@ class ExpressionEvaluatorTest {
assertDoesNotThrow(() -> ExpressionEvaluator.validateSyntax(expression, "ConditionExpression"));
}
@ParameterizedTest
@ValueSource(strings = {"a[0] = :v", "a[10] = :v"})
void aListIndexWithoutALeadingZeroIsAccepted(String expression) {
assertDoesNotThrow(() -> ExpressionEvaluator.validateSyntax(expression, "ConditionExpression"));
}
@Test
void redundantParenthesesAreReportedBeforeAnUnknownCharacter() {
AwsException e = assertThrows(AwsException.class,
@@ -117,7 +117,7 @@ class NativeDynamoDbJsonHandlerTest {
exprValues.put(":newVal", attributeValue("S", "newVal"));
JsonNode request = createRequest("Users", key,
"SET changeAttr = :changeVal, newAttr = :newVal, REMOVE delAttr",
"SET changeAttr = :changeVal, newAttr = :newVal REMOVE delAttr",
null, exprValues, "UPDATED_NEW");
Response response = null;
@@ -194,7 +194,7 @@ class NativeDynamoDbJsonHandlerTest {
exprValues.put(":newVal", attributeValue("S", "newVal"));
JsonNode request = createRequest("Users", key,
"SET changeAttr = :changeVal, newAttr = :newVal, REMOVE delAttr",
"SET changeAttr = :changeVal, newAttr = :newVal REMOVE delAttr",
null, exprValues, "UPDATED_OLD");
Response response = null;
@@ -234,7 +234,7 @@ class NativeDynamoDbJsonHandlerTest {
exprValues.put(":newVal", attributeValue("S", "newVal"));
JsonNode request = createRequest("Users", key,
"SET changeAttr = :changeVal, newAttr = :newVal, REMOVE delAttr",
"SET changeAttr = :changeVal, newAttr = :newVal REMOVE delAttr",
null, exprValues, "ALL_OLD");
Response response = null;
@@ -276,7 +276,7 @@ class NativeDynamoDbJsonHandlerTest {
exprValues.put(":newVal", attributeValue("S", "newVal"));
JsonNode request = createRequest("Users", key,
"SET changeAttr = :changeVal, newAttr = :newVal, REMOVE delAttr",
"SET changeAttr = :changeVal, newAttr = :newVal REMOVE delAttr",
null, exprValues, "ALL_NEW");
Response response = null;
@@ -318,7 +318,7 @@ class NativeDynamoDbJsonHandlerTest {
exprValues.put(":newVal", attributeValue("S", "newVal"));
JsonNode request = createRequest("Users", key,
"SET changeAttr = :changeVal, newAttr = :newVal, REMOVE delAttr",
"SET changeAttr = :changeVal, newAttr = :newVal REMOVE delAttr",
null, exprValues, "NONE");
Response response = null;
@@ -2154,6 +2154,148 @@ class NativeDynamoDbJsonHandlerTest {
assertEquals("ResourceNotFoundException", ex.getErrorCode(), ex.getMessage());
}
@ParameterizedTest
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
SET | <EOF> | SET
` ` | <EOF> | ` `
SET a = :v, | <EOF> | ,
SET a = :v b = :v | b | :v b =
REMOVE a = :v | = | a = :v
ADD a b | b | a b
ADD a :v + :v | + | :v + :v
ADD a if_not_exists(b, :v) | if_not_exists | a if_not_exists(
ADD a :v ADD b | <EOF> | b
ADD ADD :v | ADD | ADD ADD :v
ADD a :v, ADD b :v | ADD | , ADD b
SET a = :v,REMOVE b | REMOVE | ,REMOVE b
SET set = :v | set | SET set =
SET and = :v | and | SET and =
SET :v = a | :v | SET :v =
SET a[] = :v | ] | [] =
SET a[01] = :v | 1 | 01]
SET a.[1] = :v | [ | .[1
SET a[1]b = :v | b | ]b =
SET a = :v + :v + :v | + | :v + :v
SET a = -:v | - | = -:v
SET a = (:v + :v) + :v | + | ) + :v
SET a = :v - (:v + :v) | + | :v + :v
SET a = if_not_exists(b, :v + :v) | + | :v + :v
SET a = if_not_exists() | ) | ()
SET a = if_not_exists(b, :v).c | . | ).c
SET a = #f(:v) | ( | #f(:v
SET a = :v[1] | [ | :v[1
SET a = 1 | 1 | = 1
SET a = (:v = :v) | = | :v = :v
SET a = $ ((:v)) | $ | = $ (
FOO a = :v | FOO | FOO a
""")
void updateItemRejectsAMalformedUpdateExpressionBeforeTheTableLookup(String expression, String token,
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: \"" + token + "\", near: \"" + near + "\"", ex.getMessage());
}
@ParameterizedTest
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
ADD a :v ADD b | <EOF> | b
` ` | <EOF> | ` `
SET a = -:v | - | = -:v
""")
void updateItemCallersThatSkipTheHandlerGetTheSameSyntaxError(String expression, String token, String near) {
createUsersTable("eu-west-1");
AwsException ex = assertThrows(AwsException.class, () -> service.updateItem("Users", item("userId", "u1"),
null, expression, null, singleValue(":v"), "NONE", "eu-west-1"));
assertEquals("Invalid UpdateExpression: Syntax error; token: \"" + token + "\", near: \"" + near + "\"",
ex.getMessage());
assertNull(service.getItem("Users", item("userId", "u1"), "eu-west-1"));
}
@ParameterizedTest
@ValueSource(strings = {"SET a = ((:v)) $", "SET a = ((:v)) REMOVE", "SET a = :v + ((:v))",
"SET a = list_append(((:v)), :v)", "SET a = (((:v) + :v))", "SET a = ((:v + :v))"})
void updateItemRefusesRedundantParenthesesAsSoonAsItClosesThem(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("1 validation error detected: Invalid UpdateExpression: "
+ "The expression has redundant parentheses;", ex.getMessage());
}
@ParameterizedTest
@ValueSource(strings = {"SET a = (:v) + (:v)", "SET a = ((:v) + :v)", "SET a = (:v + :v)", "SET a = (b)",
"SET a = list_append((:v), :v)", "SET a = list_append(if_not_exists(b, :v), :v)",
"SET a = (if_not_exists(b, :v) + :v)", "SET a[ 1 ] = :v", "SET a[0].b[10] = :v",
"REMOVE a[0], b.c", "set a = :v remove b", "SET\ta = :v\nREMOVE b"})
void updateItemAcceptsTheUpdateExpressionGrammar(String expression) {
ObjectNode request = updateUserRequest();
request.put("TableName", "Missing");
request.put("UpdateExpression", expression);
if (expression.contains(":v")) {
request.set("ExpressionAttributeValues", singleValue(":v"));
}
AwsException ex = assertThrows(AwsException.class,
() -> handler.handle("UpdateItem", request, "eu-west-1"));
assertEquals("ResourceNotFoundException", ex.getErrorCode(), ex.getMessage());
}
@ParameterizedTest
@CsvSource(delimiter = '|', textBlock = """
SET a = | a = = b | Invalid ConditionExpression: Syntax error; token: "=", near: "= = b"
SET a = | a = :zz | Invalid ConditionExpression: An expression attribute value used in expression is not defined; attribute value: :zz
SET a = :zz | a = :yy | Invalid ConditionExpression: An expression attribute value used in expression is not defined; attribute value: :yy
SET a = ((:v)) | a = :zz | Invalid ConditionExpression: An expression attribute value used in expression is not defined; attribute value: :zz
""")
void updateItemChecksTheConditionInFullBeforeItParsesTheUpdate(String expression, String condition,
String message) {
ObjectNode request = updateUserRequest();
request.put("TableName", "Missing");
request.put("UpdateExpression", expression);
request.put("ConditionExpression", condition);
AwsException ex = assertThrows(AwsException.class,
() -> handler.handle("UpdateItem", request, "eu-west-1"));
assertEquals("1 validation error detected: " + message, ex.getMessage());
}
@ParameterizedTest
@CsvSource(delimiter = '|', textBlock = """
SET a = | attribute_exists(a) | Invalid UpdateExpression: Syntax error; token: "<EOF>", near: "="
ADD a :v, | | Invalid UpdateExpression: Syntax error; token: "<EOF>", near: ","
SET a = | a = = b | Invalid ConditionExpression: Syntax error; token: "=", near: "= = b"
SET a = | a = :zz | Invalid ConditionExpression: An expression attribute value used in expression is not defined; attribute value: :zz
""")
void transactWriteItemsParsesTheUpdateAfterTheConditionAndBeforeTheTableLookup(String expression,
String condition,
String message) {
ObjectNode update = mapper.createObjectNode();
update.set("Key", item("userId", "u1"));
update.put("UpdateExpression", expression);
if (condition != null) {
update.put("ConditionExpression", condition);
}
ObjectNode request = transactWrite("Update", update);
update.put("TableName", "Missing");
AwsException ex = assertThrows(AwsException.class,
() -> handler.handle("TransactWriteItems", request, "eu-west-1"));
assertEquals("ValidationException", ex.getErrorCode());
assertEquals(message, ex.getMessage());
}
@ParameterizedTest
@CsvSource(delimiter = '|', textBlock = """
SET a = :v SET b = :v | SET