mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): refuse overlapping UpdateExpression paths on the service path too (#9763)
The overlap check ran only in NativeDynamoDbJsonHandler, so callers that reach DynamoDbService.updateItem directly, the Step Functions DynamoDB updateItem task through NativeDynamoDbBackend and PartiQL UPDATE, still applied an UpdateExpression whose actions name the same path, or a path and one inside it, where DynamoDB refuses it. - add DynamoDbUpdateExpressionSyntax.checkPaths, which reads the sections and runs only the shared overlap check (the placeholder and value checks stay in the handler) - call it in updateItemInternal before the item lock, the read and the condition, and in the TransactWriteItems Update validation
This commit is contained in:
1 parent
0ad01626bd
commit
3440dcc7c5
3 files changed
+52
No files matched your search
@@ -1480,6 +1480,10 @@ public class DynamoDbService {
|
||||
|
||||
String itemKey = buildItemKey(table, key, true);
|
||||
|
||||
if (updateExpression != null && !updateExpression.isEmpty()) {
|
||||
DynamoDbUpdateExpressionSyntax.checkPaths(updateExpression, expressionAttrNames);
|
||||
}
|
||||
|
||||
// AWS refuses an over-limit statement before it evaluates the condition.
|
||||
if (sizeRule == UpdateSizeRule.UPDATE_ITEM && updateExpression != null) {
|
||||
DynamoDbItemSize.updateStatementSize(updateExpression, path -> parsePath(path, expressionAttrNames),
|
||||
@@ -2622,6 +2626,9 @@ public class DynamoDbService {
|
||||
JsonNode exprAttrNames = upd.has("ExpressionAttributeNames") ? upd.get("ExpressionAttributeNames") : null;
|
||||
JsonNode exprAttrValues = upd.has("ExpressionAttributeValues") ? upd.get("ExpressionAttributeValues") : null;
|
||||
if (updateExpression != null) {
|
||||
if (!updateExpression.isEmpty()) {
|
||||
DynamoDbUpdateExpressionSyntax.checkPaths(updateExpression, exprAttrNames);
|
||||
}
|
||||
applyUpdateExpression(item, updateExpression, exprAttrNames, exprAttrValues);
|
||||
}
|
||||
validateKeyNotModified(table, key, item);
|
||||
|
||||
@@ -64,6 +64,12 @@ final class DynamoDbUpdateExpressionSyntax {
|
||||
syntax.checkActions();
|
||||
}
|
||||
|
||||
static void checkPaths(String expression, JsonNode names) {
|
||||
DynamoDbUpdateExpressionSyntax syntax = new DynamoDbUpdateExpressionSyntax(expression, names, null);
|
||||
syntax.readSections();
|
||||
syntax.checkDisjointPaths();
|
||||
}
|
||||
|
||||
private void readSections() {
|
||||
do {
|
||||
if (!isSection(peek())) {
|
||||
@@ -178,6 +184,10 @@ final class DynamoDbUpdateExpressionSyntax {
|
||||
default -> { }
|
||||
}
|
||||
}
|
||||
checkDisjointPaths();
|
||||
}
|
||||
|
||||
private void checkDisjointPaths() {
|
||||
if (repeatedSection) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -246,6 +246,41 @@ class DynamoDbNativeBackendTest {
|
||||
assertEquals(json("{\"id\":{\"S\":\"k\"},\"n\":{\"N\":\"5\"}}"), backend.getItem(SCOPE, TABLE, key));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', textBlock = """
|
||||
SET parent = :m, parent.child = :v | path one: [parent], path two: [parent, child]
|
||||
ADD tags :a DELETE tags :b | path one: [tags], path two: [tags]
|
||||
""")
|
||||
void typedUpdateItemRefusesOverlappingPathsAndLeavesTheItemAlone(String expression, String paths)
|
||||
throws Exception {
|
||||
createTable("");
|
||||
JsonNode key = json("{\"id\":{\"S\":\"k\"}}");
|
||||
JsonNode stored = json("{\"id\":{\"S\":\"k\"},\"tags\":{\"SS\":[\"x\"]}}");
|
||||
backend.putItem(SCOPE, TABLE, stored, null, null, null);
|
||||
JsonNode values = json("{\":m\":{\"M\":{}},\":v\":{\"N\":\"1\"},\":a\":{\"SS\":[\"y\"]},\":b\":{\"SS\":[\"x\"]}}");
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> backend.updateItem(SCOPE, TABLE, key, null, expression.trim(), null, values, "NONE", null));
|
||||
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
assertEquals("Invalid UpdateExpression: Two document paths overlap with each other; must remove or rewrite "
|
||||
+ "one of these paths; " + paths, refused.getMessage());
|
||||
assertEquals(stored, backend.getItem(SCOPE, TABLE, key));
|
||||
}
|
||||
|
||||
@Test
|
||||
void typedUpdateItemAppliesAnExpressionWhoseActionsTargetDifferentPaths() throws Exception {
|
||||
createTable("");
|
||||
JsonNode key = json("{\"id\":{\"S\":\"k\"}}");
|
||||
backend.putItem(SCOPE, TABLE, key, null, null, null);
|
||||
|
||||
backend.updateItem(SCOPE, TABLE, key, null, "SET a = :v, b = :v", null,
|
||||
json("{\":v\":{\"N\":\"1\"}}"), "NONE", null);
|
||||
|
||||
assertEquals(json("{\"id\":{\"S\":\"k\"},\"a\":{\"N\":\"1\"},\"b\":{\"N\":\"1\"}}"),
|
||||
backend.getItem(SCOPE, TABLE, key));
|
||||
}
|
||||
|
||||
@Test
|
||||
void typedWritesStoreMapKeysInCodePointOrder() throws Exception {
|
||||
backend.createTable(SCOPE, TABLE, List.of(new KeySchemaElement("id", "HASH")),
|
||||
|
||||
Reference in new issue
Block a user