mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(dynamodb): check each expression condition in written order, its names and values first (#7870)
This commit is contained in:
1 parent
99f9a49ebe
commit
6100784e3f
4 files changed
+117
-27
No files matched your search
@@ -414,14 +414,12 @@ final class ExpressionEvaluator {
|
||||
static void validateExpression(String expression, String exprType,
|
||||
JsonNode exprAttrNames, JsonNode exprAttrValues) {
|
||||
validateSyntax(expression, exprType);
|
||||
validateSemantics(expression, exprType, exprAttrNames, exprAttrValues);
|
||||
validateSemantics(parse(expression), exprType, exprAttrNames, exprAttrValues, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* The tokenize/parse/redundant-parentheses half of {@link #validateExpression}, split out so
|
||||
* callers can run an undefined-#name/:value check between this and {@link #validateSemantics}:
|
||||
* DynamoDB reports a syntax or redundant-parentheses error before an undefined placeholder,
|
||||
* but an undefined placeholder before a semantic error like {@code contains(x, x)}.
|
||||
* callers can run other checks between this and {@link #validateSemantics}.
|
||||
*/
|
||||
static void validateSyntax(String expression, String exprType) {
|
||||
if (expression == null || expression.isEmpty()) {
|
||||
@@ -437,10 +435,26 @@ final class ExpressionEvaluator {
|
||||
}
|
||||
}
|
||||
|
||||
/** The semantic half of {@link #validateExpression}; see {@link #validateSyntax}. */
|
||||
/**
|
||||
* The semantic half of {@link #validateExpression}, which also refuses an undefined #name or
|
||||
* :value. DynamoDB checks the conditions in written order, and the names and values of a
|
||||
* condition before its other errors.
|
||||
*/
|
||||
static void validateSemantics(String expression, String exprType,
|
||||
JsonNode exprAttrNames, JsonNode exprAttrValues) {
|
||||
validateSemantics(parse(expression), exprType, exprAttrNames, exprAttrValues);
|
||||
validateSemantics(parse(expression), exprType, exprAttrNames, exprAttrValues, true);
|
||||
}
|
||||
|
||||
static AwsException undefinedName(String exprType, String name) {
|
||||
return new AwsException("ValidationException", "Invalid " + exprType
|
||||
+ ": An expression attribute name used in the document path is not defined; attribute name: "
|
||||
+ name, 400);
|
||||
}
|
||||
|
||||
static AwsException undefinedValue(String exprType, String value) {
|
||||
return new AwsException("ValidationException", "Invalid " + exprType
|
||||
+ ": An expression attribute value used in expression is not defined; attribute value: "
|
||||
+ value, 400);
|
||||
}
|
||||
|
||||
// A pair of parentheses is redundant when its entire content is itself a single
|
||||
@@ -475,13 +489,16 @@ final class ExpressionEvaluator {
|
||||
private static final Map<TokenType, String> ORDERING_OPERATORS = Map.of(
|
||||
TokenType.LT, "<", TokenType.LE, "<=", TokenType.GT, ">", TokenType.GE, ">=");
|
||||
|
||||
private static void validateSemantics(Expr expr, String exprType,
|
||||
JsonNode names, JsonNode values) {
|
||||
private static void validateSemantics(Expr expr, String exprType, JsonNode names, JsonNode values,
|
||||
boolean requireDefined) {
|
||||
if (expr == null) return;
|
||||
if (requireDefined) {
|
||||
conditionOperands(expr).forEach(operand -> requireDefined(operand, exprType, names, values));
|
||||
}
|
||||
switch (expr) {
|
||||
case AndExpr a -> a.operands().forEach(o -> validateSemantics(o, exprType, names, values));
|
||||
case OrExpr o -> o.operands().forEach(x -> validateSemantics(x, exprType, names, values));
|
||||
case NotExpr n -> validateSemantics(n.operand(), exprType, names, values);
|
||||
case AndExpr a -> a.operands().forEach(o -> validateSemantics(o, exprType, names, values, requireDefined));
|
||||
case OrExpr o -> o.operands().forEach(x -> validateSemantics(x, exprType, names, values, requireDefined));
|
||||
case NotExpr n -> validateSemantics(n.operand(), exprType, names, values, requireDefined);
|
||||
case FunctionCallExpr f -> validateFunction(f, exprType, names, values);
|
||||
case CompareExpr c when ORDERING_OPERATORS.containsKey(c.op()) ->
|
||||
requireOrderedOperands(ORDERING_OPERATORS.get(c.op()), exprType, values, c.left(), c.right());
|
||||
@@ -493,6 +510,40 @@ final class ExpressionEvaluator {
|
||||
}
|
||||
}
|
||||
|
||||
private static List<Operand> conditionOperands(Expr condition) {
|
||||
return switch (condition) {
|
||||
case CompareExpr c -> List.of(c.left(), c.right());
|
||||
case BetweenExpr b -> List.of(b.value(), b.low(), b.high());
|
||||
case InExpr in -> {
|
||||
List<Operand> operands = new ArrayList<>();
|
||||
operands.add(in.value());
|
||||
operands.addAll(in.candidates());
|
||||
yield operands;
|
||||
}
|
||||
case FunctionCallExpr f -> f.args();
|
||||
default -> List.of();
|
||||
};
|
||||
}
|
||||
|
||||
private static void requireDefined(Operand operand, String exprType, JsonNode names, JsonNode values) {
|
||||
switch (operand) {
|
||||
case PathOperand path -> {
|
||||
for (String segment : path.segments()) {
|
||||
if (segment.startsWith("#") && (names == null || !names.has(segment))) {
|
||||
throw undefinedName(exprType, segment);
|
||||
}
|
||||
}
|
||||
}
|
||||
case PlaceholderOperand placeholder -> {
|
||||
if (values == null || !values.has(placeholder.name())) {
|
||||
throw undefinedValue(exprType, placeholder.name());
|
||||
}
|
||||
}
|
||||
case FunctionOperand function ->
|
||||
function.args().forEach(arg -> requireDefined(arg, exprType, names, values));
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireOrderedOperands(String operator, String exprType, JsonNode values,
|
||||
Operand... operands) {
|
||||
for (Operand operand : operands) {
|
||||
|
||||
@@ -817,7 +817,6 @@ public class NativeDynamoDbJsonHandler {
|
||||
DynamoDbAttributeValueValidator.requireNestingWithinLimit(legacyValues(request.get("Expected")), false);
|
||||
DynamoDbExpressionSize.checkWrite(conditionExpression, "ConditionExpression");
|
||||
ExpressionEvaluator.validateSyntax(conditionExpression, "ConditionExpression");
|
||||
requireDefinedTokens(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
ExpressionEvaluator.validateSemantics(conditionExpression, "ConditionExpression",
|
||||
exprAttrNames, exprAttrValues);
|
||||
checkUnusedEan(exprAttrNames, extractHashTokens(conditionExpression));
|
||||
@@ -1015,7 +1014,6 @@ public class NativeDynamoDbJsonHandler {
|
||||
// Undefined tokens, then ConditionExpression's semantic errors, must both be caught
|
||||
// before the unused-EAN/EAV checks below.
|
||||
requireDefinedTokens(updateExpression, "UpdateExpression", exprAttrNames, exprAttrValues);
|
||||
requireDefinedTokens(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
ExpressionEvaluator.validateSemantics(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
Set<String> hashTokens = extractHashTokens(updateExpression, conditionExpression);
|
||||
checkUnusedEan(exprAttrNames, hashTokens);
|
||||
@@ -1023,7 +1021,6 @@ public class NativeDynamoDbJsonHandler {
|
||||
checkUnusedEav(exprAttrValues, colonTokensAll);
|
||||
dynamoDbService.requireAddOrDeleteOperandTypes(updateExpression, exprAttrValues, true);
|
||||
} else {
|
||||
requireDefinedTokens(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
ExpressionEvaluator.validateSemantics(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
}
|
||||
|
||||
@@ -1328,12 +1325,10 @@ public class NativeDynamoDbJsonHandler {
|
||||
ExpressionEvaluator.validateSyntax(keyConditionExpr, "KeyConditionExpression");
|
||||
ExpressionEvaluator.validateSyntax(filterExpr, "FilterExpression");
|
||||
// DynamoDB reports filter errors before projection errors, and projection before key-condition.
|
||||
requireDefinedTokens(filterExpr, "FilterExpression", exprAttrNames, exprAttrValues);
|
||||
ExpressionEvaluator.validateSemantics(filterExpr, "FilterExpression", exprAttrNames, exprAttrValues);
|
||||
ProjectionEvaluator.validateExpression(projectionExpression, exprAttrNames);
|
||||
// Must run before DynamoDbAccessPathValidator below: an unresolved #name there falls
|
||||
// through to "Query condition missed key schema element" instead of the real cause.
|
||||
requireDefinedTokens(keyConditionExpr, "KeyConditionExpression", exprAttrNames, exprAttrValues);
|
||||
ExpressionEvaluator.validateSemantics(keyConditionExpr, "KeyConditionExpression", exprAttrNames, exprAttrValues);
|
||||
|
||||
if (select != null && !VALID_SELECT.contains(select)) {
|
||||
@@ -1480,7 +1475,6 @@ public class NativeDynamoDbJsonHandler {
|
||||
DynamoDbAttributeValueValidator.requireNestingWithinLimit(exprAttrValues);
|
||||
ExpressionEvaluator.validateSyntax(filterExpr, "FilterExpression");
|
||||
// DynamoDB reports filter errors before projection errors.
|
||||
requireDefinedTokens(filterExpr, "FilterExpression", exprAttrNames, exprAttrValues);
|
||||
ExpressionEvaluator.validateSemantics(filterExpr, "FilterExpression", exprAttrNames, exprAttrValues);
|
||||
ProjectionEvaluator.validateExpression(projectionExpressionScan, exprAttrNames);
|
||||
|
||||
@@ -2956,7 +2950,6 @@ public class NativeDynamoDbJsonHandler {
|
||||
ExpressionEvaluator.validateSyntax(conditionExpression, "ConditionExpression");
|
||||
// Undefined tokens must fail here as a ValidationException, never a
|
||||
// TransactionCanceledException with a false ConditionalCheckFailed reason.
|
||||
requireDefinedTokens(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
ExpressionEvaluator.validateSemantics(conditionExpression, "ConditionExpression",
|
||||
exprAttrNames, exprAttrValues);
|
||||
|
||||
@@ -3453,9 +3446,8 @@ public class NativeDynamoDbJsonHandler {
|
||||
|
||||
/**
|
||||
* Rejects the first #name or :value token, in textual order, that neither
|
||||
* ExpressionAttributeNames nor ExpressionAttributeValues defines. This matches DynamoDB for
|
||||
* condition, key-condition and filter expressions, and for a single-action update expression;
|
||||
* with several undefined tokens spread across an UpdateExpression's actions/clauses, DynamoDB
|
||||
* ExpressionAttributeNames nor ExpressionAttributeValues defines. This matches DynamoDB for a
|
||||
* single-action update expression; with several undefined tokens spread across an UpdateExpression's actions/clauses, DynamoDB
|
||||
* may name a different one, though always with this same error type and message form.
|
||||
*/
|
||||
private static void requireDefinedTokens(String expression, String exprType,
|
||||
@@ -3463,14 +3455,10 @@ public class NativeDynamoDbJsonHandler {
|
||||
for (ExprToken token : scanExprTokens(expression)) {
|
||||
if (token.prefix() == '#') {
|
||||
if (exprAttrNames == null || !exprAttrNames.has(token.text())) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid " + exprType + ": An expression attribute name used in the document path "
|
||||
+ "is not defined; attribute name: " + token.text(), 400);
|
||||
throw ExpressionEvaluator.undefinedName(exprType, token.text());
|
||||
}
|
||||
} else if (exprAttrValues == null || !exprAttrValues.has(token.text())) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid " + exprType + ": An expression attribute value used in expression is not "
|
||||
+ "defined; attribute value: " + token.text(), 400);
|
||||
throw ExpressionEvaluator.undefinedValue(exprType, token.text());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5842,6 +5842,30 @@ given()
|
||||
deleteTable("SyntaxTable");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
|
||||
Query | {"TableName": "ConditionOrderTable", "KeyConditionExpression": "begins_with(sk, :n) AND pk = :missing", "ExpressionAttributeValues": {":n": {"N": "1"}}} | Invalid KeyConditionExpression: Incorrect operand type for operator or function; operator or function: begins_with, operand type: N
|
||||
Scan | {"TableName": "ConditionOrderTable", "FilterExpression": "(x < :v OR y = :missing) AND z = :w", "ExpressionAttributeValues": {":v": {"BOOL": true}, ":w": {"S": "w"}}} | Invalid FilterExpression: Incorrect operand type for operator or function; operator or function: <, operand type: BOOL
|
||||
PutItem | {"TableName": "ConditionOrderTable", "Item": {"pk": {"S": "a"}}, "ConditionExpression": "contains(x, x) AND y = :missing"} | 1 validation error detected: Invalid ConditionExpression: The first operand must be distinct from the remaining operands for this operator or function; operator: contains, first operand: [x]
|
||||
DeleteItem | {"TableName": "ConditionOrderTable", "Key": {"pk": {"S": "a"}}, "ConditionExpression": "x BETWEEN :b AND :a AND y = :missing", "ExpressionAttributeValues": {":a": {"N": "1"}, ":b": {"N": "2"}}} | 1 validation error detected: Invalid ConditionExpression: The BETWEEN operator requires upper bound to be greater than or equal to lower bound; lower bound operand: AttributeValue: {N:2}, upper bound operand: AttributeValue: {N:1}
|
||||
TransactWriteItems | {"TransactItems": [{"ConditionCheck": {"TableName": "ConditionOrderTable", "Key": {"pk": {"S": "a"}}, "ConditionExpression": "contains(x, x) AND y = :missing"}}]} | Invalid ConditionExpression: The first operand must be distinct from the remaining operands for this operator or function; operator: contains, first operand: [x]
|
||||
""")
|
||||
void expressionErrorsComeFromTheFirstFailingConditionInWrittenOrder(String operation, String body, String message) {
|
||||
createOnDemandTableForGsiUpdates("ConditionOrderTable");
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810." + operation)
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body(body)
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", containsString("ValidationException"))
|
||||
.body("message", equalTo(message));
|
||||
|
||||
deleteTable("ConditionOrderTable");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
|
||||
{"Put": {"TableName": "EmptyNameTable", "Item": {"pk": {"S": "a"}, "": {"S": "x"}}}}
|
||||
|
||||
@@ -805,6 +805,33 @@ class ExpressionEvaluatorTest {
|
||||
ExpressionEvaluator.validateExpression(expression, "FilterExpression", null, parsed));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', textBlock = """
|
||||
x < :v AND y = :missing | Incorrect operand type for operator or function; operator or function: <, operand type: BOOL
|
||||
y = :missing AND x < :v | An expression attribute value used in expression is not defined; attribute value: :missing
|
||||
(x < :v OR y = :missing) AND z = :w | Incorrect operand type for operator or function; operator or function: <, operand type: BOOL
|
||||
NOT x < :v AND y = :missing | Incorrect operand type for operator or function; operator or function: <, operand type: BOOL
|
||||
begins_with(x, :n) OR y = :missing | Incorrect operand type for operator or function; operator or function: begins_with, operand type: N
|
||||
y < :v AND x IN (:a, :missing) | Incorrect operand type for operator or function; operator or function: <, operand type: BOOL
|
||||
x IN (:a, :missing) AND y < :v | An expression attribute value used in expression is not defined; attribute value: :missing
|
||||
contains(x, x) AND y = :missing | The first operand must be distinct from the remaining operands for this operator or function; operator: contains, first operand: [x]
|
||||
x BETWEEN :b AND :a AND y = :missing | The BETWEEN operator requires upper bound to be greater than or equal to lower bound; lower bound operand: AttributeValue: {N:2}, upper bound operand: AttributeValue: {N:1}
|
||||
x BETWEEN :v AND :missing | An expression attribute value used in expression is not defined; attribute value: :missing
|
||||
:v < #missing | An expression attribute name used in the document path is not defined; attribute name: #missing
|
||||
:missing = #missing | An expression attribute value used in expression is not defined; attribute value: :missing
|
||||
a.#missing[0] < :v | An expression attribute name used in the document path is not defined; attribute name: #missing
|
||||
""")
|
||||
void checksEachConditionInWrittenOrderWithItsNamesAndValuesFirst(String expression, String error)
|
||||
throws Exception {
|
||||
JsonNode values = mapper.readTree("""
|
||||
{":v":{"BOOL":true},":w":{"S":"w"},":a":{"N":"1"},":b":{"N":"2"},":n":{"N":"1"}}
|
||||
""");
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () ->
|
||||
ExpressionEvaluator.validateSemantics(expression, "FilterExpression", null, values));
|
||||
assertEquals("Invalid FilterExpression: " + error, e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingOperandThrowsSyntaxError() {
|
||||
AwsException e = assertThrows(AwsException.class, () ->
|
||||
|
||||
Reference in new issue
Block a user