mirror of
https://github.com/floci-io/floci.git
synced 2026-12-04 02:28:08 -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) {
|
||||
|
||||
Reference in new issue
Block a user