mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(dynamodb): validate cursor key types (#2470)
* fix(dynamodb): validate cursor key types Closes #2468 * fix(dynamodb): reject malformed cursors
This commit is contained in:
1 parent
564dbf204f
commit
e3df59ae1f
3 files changed
+170
-15
No files matched your search
+51
-15
@@ -718,31 +718,67 @@ public class DynamoDbJsonHandler {
|
||||
return changedAttributes;
|
||||
}
|
||||
|
||||
// DynamoDB rejects an ExclusiveStartKey whose attribute set does not exactly match the
|
||||
// key schema being paged: the table's primary key, plus the index's key attributes when
|
||||
// an IndexName is supplied. Query and Scan report different messages for the same fault.
|
||||
// DynamoDB requires an ExclusiveStartKey to exactly match the paged key schema's
|
||||
// attribute names and scalar types. Query and Scan report different messages for faults.
|
||||
private void validateExclusiveStartKey(JsonNode exclusiveStartKey, TableDefinition table,
|
||||
String indexName, boolean isScan) {
|
||||
DynamoDbAccessPath accessPath, boolean isScan) {
|
||||
if (exclusiveStartKey == null || exclusiveStartKey.isNull()) return;
|
||||
Set<String> expected = new HashSet<>();
|
||||
expected.add(table.getPartitionKeyName());
|
||||
String sortKey = table.getSortKeyName();
|
||||
if (sortKey != null) expected.add(sortKey);
|
||||
if (indexName != null) {
|
||||
table.findGsi(indexName).ifPresent(g ->
|
||||
g.getKeySchema().forEach(k -> expected.add(k.getAttributeName())));
|
||||
table.findLsi(indexName).ifPresent(l ->
|
||||
l.getKeySchema().forEach(k -> expected.add(k.getAttributeName())));
|
||||
if (accessPath.isIndex()) {
|
||||
expected.addAll(accessPath.keyAttributeNames());
|
||||
}
|
||||
Set<String> actual = new HashSet<>();
|
||||
exclusiveStartKey.fieldNames().forEachRemaining(actual::add);
|
||||
if (!actual.equals(expected)) {
|
||||
throw new AwsException("ValidationException", isScan
|
||||
? "The provided starting key is invalid: The provided key element does not match the schema"
|
||||
: "The provided starting key is invalid", 400);
|
||||
if (!actual.equals(expected) || hasInvalidKeyValue(exclusiveStartKey, table, expected)) {
|
||||
throw invalidStartingKey(isScan);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasInvalidKeyValue(JsonNode exclusiveStartKey, TableDefinition table,
|
||||
Set<String> expected) {
|
||||
if (table.getAttributeDefinitions() == null) {
|
||||
return true;
|
||||
}
|
||||
for (String attribute : expected) {
|
||||
String expectedType = table.getAttributeDefinitions().stream()
|
||||
.filter(definition -> attribute.equals(definition.getAttributeName()))
|
||||
.map(AttributeDefinition::getAttributeType)
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if (expectedType == null || hasInvalidScalarValue(exclusiveStartKey.get(attribute), expectedType)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean hasInvalidScalarValue(JsonNode value, String expectedType) {
|
||||
if (value == null || !value.isObject() || value.size() != 1 || !value.has(expectedType)) {
|
||||
return true;
|
||||
}
|
||||
JsonNode payload = value.get(expectedType);
|
||||
if (payload == null || !payload.isTextual() || payload.textValue().isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
if ("N".equals(expectedType)) {
|
||||
DynamoDbNumberUtils.validateAndNormalize(payload.textValue());
|
||||
}
|
||||
if ("B".equals(expectedType)) {
|
||||
return !payload.textValue().matches(
|
||||
"(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private AwsException invalidStartingKey(boolean isScan) {
|
||||
return new AwsException("ValidationException", isScan
|
||||
? "The provided starting key is invalid: The provided key element does not match the schema"
|
||||
: "The provided starting key is invalid", 400);
|
||||
}
|
||||
|
||||
private static final Set<String> VALID_SELECT = Set.of(
|
||||
"ALL_ATTRIBUTES", "ALL_PROJECTED_ATTRIBUTES", "SPECIFIC_ATTRIBUTES", "COUNT");
|
||||
|
||||
@@ -861,7 +897,7 @@ public class DynamoDbJsonHandler {
|
||||
}
|
||||
|
||||
if (exclusiveStartKey != null) {
|
||||
validateExclusiveStartKey(exclusiveStartKey, queryTable, indexName, false);
|
||||
validateExclusiveStartKey(exclusiveStartKey, queryTable, queryAccessPath, false);
|
||||
}
|
||||
|
||||
DynamoDbService.QueryResult result = dynamoDbService.query(tableName, keyConditions,
|
||||
@@ -983,7 +1019,7 @@ public class DynamoDbJsonHandler {
|
||||
projectionExpressionScan, attributesToGetScan, exprAttrNames);
|
||||
|
||||
if (exclusiveStartKey != null) {
|
||||
validateExclusiveStartKey(exclusiveStartKey, scanTable, indexNameScan, true);
|
||||
validateExclusiveStartKey(exclusiveStartKey, scanTable, scanAccessPath, true);
|
||||
}
|
||||
|
||||
DynamoDbService.ScanResult result = dynamoDbService.scan(
|
||||
|
||||
Reference in new issue
Block a user