mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): validate query key types (#2469)
* fix(dynamodb): validate query key types Closes #2467 * fix(dynamodb): harden query validation * fix(dynamodb): validate query value payloads
This commit is contained in:
1 parent
e3df59ae1f
commit
a57c6c8112
7 files changed
+276
-27
No files matched your search
+42
-3
@@ -142,6 +142,45 @@ class DynamoDbAccessPathIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void queryRejectsKeyConditionValuesWithWrongSchemaTypes() {
|
||||
request("DynamoDB_20120810.Query", """
|
||||
{
|
||||
"TableName":"%s",
|
||||
"KeyConditionExpression":"pk = :pk AND sk > :sk",
|
||||
"ExpressionAttributeValues":{
|
||||
":pk":{"S":"p1"},
|
||||
":sk":{"N":"1"}
|
||||
}
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"))
|
||||
.body("message", equalTo("One or more parameter values were invalid: "
|
||||
+ "Condition parameter type does not match schema type"));
|
||||
|
||||
request("DynamoDB_20120810.Query", """
|
||||
{
|
||||
"TableName":"%s",
|
||||
"KeyConditionExpression":"pk = :pk",
|
||||
"ExpressionAttributeValues":{":pk":{"S":null}}
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"));
|
||||
|
||||
request("DynamoDB_20120810.Query", """
|
||||
{
|
||||
"TableName":"%s",
|
||||
"KeyConditionExpression":"pk = :pk",
|
||||
"ExpressionAttributeValues":{":pk":{"S":"p1","N":"1"}}
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void queryRejectsSelectedKeyInFilterExpression() {
|
||||
request("DynamoDB_20120810.Query", """
|
||||
{
|
||||
@@ -163,7 +202,7 @@ class DynamoDbAccessPathIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(7)
|
||||
void queryAndScanRejectNonProjectedGsiAttribute() {
|
||||
String query = """
|
||||
{
|
||||
@@ -191,7 +230,7 @@ class DynamoDbAccessPathIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
@Order(8)
|
||||
void acceptsProjectedGsiAttributesAndLsiTableFetch() {
|
||||
request("DynamoDB_20120810.Query", """
|
||||
{
|
||||
@@ -230,7 +269,7 @@ class DynamoDbAccessPathIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
@Order(9)
|
||||
void gsiStillRejectsConsistentReads() {
|
||||
request("DynamoDB_20120810.Query", """
|
||||
{
|
||||
|
||||
+112
-14
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.AttributeDefinition;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.GlobalSecondaryIndex;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.LocalSecondaryIndex;
|
||||
@@ -32,6 +33,14 @@ class DynamoDbAccessPathValidatorTest {
|
||||
table.setKeySchema(List.of(
|
||||
new KeySchemaElement("pk", "HASH"),
|
||||
new KeySchemaElement("sk", "RANGE")));
|
||||
table.setAttributeDefinitions(List.of(
|
||||
new AttributeDefinition("pk", "S"),
|
||||
new AttributeDefinition("sk", "S"),
|
||||
new AttributeDefinition("status", "S"),
|
||||
new AttributeDefinition("createdAt", "S"),
|
||||
new AttributeDefinition("alternate", "S"),
|
||||
new AttributeDefinition("tenantId", "S"),
|
||||
new AttributeDefinition("region", "S")));
|
||||
table.setGlobalSecondaryIndexes(List.of(
|
||||
new GlobalSecondaryIndex(
|
||||
"status-index",
|
||||
@@ -84,13 +93,13 @@ class DynamoDbAccessPathValidatorTest {
|
||||
valid.set("tenantId", legacyCondition("EQ", attributeValues("acme")));
|
||||
valid.set("region", legacyCondition("EQ", attributeValues("us")));
|
||||
assertDoesNotThrow(() -> DynamoDbAccessPathValidator.validateQuery(
|
||||
compositePkGsiPath, valid, null, null, null, null));
|
||||
table, compositePkGsiPath, valid, null, null, null, null, null));
|
||||
|
||||
ObjectNode missingRegion = mapper.createObjectNode();
|
||||
missingRegion.set("tenantId", legacyCondition("EQ", attributeValues("acme")));
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> DynamoDbAccessPathValidator.validateQuery(
|
||||
compositePkGsiPath, missingRegion, null, null, null, null));
|
||||
table, compositePkGsiPath, missingRegion, null, null, null, null, null));
|
||||
assertEquals("Query condition missed key schema element: region", error.getMessage());
|
||||
}
|
||||
|
||||
@@ -117,8 +126,8 @@ class DynamoDbAccessPathValidatorTest {
|
||||
names.put("#status", "status");
|
||||
|
||||
assertEquals(":status", DynamoDbAccessPathValidator.validateQuery(
|
||||
gsiPath, null, "#status = :status AND createdAt BETWEEN :start AND :end",
|
||||
null, null, names));
|
||||
table, gsiPath, null, "#status = :status AND createdAt BETWEEN :start AND :end",
|
||||
null, null, names, expressionValues(":status", ":start", ":end")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -161,7 +170,8 @@ class DynamoDbAccessPathValidatorTest {
|
||||
|
||||
AwsException filterError = assertThrows(AwsException.class,
|
||||
() -> DynamoDbAccessPathValidator.validateQuery(
|
||||
tablePath, null, "pk = :pk", "status =", null, null));
|
||||
table, tablePath, null, "pk = :pk", "status =", null, null,
|
||||
expressionValues(":pk")));
|
||||
assertEquals("Invalid FilterExpression: Syntax error", filterError.getMessage());
|
||||
}
|
||||
|
||||
@@ -171,28 +181,92 @@ class DynamoDbAccessPathValidatorTest {
|
||||
valid.set("pk", legacyCondition("EQ", attributeValues("p1")));
|
||||
valid.set("sk", legacyCondition("BETWEEN", attributeValues("a", "z")));
|
||||
assertDoesNotThrow(() -> DynamoDbAccessPathValidator.validateQuery(
|
||||
tablePath, valid, null, null, null, null));
|
||||
table, tablePath, valid, null, null, null, null, null));
|
||||
|
||||
ObjectNode nonKey = valid.deepCopy();
|
||||
nonKey.set("status", legacyCondition("EQ", attributeValues("open")));
|
||||
assertThrows(AwsException.class, () -> DynamoDbAccessPathValidator.validateQuery(
|
||||
tablePath, nonKey, null, null, null, null));
|
||||
table, tablePath, nonKey, null, null, null, null, null));
|
||||
|
||||
ObjectNode missingPartitionKey = mapper.createObjectNode();
|
||||
missingPartitionKey.set("sk", legacyCondition("EQ", attributeValues("a")));
|
||||
assertThrows(AwsException.class, () -> DynamoDbAccessPathValidator.validateQuery(
|
||||
tablePath, missingPartitionKey, null, null, null, null));
|
||||
table, tablePath, missingPartitionKey, null, null, null, null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsSelectedKeysInQueryFilters() {
|
||||
assertThrows(AwsException.class, () -> DynamoDbAccessPathValidator.validateQuery(
|
||||
gsiPath, null, "status = :status", "createdAt > :cutoff", null, null));
|
||||
table, gsiPath, null, "status = :status", "createdAt > :cutoff", null, null,
|
||||
expressionValues(":status", ":cutoff")));
|
||||
|
||||
ObjectNode queryFilter = mapper.createObjectNode();
|
||||
queryFilter.set("status", legacyCondition("EQ", attributeValues("open")));
|
||||
assertThrows(AwsException.class, () -> DynamoDbAccessPathValidator.validateQuery(
|
||||
gsiPath, null, "status = :status", null, queryFilter, null));
|
||||
table, gsiPath, null, "status = :status", null, queryFilter, null,
|
||||
expressionValues(":status")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsKeyConditionValuesWithWrongSchemaTypes() {
|
||||
ObjectNode values = expressionValues(":pk", ":sk");
|
||||
values.set(":pk", numberValue("1"));
|
||||
|
||||
AwsException expressionError = assertThrows(AwsException.class,
|
||||
() -> DynamoDbAccessPathValidator.validateQuery(
|
||||
table, tablePath, null, "pk = :pk AND sk > :sk",
|
||||
null, null, null, values));
|
||||
assertEquals("One or more parameter values were invalid: "
|
||||
+ "Condition parameter type does not match schema type", expressionError.getMessage());
|
||||
|
||||
ObjectNode legacy = mapper.createObjectNode();
|
||||
legacy.set("pk", legacyCondition("EQ", attributeValues("p1")));
|
||||
legacy.set("sk", legacyCondition("EQ", mapper.createArrayNode().add(numberValue("1"))));
|
||||
assertThrows(AwsException.class, () -> DynamoDbAccessPathValidator.validateQuery(
|
||||
table, tablePath, legacy, null, null, null, null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsMalformedKeyConditionValues() {
|
||||
ObjectNode nullString = mapper.createObjectNode();
|
||||
nullString.putNull("S");
|
||||
ObjectNode values = expressionValues(":pk");
|
||||
values.set(":pk", nullString);
|
||||
|
||||
assertThrows(AwsException.class, () -> DynamoDbAccessPathValidator.validateQuery(
|
||||
table, tablePath, null, "pk = :pk", null, null, null, values));
|
||||
|
||||
ObjectNode multipleTypes = stringValue("p1");
|
||||
multipleTypes.put("N", "1");
|
||||
values.set(":pk", multipleTypes);
|
||||
|
||||
assertThrows(AwsException.class, () -> DynamoDbAccessPathValidator.validateQuery(
|
||||
table, tablePath, null, "pk = :pk", null, null, null, values));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsMissingKeyConditionExpressionValue() {
|
||||
ObjectNode values = expressionValues(":pk");
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> DynamoDbAccessPathValidator.validateQuery(
|
||||
table, tablePath, null, "pk = :pk AND sk > :sk",
|
||||
null, null, null, values));
|
||||
|
||||
assertEquals("Invalid KeyConditionExpression: An expression attribute value used in expression "
|
||||
+ "is not defined; attribute value: :sk", error.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsMissingAttributeDefinition() {
|
||||
table.setAttributeDefinitions(null);
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> validateExpression(tablePath, "pk = :pk"));
|
||||
|
||||
assertEquals("ValidationException", error.getErrorCode());
|
||||
assertEquals("One or more parameter values were invalid: "
|
||||
+ "Condition parameter type does not match schema type", error.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -225,7 +299,25 @@ class DynamoDbAccessPathValidatorTest {
|
||||
|
||||
private void validateExpression(DynamoDbAccessPath accessPath, String expression) {
|
||||
DynamoDbAccessPathValidator.validateQuery(
|
||||
accessPath, null, expression, null, null, null);
|
||||
table, accessPath, null, expression, null, null, null,
|
||||
expressionValues(extractPlaceholders(expression)));
|
||||
}
|
||||
|
||||
private ObjectNode expressionValues(String... placeholders) {
|
||||
ObjectNode values = mapper.createObjectNode();
|
||||
for (String placeholder : placeholders) {
|
||||
values.set(placeholder, stringValue(placeholder.substring(1)));
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
private String[] extractPlaceholders(String expression) {
|
||||
return java.util.regex.Pattern.compile(":\\w+")
|
||||
.matcher(expression)
|
||||
.results()
|
||||
.map(java.util.regex.MatchResult::group)
|
||||
.distinct()
|
||||
.toArray(String[]::new);
|
||||
}
|
||||
|
||||
private ObjectNode legacyCondition(String operator, ArrayNode values) {
|
||||
@@ -238,10 +330,16 @@ class DynamoDbAccessPathValidatorTest {
|
||||
private ArrayNode attributeValues(String... values) {
|
||||
ArrayNode result = mapper.createArrayNode();
|
||||
for (String value : values) {
|
||||
ObjectNode attributeValue = mapper.createObjectNode();
|
||||
attributeValue.put("S", value);
|
||||
result.add(attributeValue);
|
||||
result.add(stringValue(value));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private ObjectNode stringValue(String value) {
|
||||
return mapper.createObjectNode().put("S", value);
|
||||
}
|
||||
|
||||
private ObjectNode numberValue(String value) {
|
||||
return mapper.createObjectNode().put("N", value);
|
||||
}
|
||||
}
|
||||
@@ -253,6 +253,18 @@ class DynamoDbServiceTest {
|
||||
assertTrue(after.getGlobalSecondaryIndexes().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createTableRejectsMissingKeyAttributeDefinition() {
|
||||
AwsException error = assertThrows(AwsException.class, () -> service.createTable(
|
||||
"Users",
|
||||
List.of(new KeySchemaElement("userId", "HASH")),
|
||||
List.of(),
|
||||
5L, 5L, "eu-west-1"));
|
||||
|
||||
assertEquals("ValidationException", error.getErrorCode());
|
||||
assertEquals("Invalid KeySchema: Some index key attribute have no definition", error.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeTable() {
|
||||
String region = "eu-west-1";
|
||||
|
||||
Reference in new issue
Block a user