mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(dynamodb): refuse a repeated section in an UpdateExpression (#8167)
This commit is contained in:
1 parent
438e01a225
commit
891c0cd58f
3 files changed
+109
-2
No files matched your search
@@ -3541,6 +3541,7 @@ public class DynamoDbService {
|
||||
// name as a bare identifier instead.
|
||||
String remaining = expression.trim().replaceAll("\\s+", " ");
|
||||
DynamoDbReservedWords.check(remaining, "UpdateExpression");
|
||||
requireSectionsUsedOnce(remaining);
|
||||
|
||||
while (!remaining.isEmpty()) {
|
||||
String upper = remaining.toUpperCase();
|
||||
@@ -4454,10 +4455,24 @@ public class DynamoDbService {
|
||||
}
|
||||
}
|
||||
|
||||
static void requireSectionsUsedOnce(String expression) {
|
||||
Set<String> seen = new HashSet<>();
|
||||
for (ExpressionEvaluator.Token token : ExpressionEvaluator.tokenize(expression)) {
|
||||
String word = token.value().toUpperCase(Locale.ROOT);
|
||||
if (token.type() == ExpressionEvaluator.TokenType.IDENTIFIER && UPDATE_SECTIONS.contains(word)
|
||||
&& !seen.add(word)) {
|
||||
throw new AwsException("ValidationException", "Invalid UpdateExpression: The \"" + word
|
||||
+ "\" section can only be used once in an update expression;", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isUpdatePunctuation(char c) {
|
||||
return "()[],.=+-".indexOf(c) >= 0;
|
||||
}
|
||||
|
||||
private static final Set<String> UPDATE_SECTIONS = Set.of("SET", "REMOVE", "ADD", "DELETE");
|
||||
|
||||
// DynamoDB reads a SET target path up to "=", so a comma before it fails on the comma.
|
||||
static void requireAssignmentAfterSetTarget(String expression) {
|
||||
List<ExpressionEvaluator.Token> tokens = ExpressionEvaluator.tokenize(expression);
|
||||
@@ -4491,7 +4506,7 @@ public class DynamoDbService {
|
||||
case COMMA -> target = inSet && depth == 0 ? i + 1 : -1;
|
||||
case IDENTIFIER -> {
|
||||
String word = token.value().toUpperCase(Locale.ROOT);
|
||||
if (depth == 0 && List.of("SET", "REMOVE", "ADD", "DELETE").contains(word)) {
|
||||
if (depth == 0 && UPDATE_SECTIONS.contains(word)) {
|
||||
inSet = word.equals("SET");
|
||||
target = inSet ? i + 1 : -1;
|
||||
}
|
||||
|
||||
@@ -1061,11 +1061,12 @@ public class NativeDynamoDbJsonHandler {
|
||||
ExpressionEvaluator.validateSemantics(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
requireNonEmptyExpression(updateExpression, "UpdateExpression");
|
||||
DynamoDbReservedWords.check(ue, "UpdateExpression");
|
||||
dynamoDbService.requireAddOrDeleteOperandTypes(updateExpression, exprAttrValues, true);
|
||||
DynamoDbService.requireSectionsUsedOnce(updateExpression);
|
||||
Set<String> hashTokens = extractHashTokens(updateExpression, conditionExpression);
|
||||
checkUnusedEan(exprAttrNames, hashTokens);
|
||||
Set<String> colonTokensAll = extractColonTokens(updateExpression, conditionExpression);
|
||||
checkUnusedEav(exprAttrValues, colonTokensAll);
|
||||
dynamoDbService.requireAddOrDeleteOperandTypes(updateExpression, exprAttrValues, true);
|
||||
} else {
|
||||
ExpressionEvaluator.validateSemantics(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
}
|
||||
@@ -3162,6 +3163,9 @@ public class NativeDynamoDbJsonHandler {
|
||||
DynamoDbExpressionSize.checkRead(updateExpression, "UpdateExpression");
|
||||
requireDefinedTokens(updateExpression, "UpdateExpression", exprAttrNames, exprAttrValues);
|
||||
dynamoDbService.requireAddOrDeleteOperandTypes(updateExpression, exprAttrValues, false);
|
||||
if (updateExpression != null) {
|
||||
DynamoDbService.requireSectionsUsedOnce(updateExpression);
|
||||
}
|
||||
|
||||
checkUnusedEan(exprAttrNames, extractHashTokens(conditionExpression, updateExpression));
|
||||
checkUnusedEav(exprAttrValues, extractColonTokens(conditionExpression, updateExpression));
|
||||
|
||||
@@ -2154,6 +2154,64 @@ class NativeDynamoDbJsonHandlerTest {
|
||||
assertEquals("ResourceNotFoundException", ex.getErrorCode(), ex.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', textBlock = """
|
||||
SET a = :v SET b = :v | SET
|
||||
REMOVE a REMOVE b | REMOVE
|
||||
ADD a :s ADD b :s | ADD
|
||||
DELETE a :s DELETE b :s | DELETE
|
||||
add a :s\tADD b :s | ADD
|
||||
REMOVE a REMOVE b REMOVE c | REMOVE
|
||||
ADD a :s REMOVE b REMOVE c ADD d :s | REMOVE
|
||||
SET a = :v DELETE b :s ADD c :s DELETE d :s SET e = :v | DELETE
|
||||
""")
|
||||
void updateItemRejectsARepeatedSectionBeforeTheTableLookup(String expression, String section) {
|
||||
ObjectNode request = updateUserRequest();
|
||||
request.put("TableName", "Missing");
|
||||
request.put("UpdateExpression", expression);
|
||||
request.set("ExpressionAttributeValues", valueAndSet());
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> handler.handle("UpdateItem", request, "eu-west-1"));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals("1 validation error detected: Invalid UpdateExpression: The \"" + section
|
||||
+ "\" section can only be used once in an update expression;", ex.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', textBlock = """
|
||||
ADD a :v ADD b :v | Incorrect operand type for operator or function; operator: ADD, operand type: STRING, typeSet: ALLOWED_FOR_ADD_OPERAND
|
||||
ADD a :s ADD b :w | An expression attribute value used in expression is not defined; attribute value: :w
|
||||
""")
|
||||
void updateItemChecksARepeatedSectionAfterTheOtherUpdateExpressionErrors(String expression, String message) {
|
||||
ObjectNode request = updateUserRequest();
|
||||
request.put("TableName", "Missing");
|
||||
request.put("UpdateExpression", expression);
|
||||
request.set("ExpressionAttributeValues", valueAndSet());
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> handler.handle("UpdateItem", request, "eu-west-1"));
|
||||
assertEquals("1 validation error detected: Invalid UpdateExpression: " + message, ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateItemRefusesARepeatedSectionForACallerThatSkipsTheHandler() throws Exception {
|
||||
createUsersTable("eu-west-1");
|
||||
ObjectNode key = item("userId", "u1");
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () -> service.updateItem("Users", key, null,
|
||||
"ADD a :s ADD b :s", null, valueAndSet(), "NONE", "eu-west-1"));
|
||||
assertEquals("Invalid UpdateExpression: The \"ADD\" section can only be used once in an update expression;",
|
||||
ex.getMessage());
|
||||
assertNull(service.getItem("Users", key, "eu-west-1"));
|
||||
}
|
||||
|
||||
private ObjectNode valueAndSet() {
|
||||
ObjectNode values = singleValue(":v");
|
||||
values.putObject(":s").putArray("SS").add("x");
|
||||
return values;
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateItemAcceptsAnUpdateExpressionOfExactly4096Bytes() throws Exception {
|
||||
createUsersTable("eu-west-1");
|
||||
@@ -2400,6 +2458,36 @@ class NativeDynamoDbJsonHandlerTest {
|
||||
assertNull(service.getItem("Users", item("userId", "u1"), "eu-west-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void transactWriteItemsRejectsARepeatedSection() {
|
||||
createUsersTable("eu-west-1");
|
||||
ObjectNode update = mapper.createObjectNode();
|
||||
update.set("Key", item("userId", "u1"));
|
||||
update.put("UpdateExpression", "REMOVE a REMOVE b");
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> handler.handle("TransactWriteItems", transactWrite("Update", update), "eu-west-1"));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals("Invalid UpdateExpression: The \"REMOVE\" section can only be used once in an update expression;",
|
||||
ex.getMessage());
|
||||
assertNull(service.getItem("Users", item("userId", "u1"), "eu-west-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void transactWriteItemsRejectsARepeatedSectionBeforeUnusedValuesAndTheTableLookup() {
|
||||
ObjectNode update = mapper.createObjectNode();
|
||||
update.set("Key", item("userId", "u1"));
|
||||
update.put("UpdateExpression", "REMOVE a REMOVE b");
|
||||
update.put("ConditionExpression", "attribute_exists(c)");
|
||||
update.set("ExpressionAttributeValues", singleValue(":u"));
|
||||
update.putObject("ExpressionAttributeNames").put("#u", "u");
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> handler.handle("TransactWriteItems", transactWrite("Update", update), "eu-west-1"));
|
||||
assertEquals("Invalid UpdateExpression: The \"REMOVE\" section can only be used once in an update expression;",
|
||||
ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void transactWriteItemsRejectsAPutItemWithALeafAtLevel33() {
|
||||
createUsersTable("eu-west-1");
|
||||
|
||||
Reference in new issue
Block a user