mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(dynamodb): report the token and the text near a condition syntax error (#7360)
* fix(dynamodb): report the token and the text near a condition syntax error * fix(dynamodb): add braces to the digit token loop
This commit is contained in:
1 parent
d74a084a61
commit
b0721f9795
5 files changed
+153
-63
No files matched your search
+68
-61
@@ -42,15 +42,20 @@ final class ExpressionEvaluator {
|
||||
GT, // >
|
||||
GE, // >=
|
||||
// Punctuation
|
||||
LPAREN, RPAREN, COMMA, DOT,
|
||||
LIST_INDEX, // [n] — list element access
|
||||
LPAREN, RPAREN, COMMA, DOT, LBRACKET, RBRACKET,
|
||||
NUMBER, // list index digits
|
||||
// Functions
|
||||
FUNCTION, // attribute_exists, attribute_not_exists, begins_with, contains, size
|
||||
UNKNOWN, // any other character
|
||||
// End
|
||||
EOF
|
||||
}
|
||||
|
||||
record Token(TokenType type, String value, int pos) {}
|
||||
record Token(TokenType type, String value, int pos) {
|
||||
int end() {
|
||||
return pos + value.length();
|
||||
}
|
||||
}
|
||||
|
||||
// ── Tokenizer ──
|
||||
|
||||
@@ -62,8 +67,8 @@ final class ExpressionEvaluator {
|
||||
while (i < len) {
|
||||
char c = expression.charAt(i);
|
||||
|
||||
// Skip whitespace
|
||||
if (Character.isWhitespace(c)) {
|
||||
// Skip whitespace. DynamoDB skips only these characters.
|
||||
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
@@ -73,19 +78,15 @@ final class ExpressionEvaluator {
|
||||
if (c == ')') { tokens.add(new Token(TokenType.RPAREN, ")", i)); i++; continue; }
|
||||
if (c == ',') { tokens.add(new Token(TokenType.COMMA, ",", i)); i++; continue; }
|
||||
if (c == '.') { tokens.add(new Token(TokenType.DOT, ".", i)); i++; continue; }
|
||||
if (c == '[') { tokens.add(new Token(TokenType.LBRACKET, "[", i)); i++; continue; }
|
||||
if (c == ']') { tokens.add(new Token(TokenType.RBRACKET, "]", i)); i++; continue; }
|
||||
|
||||
// List index [n]
|
||||
if (c == '[') {
|
||||
if (isDigit(c)) {
|
||||
int start = i;
|
||||
i++; // skip '['
|
||||
while (i < len && Character.isDigit(expression.charAt(i))) i++;
|
||||
if (i < len && expression.charAt(i) == ']') {
|
||||
i++; // skip ']'
|
||||
tokens.add(new Token(TokenType.LIST_INDEX, expression.substring(start, i), start));
|
||||
} else {
|
||||
throw new IllegalArgumentException(
|
||||
"Expected ']' after list index at position " + i + " in: " + expression);
|
||||
while (i < len && isDigit(expression.charAt(i))) {
|
||||
i++;
|
||||
}
|
||||
tokens.add(new Token(TokenType.NUMBER, expression.substring(start, i), start));
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -111,7 +112,7 @@ final class ExpressionEvaluator {
|
||||
if (c == '=') { tokens.add(new Token(TokenType.EQ, "=", i)); i++; continue; }
|
||||
|
||||
// Value reference :name
|
||||
if (c == ':') {
|
||||
if (c == ':' && i + 1 < len && isNameChar(expression.charAt(i + 1))) {
|
||||
int start = i;
|
||||
i++; // skip ':'
|
||||
while (i < len && isNameChar(expression.charAt(i))) i++;
|
||||
@@ -120,7 +121,7 @@ final class ExpressionEvaluator {
|
||||
}
|
||||
|
||||
// Name reference #name
|
||||
if (c == '#') {
|
||||
if (c == '#' && i + 1 < len && isNameChar(expression.charAt(i + 1))) {
|
||||
int start = i;
|
||||
i++; // skip '#'
|
||||
while (i < len && isNameChar(expression.charAt(i))) i++;
|
||||
@@ -154,13 +155,9 @@ final class ExpressionEvaluator {
|
||||
continue;
|
||||
}
|
||||
|
||||
int nearStart = Math.max(0, i - 15);
|
||||
int nearEnd = Math.min(expression.length(), i + 15);
|
||||
|
||||
String near = expression.substring(nearStart, nearEnd);
|
||||
|
||||
throw new IllegalArgumentException(
|
||||
"token: \"%c\", near: \"%s\"".formatted(c, near));
|
||||
String unknown = Character.toString(expression.codePointAt(i));
|
||||
tokens.add(new Token(TokenType.UNKNOWN, unknown, i));
|
||||
i += unknown.length();
|
||||
}
|
||||
|
||||
tokens.add(new Token(TokenType.EOF, "", len));
|
||||
@@ -168,11 +165,15 @@ final class ExpressionEvaluator {
|
||||
}
|
||||
|
||||
private static boolean isNameStartChar(char c) {
|
||||
return Character.isLetter(c) || c == '_';
|
||||
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
|
||||
}
|
||||
|
||||
private static boolean isNameChar(char c) {
|
||||
return Character.isLetterOrDigit(c) || c == '_';
|
||||
return isNameStartChar(c) || isDigit(c) || c == '_';
|
||||
}
|
||||
|
||||
private static boolean isDigit(char c) {
|
||||
return '0' <= c && c <= '9';
|
||||
}
|
||||
|
||||
// ── AST nodes ──
|
||||
@@ -195,10 +196,12 @@ final class ExpressionEvaluator {
|
||||
// ── Parser ──
|
||||
|
||||
static final class Parser {
|
||||
private final String expression;
|
||||
private final List<Token> tokens;
|
||||
private int pos;
|
||||
|
||||
Parser(List<Token> tokens) {
|
||||
Parser(String expression, List<Token> tokens) {
|
||||
this.expression = expression;
|
||||
this.tokens = tokens;
|
||||
this.pos = 0;
|
||||
}
|
||||
@@ -207,12 +210,20 @@ final class ExpressionEvaluator {
|
||||
private Token advance() { return tokens.get(pos++); }
|
||||
|
||||
private Token expect(TokenType type) {
|
||||
Token t = advance();
|
||||
if (t.type() != type) {
|
||||
throw new IllegalArgumentException(
|
||||
"Expected %s but got %s ('%s') at position %d".formatted(type, t.type(), t.value(), t.pos()));
|
||||
if (peek().type() != type) {
|
||||
throw syntaxError();
|
||||
}
|
||||
return t;
|
||||
return advance();
|
||||
}
|
||||
|
||||
// DynamoDB quotes the text from the token before the failing one through the token after it.
|
||||
private IllegalArgumentException syntaxError() {
|
||||
Token token = peek();
|
||||
int from = pos > 0 ? tokens.get(pos - 1).pos() : 0;
|
||||
int to = pos + 1 < tokens.size() ? tokens.get(pos + 1).end() : expression.length();
|
||||
String text = token.type() == TokenType.EOF ? "<EOF>" : token.value();
|
||||
return new IllegalArgumentException(
|
||||
"token: \"%s\", near: \"%s\"".formatted(text, expression.substring(from, to)));
|
||||
}
|
||||
|
||||
Expr parseExpression() {
|
||||
@@ -301,9 +312,7 @@ final class ExpressionEvaluator {
|
||||
return new CompareExpr(left, op, right);
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException(
|
||||
"Expected comparator, IN, or BETWEEN after operand, but got %s ('%s') at position %d"
|
||||
.formatted(next.type(), next.value(), next.pos()));
|
||||
throw syntaxError();
|
||||
}
|
||||
|
||||
private Operand parseOperand() {
|
||||
@@ -328,8 +337,8 @@ final class ExpressionEvaluator {
|
||||
ArrayList<String> segments = new ArrayList<>();
|
||||
segments.add(advance().value());
|
||||
// Consume any immediately-following list indices (no dot needed before [n])
|
||||
while (peek().type() == TokenType.LIST_INDEX) {
|
||||
segments.add(advance().value());
|
||||
while (peek().type() == TokenType.LBRACKET) {
|
||||
segments.add(parseListIndex());
|
||||
}
|
||||
// Consume .name and .name[n] sequences
|
||||
while (peek().type() == TokenType.DOT) {
|
||||
@@ -337,20 +346,24 @@ final class ExpressionEvaluator {
|
||||
Token seg = peek();
|
||||
if (seg.type() == TokenType.IDENTIFIER || seg.type() == TokenType.NAME_REF) {
|
||||
segments.add(advance().value());
|
||||
while (peek().type() == TokenType.LIST_INDEX) {
|
||||
segments.add(advance().value());
|
||||
while (peek().type() == TokenType.LBRACKET) {
|
||||
segments.add(parseListIndex());
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException(
|
||||
"Expected identifier after '.' at position %d".formatted(seg.pos()));
|
||||
throw syntaxError();
|
||||
}
|
||||
}
|
||||
return new PathOperand(segments);
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException(
|
||||
"Expected operand but got %s ('%s') at position %d"
|
||||
.formatted(current.type(), current.value(), current.pos()));
|
||||
throw syntaxError();
|
||||
}
|
||||
|
||||
private String parseListIndex() {
|
||||
advance(); // consume [
|
||||
String index = expect(TokenType.NUMBER).value();
|
||||
expect(TokenType.RBRACKET);
|
||||
return "[" + index + "]";
|
||||
}
|
||||
|
||||
private List<Operand> parseOperandList() {
|
||||
@@ -374,13 +387,14 @@ final class ExpressionEvaluator {
|
||||
|
||||
static Expr parse(String expression) {
|
||||
if (expression == null || expression.isBlank()) return null;
|
||||
List<Token> tokens = tokenize(expression.trim());
|
||||
Parser parser = new Parser(tokens);
|
||||
return parse(expression, tokenize(expression));
|
||||
}
|
||||
|
||||
private static Expr parse(String expression, List<Token> tokens) {
|
||||
Parser parser = new Parser(expression, tokens);
|
||||
Expr expr = parser.parseExpression();
|
||||
if (parser.peek().type() != TokenType.EOF) {
|
||||
throw new IllegalArgumentException(
|
||||
"Unexpected token '%s' at position %d after parsing complete expression"
|
||||
.formatted(parser.peek().value(), parser.peek().pos()));
|
||||
throw parser.syntaxError();
|
||||
}
|
||||
return expr;
|
||||
}
|
||||
@@ -410,23 +424,16 @@ final class ExpressionEvaluator {
|
||||
* 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()) {
|
||||
if (expression == null || expression.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
List<Token> tokens = tokenize(expression.trim());
|
||||
List<Token> tokens = tokenize(expression);
|
||||
checkRedundantParentheses(tokens, exprType);
|
||||
parse(expression);
|
||||
parse(expression, tokens);
|
||||
} catch (IllegalArgumentException e) {
|
||||
String detail = e.getMessage();
|
||||
|
||||
if (detail.startsWith("token:")) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid " + exprType + ": Syntax error; " + detail, 400);
|
||||
} else {
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid " + exprType + ": Syntax error", 400);
|
||||
}
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid " + exprType + ": Syntax error; " + e.getMessage(), 400);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -198,13 +198,13 @@ class DynamoDbAccessPathValidatorTest {
|
||||
void rejectsMalformedKeyAndFilterExpressions() {
|
||||
AwsException keyError = assertThrows(AwsException.class,
|
||||
() -> validateExpression(tablePath, "pk ="));
|
||||
assertEquals("Invalid KeyConditionExpression: Syntax error", keyError.getMessage());
|
||||
assertEquals("Invalid KeyConditionExpression: Syntax error; token: \"<EOF>\", near: \"=\"", keyError.getMessage());
|
||||
|
||||
AwsException filterError = assertThrows(AwsException.class,
|
||||
() -> DynamoDbAccessPathValidator.validateQuery(
|
||||
table, tablePath, null, "pk = :pk", "status =", null, null,
|
||||
expressionValues(":pk")));
|
||||
assertEquals("Invalid FilterExpression: Syntax error", filterError.getMessage());
|
||||
assertEquals("Invalid FilterExpression: Syntax error; token: \"<EOF>\", near: \"=\"", filterError.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+36
@@ -11,6 +11,8 @@ import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
@@ -5807,6 +5809,40 @@ given()
|
||||
deleteTable(tableName);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
|
||||
Query | {"TableName": "SyntaxTable", "KeyConditionExpression": " ", "ExpressionAttributeValues": {":v": {"S": "x"}}} | Invalid KeyConditionExpression: Syntax error; token: "<EOF>", near: " "
|
||||
Scan | {"TableName": "SyntaxTable", "FilterExpression": "a = = b", "ExpressionAttributeValues": {":v": {"S": "x"}}} | Invalid FilterExpression: Syntax error; token: "=", near: "= = b"
|
||||
TransactWriteItems | {"TransactItems": [{"ConditionCheck": {"TableName": "SyntaxTable", "Key": {"pk": {"S": "a"}}, "ConditionExpression": "attribute_not_exists(", "ExpressionAttributeValues": {":v": {"S": "x"}}}}]} | Invalid ConditionExpression: Syntax error; token: "<EOF>", near: "("
|
||||
""")
|
||||
void conditionSyntaxErrorsNameTheTokenAndTheTextAroundIt(String operation, String body, String message) {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "SyntaxTable",
|
||||
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
|
||||
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
|
||||
"BillingMode": "PAY_PER_REQUEST"
|
||||
}
|
||||
""")
|
||||
.when().post("/")
|
||||
.then().statusCode(200);
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810." + operation)
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body(body)
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", containsString("ValidationException"))
|
||||
.body("message", equalTo(message));
|
||||
|
||||
deleteTable("SyntaxTable");
|
||||
}
|
||||
|
||||
private void deleteTable(String tableName) {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")
|
||||
|
||||
+45
@@ -6,6 +6,9 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -792,5 +795,47 @@ class ExpressionEvaluatorTest {
|
||||
assertEquals("ValidationException", e.getErrorCode());
|
||||
assertTrue(e.getMessage().contains("Invalid ConditionExpression: Syntax error"), e.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
|
||||
attribute_not_exists( | <EOF> | (
|
||||
a = = b | = | = = b
|
||||
` ) a` | ) | ` ) a`
|
||||
a = :v b | b | :v b
|
||||
a IN :v | :v | IN :v
|
||||
a BETWEEN :v OR :v | OR | :v OR :v
|
||||
a | <EOF> | a
|
||||
` ` | <EOF> | ` `
|
||||
`attribute_not_exists( ` | <EOF> | `( `
|
||||
a.b. = :v | = | . = :v
|
||||
a[x] = :v | x | [x]
|
||||
a[1 = :v | = | 1 = :v
|
||||
a = 1.5 | 1 | = 1.
|
||||
_a = :v | _ | _a
|
||||
a\u00e9 = :v | \u00e9 | a\u00e9 =
|
||||
a = : | : | = :
|
||||
a = :v \uD83D\uDE00 | \uD83D\uDE00 | :v \uD83D\uDE00
|
||||
`a\u3000= :v` | `\u3000` | `a\u3000=`
|
||||
a = :v AND $ = = b | $ | AND $ =
|
||||
""")
|
||||
void syntaxErrorsNameTheTokenAndTheTextAroundIt(String expression, String token, String near) {
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> ExpressionEvaluator.validateSyntax(expression, "ConditionExpression"));
|
||||
assertEquals("Invalid ConditionExpression: Syntax error; token: \"" + token + "\", near: \"" + near + "\"",
|
||||
e.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"a[ 1 ] = :v", "a [1] = :v", "a_b = :v"})
|
||||
void spacesInAListIndexAndUnderscoresInsideANameAreAccepted(String expression) {
|
||||
assertDoesNotThrow(() -> ExpressionEvaluator.validateSyntax(expression, "ConditionExpression"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void redundantParenthesesAreReportedBeforeAnUnknownCharacter() {
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> ExpressionEvaluator.validateSyntax("((a = :v)) $", "ConditionExpression"));
|
||||
assertEquals("Invalid ConditionExpression: The expression has redundant parentheses;", e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
@@ -2427,6 +2427,8 @@ class NativeDynamoDbJsonHandlerTest {
|
||||
[{"Put":{"TableName":"Missing","Item":{},"ConditionExpression":""}}] | Invalid ConditionExpression: The expression can not be empty;
|
||||
[{"Update":{"TableName":"Missing","Key":{},"UpdateExpression":"","ConditionExpression":""}}] | Invalid ConditionExpression: The expression can not be empty;
|
||||
[{"Update":{"TableName":"Missing","Key":{},"UpdateExpression":""}}] | Invalid UpdateExpression: The expression can not be empty;
|
||||
[{"Put":{"TableName":"Missing","Item":{},"ConditionExpression":" "}}] | Invalid ConditionExpression: Syntax error; token: "<EOF>", near: " "
|
||||
[{"Put":{"TableName":"Missing","Item":{}}},{"ConditionCheck":{"TableName":"Missing","Key":{},"ConditionExpression":"attribute_not_exists("}}] | Invalid ConditionExpression: Syntax error; token: "<EOF>", near: "("
|
||||
[{"Update":{"TableName":"Missing","Key":{},"UpdateExpression":"SET a = :zz","ConditionExpression":"a = :yy"}}] | Invalid ConditionExpression: An expression attribute value used in expression is not defined; attribute value: :yy
|
||||
[{"Update":{"TableName":"Missing","Key":{},"UpdateExpression":"SET a = :zz","ConditionExpression":"contains(a, a)"}}] | Invalid ConditionExpression: The first operand must be distinct from the remaining operands for this operator or function; operator: contains, first operand: [a]
|
||||
[{"Put":{"TableName":"Missing","Item":{},"ConditionExpression":"attribute_not_exists(a)","ExpressionAttributeNames":{"#n":"n"},"ExpressionAttributeValues":{":v":{"S":"x"}}}}] | Value provided in ExpressionAttributeNames unused in expressions: keys: {#n}
|
||||
|
||||
Reference in new issue
Block a user