mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): refuse a reserved attribute name in written order before the table lookup (#8274)
This commit is contained in:
1 parent
869a5fcb59
commit
c5e7c3d84e
8 files changed
+131
-35
No files matched your search
@@ -17,8 +17,7 @@ final class DynamoDbReservedWords {
|
||||
// and should not be treated as attribute name tokens.
|
||||
private static final Set<String> CLAUSE_KEYWORDS = Set.of(
|
||||
"SET", "REMOVE", "ADD", "DELETE",
|
||||
"AND", "OR", "NOT", "BETWEEN", "IN",
|
||||
"TRUE", "FALSE"
|
||||
"AND", "OR", "NOT", "BETWEEN", "IN"
|
||||
);
|
||||
|
||||
// DynamoDB reserved words list (uppercase). DynamoDB documents SIZE too, but accepts it as an attribute name.
|
||||
@@ -128,7 +127,7 @@ final class DynamoDbReservedWords {
|
||||
static AwsException reservedWord(String expressionType, String ident) {
|
||||
return new AwsException("ValidationException",
|
||||
"Invalid " + expressionType + ": Attribute name is a reserved keyword; "
|
||||
+ "reserved word: " + ident, 400);
|
||||
+ "reserved keyword: " + ident, 400);
|
||||
}
|
||||
|
||||
private static List<String> extractBareIdentifiers(String expr) {
|
||||
|
||||
@@ -537,6 +537,9 @@ final class ExpressionEvaluator {
|
||||
if (segment.startsWith("#") && (names == null || !names.has(segment))) {
|
||||
throw undefinedName(exprType, segment);
|
||||
}
|
||||
if (DynamoDbReservedWords.isReserved(segment)) {
|
||||
throw DynamoDbReservedWords.reservedWord(exprType, segment);
|
||||
}
|
||||
}
|
||||
}
|
||||
case PlaceholderOperand placeholder -> {
|
||||
|
||||
@@ -1388,13 +1388,13 @@ public class NativeDynamoDbJsonHandler {
|
||||
DynamoDbAttributeValueValidator.requireNestingWithinLimit(legacyValues(queryFilter), false);
|
||||
ExpressionEvaluator.validateSyntax(keyConditionExpr, "KeyConditionExpression");
|
||||
ExpressionEvaluator.validateSyntax(filterExpr, "FilterExpression");
|
||||
// DynamoDB reports filter errors before projection errors, and projection before key-condition.
|
||||
// DynamoDB reports filter errors first, then key-condition errors, then projection errors.
|
||||
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.
|
||||
ExpressionEvaluator.validateSemantics(keyConditionExpr, "KeyConditionExpression", exprAttrNames, exprAttrValues);
|
||||
DynamoDbAccessPathValidator.validateKeyConditionAttributes(keyConditionExpr, exprAttrNames);
|
||||
ProjectionEvaluator.validateExpression(projectionExpression, exprAttrNames);
|
||||
|
||||
if (select != null && !VALID_SELECT.contains(select)) {
|
||||
throw SmithyValidationErrors.validationException(
|
||||
@@ -3279,6 +3279,18 @@ public class NativeDynamoDbJsonHandler {
|
||||
"ProjectionExpression");
|
||||
}
|
||||
|
||||
// DynamoDB checks every ProjectionExpression before the table lookup.
|
||||
for (JsonNode txItem : transactItemsNode) {
|
||||
JsonNode get = txItem.has("Get") ? txItem.get("Get") : null;
|
||||
if (get == null) {
|
||||
continue;
|
||||
}
|
||||
String pe = get.has("ProjectionExpression") ? get.get("ProjectionExpression").asText() : null;
|
||||
if (pe != null) {
|
||||
ProjectionEvaluator.validateExpression(pe, get.get("ExpressionAttributeNames"));
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, TableDefinition> tableCache = new HashMap<>();
|
||||
Set<String> seenGet = new HashSet<>();
|
||||
for (JsonNode txItem : transactItemsNode) {
|
||||
@@ -3303,16 +3315,6 @@ public class NativeDynamoDbJsonHandler {
|
||||
}
|
||||
}
|
||||
|
||||
// Validate the ProjectionExpression in each Get action before executing
|
||||
for (JsonNode txItem : transactItemsNode) {
|
||||
JsonNode get = txItem.has("Get") ? txItem.get("Get") : null;
|
||||
if (get == null) continue;
|
||||
String pe = get.has("ProjectionExpression") ? get.get("ProjectionExpression").asText() : null;
|
||||
if (pe != null) {
|
||||
ProjectionEvaluator.validateExpression(pe, get.get("ExpressionAttributeNames"));
|
||||
}
|
||||
}
|
||||
|
||||
List<JsonNode> transactItems = new ArrayList<>();
|
||||
transactItemsNode.forEach(transactItems::add);
|
||||
|
||||
|
||||
@@ -101,13 +101,13 @@ final class ProjectionEvaluator {
|
||||
|
||||
static void validateExpression(String expression, JsonNode exprAttrNames) {
|
||||
validateSyntax(expression, "ProjectionExpression");
|
||||
DynamoDbReservedWords.check(expression, "ProjectionExpression");
|
||||
validatePaths(expression, exprAttrNames);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rejects an undefined #alias, and any two paths where one covers the other.
|
||||
* DynamoDB applies both before the read, so a request that matches nothing still fails.
|
||||
* Rejects an undefined #alias, a reserved word, and a path that overlaps an earlier one.
|
||||
* DynamoDB checks each path in written order before the read, so a request that matches
|
||||
* nothing still fails.
|
||||
*/
|
||||
static void validatePaths(String expression, JsonNode exprAttrNames) {
|
||||
if (expression == null || expression.isBlank()) {
|
||||
@@ -116,21 +116,18 @@ final class ProjectionEvaluator {
|
||||
List<List<PathSegment>> paths = new ArrayList<>();
|
||||
for (String rawPath : splitProjectionPaths(expression)) {
|
||||
List<PathSegment> segments = resolvePath(rawPath.trim(), exprAttrNames, true);
|
||||
if (!segments.isEmpty()) {
|
||||
paths.add(segments);
|
||||
if (segments.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < paths.size(); i++) {
|
||||
List<PathSegment> first = paths.get(i);
|
||||
for (int j = i + 1; j < paths.size(); j++) {
|
||||
List<PathSegment> second = paths.get(j);
|
||||
if (covers(first, second) || covers(second, first)) {
|
||||
for (List<PathSegment> earlier : paths) {
|
||||
if (covers(earlier, segments) || covers(segments, earlier)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid ProjectionExpression: Two document paths overlap with each other; "
|
||||
+ "must remove or rewrite one of these paths; path one: " + render(first)
|
||||
+ ", path two: " + render(second), 400);
|
||||
+ "must remove or rewrite one of these paths; path one: " + render(earlier)
|
||||
+ ", path two: " + render(segments), 400);
|
||||
}
|
||||
}
|
||||
paths.add(segments);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -255,6 +252,9 @@ final class ProjectionEvaluator {
|
||||
|
||||
private static String resolveSegment(String seg, JsonNode exprAttrNames, boolean requireDefinedNames) {
|
||||
if (!seg.startsWith("#")) {
|
||||
if (requireDefinedNames && DynamoDbReservedWords.isReserved(seg)) {
|
||||
throw DynamoDbReservedWords.reservedWord("ProjectionExpression", seg);
|
||||
}
|
||||
return seg;
|
||||
}
|
||||
JsonNode resolved = exprAttrNames != null ? exprAttrNames.get(seg) : null;
|
||||
|
||||
Reference in new issue
Block a user