fix(dynamodb): refuse an empty attribute name in a TransactWriteItems item, key or value (#7389)

This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-03 08:20:20 -04:00
1 parent 886f626b4d
commit 121dc22563
4 files changed
+75 -1

No files matched your search

@@ -210,6 +210,43 @@ final class DynamoDbAttributeValueValidator {
return true;
}
static final String EMPTY_ATTRIBUTE_NAME = "Empty attribute name";
static void requireNonEmptyNames(JsonNode attributes) {
if (attributes != null && attributes.has("")) {
throw validationEx(EMPTY_ATTRIBUTE_NAME);
}
}
static void requireNonEmptyMemberNames(JsonNode values) {
if (values == null) {
return;
}
for (JsonNode value : values) {
if (hasEmptyMemberName(value)) {
throw validationEx(EMPTY_ATTRIBUTE_NAME);
}
}
}
private static boolean hasEmptyMemberName(JsonNode value) {
JsonNode members = value.path("M");
if (members.has("")) {
return true;
}
for (JsonNode member : members) {
if (hasEmptyMemberName(member)) {
return true;
}
}
for (JsonNode element : value.path("L")) {
if (hasEmptyMemberName(element)) {
return true;
}
}
return false;
}
private static int depthOf(JsonNode value) {
if (!value.isObject()) {
return 1;
@@ -2794,9 +2794,14 @@ public class NativeDynamoDbJsonHandler {
private static void requireTransactItemOrKey(TransactAction action) {
JsonNode op = action.op();
JsonNode values = op.get("Put".equals(action.type()) ? "Item" : "Key");
// In a request too deep for the newer validator, DynamoDB checks the top-level names before the depth
// and the names inside a map after it.
DynamoDbAttributeValueValidator.requireNonEmptyNames(values);
DynamoDbAttributeValueValidator.requireNestingWithinLimit(op.get("Item"), false);
DynamoDbAttributeValueValidator.requireNestingWithinLimit(op.get("Key"), false);
requireReadableValues(op.get("Put".equals(action.type()) ? "Item" : "Key"), "");
requireReadableValues(values, "");
DynamoDbAttributeValueValidator.requireNonEmptyMemberNames(values);
}
private void requireValidTransactExpressions(JsonNode op, boolean older) {
@@ -2812,6 +2817,7 @@ public class NativeDynamoDbJsonHandler {
requireNamedExpressionValues(exprAttrValues);
} else {
requireReadableValues(exprAttrValues, "");
DynamoDbAttributeValueValidator.requireNonEmptyMemberNames(exprAttrValues);
}
requireNonEmptyExpression(conditionExpression, "ConditionExpression");
@@ -2847,6 +2853,7 @@ public class NativeDynamoDbJsonHandler {
try {
DynamoDbAttributeValueValidator.requireNestingWithinLimit(value, false);
requireReadableValues(value, "");
DynamoDbAttributeValueValidator.requireNonEmptyMemberNames(value);
} catch (AwsException e) {
throw new AwsException("ValidationException", "ExpressionAttributeValues contains invalid value: "
+ e.getMessage() + " for key " + entry.getKey(), 400);
@@ -5843,6 +5843,28 @@ given()
deleteTable("SyntaxTable");
}
@ParameterizedTest
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
{"Put": {"TableName": "EmptyNameTable", "Item": {"pk": {"S": "a"}, "": {"S": "x"}}}}
{"Put": {"TableName": "EmptyNameTable", "Item": {"pk": {"S": "a"}, "l": {"L": [{"M": {"": {"S": "x"}}}]}}}}
{"ConditionCheck": {"TableName": "EmptyNameTable", "Key": {"pk": {"S": "a"}}, "ConditionExpression": "m = :m", "ExpressionAttributeValues": {":m": {"M": {"": {"S": "x"}}}}}}
""")
void transactWriteItemsRefusesAnEmptyAttributeName(String action) {
createOnDemandTableForGsiUpdates("EmptyNameTable");
given()
.header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("{\"TransactItems\": [" + action + "]}")
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Empty attribute name"));
deleteTable("EmptyNameTable");
}
private void deleteTable(String tableName) {
given()
.header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")
@@ -2388,6 +2388,10 @@ class NativeDynamoDbJsonHandlerTest {
[{"ConditionCheck":{"TableName":"Missing","Key":{},"ConditionExpression":"d = :deep","ExpressionAttributeValues":{":deep":"DEEP"}}}] | ExpressionAttributeValues contains invalid value: Nesting Levels have exceeded supported limits: Attributes in the item have nested levels beyond supported limit for key :deep
[{"Put":{"TableName":"Missing","Item":{"d":"DEEP"},"ConditionExpression":"a = :n","ExpressionAttributeValues":{":n":{"N":"abc"}}}}] | ExpressionAttributeValues contains invalid value: The parameter cannot be converted to a numeric value: abc for key :n
[{"Put":{"TableName":"Missing","Item":{},"ExpressionAttributeValues":{":deep":"DEEP"}}}] | Requested resource not found
[{"Put":{"TableName":"Missing","Item":{"d":"DEEP","":{"S":"x"}}}}] | Empty attribute name
[{"Put":{"TableName":"Missing","Item":{"d":"DEEP","m":{"M":{"":{"S":"x"}}}}}}] | Nesting Levels have exceeded supported limits: Attributes in the item have nested levels beyond supported limit
[{"Put":{"TableName":"Missing","Item":{"":{"S":"x"},"d":"DEEP"},"ConditionExpression":"a = = b"}}] | Invalid ConditionExpression: Syntax error; token: "=", near: "= = b"
[{"Put":{"TableName":"Missing","Item":{"d":"DEEP"},"ConditionExpression":"a = :v","ExpressionAttributeValues":{":v":{"M":{"":{"S":"x"}}}}}}] | ExpressionAttributeValues contains invalid value: Empty attribute name for key :v
[{"Put":{"TableName":"bad!name","Item":{"d":"DEEP"},"ReturnValuesOnConditionCheckFailure":"BOGUS"}}] | 1 validation error detected: Value 'BOGUS' at 'transactItems.1.member.put.returnValuesOnConditionCheckFailure' failed to satisfy constraint: Member must satisfy enum value set: [ALL_OLD, NONE]
""")
void aTransactionTooDeepForTheNewerValidatorChecksItsExpressionsBeforeItsItem(String items, String message)
@@ -2429,6 +2433,10 @@ class NativeDynamoDbJsonHandlerTest {
[{"Update":{"TableName":"Missing","Key":{},"UpdateExpression":""}}] | Invalid UpdateExpression: The expression can not be empty;
[{"Put":{"TableName":"Missing","Item":{},"ConditionExpression":" "}}] | Invalid ConditionExpression: Syntax error; token: "<EOF>", near: " "
[{"Put":{"TableName":"Missing","Item":{}}},{"ConditionCheck":{"TableName":"Missing","Key":{},"ConditionExpression":"attribute_not_exists("}}] | Invalid ConditionExpression: Syntax error; token: "<EOF>", near: "("
[{"Put":{"TableName":"Missing","Item":{"":{"S":"x"}},"ReturnValuesOnConditionCheckFailure":"BOGUS"}}] | Empty attribute name
[{"Delete":{"TableName":"Missing","Key":{"l":{"L":[{"M":{"":{"S":"x"}}}]}}}}] | Empty attribute name
[{"ConditionCheck":{"TableName":"Missing","Key":{},"ConditionExpression":"a = = b","ExpressionAttributeValues":{":v":{"M":{"":{"S":"x"}}}}}}] | Empty attribute name
[{"Put":{"TableName":"Missing","Item":{},"ExpressionAttributeValues":{":v":{"M":{"":{"S":"x"}}}}}}] | Requested resource not found
[{"Update":{"TableName":"Missing","Key":{},"UpdateExpression":"SET a = :zz","ConditionExpression":"a = :yy"}}] | Invalid ConditionExpression: An expression attribute value used in expression is not defined; attribute value: :yy
[{"Update":{"TableName":"Missing","Key":{},"UpdateExpression":"SET a = :zz","ConditionExpression":"contains(a, a)"}}] | Invalid ConditionExpression: The first operand must be distinct from the remaining operands for this operator or function; operator: contains, first operand: [a]
[{"Put":{"TableName":"Missing","Item":{},"ConditionExpression":"attribute_not_exists(a)","ExpressionAttributeNames":{"#n":"n"},"ExpressionAttributeValues":{":v":{"S":"x"}}}}] | Value provided in ExpressionAttributeNames unused in expressions: keys: {#n}