mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(dynamodb): refuse a PartiQL read of a multi-attribute key index (#7790)
This commit is contained in:
1 parent
afb6608231
commit
2ceb668009
5 files changed
+108
-7
No files matched your search
@@ -77,6 +77,10 @@ record DynamoDbAccessPath(String indexName, Kind kind, List<KeySchemaElement> ke
|
||||
.collect(Collectors.toUnmodifiableSet());
|
||||
}
|
||||
|
||||
boolean hasMultiAttributeKey() {
|
||||
return 1 < partitionKeyNames().size() || 1 < sortKeyNames().size();
|
||||
}
|
||||
|
||||
boolean isIndex() {
|
||||
return kind != Kind.TABLE;
|
||||
}
|
||||
|
||||
@@ -44,6 +44,28 @@ class DynamoDbPartiQLHandler {
|
||||
|
||||
// --- SELECT ---
|
||||
|
||||
// DynamoDB refuses a read of an index whose key has more than one HASH or RANGE attribute before it
|
||||
// checks any term. The message names tables, but a read of the table itself runs.
|
||||
void requireReadableIndex(Stmt stmt, String region) {
|
||||
if (!(stmt instanceof Stmt.Select select) || select.index() == null) {
|
||||
return;
|
||||
}
|
||||
Optional<TableDefinition> table;
|
||||
try {
|
||||
table = service.findTableForItemCall(select.table(), region);
|
||||
} catch (AwsException ignored) {
|
||||
// The read reports a table name it cannot resolve once the terms pass.
|
||||
return;
|
||||
}
|
||||
boolean multiAttributeKey = table.filter(found -> found.findGsi(select.index()).isPresent())
|
||||
.map(found -> DynamoDbAccessPath.resolve(found, select.index()).hasMultiAttributeKey())
|
||||
.orElse(false);
|
||||
if (multiAttributeKey) {
|
||||
throw new AwsException("ValidationException", "PartiQL statements cannot be executed on tables with "
|
||||
+ "multi-attribute HASH keys or multi-attribute RANGE keys.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private Result executeSelect(Stmt.Select stmt, PartiQLExecuteContext ctx, String region) {
|
||||
TableDefinition table = service.requireTableForItemCall(stmt.table(), region);
|
||||
// A vector index is reachable through SearchVectors alone, and PartiQL says so
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.math.BigInteger;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -501,15 +502,21 @@ public class DynamoDbPartiQLParser {
|
||||
}
|
||||
|
||||
static Stmt parse(String statement, List<JsonNode> parameters, boolean transaction) {
|
||||
return parse(statement, parameters, transaction, stmt -> { });
|
||||
}
|
||||
|
||||
// targetCheck runs once the FROM clause checks pass, before any term check.
|
||||
static Stmt parse(String statement, List<JsonNode> parameters, boolean transaction, Consumer<Stmt> targetCheck) {
|
||||
try {
|
||||
return parseStatement(statement, parameters, transaction);
|
||||
return parseStatement(statement, parameters, transaction, targetCheck);
|
||||
} catch (StackOverflowError expected) {
|
||||
// DynamoDB answers a statement nested too deep for its parser the same way, with no message.
|
||||
throw new AwsException("InternalFailure", null, 500);
|
||||
}
|
||||
}
|
||||
|
||||
private static Stmt parseStatement(String statement, List<JsonNode> parameters, boolean transaction) {
|
||||
private static Stmt parseStatement(String statement, List<JsonNode> parameters, boolean transaction,
|
||||
Consumer<Stmt> targetCheck) {
|
||||
parameters.forEach(DynamoDbAttributeValueValidator::validate);
|
||||
DynamoDbPartiQLParser parser = new DynamoDbPartiQLParser(statement, parameters);
|
||||
Stmt stmt = parser.parseStmt(transaction);
|
||||
@@ -530,6 +537,7 @@ public class DynamoDbPartiQLParser {
|
||||
if (parser.fromClauseError != null) {
|
||||
throw validationEx(parser.fromClauseError);
|
||||
}
|
||||
targetCheck.accept(stmt);
|
||||
if (parser.deferredError != null) {
|
||||
throw validationEx(parser.deferredError);
|
||||
}
|
||||
|
||||
@@ -4168,7 +4168,7 @@ public class NativeDynamoDbJsonHandler {
|
||||
throwValidationErrors(validationErrors);
|
||||
String statement = request.path("Statement").asText();
|
||||
List<JsonNode> parameters = toPartiQLParams(request.path("Parameters"));
|
||||
DynamoDbPartiQLParser.Stmt stmt = DynamoDbPartiQLParser.parse(statement, parameters);
|
||||
DynamoDbPartiQLParser.Stmt stmt = parsePartiQLStatement(statement, parameters, region);
|
||||
PartiQLExecuteContext ctx = PartiQLExecuteContext.builder()
|
||||
.limit(request.has("Limit") ? request.get("Limit").asInt() : null)
|
||||
.nextToken(request.has("NextToken") ? request.get("NextToken").asText() : null)
|
||||
@@ -4338,7 +4338,7 @@ public class NativeDynamoDbJsonHandler {
|
||||
ObjectNode slot = objectMapper.createObjectNode();
|
||||
String tableName = null;
|
||||
try {
|
||||
DynamoDbPartiQLParser.Stmt stmt = parsePartiQLStatement(statement);
|
||||
DynamoDbPartiQLParser.Stmt stmt = parsePartiQLStatement(statement, region);
|
||||
tableName = stmt.table();
|
||||
if (stmt instanceof DynamoDbPartiQLParser.Stmt.Select select) {
|
||||
requireBatchSelectReadsByKey(select, region);
|
||||
@@ -4403,9 +4403,15 @@ public class NativeDynamoDbJsonHandler {
|
||||
"Select statements within BatchExecuteStatement must specify the primary key in the where clause.", 400);
|
||||
}
|
||||
|
||||
private DynamoDbPartiQLParser.Stmt parsePartiQLStatement(JsonNode statement) {
|
||||
return DynamoDbPartiQLParser.parse(statement.path("Statement").asText(),
|
||||
toPartiQLParams(statement.path("Parameters")));
|
||||
private DynamoDbPartiQLParser.Stmt parsePartiQLStatement(JsonNode statement, String region) {
|
||||
return parsePartiQLStatement(statement.path("Statement").asText(),
|
||||
toPartiQLParams(statement.path("Parameters")), region);
|
||||
}
|
||||
|
||||
private DynamoDbPartiQLParser.Stmt parsePartiQLStatement(String statement, List<JsonNode> parameters,
|
||||
String region) {
|
||||
return DynamoDbPartiQLParser.parse(statement, parameters, false,
|
||||
parsed -> partiQLHandler.requireReadableIndex(parsed, region));
|
||||
}
|
||||
|
||||
private List<JsonNode> toPartiQLParams(JsonNode node) {
|
||||
|
||||
+61
@@ -789,6 +789,67 @@ class DynamoDbPartiQLEdgeCaseIntegrationTest {
|
||||
.body("message", equalTo(overlapping));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(33)
|
||||
void refusesAReadOfAMultiAttributeIndexBeforeItsTerms() {
|
||||
request("DynamoDB_20120810.CreateTable", """
|
||||
{
|
||||
"TableName": "partiql-edge-cases-multi",
|
||||
"AttributeDefinitions": [
|
||||
{"AttributeName":"pk","AttributeType":"S"},
|
||||
{"AttributeName":"h1","AttributeType":"S"},
|
||||
{"AttributeName":"h2","AttributeType":"S"},
|
||||
{"AttributeName":"r1","AttributeType":"S"}
|
||||
],
|
||||
"KeySchema": [{"AttributeName":"pk","KeyType":"HASH"}],
|
||||
"GlobalSecondaryIndexes": [{
|
||||
"IndexName": "multi",
|
||||
"KeySchema": [{"AttributeName":"h1","KeyType":"HASH"},{"AttributeName":"h2","KeyType":"HASH"},
|
||||
{"AttributeName":"r1","KeyType":"RANGE"}],
|
||||
"Projection": {"ProjectionType":"ALL"}
|
||||
}, {
|
||||
"IndexName": "single",
|
||||
"KeySchema": [{"AttributeName":"h1","KeyType":"HASH"},{"AttributeName":"r1","KeyType":"RANGE"}],
|
||||
"Projection": {"ProjectionType":"ALL"}
|
||||
}],
|
||||
"BillingMode": "PAY_PER_REQUEST"
|
||||
}
|
||||
""")
|
||||
.statusCode(200);
|
||||
request("DynamoDB_20120810.PutItem", """
|
||||
{"TableName":"partiql-edge-cases-multi",
|
||||
"Item":{"pk":{"S":"p"},"h1":{"S":"a"},"h2":{"S":"x"},"r1":{"S":"m"}}}
|
||||
""")
|
||||
.statusCode(200);
|
||||
String table = "\"partiql-edge-cases-multi\"";
|
||||
String refused = "PartiQL statements cannot be executed on tables with multi-attribute HASH keys"
|
||||
+ " or multi-attribute RANGE keys.";
|
||||
|
||||
statement("SELECT * FROM " + table + ".\"multi\" WHERE foo(1) AND h1 = <<>>")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(refused));
|
||||
statement("SELECT * FROM " + table + ".\"multi\" WHERE h1 = 1e200")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(refused));
|
||||
statement("SELECT * FROM " + table + ".\"multi\" AS x WHERE h1 = 'a'")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo("Aliasing is not supported"));
|
||||
statement("SELECT pk FROM " + table + ".\"single\" WHERE h1 = 'a'")
|
||||
.statusCode(200)
|
||||
.body("Items[0].pk.S", equalTo("p"));
|
||||
request("DynamoDB_20120810.BatchExecuteStatement", "{\"Statements\":[{\"Statement\":"
|
||||
+ json("SELECT * FROM " + table + ".\"multi\" WHERE h1 = 'a' AND h2 = 'x' AND r1 = 'm'")
|
||||
+ "},{\"Statement\":" + json("SELECT * FROM " + table + " WHERE pk = 'p'") + "}]}")
|
||||
.statusCode(200)
|
||||
.body("Responses[0].Error.Code", equalTo("ValidationError"))
|
||||
.body("Responses[0].Error.Message", equalTo(refused))
|
||||
.body("Responses[1].Item.pk.S", equalTo("p"));
|
||||
transaction("SELECT * FROM " + table + ".\"multi\" WHERE pk = 'p'")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo("Validation failed in TransactStatements[0]: "
|
||||
+ "Reads on indices are not supported within transactions."));
|
||||
}
|
||||
|
||||
private static ValidatableResponse getItem(String pk) {
|
||||
return request("DynamoDB_20120810.GetItem", """
|
||||
{"TableName":"%s","Key":{"pk":{"S":"%s"},"sk":{"S":"1"}},"ConsistentRead":true}
|
||||
|
||||
Reference in new issue
Block a user