fix(dynamodb): refuse a nested or repeated key condition attribute before the table lookup (#7883)

* fix(dynamodb): refuse a nested or repeated key condition attribute before the table lookup

DynamoDB reports a KeyConditionExpression condition on a nested attribute,
or a second condition on the same attribute, with the
"Invalid KeyConditionExpression: " prefix. It checks both after the
expression walk and before the Select enum, the table lookup and the key
schema checks.

* fix(dynamodb): read key conditions grouped in parentheses as one list

DynamoDB reads AND groups in parentheses in a KeyConditionExpression as
if the parentheses were not there. This holds for the nested and
repeated attribute checks and for the key schema checks.

* fix(dynamodb): read grouped key conditions as one list for dynamodb:LeadingKeys

The IAM condition key reader now flattens AND groups the same way as the
key condition checks. A grouped condition like
"(h1 = :a AND h2 = :b) AND r1 = :c" on a multi-attribute index gave no
leading key, and a ForAllValues condition passes when that key is absent.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-10 09:33:07 -04:00
1 parent 4512de1402
commit 2e158f7d1d
6 files changed
+113 -33

No files matched your search

@@ -116,35 +116,46 @@ final class DynamoDbAccessPathValidator {
}
}
// DynamoDB checks these before it looks up the table.
static void validateKeyConditionAttributes(String expression, JsonNode names) {
if (expression == null) {
return;
}
Set<String> conditionedAttributes = new HashSet<>();
for (Expr rawCondition : keyConditions(expression)) {
Expr condition = normalizeOperandOrder(rawCondition);
if (conditionOperand(condition) instanceof PathOperand(List<String> segments) && segments.size() > 1) {
throw validationException("Invalid KeyConditionExpression: "
+ "KeyConditionExpressions cannot have conditions on nested attributes");
}
String attribute = conditionAttribute(condition, names);
if (attribute != null && !conditionedAttributes.add(attribute)) {
throw validationException("Invalid KeyConditionExpression: "
+ "KeyConditionExpressions must only contain one condition per key");
}
}
}
private static List<Expr> keyConditions(String expression) {
return DynamoDbKeyConditionParser.conditions(parseExpression(expression, "KeyConditionExpression"));
}
private static String validateKeyConditionExpression(TableDefinition table,
DynamoDbAccessPath accessPath,
String expression, JsonNode names,
JsonNode values) {
Expr root = parseExpression(expression, "KeyConditionExpression");
List<Expr> conditions = root instanceof AndExpr and
? and.operands() : List.of(root);
List<String> partitionKeys = accessPath.partitionKeyNames();
Set<String> partitionKeySet = Set.copyOf(partitionKeys);
List<String> sortKeys = accessPath.sortKeyNames();
Set<String> conditionedPartitionKeys = new HashSet<>();
String partitionKeyValuePlaceholder = null;
Set<String> conditionedAttributes = new HashSet<>();
Map<String, Boolean> sortKeyEqualities = new HashMap<>();
boolean conditionOnNonKeyAttribute = false;
for (Expr rawCondition : conditions) {
for (Expr rawCondition : keyConditions(expression)) {
Expr condition = normalizeOperandOrder(rawCondition);
if (conditionOperand(condition) instanceof PathOperand(List<String> segments) && segments.size() > 1) {
throw new AwsException("ValidationException",
"KeyConditionExpressions cannot have conditions on nested attributes", 400);
}
String attribute = conditionAttribute(condition, names);
if (attribute != null && !conditionedAttributes.add(attribute)) {
throw new AwsException("ValidationException",
"KeyConditionExpressions must only contain one condition per key", 400);
}
if (attribute != null && partitionKeySet.contains(attribute)) {
if (!isPartitionKeyEquality(condition)) {
throw new AwsException("ValidationException", "Query key condition not supported", 400);
@@ -8,6 +8,7 @@ import io.floci.aws.services.dynamodb.ExpressionEvaluator.PathOperand;
import io.floci.aws.services.dynamodb.ExpressionEvaluator.PlaceholderOperand;
import io.floci.aws.services.dynamodb.ExpressionEvaluator.TokenType;
import java.util.ArrayList;
import java.util.List;
/**
@@ -49,8 +50,7 @@ final class DynamoDbKeyConditionParser {
if (root == null) {
return null;
}
List<Expr> conditions = root instanceof AndExpr and ? and.operands() : List.of(root);
for (Expr condition : conditions) {
for (Expr condition : conditions(root)) {
if (!(condition instanceof CompareExpr compare) || compare.op() != TokenType.EQ) {
continue;
}
@@ -66,6 +66,21 @@ final class DynamoDbKeyConditionParser {
return null;
}
// DynamoDB reads AND groups in parentheses as one list of key conditions.
static List<Expr> conditions(Expr root) {
List<Expr> conditions = new ArrayList<>();
addConditions(root, conditions);
return conditions;
}
private static void addConditions(Expr expression, List<Expr> conditions) {
if (expression instanceof AndExpr and) {
and.operands().forEach(operand -> addConditions(operand, conditions));
} else {
conditions.add(expression);
}
}
/** Resolves a single-segment path operand, following a {@code #alias} when one is used. */
private static String topLevelAttribute(Object operand, JsonNode exprAttrNames) {
if (!(operand instanceof PathOperand path) || path.segments().size() != 1) {
@@ -1330,6 +1330,7 @@ public class NativeDynamoDbJsonHandler {
// Must run before DynamoDbAccessPathValidator below: an unresolved #name there falls
// through to "Query condition missed key schema element" instead of the real cause.
ExpressionEvaluator.validateSemantics(keyConditionExpr, "KeyConditionExpression", exprAttrNames, exprAttrValues);
DynamoDbAccessPathValidator.validateKeyConditionAttributes(keyConditionExpr, exprAttrNames);
if (select != null && !VALID_SELECT.contains(select)) {
throw SmithyValidationErrors.validationException(