From 3440dcc7c5e8525397e873cfcc91a84b1f3807e1 Mon Sep 17 00:00:00 2001 From: Mahmoud Khalifa <32329050+M-khalifa@users.noreply.github.com> Date: Sun, 29 Nov 2026 11:49:10 -0600 Subject: [PATCH] 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 --- .../services/dynamodb/DynamoDbService.java | 7 ++++ .../DynamoDbUpdateExpressionSyntax.java | 10 ++++++ .../backend/DynamoDbNativeBackendTest.java | 35 +++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/src/main/java/io/floci/aws/services/dynamodb/DynamoDbService.java b/src/main/java/io/floci/aws/services/dynamodb/DynamoDbService.java index b11616d06b..faf30e1fa0 100644 --- a/src/main/java/io/floci/aws/services/dynamodb/DynamoDbService.java +++ b/src/main/java/io/floci/aws/services/dynamodb/DynamoDbService.java @@ -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); diff --git a/src/main/java/io/floci/aws/services/dynamodb/DynamoDbUpdateExpressionSyntax.java b/src/main/java/io/floci/aws/services/dynamodb/DynamoDbUpdateExpressionSyntax.java index 21e3fc062a..1b9a306a9b 100644 --- a/src/main/java/io/floci/aws/services/dynamodb/DynamoDbUpdateExpressionSyntax.java +++ b/src/main/java/io/floci/aws/services/dynamodb/DynamoDbUpdateExpressionSyntax.java @@ -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; } diff --git a/src/test/java/io/floci/aws/services/dynamodb/backend/DynamoDbNativeBackendTest.java b/src/test/java/io/floci/aws/services/dynamodb/backend/DynamoDbNativeBackendTest.java index 6190c89c8c..049bb57476 100644 --- a/src/test/java/io/floci/aws/services/dynamodb/backend/DynamoDbNativeBackendTest.java +++ b/src/test/java/io/floci/aws/services/dynamodb/backend/DynamoDbNativeBackendTest.java @@ -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")),