mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(dynamodb): more request validations in Query and Scan (#3360)
* fix(dynamodb): accept and report Query key conditions the way AWS does Three gaps in KeyConditionExpression validation. A sort-key comparison with the value on the left, such as ":lo <= sk", is valid on AWS and means "sk >= :lo". Floci rejected it because it only looked for the attribute on the left. The operands are now normalised before the key checks run. A nested path on a key attribute now reports that it cannot carry a condition, instead of the generic unsupported message. A condition on a non-key attribute now reports the partition key it left unconditioned, which is what AWS answers. Closes 2 TIER 1 and 1 TIER 3 case in the dynamodb-conformance suite. * fix(dynamodb): bound Segment and TotalSegments on Scan A parallel Scan accepted a negative Segment. A TotalSegments of 0 was rejected, but with the zero-based wording instead of the AWS constraint wording. Both are now rejected with the AWS constraint wording, before the required-parameter checks run. The TotalSegments upper bound from #3287 moves into the same block, so several violations are reported in one message. Closes 1 TIER 3 case in the dynamodb-conformance suite. * fix(dynamodb): report TotalSegments before Segment on Scan and test the lower bound
This commit is contained in:
1 parent
6b17ef2b1c
commit
fbe92aa411
5 files changed
+158
-11
No files matched your search
+40
-5
@@ -106,7 +106,14 @@ final class DynamoDbAccessPathValidator {
|
||||
Set<String> conditionedAttributes = new HashSet<>();
|
||||
Map<String, Boolean> sortKeyEqualities = new HashMap<>();
|
||||
|
||||
for (Expr condition : conditions) {
|
||||
boolean conditionOnNonKeyAttribute = false;
|
||||
|
||||
for (var rawCondition : conditions) {
|
||||
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",
|
||||
@@ -126,7 +133,8 @@ final class DynamoDbAccessPathValidator {
|
||||
}
|
||||
sortKeyEqualities.put(attribute, isEqualityCondition(condition));
|
||||
} else {
|
||||
throw new AwsException("ValidationException", "Query key condition not supported", 400);
|
||||
conditionOnNonKeyAttribute = true;
|
||||
continue;
|
||||
}
|
||||
validateConditionValueTypes(table, attribute, condition, values);
|
||||
}
|
||||
@@ -138,6 +146,9 @@ final class DynamoDbAccessPathValidator {
|
||||
throw new AwsException("ValidationException",
|
||||
"Query condition missed key schema element: " + missing, 400);
|
||||
}
|
||||
if (conditionOnNonKeyAttribute) {
|
||||
throw new AwsException("ValidationException", "Query key condition not supported", 400);
|
||||
}
|
||||
validateCompositeSortKeyConditions(sortKeys, sortKeyEqualities);
|
||||
return partitionKeyValuePlaceholder;
|
||||
}
|
||||
@@ -187,15 +198,39 @@ final class DynamoDbAccessPathValidator {
|
||||
return false;
|
||||
}
|
||||
|
||||
private static String conditionAttribute(Expr condition, JsonNode names) {
|
||||
Operand operand = switch (condition) {
|
||||
// AWS accepts the value on the left of a sort-key comparison (":lo <= #sk"), which means
|
||||
// the same as "#sk >= :lo".
|
||||
private static Expr normalizeOperandOrder(Expr condition) {
|
||||
if (condition instanceof CompareExpr(Operand left, TokenType op, Operand right)
|
||||
&& left instanceof PlaceholderOperand
|
||||
&& right instanceof PathOperand) {
|
||||
return new CompareExpr(right, flipComparator(op), left);
|
||||
}
|
||||
return condition;
|
||||
}
|
||||
|
||||
private static TokenType flipComparator(TokenType op) {
|
||||
return switch (op) {
|
||||
case LT -> TokenType.GT;
|
||||
case LE -> TokenType.GE;
|
||||
case GT -> TokenType.LT;
|
||||
case GE -> TokenType.LE;
|
||||
default -> op;
|
||||
};
|
||||
}
|
||||
|
||||
private static Operand conditionOperand(Expr condition) {
|
||||
return switch (condition) {
|
||||
case CompareExpr compare -> compare.left();
|
||||
case BetweenExpr between -> between.value();
|
||||
case FunctionCallExpr function when "begins_with".equals(function.functionName())
|
||||
&& !function.args().isEmpty() -> function.args().getFirst();
|
||||
default -> null;
|
||||
};
|
||||
if (!(operand instanceof PathOperand path) || path.segments().size() != 1) {
|
||||
}
|
||||
|
||||
private static String conditionAttribute(Expr condition, JsonNode names) {
|
||||
if (!(conditionOperand(condition) instanceof PathOperand path) || path.segments().size() != 1) {
|
||||
return null;
|
||||
}
|
||||
return topLevelAttribute(path, names);
|
||||
|
||||
+24
-6
@@ -833,6 +833,8 @@ public class DynamoDbJsonHandler {
|
||||
return changedAttributes;
|
||||
}
|
||||
|
||||
private static final int MAX_TOTAL_SEGMENTS = 1_000_000;
|
||||
|
||||
private static final Set<String> VALID_SELECT = Set.of(
|
||||
"ALL_ATTRIBUTES", "ALL_PROJECTED_ATTRIBUTES", "SPECIFIC_ATTRIBUTES", "COUNT");
|
||||
|
||||
@@ -1038,6 +1040,28 @@ public class DynamoDbJsonHandler {
|
||||
+ "Member must have value greater than or equal to 1", 400);
|
||||
}
|
||||
|
||||
var segmentErrors = new ArrayList<String>();
|
||||
if (totalSegments != null) {
|
||||
if (totalSegments < 1) {
|
||||
segmentErrors.add("Value '" + totalSegments + "' at 'totalSegments' failed to satisfy constraint: "
|
||||
+ "Member must have value greater than or equal to 1");
|
||||
}
|
||||
if (MAX_TOTAL_SEGMENTS < totalSegments) {
|
||||
segmentErrors.add("Value '" + totalSegments + "' at 'totalSegments' failed to satisfy constraint: "
|
||||
+ "Member must have value less than or equal to " + MAX_TOTAL_SEGMENTS);
|
||||
}
|
||||
}
|
||||
if (segment != null && segment < 0) {
|
||||
segmentErrors.add("Value '" + segment + "' at 'segment' failed to satisfy constraint: "
|
||||
+ "Member must have value greater than or equal to 0");
|
||||
}
|
||||
if (!segmentErrors.isEmpty()) {
|
||||
var n = segmentErrors.size();
|
||||
throw new AwsException("ValidationException",
|
||||
n + " validation error" + (n > 1 ? "s" : "") + " detected: "
|
||||
+ String.join("; ", segmentErrors), 400);
|
||||
}
|
||||
|
||||
if (segment != null && totalSegments == null) {
|
||||
throw new AwsException("ValidationException",
|
||||
"The TotalSegments parameter is required but was not present in the request when Segment parameter is present", 400);
|
||||
@@ -1046,12 +1070,6 @@ public class DynamoDbJsonHandler {
|
||||
throw new AwsException("ValidationException",
|
||||
"The Segment parameter is required but was not present in the request when parameter TotalSegments is present", 400);
|
||||
}
|
||||
if (totalSegments != null && totalSegments > 1_000_000) {
|
||||
throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value '" + totalSegments
|
||||
+ "' at 'totalSegments' failed to satisfy constraint: "
|
||||
+ "Member must have value less than or equal to 1000000", 400);
|
||||
}
|
||||
if (segment != null && totalSegments != null && segment >= totalSegments) {
|
||||
throw new AwsException("ValidationException",
|
||||
"The Segment parameter is zero-based and must be less than parameter TotalSegments: "
|
||||
|
||||
Reference in new issue
Block a user