mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): refuse BETWEEN bounds of different types (#9351)
A BETWEEN whose bounds have different types is now refused with DynamoDB's ValidationException in a FilterExpression, KeyConditionExpression, ConditionExpression and PartiQL statement, and PartiQL also refuses reversed bounds. Each check runs with the operand type checks, so the first failing condition in written order wins. The one-violation envelope moves to SmithyValidationErrors.inEnvelope. PutItem, UpdateItem and DeleteItem condition errors carry it, through the Step Functions tasks too, and TransactWriteItems ones no longer do, as on DynamoDB.
This commit is contained in:
1 parent
67a619d58e
commit
2f4b2c492a
9 files changed
+272
-32
No files matched your search
@@ -55,6 +55,14 @@ public final class SmithyValidationErrors {
|
||||
return validationException(List.of(violation));
|
||||
}
|
||||
|
||||
/** The error inside the one-violation envelope. Other errors, and messages already inside it, pass through. */
|
||||
public static AwsException inEnvelope(AwsException e) {
|
||||
if (!"ValidationException".equals(e.getErrorCode()) || e.getMessage().startsWith(message(List.of("")))) {
|
||||
return e;
|
||||
}
|
||||
return validationException(e.getMessage());
|
||||
}
|
||||
|
||||
/** The awsQuery protocol's code. */
|
||||
public static AwsException validationError(List<String> violations) {
|
||||
return exception("ValidationError", violations);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.floci.aws.services.dynamodb;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
@@ -1238,6 +1239,47 @@ public class DynamoDbPartiQLParser {
|
||||
}
|
||||
}
|
||||
|
||||
private void requireBetweenBounds(PVal lo, PVal hi) {
|
||||
String loType = typeCode(lo);
|
||||
String hiType = typeCode(hi);
|
||||
if (!ORDERED_TYPES.contains(loType) || !ORDERED_TYPES.contains(hiType)) {
|
||||
return;
|
||||
}
|
||||
String message = ExpressionEvaluator.betweenBoundsError(boundNode(lo), boundNode(hi),
|
||||
DynamoDbPartiQLParser::displayBound);
|
||||
if (message != null) {
|
||||
deferExpression(message);
|
||||
}
|
||||
}
|
||||
|
||||
private static JsonNode boundNode(PVal val) {
|
||||
return switch (val) {
|
||||
case PVal.Str s -> JsonNodeFactory.instance.objectNode().put("S", s.v());
|
||||
case PVal.Num n -> JsonNodeFactory.instance.objectNode().put("N", n.v());
|
||||
default -> ((PVal.Av) val).node();
|
||||
};
|
||||
}
|
||||
|
||||
private static String displayBound(JsonNode node) {
|
||||
String type = node.fieldNames().next();
|
||||
String text = node.get(type).asText();
|
||||
return switch (type) {
|
||||
case "N" -> new BigDecimal(text).toString();
|
||||
case "B" -> "[" + text + "]";
|
||||
default -> text;
|
||||
};
|
||||
}
|
||||
|
||||
static String betweenTypeMismatch(String lower, String upper) {
|
||||
return "The BETWEEN operator requires same data type for lower and upper bounds; lower bound operand: "
|
||||
+ lower + ", upper bound operand: " + upper;
|
||||
}
|
||||
|
||||
static String betweenReversed(String lower, String upper) {
|
||||
return "The BETWEEN operator requires upper bound to be greater than or equal to lower bound; "
|
||||
+ "lower bound operand: " + lower + ", upper bound operand: " + upper;
|
||||
}
|
||||
|
||||
static String incorrectOperandType(String op, String type) {
|
||||
return "Incorrect operand type for operator or function; "
|
||||
+ "operator or function: " + op + ", operand type: " + type;
|
||||
@@ -1288,6 +1330,7 @@ public class DynamoDbPartiQLParser {
|
||||
PVal hi = parseValue();
|
||||
requireOrdered("BETWEEN", lo);
|
||||
requireOrdered("BETWEEN", hi);
|
||||
requireBetweenBounds(lo, hi);
|
||||
return new Cond.Between(path, lo, hi);
|
||||
}
|
||||
if (peek().type() == TType.IN) {
|
||||
|
||||
@@ -3750,7 +3750,11 @@ public class DynamoDbService {
|
||||
|
||||
public void validateConditionExpression(String conditionExpression, JsonNode exprAttrNames,
|
||||
JsonNode exprAttrValues) {
|
||||
validateExpression(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
try {
|
||||
validateExpression(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
} catch (AwsException e) {
|
||||
throw SmithyValidationErrors.inEnvelope(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void validateFilterExpression(String filterExpression, JsonNode exprAttrNames, JsonNode exprAttrValues) {
|
||||
|
||||
@@ -16,6 +16,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
@@ -572,30 +573,42 @@ final class ExpressionEvaluator {
|
||||
return invalidExpression(exprType, DynamoDbPartiQLParser.incorrectOperandType(operator, type));
|
||||
}
|
||||
|
||||
// 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.
|
||||
// AWS rejects a BETWEEN with bounds of different types or 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) {
|
||||
JsonNode low = placeholderValue(between.low(), values);
|
||||
JsonNode high = placeholderValue(between.high(), values);
|
||||
String message = betweenBoundsError(placeholderValue(between.low(), values),
|
||||
placeholderValue(between.high(), values), ExpressionEvaluator::displayAttributeValue);
|
||||
if (message != null) {
|
||||
throw invalidExpression(exprType, message);
|
||||
}
|
||||
}
|
||||
|
||||
static String betweenBoundsError(JsonNode low, JsonNode high, Function<JsonNode, String> display) {
|
||||
if (low == null || high == null) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
String lowType = low.fieldNames().next();
|
||||
if (!lowType.equals(high.fieldNames().next()) || compareBoundValues(low, high) <= 0) {
|
||||
return;
|
||||
try {
|
||||
if (!low.fieldNames().next().equals(high.fieldNames().next())) {
|
||||
return DynamoDbPartiQLParser.betweenTypeMismatch(displayWrapped(low, display),
|
||||
displayWrapped(high, display));
|
||||
}
|
||||
if (compareBoundValues(low, high) > 0) {
|
||||
return DynamoDbPartiQLParser.betweenReversed(displayWrapped(low, display),
|
||||
displayWrapped(high, display));
|
||||
}
|
||||
} catch (NumberFormatException ignored) {
|
||||
// Another check refuses a number that does not parse.
|
||||
}
|
||||
// AWS wraps the ConditionExpression form in its validation-error envelope, but reports
|
||||
// the FilterExpression and KeyConditionExpression forms on their own.
|
||||
String envelope = "ConditionExpression".equals(exprType) ? "1 validation error detected: " : "";
|
||||
throw new AwsException("ValidationException", envelope
|
||||
+ "Invalid " + exprType + ": The BETWEEN operator requires upper bound to be greater than "
|
||||
+ "or equal to lower bound; lower bound operand: " + displayAttributeValue(low)
|
||||
+ ", upper bound operand: " + displayAttributeValue(high), 400);
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String displayWrapped(JsonNode value, Function<JsonNode, String> display) {
|
||||
return "AttributeValue: {" + value.fieldNames().next() + ":" + display.apply(value) + "}";
|
||||
}
|
||||
|
||||
// DynamoDB orders strings by their UTF-8 bytes, which differs from Java's UTF-16
|
||||
// ordering above the basic plane: U+E000 sorts before U+10000 on AWS but after it here.
|
||||
private static int compareBoundValues(JsonNode low, JsonNode high) {
|
||||
static int compareBoundValues(JsonNode low, JsonNode high) {
|
||||
if (low.has("S") && high.has("S")) {
|
||||
return Arrays.compareUnsigned(
|
||||
low.get("S").asText().getBytes(StandardCharsets.UTF_8),
|
||||
@@ -629,8 +642,7 @@ final class ExpressionEvaluator {
|
||||
}
|
||||
|
||||
private static String displayAttributeValue(JsonNode value) {
|
||||
String type = value.fieldNames().next();
|
||||
return "AttributeValue: {" + type + ":" + value.get(type).asText() + "}";
|
||||
return value.get(value.fieldNames().next()).asText();
|
||||
}
|
||||
|
||||
private static void validateFunction(FunctionCallExpr f, String exprType,
|
||||
|
||||
@@ -893,18 +893,10 @@ public class NativeDynamoDbJsonHandler {
|
||||
checkUnusedEan(exprAttrNames, extractHashTokens(conditionExpression));
|
||||
checkUnusedEav(exprAttrValues, extractColonTokens(conditionExpression));
|
||||
} catch (AwsException e) {
|
||||
throw inValidationEnvelope(e);
|
||||
throw SmithyValidationErrors.inEnvelope(e);
|
||||
}
|
||||
}
|
||||
|
||||
// A size check and a BETWEEN error add the envelope themselves.
|
||||
private static AwsException inValidationEnvelope(AwsException e) {
|
||||
if (!"ValidationException".equals(e.getErrorCode()) || e.getMessage().startsWith(VALIDATION_ENVELOPE)) {
|
||||
return e;
|
||||
}
|
||||
return SmithyValidationErrors.validationException(e.getMessage());
|
||||
}
|
||||
|
||||
private static void requireNonEmptyMap(JsonNode map, String member) {
|
||||
if (map != null && map.isObject() && map.isEmpty()) {
|
||||
throw new AwsException("ValidationException", member + " must not be empty", 400);
|
||||
@@ -1090,7 +1082,7 @@ public class NativeDynamoDbJsonHandler {
|
||||
checkUnusedEav(exprAttrValues, colonTokensAll);
|
||||
}
|
||||
} catch (AwsException e) {
|
||||
throw inValidationEnvelope(e);
|
||||
throw SmithyValidationErrors.inEnvelope(e);
|
||||
}
|
||||
|
||||
if (exprAttrValues != null) {
|
||||
|
||||
Reference in new issue
Block a user