mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(dynamodb): put UpdateItem expression errors in the validation envelope (#7882)
DynamoDB gives an empty UpdateExpression, a reserved word in it, and AttributeUpdates with an UpdateExpression in the "1 validation error detected: " envelope, before the table lookup. It checks the ConditionExpression before the first two.
This commit is contained in:
1 parent
43ad134107
commit
77242aaceb
2 files changed
+90
-51
No files matched your search
@@ -822,14 +822,18 @@ public class NativeDynamoDbJsonHandler {
|
||||
checkUnusedEan(exprAttrNames, extractHashTokens(conditionExpression));
|
||||
checkUnusedEav(exprAttrValues, extractColonTokens(conditionExpression));
|
||||
} catch (AwsException e) {
|
||||
// The size check and a BETWEEN error add the envelope themselves.
|
||||
if (!"ValidationException".equals(e.getErrorCode()) || e.getMessage().startsWith(VALIDATION_ENVELOPE)) {
|
||||
throw e;
|
||||
}
|
||||
throw SmithyValidationErrors.validationException(e.getMessage());
|
||||
throw inValidationEnvelope(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);
|
||||
@@ -972,9 +976,9 @@ public class NativeDynamoDbJsonHandler {
|
||||
? request.get("ConditionalOperator").asText() : "AND";
|
||||
|
||||
if (updateData != null && updateExpression != null) {
|
||||
throw new AwsException("ValidationException",
|
||||
throw SmithyValidationErrors.validationException(
|
||||
"Can not use both expression and non-expression parameters in the same request: "
|
||||
+ "Non-expression parameters: {AttributeUpdates} Expression parameters: {UpdateExpression}", 400);
|
||||
+ "Non-expression parameters: {AttributeUpdates} Expression parameters: {UpdateExpression}");
|
||||
}
|
||||
requireReadableValues(exprAttrValues, VALIDATION_ENVELOPE);
|
||||
|
||||
@@ -984,44 +988,50 @@ public class NativeDynamoDbJsonHandler {
|
||||
"UpdateExpression is null, ConditionExpression is null");
|
||||
requireReadableValues(attributeUpdateValues, VALIDATION_ENVELOPE);
|
||||
|
||||
ExpressionEvaluator.validateSyntax(conditionExpression, "ConditionExpression");
|
||||
try {
|
||||
ExpressionEvaluator.validateSyntax(conditionExpression, "ConditionExpression");
|
||||
|
||||
if (conditionExpression != null && expectedUpd != null) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Can not use both expression and non-expression parameters in the same request: "
|
||||
+ "Non-expression parameters: {Expected} Expression parameters: {ConditionExpression}", 400);
|
||||
}
|
||||
|
||||
// Validate EAN/EAV usage across UpdateExpression + ConditionExpression
|
||||
if (updateExpression != null) {
|
||||
// Pre-validate syntax: expression must start with a valid clause keyword.
|
||||
// AWS tokenizes UpdateExpression whitespace-insensitively, so collapse runs
|
||||
// of whitespace (newlines, tabs, multiple spaces) before matching keywords.
|
||||
String ue = updateExpression.trim().replaceAll("\\s+", " ");
|
||||
if (!ue.isEmpty()) {
|
||||
String upper = ue.toUpperCase();
|
||||
if (!upper.startsWith("SET ") && !upper.startsWith("REMOVE ") && !upper.startsWith("ADD ") && !upper.startsWith("DELETE ")) {
|
||||
String[] parts = ue.split("\\s+", 3);
|
||||
String token = parts[0];
|
||||
String near = parts.length >= 2
|
||||
? (token + " " + parts[1]).substring(0, Math.min(token.length() + 1 + parts[1].length(), 20))
|
||||
: token;
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid UpdateExpression: Syntax error; token: \"" + token + "\", near: \"" + near + "\"", 400);
|
||||
}
|
||||
DynamoDbService.requireClosedParentheses(updateExpression, "1 validation error detected: ");
|
||||
if (conditionExpression != null && expectedUpd != null) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Can not use both expression and non-expression parameters in the same request: "
|
||||
+ "Non-expression parameters: {Expected} Expression parameters: {ConditionExpression}", 400);
|
||||
}
|
||||
// Undefined tokens, then ConditionExpression's semantic errors, must both be caught
|
||||
// before the unused-EAN/EAV checks below.
|
||||
requireDefinedTokens(updateExpression, "UpdateExpression", 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 {
|
||||
ExpressionEvaluator.validateSemantics(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
|
||||
// Validate EAN/EAV usage across UpdateExpression + ConditionExpression
|
||||
if (updateExpression != null) {
|
||||
// Pre-validate syntax: expression must start with a valid clause keyword.
|
||||
// AWS tokenizes UpdateExpression whitespace-insensitively, so collapse runs
|
||||
// of whitespace (newlines, tabs, multiple spaces) before matching keywords.
|
||||
String ue = updateExpression.trim().replaceAll("\\s+", " ");
|
||||
if (!ue.isEmpty()) {
|
||||
String upper = ue.toUpperCase();
|
||||
if (!upper.startsWith("SET ") && !upper.startsWith("REMOVE ") && !upper.startsWith("ADD ") && !upper.startsWith("DELETE ")) {
|
||||
String[] parts = ue.split("\\s+", 3);
|
||||
String token = parts[0];
|
||||
String near = parts.length >= 2
|
||||
? (token + " " + parts[1]).substring(0, Math.min(token.length() + 1 + parts[1].length(), 20))
|
||||
: token;
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid UpdateExpression: Syntax error; token: \"" + token + "\", near: \"" + near + "\"", 400);
|
||||
}
|
||||
DynamoDbService.requireClosedParentheses(updateExpression, "1 validation error detected: ");
|
||||
}
|
||||
// Undefined tokens, then ConditionExpression's semantic errors, must both be caught
|
||||
// before the unused-EAN/EAV checks below.
|
||||
requireDefinedTokens(updateExpression, "UpdateExpression", exprAttrNames, exprAttrValues);
|
||||
ExpressionEvaluator.validateSemantics(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
requireNonEmptyExpression(updateExpression, "UpdateExpression");
|
||||
DynamoDbReservedWords.check(ue, "UpdateExpression");
|
||||
Set<String> hashTokens = extractHashTokens(updateExpression, conditionExpression);
|
||||
checkUnusedEan(exprAttrNames, hashTokens);
|
||||
Set<String> colonTokensAll = extractColonTokens(updateExpression, conditionExpression);
|
||||
checkUnusedEav(exprAttrValues, colonTokensAll);
|
||||
dynamoDbService.requireAddOrDeleteOperandTypes(updateExpression, exprAttrValues, true);
|
||||
} else {
|
||||
ExpressionEvaluator.validateSemantics(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
|
||||
}
|
||||
} catch (AwsException e) {
|
||||
throw inValidationEnvelope(e);
|
||||
}
|
||||
|
||||
Consumer<JsonNode> expectedCheck = legacyExpectedCheck(tableName, region, key, true, expectedUpd,
|
||||
|
||||
@@ -5179,7 +5179,7 @@ given()
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", containsString("ValidationException"))
|
||||
.body("message", equalTo("Invalid ConditionExpression: An expression attribute value "
|
||||
.body("message", equalTo("1 validation error detected: Invalid ConditionExpression: An expression attribute value "
|
||||
+ "used in expression is not defined; attribute value: :v"));
|
||||
|
||||
// UpdateItem: ConditionExpression references an undefined #name.
|
||||
@@ -5199,7 +5199,7 @@ given()
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", containsString("ValidationException"))
|
||||
.body("message", equalTo("Invalid ConditionExpression: An expression attribute name "
|
||||
.body("message", equalTo("1 validation error detected: 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.
|
||||
@@ -5219,7 +5219,7 @@ given()
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", containsString("ValidationException"))
|
||||
.body("message", equalTo("Invalid ConditionExpression: An expression attribute value "
|
||||
.body("message", equalTo("1 validation error detected: 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.
|
||||
@@ -5240,7 +5240,7 @@ given()
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", containsString("ValidationException"))
|
||||
.body("message", equalTo("Invalid ConditionExpression: The first operand must be distinct "
|
||||
.body("message", equalTo("1 validation error detected: Invalid ConditionExpression: The first operand must be distinct "
|
||||
+ "from the remaining operands for this operator or function; operator: contains, "
|
||||
+ "first operand: [sk]"));
|
||||
|
||||
@@ -5490,7 +5490,7 @@ given()
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", containsString("ValidationException"))
|
||||
.body("message", equalTo("Invalid UpdateExpression: An expression attribute value "
|
||||
.body("message", equalTo("1 validation error detected: Invalid UpdateExpression: An expression attribute value "
|
||||
+ "used in expression is not defined; attribute value: :d"));
|
||||
|
||||
// Previously this silently stored an attribute literally named "#n".
|
||||
@@ -5509,7 +5509,7 @@ given()
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", containsString("ValidationException"))
|
||||
.body("message", equalTo("Invalid UpdateExpression: An expression attribute name "
|
||||
.body("message", equalTo("1 validation error detected: 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.
|
||||
@@ -5527,7 +5527,7 @@ given()
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", containsString("ValidationException"))
|
||||
.body("message", equalTo("Invalid UpdateExpression: An expression attribute name "
|
||||
.body("message", equalTo("1 validation error detected: 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.
|
||||
@@ -5545,7 +5545,7 @@ given()
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", containsString("ValidationException"))
|
||||
.body("message", equalTo("Invalid UpdateExpression: An expression attribute value "
|
||||
.body("message", equalTo("1 validation error detected: 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).
|
||||
@@ -5866,6 +5866,35 @@ given()
|
||||
deleteTable("ConditionOrderTable");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
|
||||
"UpdateExpression": "SET q = :w", "ConditionExpression": "a = = b", "ExpressionAttributeValues": {":w": {"S": "w"}} | Invalid ConditionExpression: Syntax error; token: "=", near: "= = b"
|
||||
"UpdateExpression": "SET q = :w", "ConditionExpression": "((a = :w))", "ExpressionAttributeValues": {":w": {"S": "w"}} | Invalid ConditionExpression: The expression has redundant parentheses;
|
||||
"UpdateExpression": "SET q = :w", "ConditionExpression": "x < :v", "ExpressionAttributeValues": {":w": {"S": "w"}, ":v": {"BOOL": true}} | Invalid ConditionExpression: Incorrect operand type for operator or function; operator or function: <, operand type: BOOL
|
||||
"ConditionExpression": "contains(x, x)" | Invalid ConditionExpression: The first operand must be distinct from the remaining operands for this operator or function; operator: contains, first operand: [x]
|
||||
"UpdateExpression": "FOO q = :w", "ExpressionAttributeValues": {":w": {"S": "w"}} | Invalid UpdateExpression: Syntax error; token: "FOO", near: "FOO q"
|
||||
"UpdateExpression": "SET q = :w", "ExpressionAttributeValues": {":w": {"S": "w"}, ":z": {"S": "z"}} | Value provided in ExpressionAttributeValues unused in expressions: keys: {:z}
|
||||
"UpdateExpression": "" | Invalid UpdateExpression: The expression can not be empty;
|
||||
"UpdateExpression": "", "ConditionExpression": "a = :missing" | Invalid ConditionExpression: An expression attribute value used in expression is not defined; attribute value: :missing
|
||||
"UpdateExpression": "SET name = :w", "ExpressionAttributeValues": {":w": {"S": "w"}} | Invalid UpdateExpression: Attribute name is a reserved keyword; reserved word: name
|
||||
"UpdateExpression": "SET q = :w", "AttributeUpdates": {"q": {"Action": "PUT", "Value": {"S": "w"}}} | Can not use both expression and non-expression parameters in the same request: Non-expression parameters: {AttributeUpdates} Expression parameters: {UpdateExpression}
|
||||
""")
|
||||
void updateItemPutsExpressionErrorsInTheValidationEnvelope(String members, String message) {
|
||||
createOnDemandTableForGsiUpdates("UpdateEnvelopeTable");
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("{\"TableName\": \"UpdateEnvelopeTable\", \"Key\": {\"pk\": {\"S\": \"a\"}}, " + members + "}")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", containsString("ValidationException"))
|
||||
.body("message", equalTo("1 validation error detected: " + message));
|
||||
|
||||
deleteTable("UpdateEnvelopeTable");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
|
||||
{"Put": {"TableName": "EmptyNameTable", "Item": {"pk": {"S": "a"}, "": {"S": "x"}}}}
|
||||
|
||||
Reference in new issue
Block a user