mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): reject key attribute values whose type does not match the key schema (#2625)
* fix(dynamodb): reject key attribute values whose type does not match the key schema
PutItem/UpdateItem (and Get/Delete/BatchWrite/TransactWrite via the same
path) accepted a NULL-typed partition or sort key, which real DynamoDB
and LocalStack reject with a ValidationException. Worse, non-scalar key
values collapsed to an empty storage key, so all such items collided on
one slot.
buildItemKey now validates each key attribute value against the table's
AttributeDefinitions, using AWS's wording for both flavours: the Key
argument form ("The provided key element does not match the schema")
and the PutItem item form ("One or more parameter values were invalid:
Type mismatch for key <name> expected: <type> actual: <type>").
Fixes #2624
* test(dynamodb): cover NULL-typed key rejection at the HTTP layer
* fix(dynamodb): validate key value shapes and index key types on writes
Review follow-up for #2625, all behaviours verified against real DynamoDB:
- Key attribute values that are empty objects or carry multiple type
members are rejected with AWS's "Supplied AttributeValue ..."
messages; non-object values get a SerializationException like AWS's
protocol layer, instead of aliasing onto another item's storage key.
- GSI/LSI key attributes present in a written item are validated
against AttributeDefinitions on PutItem and UpdateItem (covering
batch and transact paths, which reuse them), matching AWS's
"Type mismatch for Index Key <name> Expected: <T> Actual: <T>
IndexName: <idx>" wording. Absent attributes stay valid (sparse
indexes).
- Brace the new single-line conditionals per AGENTS.md.
* fix(dynamodb): apply value shape and empty-string rules to index keys
Review follow-up for #2625. The malformed-shape checks added for base
keys skipped GSI/LSI key attributes: validateIndexKeySchema's guard
stepped over empty, multi-member, and non-object values instead of
rejecting them. The three checks now live in a shared
validateAttributeValueShape helper that both key paths run, so they
cannot drift again.
Empty-string index key values are also rejected now, verified against
real DynamoDB, which uses different wording per operation: PutItem
names the index ("... IndexName: gsi1, IndexKey: gsiKey") while
UpdateItem uses the update-expression form without it.
Number normalization no longer rebuilds a multi-member AttributeValue
around its N member — {"S":"x","N":"1"} was being laundered
into a well-formed {"N":"1"} before validation could see it, which
turned the AWS shape error into a type-mismatch error.
This commit is contained in:
1 parent
e07ddcf155
commit
bf6c7b001c
4 files changed
+489
-4
No files matched your search
+74
@@ -3436,6 +3436,80 @@ given()
|
||||
deleteTable(tableName);
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullTypedKeyAttributeIsRejected() {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "NullKeyTable",
|
||||
"KeySchema": [{"AttributeName": "id", "KeyType": "HASH"}],
|
||||
"AttributeDefinitions": [{"AttributeName": "id", "AttributeType": "S"}],
|
||||
"BillingMode": "PAY_PER_REQUEST"
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
// PutItem: mismatch is reported against the item body.
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "NullKeyTable",
|
||||
"Item": {"id": {"NULL": true}, "name": {"S": "created"}}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"))
|
||||
.body("message", equalTo("One or more parameter values were invalid: "
|
||||
+ "Type mismatch for key id expected: S actual: NULL"));
|
||||
|
||||
// UpdateItem: mismatch in the Key argument uses the key-schema wording.
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "NullKeyTable",
|
||||
"Key": {"id": {"NULL": true}},
|
||||
"UpdateExpression": "SET #n = :v",
|
||||
"ExpressionAttributeNames": {"#n": "name"},
|
||||
"ExpressionAttributeValues": {":v": {"S": "updated"}}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"))
|
||||
.body("message", equalTo("The provided key element does not match the schema"));
|
||||
|
||||
// Control: NULL is a valid type for a non-key attribute.
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "NullKeyTable",
|
||||
"Item": {"id": {"S": "1"}, "name": {"NULL": true}}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
deleteTable("NullKeyTable");
|
||||
}
|
||||
|
||||
private void deleteTable(String tableName) {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")
|
||||
|
||||
@@ -2090,6 +2090,310 @@ class DynamoDbServiceTest {
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putItemNullPartitionKeyThrowsTypeMismatch() {
|
||||
String region = "eu-west-1";
|
||||
createUsersTable(region);
|
||||
|
||||
ObjectNode nullKeyItem = mapper.createObjectNode();
|
||||
nullKeyItem.set("userId", mapper.createObjectNode().put("NULL", true));
|
||||
nullKeyItem.set("name", attributeValue("S", "created"));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.putItem("Users", nullKeyItem, region));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals("One or more parameter values were invalid: "
|
||||
+ "Type mismatch for key userId expected: S actual: NULL", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putItemWrongScalarTypePartitionKeyThrowsTypeMismatch() {
|
||||
String region = "eu-west-1";
|
||||
createUsersTable(region);
|
||||
|
||||
ObjectNode numberKeyItem = mapper.createObjectNode();
|
||||
numberKeyItem.set("userId", attributeValue("N", "42"));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.putItem("Users", numberKeyItem, region));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals("One or more parameter values were invalid: "
|
||||
+ "Type mismatch for key userId expected: S actual: N", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putItemNullSortKeyThrowsTypeMismatch() {
|
||||
String region = "eu-west-1";
|
||||
createOrdersTable(region);
|
||||
|
||||
ObjectNode nullSkItem = item("customerId", "c1");
|
||||
nullSkItem.set("orderId", mapper.createObjectNode().put("NULL", true));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.putItem("Orders", nullSkItem, region));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals("One or more parameter values were invalid: "
|
||||
+ "Type mismatch for key orderId expected: S actual: NULL", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putItemNullNonKeyAttributeIsAccepted() {
|
||||
String region = "eu-west-1";
|
||||
createUsersTable(region);
|
||||
|
||||
ObjectNode nullAttrItem = item("userId", "u1");
|
||||
nullAttrItem.set("name", mapper.createObjectNode().put("NULL", true));
|
||||
service.putItem("Users", nullAttrItem, region);
|
||||
|
||||
JsonNode stored = service.getItem("Users", item("userId", "u1"), region);
|
||||
assertNotNull(stored);
|
||||
assertTrue(stored.get("name").get("NULL").asBoolean());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putItemMultiTypeKeyValueThrowsValidationException() {
|
||||
String region = "eu-west-1";
|
||||
createUsersTable(region);
|
||||
|
||||
ObjectNode multiTypeItem = mapper.createObjectNode();
|
||||
multiTypeItem.set("userId", mapper.createObjectNode().put("S", "1").put("NULL", true));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.putItem("Users", multiTypeItem, region));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals("Supplied AttributeValue has more than one datatypes set, "
|
||||
+ "must contain exactly one of the supported datatypes", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putItemEmptyKeyValueThrowsValidationException() {
|
||||
String region = "eu-west-1";
|
||||
createUsersTable(region);
|
||||
|
||||
ObjectNode emptyValueItem = mapper.createObjectNode();
|
||||
emptyValueItem.set("userId", mapper.createObjectNode());
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.putItem("Users", emptyValueItem, region));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals("Supplied AttributeValue is empty, "
|
||||
+ "must contain exactly one of the supported datatypes", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putItemNonObjectKeyValueThrowsSerializationException() {
|
||||
String region = "eu-west-1";
|
||||
createUsersTable(region);
|
||||
|
||||
ObjectNode rawValueItem = mapper.createObjectNode();
|
||||
rawValueItem.put("userId", "1");
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.putItem("Users", rawValueItem, region));
|
||||
assertEquals("SerializationException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
private TableDefinition createIndexedTable(String region) {
|
||||
GlobalSecondaryIndex gsi = new GlobalSecondaryIndex("gsi1",
|
||||
List.of(new KeySchemaElement("gsiKey", "HASH")), null, "ALL", null);
|
||||
LocalSecondaryIndex lsi = new LocalSecondaryIndex("lsi1",
|
||||
List.of(
|
||||
new KeySchemaElement("customerId", "HASH"),
|
||||
new KeySchemaElement("lsiKey", "RANGE")), null, "ALL");
|
||||
return service.createTable("Indexed",
|
||||
List.of(
|
||||
new KeySchemaElement("customerId", "HASH"),
|
||||
new KeySchemaElement("orderId", "RANGE")),
|
||||
List.of(
|
||||
new AttributeDefinition("customerId", "S"),
|
||||
new AttributeDefinition("orderId", "S"),
|
||||
new AttributeDefinition("gsiKey", "S"),
|
||||
new AttributeDefinition("lsiKey", "N")),
|
||||
5L, 5L, List.of(gsi), List.of(lsi), region);
|
||||
}
|
||||
|
||||
@Test
|
||||
void putItemNullGsiKeyThrowsIndexTypeMismatch() {
|
||||
String region = "eu-west-1";
|
||||
createIndexedTable(region);
|
||||
|
||||
ObjectNode nullGsiItem = item("customerId", "c1", "orderId", "o1");
|
||||
nullGsiItem.set("gsiKey", mapper.createObjectNode().put("NULL", true));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.putItem("Indexed", nullGsiItem, region));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals("One or more parameter values were invalid: "
|
||||
+ "Type mismatch for Index Key gsiKey Expected: S Actual: NULL IndexName: gsi1",
|
||||
ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putItemWrongTypeLsiKeyThrowsIndexTypeMismatch() {
|
||||
String region = "eu-west-1";
|
||||
createIndexedTable(region);
|
||||
|
||||
ObjectNode wrongLsiItem = item("customerId", "c1", "orderId", "o1", "lsiKey", "oops");
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.putItem("Indexed", wrongLsiItem, region));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals("One or more parameter values were invalid: "
|
||||
+ "Type mismatch for Index Key lsiKey Expected: N Actual: S IndexName: lsi1",
|
||||
ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putItemOmittingIndexKeyAttributesIsAccepted() {
|
||||
String region = "eu-west-1";
|
||||
createIndexedTable(region);
|
||||
|
||||
// Sparse index: absent GSI/LSI key attributes are valid.
|
||||
service.putItem("Indexed", item("customerId", "c1", "orderId", "o1"), region);
|
||||
|
||||
assertNotNull(service.getItem("Indexed",
|
||||
item("customerId", "c1", "orderId", "o1"), region));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateItemSettingWrongTypeGsiKeyThrowsIndexTypeMismatch() {
|
||||
String region = "eu-west-1";
|
||||
createIndexedTable(region);
|
||||
service.putItem("Indexed", item("customerId", "c1", "orderId", "o1"), region);
|
||||
|
||||
ObjectNode exprValues = mapper.createObjectNode();
|
||||
exprValues.set(":v", mapper.createObjectNode().put("NULL", true));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.updateItem("Indexed", item("customerId", "c1", "orderId", "o1"), null,
|
||||
"SET gsiKey = :v", null, exprValues, null, region));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals("One or more parameter values were invalid: "
|
||||
+ "Type mismatch for Index Key gsiKey Expected: S Actual: NULL IndexName: gsi1",
|
||||
ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putItemMultiTypeGsiKeyValueThrowsValidationException() {
|
||||
String region = "eu-west-1";
|
||||
createIndexedTable(region);
|
||||
|
||||
ObjectNode multiTypeGsiItem = item("customerId", "c1", "orderId", "o1");
|
||||
multiTypeGsiItem.set("gsiKey", mapper.createObjectNode().put("S", "x").put("N", "1"));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.putItem("Indexed", multiTypeGsiItem, region));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals("Supplied AttributeValue has more than one datatypes set, "
|
||||
+ "must contain exactly one of the supported datatypes", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putItemEmptyObjectGsiKeyValueThrowsValidationException() {
|
||||
String region = "eu-west-1";
|
||||
createIndexedTable(region);
|
||||
|
||||
ObjectNode emptyGsiItem = item("customerId", "c1", "orderId", "o1");
|
||||
emptyGsiItem.set("gsiKey", mapper.createObjectNode());
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.putItem("Indexed", emptyGsiItem, region));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals("Supplied AttributeValue is empty, "
|
||||
+ "must contain exactly one of the supported datatypes", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putItemNonObjectGsiKeyValueThrowsSerializationException() {
|
||||
String region = "eu-west-1";
|
||||
createIndexedTable(region);
|
||||
|
||||
ObjectNode rawGsiItem = item("customerId", "c1", "orderId", "o1");
|
||||
rawGsiItem.put("gsiKey", "x");
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.putItem("Indexed", rawGsiItem, region));
|
||||
assertEquals("SerializationException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putItemEmptyStringGsiKeyThrowsValidationException() {
|
||||
String region = "eu-west-1";
|
||||
createIndexedTable(region);
|
||||
|
||||
ObjectNode emptyStringGsiItem = item("customerId", "c1", "orderId", "o1", "gsiKey", "");
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.putItem("Indexed", emptyStringGsiItem, region));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals("One or more parameter values are not valid. A value specified for a secondary "
|
||||
+ "index key is not supported. The AttributeValue for a key attribute cannot "
|
||||
+ "contain an empty string value. IndexName: gsi1, IndexKey: gsiKey", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateItemSettingEmptyStringGsiKeyThrowsValidationException() {
|
||||
String region = "eu-west-1";
|
||||
createIndexedTable(region);
|
||||
service.putItem("Indexed", item("customerId", "c1", "orderId", "o1"), region);
|
||||
|
||||
ObjectNode exprValues = mapper.createObjectNode();
|
||||
exprValues.set(":v", attributeValue("S", ""));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.updateItem("Indexed", item("customerId", "c1", "orderId", "o1"), null,
|
||||
"SET gsiKey = :v", null, exprValues, null, region));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals("One or more parameter values are not valid. The update expression attempted to "
|
||||
+ "update a secondary index key to a value that is not supported. "
|
||||
+ "The AttributeValue for a key attribute cannot contain an empty string value.", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateItemNullPartitionKeyThrowsValidationException() {
|
||||
String region = "eu-west-1";
|
||||
createUsersTable(region);
|
||||
|
||||
ObjectNode nullKey = mapper.createObjectNode();
|
||||
nullKey.set("userId", mapper.createObjectNode().put("NULL", true));
|
||||
ObjectNode exprValues = mapper.createObjectNode();
|
||||
exprValues.set(":val", attributeValue("S", "updated"));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.updateItem("Users", nullKey, null,
|
||||
"SET name = :val", null, exprValues, null, region));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals("The provided key element does not match the schema", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getItemNullPartitionKeyThrowsValidationException() {
|
||||
String region = "eu-west-1";
|
||||
createUsersTable(region);
|
||||
|
||||
ObjectNode nullKey = mapper.createObjectNode();
|
||||
nullKey.set("userId", mapper.createObjectNode().put("NULL", true));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.getItem("Users", nullKey, region));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals("The provided key element does not match the schema", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteItemNullPartitionKeyThrowsValidationException() {
|
||||
String region = "eu-west-1";
|
||||
createUsersTable(region);
|
||||
|
||||
ObjectNode nullKey = mapper.createObjectNode();
|
||||
nullKey.set("userId", mapper.createObjectNode().put("NULL", true));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.deleteItem("Users", nullKey, region));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals("The provided key element does not match the schema", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateExpressionAcceptsNewlineBetweenSetAndAdd() {
|
||||
String region = "eu-west-1";
|
||||
|
||||
Reference in new issue
Block a user