mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(dynamodb): validate query key types (#2469)
* fix(dynamodb): validate query key types Closes #2467 * fix(dynamodb): harden query validation * fix(dynamodb): validate query value payloads
This commit is contained in:
1 parent
e3df59ae1f
commit
a57c6c8112
7 files changed
+276
-27
No files matched your search
+83
-7
@@ -15,6 +15,7 @@ import io.github.hectorvent.floci.services.dynamodb.ExpressionEvaluator.OrExpr;
|
||||
import io.github.hectorvent.floci.services.dynamodb.ExpressionEvaluator.PathOperand;
|
||||
import io.github.hectorvent.floci.services.dynamodb.ExpressionEvaluator.PlaceholderOperand;
|
||||
import io.github.hectorvent.floci.services.dynamodb.ExpressionEvaluator.TokenType;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.AttributeDefinition;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;
|
||||
|
||||
import java.util.HashSet;
|
||||
@@ -23,6 +24,8 @@ import java.util.Set;
|
||||
|
||||
final class DynamoDbAccessPathValidator {
|
||||
|
||||
private static final String KEY_TYPE_MISMATCH =
|
||||
"One or more parameter values were invalid: Condition parameter type does not match schema type";
|
||||
private static final Set<TokenType> SORT_KEY_COMPARATORS = Set.of(
|
||||
TokenType.EQ, TokenType.LT, TokenType.LE, TokenType.GT, TokenType.GE);
|
||||
private static final Set<String> LEGACY_SORT_KEY_COMPARATORS = Set.of(
|
||||
@@ -30,15 +33,17 @@ final class DynamoDbAccessPathValidator {
|
||||
|
||||
private DynamoDbAccessPathValidator() {}
|
||||
|
||||
static String validateQuery(DynamoDbAccessPath accessPath, JsonNode keyConditions,
|
||||
static String validateQuery(TableDefinition table, DynamoDbAccessPath accessPath, JsonNode keyConditions,
|
||||
String keyConditionExpression, String filterExpression,
|
||||
JsonNode queryFilter, JsonNode expressionAttributeNames) {
|
||||
JsonNode queryFilter, JsonNode expressionAttributeNames,
|
||||
JsonNode expressionAttributeValues) {
|
||||
String partitionKeyValuePlaceholder = null;
|
||||
if (keyConditionExpression != null) {
|
||||
partitionKeyValuePlaceholder = validateKeyConditionExpression(
|
||||
accessPath, keyConditionExpression, expressionAttributeNames);
|
||||
table, accessPath, keyConditionExpression,
|
||||
expressionAttributeNames, expressionAttributeValues);
|
||||
} else {
|
||||
validateLegacyKeyConditions(accessPath, keyConditions);
|
||||
validateLegacyKeyConditions(table, accessPath, keyConditions);
|
||||
}
|
||||
validateFilterExpression(accessPath, filterExpression, expressionAttributeNames);
|
||||
validateLegacyQueryFilter(accessPath, queryFilter);
|
||||
@@ -83,8 +88,10 @@ final class DynamoDbAccessPathValidator {
|
||||
}
|
||||
}
|
||||
|
||||
private static String validateKeyConditionExpression(DynamoDbAccessPath accessPath,
|
||||
String expression, JsonNode names) {
|
||||
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
|
||||
@@ -117,6 +124,7 @@ final class DynamoDbAccessPathValidator {
|
||||
} else {
|
||||
throw new AwsException("ValidationException", "Query key condition not supported", 400);
|
||||
}
|
||||
validateConditionValueTypes(table, attribute, condition, values);
|
||||
}
|
||||
|
||||
if (!conditionedPartitionKeys.containsAll(partitionKeySet)) {
|
||||
@@ -166,7 +174,39 @@ final class DynamoDbAccessPathValidator {
|
||||
return topLevelAttribute(path, names);
|
||||
}
|
||||
|
||||
private static void validateLegacyKeyConditions(DynamoDbAccessPath accessPath, JsonNode keyConditions) {
|
||||
private static void validateConditionValueTypes(TableDefinition table, String attribute,
|
||||
Expr condition, JsonNode values) {
|
||||
String expectedType = attributeType(table, attribute);
|
||||
for (String placeholder : conditionValuePlaceholders(condition)) {
|
||||
if (values == null || !values.has(placeholder)) {
|
||||
throw validationException("Invalid KeyConditionExpression: An expression attribute value used "
|
||||
+ "in expression is not defined; attribute value: " + placeholder);
|
||||
}
|
||||
if (!isValidKeyValue(values.get(placeholder), expectedType)) {
|
||||
throw validationException(KEY_TYPE_MISMATCH);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static List<String> conditionValuePlaceholders(Expr condition) {
|
||||
return switch (condition) {
|
||||
case CompareExpr compare when compare.right() instanceof PlaceholderOperand placeholder ->
|
||||
List.of(placeholder.name());
|
||||
case BetweenExpr between
|
||||
when between.low() instanceof PlaceholderOperand low
|
||||
&& between.high() instanceof PlaceholderOperand high ->
|
||||
List.of(low.name(), high.name());
|
||||
case FunctionCallExpr function
|
||||
when function.args().size() == 2
|
||||
&& function.args().get(1) instanceof PlaceholderOperand placeholder ->
|
||||
List.of(placeholder.name());
|
||||
default -> List.of();
|
||||
};
|
||||
}
|
||||
|
||||
private static void validateLegacyKeyConditions(TableDefinition table,
|
||||
DynamoDbAccessPath accessPath,
|
||||
JsonNode keyConditions) {
|
||||
List<String> partitionKeys = accessPath.partitionKeyNames();
|
||||
for (String partitionKey : partitionKeys) {
|
||||
if (keyConditions == null || !keyConditions.has(partitionKey)) {
|
||||
@@ -190,9 +230,45 @@ final class DynamoDbAccessPathValidator {
|
||||
|| ("BETWEEN".equals(operator) ? values != 2 : values != 1)) {
|
||||
throw new AwsException("ValidationException", "Query key condition not supported", 400);
|
||||
}
|
||||
String expectedType = attributeType(table, attribute);
|
||||
entry.getValue().path("AttributeValueList").forEach(value -> {
|
||||
if (!isValidKeyValue(value, expectedType)) {
|
||||
throw validationException(KEY_TYPE_MISMATCH);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private static String attributeType(TableDefinition table, String attribute) {
|
||||
List<AttributeDefinition> definitions = table.getAttributeDefinitions();
|
||||
if (definitions == null) {
|
||||
throw validationException(KEY_TYPE_MISMATCH);
|
||||
}
|
||||
return definitions.stream()
|
||||
.filter(definition -> attribute.equals(definition.getAttributeName()))
|
||||
.map(AttributeDefinition::getAttributeType)
|
||||
.findFirst()
|
||||
.orElseThrow(() -> validationException(KEY_TYPE_MISMATCH));
|
||||
}
|
||||
|
||||
private static boolean isValidKeyValue(JsonNode value, String expectedType) {
|
||||
if (value == null || !value.isObject() || value.size() != 1 || !value.has(expectedType)) {
|
||||
return false;
|
||||
}
|
||||
JsonNode payload = value.get(expectedType);
|
||||
if (payload == null || !payload.isTextual() || payload.textValue().isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if ("N".equals(expectedType)) {
|
||||
DynamoDbNumberUtils.validateAndNormalize(payload.textValue());
|
||||
}
|
||||
if ("B".equals(expectedType)) {
|
||||
return payload.textValue().matches(
|
||||
"(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void validateFilterExpression(DynamoDbAccessPath accessPath,
|
||||
String expression, JsonNode names) {
|
||||
if (expression == null) {
|
||||
|
||||
@@ -866,8 +866,8 @@ public class DynamoDbJsonHandler {
|
||||
|
||||
TableDefinition queryTable = dynamoDbService.describeTable(tableName, region);
|
||||
DynamoDbAccessPath queryAccessPath = DynamoDbAccessPath.resolve(queryTable, indexName);
|
||||
DynamoDbAccessPathValidator.validateQuery(queryAccessPath, keyConditions, keyConditionExpr,
|
||||
filterExpr, queryFilter, exprAttrNames);
|
||||
DynamoDbAccessPathValidator.validateQuery(queryTable, queryAccessPath, keyConditions,
|
||||
keyConditionExpr, filterExpr, queryFilter, exprAttrNames, exprAttrValues);
|
||||
DynamoDbAccessPathValidator.validateSelection(queryTable, queryAccessPath, select,
|
||||
projectionExpression, attributesToGet, exprAttrNames);
|
||||
|
||||
|
||||
@@ -271,6 +271,15 @@ public class DynamoDbService {
|
||||
if (lsis != null) {
|
||||
lsis.forEach(l -> l.getKeySchema().forEach(k -> referencedAttrs.add(k.getAttributeName())));
|
||||
}
|
||||
Set<String> definedAttrs = attributeDefinitions == null
|
||||
? Set.of()
|
||||
: attributeDefinitions.stream()
|
||||
.map(AttributeDefinition::getAttributeName)
|
||||
.collect(java.util.stream.Collectors.toSet());
|
||||
if (!definedAttrs.containsAll(referencedAttrs)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid KeySchema: Some index key attribute have no definition", 400);
|
||||
}
|
||||
if (attributeDefinitions != null) {
|
||||
for (AttributeDefinition ad : attributeDefinitions) {
|
||||
if (!referencedAttrs.contains(ad.getAttributeName())) {
|
||||
@@ -755,7 +764,8 @@ public class DynamoDbService {
|
||||
|
||||
DynamoDbAccessPath accessPath = DynamoDbAccessPath.resolve(table, indexName);
|
||||
String partitionKeyValuePlaceholder = DynamoDbAccessPathValidator.validateQuery(
|
||||
accessPath, keyConditions, keyConditionExpression, filterExpression, null, exprAttrNames);
|
||||
table, accessPath, keyConditions, keyConditionExpression, filterExpression,
|
||||
null, exprAttrNames, expressionAttrValues);
|
||||
String pkName = accessPath.partitionKeyName();
|
||||
List<String> pkNames = accessPath.partitionKeyNames();
|
||||
String skName = accessPath.sortKeyName();
|
||||
|
||||
Reference in new issue
Block a user