fix(dynamodb): read a PartiQL function call statement before refusing it (#7779)

This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-09 02:05:30 -04:00
1 parent 21b8e9346b
commit 9ba53ec87d
3 files changed
+81 -7

No files matched your search

@@ -506,7 +506,7 @@ public class DynamoDbPartiQLParser {
private static Stmt parseStatement(String statement, List<JsonNode> parameters, boolean transaction) {
parameters.forEach(DynamoDbAttributeValueValidator::validate);
DynamoDbPartiQLParser parser = new DynamoDbPartiQLParser(statement, parameters);
Stmt stmt = parser.parseStmt();
Stmt stmt = parser.parseStmt(transaction);
if (parser.peek().type() == TType.SEMICOLON) {
parser.advance();
}
@@ -515,7 +515,7 @@ public class DynamoDbPartiQLParser {
}
// Outside a transaction DynamoDB refuses EXISTS before it checks anything inside it.
if (stmt instanceof Stmt.Exists && !transaction) {
throw validationEx("EXISTS can only be used in ExecuteTransaction write requests.");
throw validationEx(EXISTS_OUTSIDE_TRANSACTION);
}
if (parser.fromClauseError != null) {
throw validationEx(parser.fromClauseError);
@@ -552,11 +552,13 @@ public class DynamoDbPartiQLParser {
return !tokens.isEmpty() && OPERAND_ENDS.contains(tokens.getLast().type());
}
private Stmt parseStmt() {
if (peekFunction("exists")) {
private static final String EXISTS_OUTSIDE_TRANSACTION =
"EXISTS can only be used in ExecuteTransaction write requests.";
private Stmt parseStmt(boolean transaction) {
if (peekFunction("exists") && tokens.get(pos + 2).type() == TType.SELECT) {
advance();
consume(TType.LPAREN);
requireTermStart();
Stmt.Select select = parseSelect();
consume(TType.RPAREN);
return new Stmt.Exists(select);
@@ -569,13 +571,13 @@ public class DynamoDbPartiQLParser {
case DELETE -> parseDelete();
default -> {
requireTermStart();
throw validationEx(expressionStatementRefusal());
throw validationEx(expressionStatementRefusal(transaction));
}
};
}
// DynamoDB reads any other statement as one expression and refuses it by the kind of that expression.
private String expressionStatementRefusal() {
private String expressionStatementRefusal(boolean transaction) {
String kind = parseTermKind();
if (kind == null || continuesExpression(peek())) {
return NOT_WELL_FORMED + "Expected data manipulation";
@@ -586,12 +588,18 @@ public class DynamoDbPartiQLParser {
if (peek().type() != TType.EOF) {
return UNEXPECTED_TOKEN;
}
// Outside a transaction DynamoDB answers any call as it answers EXISTS.
if (CALL.equals(kind)) {
return transaction ? "Unsupported operation in transaction." : EXISTS_OUTSIDE_TRANSACTION;
}
return OPERATOR.equals(kind) ? NOT_WELL_FORMED + "Expected data manipulation" : "Unsupported operation: " + kind;
}
// A sign on anything but a number, or IS NOT. DynamoDB refuses it with a message of its own.
private static final String OPERATOR = "Operator";
private static final String CALL = "Call";
// A sign folds into the number it applies to. It binds tighter than IS and looser than a path step.
private String parseTermKind() {
Deque<Boolean> signedGroups = new ArrayDeque<>();
@@ -675,6 +683,11 @@ public class DynamoDbPartiQLParser {
};
}
case IDENT -> {
if (peekCall()) {
advance();
parseCallArguments(t, false);
yield AGGREGATE_FUNCTIONS.contains(t.value().toLowerCase(Locale.ROOT)) ? "CallAgg" : CALL;
}
if (isBareWord(t) && KEYWORDS.contains(t.value().toLowerCase(Locale.ROOT))
|| tokens.get(pos + 1).type() == TType.LPAREN) {
yield null;
@@ -858,6 +858,28 @@ class DynamoDbPartiQLIntegrationTest {
+ "Number of parameters in request and statement don't match."));
}
@Test
@Order(40)
void aCallStatementIsRefusedBeforeItsParametersAreCounted() {
String outside = "EXISTS can only be used in ExecuteTransaction write requests.";
statement("foo(?)")
.statusCode(400)
.body("__type", equalTo("ValidationException"))
.body("message", equalTo(outside));
request("DynamoDB_20120810.BatchExecuteStatement", """
{"Statements":[{"Statement":"foo(1)"}]}
""")
.statusCode(200)
.body("Responses[0].Error.Code", equalTo("ValidationError"))
.body("Responses[0].Error.Message", equalTo(outside));
request("DynamoDB_20120810.ExecuteTransaction", """
{"TransactStatements":[{"Statement":"foo(?)"}]}
""")
.statusCode(400)
.body("message", equalTo("Validation failed in TransactStatements[0]: Unsupported operation in transaction."));
}
private static void putItem(String pk, String attributes) {
request("DynamoDB_20120810.PutItem", """
{"TableName":"%s","Item":{"pk":{"S":"%s"},%s}}
@@ -968,6 +968,45 @@ class DynamoDbPartiQLParserTest {
assertEquals(expand.apply(inTransaction), statementRefusal(statement, parameters, true));
}
@ParameterizedTest
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
foo(1) | CALL
foo() | CALL
"foo"(1) | CALL
FOO (1) | CALL
foo(?) | CALL
foo(<<>>) | CALL
foo(1e999999) | CALL
foo(foo(1), a.b[0]) | CALL
size(1) | CALL
attribute_exists(a) | CALL
upper('a') | CALL
EXISTS() | CALL
EXISTS(1, 2) | CALL
exists(foo(1)) | CALL
((foo(1))); | CALL
count(1) | Unsupported operation: CallAgg
count(*) | Unsupported operation: CallAgg
count(DISTINCT a) | Unsupported operation: CallAgg
foo(1).a | Unsupported operation: Path
foo(1)[0] | Unsupported operation: Path
foo(1) IS MISSING | Unsupported operation: IsType
foo(1)(2) | NWF Unexpected token after expression
foo(1 2) | NWF Expected RIGHT_PAREN
foo(1,) | NWF Unexpected term
foo(a, *) | NWF Unexpected term
foo(*) | NWF foo(*) is not allowed
count() | NWF Aggregate functions are always unary
""")
void refusesACallStatementOnceItParses(String statement, String message) {
String expected = message.replace("NWF ", "Statement wasn't well formed, can't be processed: ");
assertEquals(expected.replace("CALL", "EXISTS can only be used in ExecuteTransaction write requests."),
statementRefusal(statement, 0));
assertEquals(expected.replace("CALL", "Unsupported operation in transaction."),
statementRefusal(statement, 0, true));
}
@Test
void countsInOperandsWithTheOperandChecks() {
String tooMany = "The IN operator is provided with too many operands; number of operands: 101";