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:
Niklas Herder authored and GitHub committed 2026-09-28 12:13:36 -04:00
1 parent f40402fa93
commit 5ec18c507c
3 files changed
+970 -44

No files matched your search

@@ -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
@@ -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 "&lt;param&gt; can only be specified when using
@@ -4805,6 +4805,851 @@ given()
deleteTable(tableName);
}
// A filter's semantic error is reported before an undefined #name in the projection.
@Test
void filterSemanticErrorWinsOverUndefinedProjectionName() {
String tableName = "FilterVsProjectionTable";
given()
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"KeySchema": [
{"AttributeName": "pk", "KeyType": "HASH"},
{"AttributeName": "sk", "KeyType": "RANGE"}
],
"AttributeDefinitions": [
{"AttributeName": "pk", "AttributeType": "S"},
{"AttributeName": "sk", "AttributeType": "S"}
],
"BillingMode": "PAY_PER_REQUEST"
}
""".formatted(tableName))
.when().post("/")
.then().statusCode(200);
given()
.header("X-Amz-Target", "DynamoDB_20120810.Query")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"KeyConditionExpression": "pk = :pk",
"FilterExpression": "contains(a, a)",
"ProjectionExpression": "#p",
"ExpressionAttributeValues": {":pk": {"S": "a"}}
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid FilterExpression: The first operand must be distinct "
+ "from the remaining operands for this operator or function; operator: contains, "
+ "first operand: [a]"));
given()
.header("X-Amz-Target", "DynamoDB_20120810.Scan")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"FilterExpression": "contains(a, a)",
"ProjectionExpression": "#p"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid FilterExpression: The first operand must be distinct "
+ "from the remaining operands for this operator or function; operator: contains, "
+ "first operand: [a]"));
deleteTable(tableName);
}
// An undefined #name/:value in ConditionExpression must reject, not false-fail the condition.
@Test
void undefinedTokensInConditionExpressionAreRejected() {
String tableName = "ConditionExprTokenTable";
given()
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"KeySchema": [
{"AttributeName": "pk", "KeyType": "HASH"},
{"AttributeName": "sk", "KeyType": "RANGE"}
],
"AttributeDefinitions": [
{"AttributeName": "pk", "AttributeType": "S"},
{"AttributeName": "sk", "AttributeType": "S"}
],
"BillingMode": "PAY_PER_REQUEST"
}
""".formatted(tableName))
.when().post("/")
.then().statusCode(200);
// PutItem: ConditionExpression references an undefined :value.
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Item": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "sk = :v"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :v"));
// PutItem: ConditionExpression references an undefined #name.
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Item": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "#n = sk"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #n"));
// Both undefined; the textually-first token (:v) is reported, not names before values.
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Item": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": ":v = #n"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :v"));
// The undefined name must be reported ahead of contains()'s distinct-operand semantic error.
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Item": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "contains(#missing, #missing)"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #missing"));
// DeleteItem: ConditionExpression references an undefined :value.
given()
.header("X-Amz-Target", "DynamoDB_20120810.DeleteItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "sk = :v"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :v"));
// DeleteItem: ConditionExpression references an undefined #name.
given()
.header("X-Amz-Target", "DynamoDB_20120810.DeleteItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "#n = sk"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #n"));
// UpdateItem: valid UpdateExpression, but ConditionExpression is undefined.
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET payload = :d",
"ExpressionAttributeValues": {":d": {"S": "x"}},
"ConditionExpression": "sk = :v"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :v"));
// UpdateItem: ConditionExpression references an undefined #name.
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET payload = :d",
"ExpressionAttributeValues": {":d": {"S": "x"}},
"ConditionExpression": "#n = sk"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #n"));
// Undefined :v must win over the unused-EAV check for the stray :z.
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET payload = :d",
"ConditionExpression": "sk = :v",
"ExpressionAttributeValues": {":d": {"S": "x"}, ":z": {"S": "unused"}}
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :v"));
// contains()'s distinct-operand error must win over the unused-EAN check for the stray #z.
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET payload = :d",
"ConditionExpression": "contains(#m, #m)",
"ExpressionAttributeNames": {"#m": "sk", "#z": "unused"},
"ExpressionAttributeValues": {":d": {"S": "x"}}
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: The first operand must be distinct "
+ "from the remaining operands for this operator or function; operator: contains, "
+ "first operand: [sk]"));
// Edge case: nested path #a.#b, only #a defined.
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Item": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "attribute_not_exists(#a.#b)",
"ExpressionAttributeNames": {"#a": "payload"}
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #b"));
// Edge case: the same undefined token used twice must still report once, not crash.
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Item": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "sk = :v OR pk = :v"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :v"));
// Control: a different token than the one supplied is missing and must be named.
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Item": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "#n = :v",
"ExpressionAttributeNames": {"#n": "sk"},
"ExpressionAttributeValues": {":other": {"S": "x"}}
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :v"));
// Control: a fully-defined ConditionExpression is still accepted; seed the item first.
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Item": {"pk": {"S": "a"}, "sk": {"S": "b"}}
}
""".formatted(tableName))
.when().post("/")
.then().statusCode(200);
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Item": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "#n = :v",
"ExpressionAttributeNames": {"#n": "sk"},
"ExpressionAttributeValues": {":v": {"S": "b"}}
}
""".formatted(tableName))
.when().post("/")
.then().statusCode(200);
deleteTable(tableName);
}
// Query/Scan must reject an undefined #name/:value, never silently treat it as "no match".
@Test
void undefinedTokensInKeyConditionAndFilterExpressionsAreRejected() {
String tableName = "QueryScanTokenTable";
given()
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"KeySchema": [
{"AttributeName": "pk", "KeyType": "HASH"},
{"AttributeName": "sk", "KeyType": "RANGE"}
],
"AttributeDefinitions": [
{"AttributeName": "pk", "AttributeType": "S"},
{"AttributeName": "sk", "AttributeType": "S"}
],
"BillingMode": "PAY_PER_REQUEST"
}
""".formatted(tableName))
.when().post("/")
.then().statusCode(200);
// Must be the real cause, not "Query condition missed key schema element".
given()
.header("X-Amz-Target", "DynamoDB_20120810.Query")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"KeyConditionExpression": "#n = :pk",
"ExpressionAttributeValues": {":pk": {"S": "a"}}
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid KeyConditionExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #n"));
// Must not silently evaluate as "no match" and return an empty page.
given()
.header("X-Amz-Target", "DynamoDB_20120810.Query")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"KeyConditionExpression": "pk = :pk",
"FilterExpression": "payload = :d",
"ExpressionAttributeValues": {":pk": {"S": "a"}}
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid FilterExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :d"));
// Scan: FilterExpression references an undefined :value.
given()
.header("X-Amz-Target", "DynamoDB_20120810.Scan")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"FilterExpression": "payload = :d"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid FilterExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :d"));
// Scan: FilterExpression references an undefined #name.
given()
.header("X-Amz-Target", "DynamoDB_20120810.Scan")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"FilterExpression": "#n = :d",
"ExpressionAttributeValues": {":d": {"S": "x"}}
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid FilterExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #n"));
// Edge case: :p is a function argument, begins_with(#n, :p), not a bare operand.
given()
.header("X-Amz-Target", "DynamoDB_20120810.Scan")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"FilterExpression": "begins_with(#n, :p)",
"ExpressionAttributeNames": {"#n": "payload"}
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid FilterExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :p"));
deleteTable(tableName);
}
// UpdateExpression must reject an undefined #name the same way it already rejects :value.
@Test
void undefinedTokensInUpdateExpressionAreRejected() {
String tableName = "UpdateExprTokenTable";
given()
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"KeySchema": [
{"AttributeName": "pk", "KeyType": "HASH"},
{"AttributeName": "sk", "KeyType": "RANGE"}
],
"AttributeDefinitions": [
{"AttributeName": "pk", "AttributeType": "S"},
{"AttributeName": "sk", "AttributeType": "S"}
],
"BillingMode": "PAY_PER_REQUEST"
}
""".formatted(tableName))
.when().post("/")
.then().statusCode(200);
// The UpdateExpression itself references an undefined :value.
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET notes = :d"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid UpdateExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :d"));
// Previously this silently stored an attribute literally named "#n".
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET #n = :d",
"ExpressionAttributeValues": {":d": {"S": "x"}}
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid UpdateExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #n"));
// Both undefined; #n comes first in the text and is reported.
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET #n = :d"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid UpdateExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #n"));
// :d is inside list_append's args but still comes before #n in the text.
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET notes = list_append(:d, #n)"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid UpdateExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :d"));
// Control: a fully-defined UpdateExpression #name is still accepted (no regression).
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET #n = :d",
"ExpressionAttributeNames": {"#n": "notes"},
"ExpressionAttributeValues": {":d": {"S": "x"}}
}
""".formatted(tableName))
.when().post("/")
.then().statusCode(200);
deleteTable(tableName);
}
// An undefined token must reject, never fall through to a false ConditionalCheckFailed.
@Test
void undefinedTokensInTransactWriteItemsAreRejected() {
String tableName = "TransactTokenTable";
given()
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"KeySchema": [
{"AttributeName": "pk", "KeyType": "HASH"},
{"AttributeName": "sk", "KeyType": "RANGE"}
],
"AttributeDefinitions": [
{"AttributeName": "pk", "AttributeType": "S"},
{"AttributeName": "sk", "AttributeType": "S"}
],
"BillingMode": "PAY_PER_REQUEST"
}
""".formatted(tableName))
.when().post("/")
.then().statusCode(200);
// ConditionCheck's ConditionExpression references an undefined :value.
given()
.header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TransactItems": [{
"ConditionCheck": {
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "sk = :v"
}
}]
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :v"));
// ConditionCheck's ConditionExpression references an undefined #name.
given()
.header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TransactItems": [{
"ConditionCheck": {
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "#n = sk"
}
}]
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #n"));
// Put's ConditionExpression references an undefined :value.
given()
.header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TransactItems": [{
"Put": {
"TableName": "%s",
"Item": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "sk = :v"
}
}]
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :v"));
// Delete's ConditionExpression references an undefined #name.
given()
.header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TransactItems": [{
"Delete": {
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "#n = sk"
}
}]
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #n"));
// Update's ConditionExpression is undefined despite an otherwise-valid UpdateExpression.
given()
.header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TransactItems": [{
"Update": {
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET notes = :d",
"ExpressionAttributeValues": {":d": {"S": "x"}},
"ConditionExpression": "sk = :v"
}
}]
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :v"));
// Previously this silently no-op'd instead of erroring.
given()
.header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TransactItems": [{
"Update": {
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET notes = :d"
}
}]
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid UpdateExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :d"));
// Previously this silently stored an attribute literally named "#n".
given()
.header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TransactItems": [{
"Update": {
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET #n = :d",
"ExpressionAttributeValues": {":d": {"S": "x"}}
}
}]
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid UpdateExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #n"));
// Both undefined; UpdateExpression's own token is reported first.
given()
.header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TransactItems": [{
"Update": {
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET notes = :d",
"ConditionExpression": "sk = :v"
}
}]
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid UpdateExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :d"));
// Control: a fully-defined transaction is still accepted; seed the item first.
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Item": {"pk": {"S": "txn-control"}, "sk": {"S": "b"}}
}
""".formatted(tableName))
.when().post("/")
.then().statusCode(200);
given()
.header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TransactItems": [{
"Update": {
"TableName": "%s",
"Key": {"pk": {"S": "txn-control"}, "sk": {"S": "b"}},
"UpdateExpression": "SET #n = :d",
"ExpressionAttributeNames": {"#n": "notes"},
"ConditionExpression": "sk = :v",
"ExpressionAttributeValues": {":v": {"S": "b"}, ":d": {"S": "x"}}
}
}]
}
""".formatted(tableName))
.when().post("/")
.then().statusCode(200);
deleteTable(tableName);
}
private void deleteTable(String tableName) {
given()
.header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")