mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(dynamodb): refuse an ordering comparison or BETWEEN operand with no ordering (#7841)
* fix(dynamodb): refuse an ordering comparison or BETWEEN operand with no ordering * test(dynamodb): pin the SearchVectors ordering operand error before the comparator error
This commit is contained in:
1 parent
2a3dbb1aa4
commit
7748d2b51f
6 files changed
+90
-5
No files matched your search
@@ -1207,7 +1207,7 @@ public class DynamoDbPartiQLParser {
|
||||
}
|
||||
}
|
||||
|
||||
private static String incorrectOperandType(String op, String type) {
|
||||
static String incorrectOperandType(String op, String type) {
|
||||
return "Incorrect operand type for operator or function; "
|
||||
+ "operator or function: " + op + ", operand type: " + type;
|
||||
}
|
||||
|
||||
@@ -472,6 +472,9 @@ final class ExpressionEvaluator {
|
||||
return -1;
|
||||
}
|
||||
|
||||
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) {
|
||||
if (expr == null) return;
|
||||
@@ -480,11 +483,31 @@ final class ExpressionEvaluator {
|
||||
case OrExpr o -> o.operands().forEach(x -> validateSemantics(x, exprType, names, values));
|
||||
case NotExpr n -> validateSemantics(n.operand(), exprType, names, values);
|
||||
case FunctionCallExpr f -> validateFunction(f, exprType, names, values);
|
||||
case BetweenExpr b -> validateBetween(b, exprType, values);
|
||||
case CompareExpr c when ORDERING_OPERATORS.containsKey(c.op()) ->
|
||||
requireOrderedOperands(ORDERING_OPERATORS.get(c.op()), exprType, values, c.left(), c.right());
|
||||
case BetweenExpr b -> {
|
||||
requireOrderedOperands("BETWEEN", exprType, values, b.value(), b.low(), b.high());
|
||||
validateBetween(b, exprType, values);
|
||||
}
|
||||
default -> {}
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireOrderedOperands(String operator, String exprType, JsonNode values,
|
||||
Operand... operands) {
|
||||
for (Operand operand : operands) {
|
||||
String type = operandValueType(operand, values);
|
||||
if (type != null && !DynamoDbPartiQLParser.ORDERED_TYPES.contains(type)) {
|
||||
throw incorrectOperandType(exprType, operator, type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static AwsException incorrectOperandType(String exprType, String operator, String type) {
|
||||
return new AwsException("ValidationException", "Invalid " + exprType + ": "
|
||||
+ DynamoDbPartiQLParser.incorrectOperandType(operator, type), 400);
|
||||
}
|
||||
|
||||
// AWS rejects a BETWEEN whose bounds are the wrong way round when it parses the
|
||||
// expression, rather than letting the condition fail at evaluation time.
|
||||
private static void validateBetween(BetweenExpr between, String exprType, JsonNode values) {
|
||||
@@ -559,9 +582,7 @@ final class ExpressionEvaluator {
|
||||
if (name.equals("begins_with") && f.args().size() >= 2) {
|
||||
String type = operandValueType(f.args().get(1), values);
|
||||
if (type != null && !type.equals("S") && !type.equals("B")) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid " + exprType + ": Incorrect operand type for operator or function; "
|
||||
+ "operator or function: begins_with, operand type: " + type, 400);
|
||||
throw incorrectOperandType(exprType, "begins_with", type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -897,6 +897,24 @@ class DynamoDbAccessPathIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(30)
|
||||
void queryRefusesAnOrderingOperandWithNoOrderingBeforeTheTableLookup() {
|
||||
String operandError = "Invalid KeyConditionExpression: Incorrect operand type for operator or function; "
|
||||
+ "operator or function: <, operand type: BOOL";
|
||||
for (String table : List.of(TABLE, "access-path-validation-missing")) {
|
||||
request("DynamoDB_20120810.Query", """
|
||||
{
|
||||
"TableName":"%s",
|
||||
"KeyConditionExpression":"pk = :pk AND sk < :sk",
|
||||
"ExpressionAttributeValues":{":pk":{"S":"p1"},":sk":{"BOOL":true}}
|
||||
}
|
||||
""".formatted(table))
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(operandError));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(99)
|
||||
void deleteTable() {
|
||||
|
||||
@@ -512,6 +512,18 @@ class DynamoDbVectorSearchIntegrationTest {
|
||||
+ "SearchConditionExpression");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(19)
|
||||
void searchVectorsChecksAnOrderingOperandTypeBeforeTheComparator() {
|
||||
expectValidation("SearchVectors", search(SCHEMA, "schema", 4, """
|
||||
,
|
||||
"SearchConditionExpression": "tenant < :t",
|
||||
"ExpressionAttributeValues": {":t": {"BOOL": true}}
|
||||
"""),
|
||||
"Invalid SearchConditionExpression: Incorrect operand type for operator or function; "
|
||||
+ "operator or function: <, operand type: BOOL");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(20)
|
||||
void searchVectorsRejectsADimensionMismatch() {
|
||||
|
||||
@@ -772,6 +772,39 @@ class ExpressionEvaluatorTest {
|
||||
"ConditionExpression", null, values));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
|
||||
n < :v | {":v":{"BOOL":true}} | <, operand type: BOOL
|
||||
:v >= n | {":v":{"NULL":true}} | >=, operand type: NULL
|
||||
size(n) > :v | {":v":{"SS":["a"]}} | >, operand type: SS
|
||||
NOT n <= :v | {":v":{"L":[]}} | <=, operand type: L
|
||||
n BETWEEN :lo AND :hi | {":lo":{"N":"1"},":hi":{"BOOL":true}} | BETWEEN, operand type: BOOL
|
||||
n BETWEEN :lo AND :hi | {":lo":{"M":{}},":hi":{"BOOL":true}} | BETWEEN, operand type: M
|
||||
n BETWEEN :lo AND :hi | {":lo":{"N":"5"},":hi":{"NS":["1"]}} | BETWEEN, operand type: NS
|
||||
""")
|
||||
void anOrderingOperatorRefusesAnOperandTypeWithNoOrdering(String expression, String values, String error)
|
||||
throws Exception {
|
||||
JsonNode parsed = mapper.readTree(values);
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () ->
|
||||
ExpressionEvaluator.validateExpression(expression, "FilterExpression", null, parsed));
|
||||
assertEquals("Invalid FilterExpression: Incorrect operand type for operator or function; "
|
||||
+ "operator or function: " + error, e.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
|
||||
n < :v | {":v":{"B":"AQ=="}}
|
||||
n = :v | {":v":{"BOOL":true}}
|
||||
n <> :v | {":v":{"NULL":true}}
|
||||
""")
|
||||
void orderedTypesAndOtherComparisonsAreAccepted(String expression, String values) throws Exception {
|
||||
JsonNode parsed = mapper.readTree(values);
|
||||
|
||||
assertDoesNotThrow(() ->
|
||||
ExpressionEvaluator.validateExpression(expression, "FilterExpression", null, parsed));
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingOperandThrowsSyntaxError() {
|
||||
AwsException e = assertThrows(AwsException.class, () ->
|
||||
|
||||
@@ -2407,6 +2407,7 @@ class NativeDynamoDbJsonHandlerTest {
|
||||
PutItem | {"TableName":"Missing","Item":{"pk":{"S":"a"}},"ConditionExpression":"","ExpressionAttributeValues":{":v":{"S":"x"}}} | 1 validation error detected: Invalid ConditionExpression: The expression can not be empty;
|
||||
DeleteItem | {"TableName":"Missing","Key":{"pk":{"S":"a"}},"ConditionExpression":"a = :v"} | 1 validation error detected: Invalid ConditionExpression: An expression attribute value used in expression is not defined; attribute value: :v
|
||||
PutItem | {"TableName":"Missing","Item":{"pk":{"S":"a"}},"ConditionExpression":"a BETWEEN :b AND :a","ExpressionAttributeValues":{":a":{"N":"1"},":b":{"N":"5"}}} | 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:5}, upper bound operand: AttributeValue: {N:1}
|
||||
DeleteItem | {"TableName":"Missing","Key":{"pk":{"S":"a"}},"ConditionExpression":"a < :v","ExpressionAttributeValues":{":v":{"BOOL":true}}} | 1 validation error detected: Invalid ConditionExpression: Incorrect operand type for operator or function; operator or function: <, operand type: BOOL
|
||||
""")
|
||||
void aPutOrDeleteChecksItsConditionMembersInOrderInsideTheEnvelope(String operation, String request,
|
||||
String message) throws Exception {
|
||||
|
||||
Reference in new issue
Block a user