mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(dynamodb): reject undefined expression attribute placeholders (#4398)
* fix(dynamodb): reject undefined expression attribute placeholders PutItem/DeleteItem/UpdateItem ConditionExpression, UpdateItem's own UpdateExpression, Query KeyConditionExpression, Query/Scan FilterExpression, and TransactWriteItems' ConditionCheck/Put/Delete/Update ConditionExpression and Update UpdateExpression accepted a #name or :value placeholder with no matching ExpressionAttributeNames/Values entry, instead of rejecting it with AWS's ValidationException. Writes fell through to a false ConditionalCheckFailedException (or, inside a transaction, a TransactionCanceledException with a false ConditionalCheckFailed reason), reads silently evaluated the condition as non-matching (Query) or ignored it entirely (Scan), and an undefined #name in an UpdateExpression's SET clause was silently stored as an attribute literally named "#n" instead of being rejected (standalone UpdateItem and inside a transaction alike). An undefined #name in KeyConditionExpression additionally surfaced the wrong error, "Query condition missed key schema element", because attribute resolution silently left the alias unresolved. Generalizes the pattern already used for UpdateExpression's undefined :value check and Query's FilterExpression undefined #name check into a shared requireDefinedTokens helper, applied to the remaining expression/handler combinations, and built on the same ordered token scanner extractHashTokens and extractColonTokens already used. The helper reports whichever undefined token appears first in the text; in UpdateItem the ConditionExpression check now runs before the unused-EAN/EAV checks rather than after. ProjectionExpression's undefined #name check and KeyConditionExpression's undefined :value check were already correct and are untouched. * fix(dynamodb): check undefined placeholders before semantic validation ExpressionEvaluator.validateExpression's semantic checks (contains()'s distinct-operand rule, begins_with()'s operand type, BETWEEN's bound ordering) ran before requireDefinedTokens at every call site, so an undefined #name/:value could surface the wrong error, e.g. contains(#missing, #missing) reported the distinct-operand error instead of the undefined name. Verified against amazon/dynamodb-local:3.3.1 that DynamoDB reports the undefined placeholder first for contains(), begins_with() type mismatch, and BETWEEN bound ordering, while a syntax error or redundant parentheses still outrank an undefined placeholder. Splits validateExpression into validateSyntax (tokenize/parse/redundant- parentheses, unchanged position) and validateSemantics (the checks above), and reorders each handler to run requireDefinedTokens between them. validateExpression itself is untouched and still used as-is by DynamoDbVectorSearch's SearchConditionExpression. In UpdateItem, ConditionExpression's semantic check must also outrank the unused-EAN/EAV checks (#2893), not just requireDefinedTokens: moved validateSemantics to run immediately after requireDefinedTokens(conditionExpression) in both the UpdateExpression-present and UpdateExpression-absent branches, ahead of checkUnusedEan/checkUnusedEav. Verified the same ordering already held in PutItem, DeleteItem, Query and Scan (their unused checks already ran after validateSemantics), and that TransactWriteItems has no unused-EAN/EAV check to conflict with. Also adds braces to the unbraced conditionals in the token scanner added by the previous commit, per AGENTS.md. * fix(dynamodb): keep projection validation after filter semantics The syntax/semantics split moved FilterExpression's undefined-token and semantic checks below ProjectionExpression's in Query and Scan, so a Query/Scan with FilterExpression "contains(a, a)" and an undefined #name in ProjectionExpression now reports the projection error instead of the filter one. Verified against amazon/dynamodb-local:3.3.1: FilterExpression's checks outrank ProjectionExpression's, which in turn outrank KeyConditionExpression's (unchanged, and already correctly ordered).
This commit is contained in:
1 parent
f40402fa93
commit
5ec18c507c
3 files changed
+970
-44
No files matched your search
+25
-9
@@ -395,25 +395,41 @@ final class ExpressionEvaluator {
|
||||
*/
|
||||
static void validateExpression(String expression, String exprType,
|
||||
JsonNode exprAttrNames, JsonNode exprAttrValues) {
|
||||
if (expression == null || expression.isBlank()) return;
|
||||
List<Token> tokens;
|
||||
Expr expr;
|
||||
validateSyntax(expression, exprType);
|
||||
validateSemantics(expression, exprType, exprAttrNames, exprAttrValues);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)}.
|
||||
*/
|
||||
static void validateSyntax(String expression, String exprType) {
|
||||
if (expression == null || expression.isBlank()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
tokens = tokenize(expression.trim());
|
||||
List<Token> tokens = tokenize(expression.trim());
|
||||
checkRedundantParentheses(tokens, exprType);
|
||||
expr = parse(expression);
|
||||
parse(expression);
|
||||
} catch (IllegalArgumentException e) {
|
||||
String detail = e.getMessage();
|
||||
|
||||
|
||||
if (detail.startsWith("token:")) {
|
||||
throw new AwsException("ValidationException",
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid " + exprType + ": Syntax error; " + detail, 400);
|
||||
} else {
|
||||
throw new AwsException("ValidationException",
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid " + exprType + ": Syntax error", 400);
|
||||
}
|
||||
}
|
||||
validateSemantics(expr, exprType, exprAttrNames, exprAttrValues);
|
||||
}
|
||||
|
||||
/** The semantic half of {@link #validateExpression}; see {@link #validateSyntax}. */
|
||||
static void validateSemantics(String expression, String exprType,
|
||||
JsonNode exprAttrNames, JsonNode exprAttrValues) {
|
||||
validateSemantics(parse(expression), exprType, exprAttrNames, exprAttrValues);
|
||||
}
|
||||
|
||||
// A pair of parentheses is redundant when its entire content is itself a single
|
||||
|
||||
+100
-35
@@ -566,7 +566,9 @@ public class NativeDynamoDbJsonHandler {
|
||||
rejectExprAttrsWithoutExpression(exprAttrNames, exprAttrValues,
|
||||
conditionExpression != null, "ConditionExpression is null");
|
||||
|
||||
ExpressionEvaluator.validateExpression(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
ExpressionEvaluator.validateSyntax(conditionExpression, "ConditionExpression");
|
||||
requireDefinedTokens(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
ExpressionEvaluator.validateSemantics(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
|
||||
// Reject ExpressionAttributeNames/Values entries not referenced by ConditionExpression (AWS parity, #2893)
|
||||
checkUnusedEan(exprAttrNames, extractHashTokens(conditionExpression));
|
||||
@@ -695,7 +697,9 @@ public class NativeDynamoDbJsonHandler {
|
||||
rejectExprAttrsWithoutExpression(exprAttrNames, exprAttrValues,
|
||||
conditionExpression != null, "ConditionExpression is null");
|
||||
|
||||
ExpressionEvaluator.validateExpression(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
ExpressionEvaluator.validateSyntax(conditionExpression, "ConditionExpression");
|
||||
requireDefinedTokens(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
ExpressionEvaluator.validateSemantics(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
|
||||
// Reject ExpressionAttributeNames/Values entries not referenced by ConditionExpression (AWS parity, #2893)
|
||||
checkUnusedEan(exprAttrNames, extractHashTokens(conditionExpression));
|
||||
@@ -777,7 +781,7 @@ public class NativeDynamoDbJsonHandler {
|
||||
updateExpression != null || conditionExpression != null,
|
||||
"UpdateExpression is null, ConditionExpression is null");
|
||||
|
||||
ExpressionEvaluator.validateExpression(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
ExpressionEvaluator.validateSyntax(conditionExpression, "ConditionExpression");
|
||||
|
||||
if (conditionExpression != null && expectedUpd != null) {
|
||||
throw new AwsException("ValidationException",
|
||||
@@ -803,18 +807,19 @@ public class NativeDynamoDbJsonHandler {
|
||||
"Invalid UpdateExpression: Syntax error; token: \"" + token + "\", near: \"" + near + "\"", 400);
|
||||
}
|
||||
}
|
||||
Set<String> colonTokens = extractColonTokens(updateExpression);
|
||||
for (String token : colonTokens) {
|
||||
if (exprAttrValues == null || !exprAttrValues.has(token)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid UpdateExpression: An expression attribute value used in expression is not defined; attribute value: " + token, 400);
|
||||
}
|
||||
}
|
||||
// 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);
|
||||
Set<String> colonTokensAll = extractColonTokens(updateExpression, conditionExpression);
|
||||
checkUnusedEav(exprAttrValues, colonTokensAll);
|
||||
dynamoDbService.requireAddOrDeleteOperandTypes(updateExpression, exprAttrValues, true);
|
||||
} else {
|
||||
requireDefinedTokens(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
ExpressionEvaluator.validateSemantics(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
}
|
||||
|
||||
if (expectedUpd != null) {
|
||||
@@ -1072,9 +1077,16 @@ public class NativeDynamoDbJsonHandler {
|
||||
DynamoDbNumberUtils.requireStorable(exprAttrValues, false);
|
||||
DynamoDbAttributeValueValidator.requireNestingWithinLimit(legacyValues(keyConditions), false);
|
||||
DynamoDbAttributeValueValidator.requireNestingWithinLimit(legacyValues(queryFilter), false);
|
||||
ExpressionEvaluator.validateExpression(keyConditionExpr, "KeyConditionExpression", exprAttrNames, exprAttrValues);
|
||||
ExpressionEvaluator.validateExpression(filterExpr, "FilterExpression", exprAttrNames, exprAttrValues);
|
||||
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)) {
|
||||
throw new AwsException("ValidationException",
|
||||
@@ -1097,15 +1109,6 @@ public class NativeDynamoDbJsonHandler {
|
||||
projectionExpression, attributesToGet, exprAttrNames);
|
||||
rejectConsistentReadOnGsi(request, queryAccessPath);
|
||||
|
||||
// Check undefined #tokens in FilterExpression
|
||||
if (filterExpr != null) {
|
||||
for (String token : extractHashTokens(filterExpr)) {
|
||||
if (exprAttrNames == null || !exprAttrNames.has(token)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid FilterExpression: An expression attribute name used in the document path is not defined; attribute name: " + token, 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Reject ExpressionAttributeNames/Values entries not referenced by any expression
|
||||
// (AWS parity, #2893). Unconditional: a legacy KeyConditions request that still
|
||||
// carries EAN/EAV must be rejected, and either map can only be referenced from
|
||||
@@ -1244,7 +1247,10 @@ public class NativeDynamoDbJsonHandler {
|
||||
DynamoDbExpressionSize.checkRead(projectionExpressionScan, "ProjectionExpression");
|
||||
DynamoDbAttributeValueValidator.requireNestingWithinLimit(exprAttrValues);
|
||||
DynamoDbNumberUtils.requireStorable(exprAttrValues, false);
|
||||
ExpressionEvaluator.validateExpression(filterExpr, "FilterExpression", exprAttrNames, 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);
|
||||
|
||||
// Reject ExpressionAttributeNames/Values entries not referenced by any expression (AWS parity, #2893)
|
||||
@@ -1858,14 +1864,22 @@ public class NativeDynamoDbJsonHandler {
|
||||
for (JsonNode op : txItem) {
|
||||
String conditionExpression = op.has("ConditionExpression")
|
||||
? op.get("ConditionExpression").asText() : null;
|
||||
String updateExpression = op.has("UpdateExpression")
|
||||
? op.get("UpdateExpression").asText() : null;
|
||||
JsonNode opExprAttrNames = op.get("ExpressionAttributeNames");
|
||||
JsonNode opExprAttrValues = op.get("ExpressionAttributeValues");
|
||||
DynamoDbExpressionSize.checkRead(op.path("UpdateExpression").textValue(), "UpdateExpression");
|
||||
DynamoDbExpressionSize.checkReadWithSize(conditionExpression, "ConditionExpression");
|
||||
ExpressionEvaluator.validateExpression(conditionExpression, "ConditionExpression",
|
||||
op.get("ExpressionAttributeNames"),
|
||||
op.get("ExpressionAttributeValues"));
|
||||
ExpressionEvaluator.validateSyntax(conditionExpression, "ConditionExpression");
|
||||
// Undefined tokens must fail here as a ValidationException, never a
|
||||
// TransactionCanceledException with a false ConditionalCheckFailed reason.
|
||||
requireDefinedTokens(updateExpression, "UpdateExpression", opExprAttrNames, opExprAttrValues);
|
||||
requireDefinedTokens(conditionExpression, "ConditionExpression", opExprAttrNames, opExprAttrValues);
|
||||
ExpressionEvaluator.validateSemantics(conditionExpression, "ConditionExpression",
|
||||
opExprAttrNames, opExprAttrValues);
|
||||
DynamoDbAttributeValueValidator.requireNestingWithinLimit(op.get("Item"), false);
|
||||
dynamoDbService.requireAddOrDeleteOperandTypes(op.path("UpdateExpression").textValue(),
|
||||
op.get("ExpressionAttributeValues"), false);
|
||||
opExprAttrValues, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2314,32 +2328,83 @@ public class NativeDynamoDbJsonHandler {
|
||||
|
||||
// ── Expression token utilities ──
|
||||
|
||||
private static void extractPrefixedTokens(String expr, char prefix, Set<String> out) {
|
||||
if (expr == null) return;
|
||||
// A #name or :value placeholder found while scanning an expression left to right.
|
||||
private record ExprToken(char prefix, String text) {}
|
||||
|
||||
// Scans an expression once, left to right, for #name and :value placeholders, in the order
|
||||
// they occur in the text. Both extractHashTokens/extractColonTokens (membership checks) and
|
||||
// requireDefinedTokens (first-undefined-token-wins) build on this single scan.
|
||||
private static List<ExprToken> scanExprTokens(String expr) {
|
||||
List<ExprToken> tokens = new ArrayList<>();
|
||||
if (expr == null) {
|
||||
return tokens;
|
||||
}
|
||||
int i = 0;
|
||||
while (i < expr.length()) {
|
||||
if (expr.charAt(i) == prefix) {
|
||||
int start = i++;
|
||||
while (i < expr.length() && (Character.isLetterOrDigit(expr.charAt(i)) || expr.charAt(i) == '_')) i++;
|
||||
if (i > start + 1) out.add(expr.substring(start, i));
|
||||
} else {
|
||||
char c = expr.charAt(i);
|
||||
if (c != '#' && c != ':') {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
int start = i++;
|
||||
while (i < expr.length() && (Character.isLetterOrDigit(expr.charAt(i)) || expr.charAt(i) == '_')) {
|
||||
i++;
|
||||
}
|
||||
if (i > start + 1) {
|
||||
tokens.add(new ExprToken(c, expr.substring(start, i)));
|
||||
}
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
|
||||
private static Set<String> extractHashTokens(String... expressions) {
|
||||
Set<String> tokens = new LinkedHashSet<>();
|
||||
for (String expr : expressions) extractPrefixedTokens(expr, '#', tokens);
|
||||
for (String expr : expressions) {
|
||||
for (ExprToken t : scanExprTokens(expr)) {
|
||||
if (t.prefix() == '#') {
|
||||
tokens.add(t.text());
|
||||
}
|
||||
}
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
|
||||
private static Set<String> extractColonTokens(String... expressions) {
|
||||
Set<String> tokens = new LinkedHashSet<>();
|
||||
for (String expr : expressions) extractPrefixedTokens(expr, ':', tokens);
|
||||
for (String expr : expressions) {
|
||||
for (ExprToken t : scanExprTokens(expr)) {
|
||||
if (t.prefix() == ':') {
|
||||
tokens.add(t.text());
|
||||
}
|
||||
}
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* may name a different one, though always with this same error type and message form.
|
||||
*/
|
||||
private static void requireDefinedTokens(String expression, String exprType,
|
||||
JsonNode exprAttrNames, JsonNode exprAttrValues) {
|
||||
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);
|
||||
}
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Rejects ExpressionAttributeNames/Values supplied when the request carries no expression to
|
||||
* reference them. AWS answers this sub-case with "<param> can only be specified when using
|
||||
|
||||
Reference in new issue
Block a user