mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -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
@@ -90,6 +90,13 @@ final class DynamoDbNumberUtils {
|
||||
private static JsonNode normalizeAttrValue(JsonNode attr) {
|
||||
if (attr == null) return null;
|
||||
|
||||
// A well-formed AttributeValue has exactly one type member. Rebuilding a
|
||||
// malformed one (e.g. {"S":"x","N":"1"}) around its "N" member would launder
|
||||
// it into a valid value before validation can reject it — pass it through.
|
||||
if (attr.isObject() && attr.size() != 1) {
|
||||
return attr;
|
||||
}
|
||||
|
||||
if (attr.has("N")) {
|
||||
String raw = attr.get("N").asText();
|
||||
String normalized = validateAndNormalize(raw);
|
||||
|
||||
@@ -512,6 +512,7 @@ public class DynamoDbService implements ResourceProvider {
|
||||
final JsonNode normalizedItem = DynamoDbNumberUtils.normalizeNumbersInItem(item);
|
||||
DynamoDbItemSize.validateSize(normalizedItem);
|
||||
String itemKey = buildItemKey(table, normalizedItem);
|
||||
validateIndexKeyTypes(table, normalizedItem, false);
|
||||
|
||||
withItemLock(storageKey, itemKey, () -> {
|
||||
var tableItems = itemsByTable.computeIfAbsent(scopedItemsKey(storageKey), k -> new ConcurrentSkipListMap<>());
|
||||
@@ -733,6 +734,9 @@ public class DynamoDbService implements ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
// AWS validates index key values against the item the update produces.
|
||||
validateIndexKeyTypes(table, item, true);
|
||||
|
||||
items.put(itemKey, item);
|
||||
persistItems(storageKey);
|
||||
LOG.tracev("Updated item in {0}: key={1} updateExpression={2} item={3}",
|
||||
@@ -2548,7 +2552,7 @@ public class DynamoDbService implements ResourceProvider {
|
||||
throw new AwsException("ValidationException",
|
||||
"One of the required keys was not given a value", 400);
|
||||
}
|
||||
validateKeyAttributeValue(pkAttr, pkName);
|
||||
validateKeyAttributeValue(table, pkAttr, pkName, isKeyArg);
|
||||
|
||||
String pk = extractScalarValue(pkAttr);
|
||||
String skName = table.getSortKeyName();
|
||||
@@ -2562,20 +2566,116 @@ public class DynamoDbService implements ResourceProvider {
|
||||
throw new AwsException("ValidationException",
|
||||
"One of the required keys was not given a value", 400);
|
||||
}
|
||||
validateKeyAttributeValue(skAttr, skName);
|
||||
validateKeyAttributeValue(table, skAttr, skName, isKeyArg);
|
||||
return pk + "#" + extractScalarValue(skAttr);
|
||||
}
|
||||
return pk;
|
||||
}
|
||||
|
||||
private void validateKeyAttributeValue(JsonNode attr, String keyName) {
|
||||
if (attr != null && attr.has("S") && attr.get("S").asText().isEmpty()) {
|
||||
// A wire-format AttributeValue must be a JSON object with exactly one type member.
|
||||
// AWS enforces this for every attribute value; floci additionally relies on it
|
||||
// wherever a value becomes part of a storage or index key.
|
||||
private void validateAttributeValueShape(JsonNode attr) {
|
||||
if (!attr.isObject()) {
|
||||
// AWS's protocol layer rejects non-object values before validation runs.
|
||||
throw new AwsException("SerializationException", "Unexpected value type in payload", 400);
|
||||
}
|
||||
if (attr.isEmpty()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Supplied AttributeValue is empty, must contain exactly one of the supported datatypes", 400);
|
||||
}
|
||||
if (attr.size() > 1) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Supplied AttributeValue has more than one datatypes set, "
|
||||
+ "must contain exactly one of the supported datatypes", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private void validateKeyAttributeValue(TableDefinition table, JsonNode attr, String keyName, boolean isKeyArg) {
|
||||
if (attr == null) {
|
||||
return;
|
||||
}
|
||||
validateAttributeValueShape(attr);
|
||||
String expectedType = keyAttributeType(table, keyName);
|
||||
if (expectedType != null && !attr.has(expectedType)) {
|
||||
// AWS words the type mismatch differently for a Key argument
|
||||
// (Get/Delete/Update) than for a PutItem item body.
|
||||
if (isKeyArg) {
|
||||
throw new AwsException("ValidationException",
|
||||
"The provided key element does not match the schema", 400);
|
||||
}
|
||||
throw new AwsException("ValidationException",
|
||||
"One or more parameter values were invalid: Type mismatch for key " + keyName
|
||||
+ " expected: " + expectedType + " actual: " + attr.fieldNames().next(), 400);
|
||||
}
|
||||
if (attr.has("S") && attr.get("S").asText().isEmpty()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"One or more parameter values were invalid: "
|
||||
+ "The AttributeValue for a key attribute cannot contain an empty string value. Key: " + keyName, 400);
|
||||
}
|
||||
}
|
||||
|
||||
private String keyAttributeType(TableDefinition table, String keyName) {
|
||||
if (table.getAttributeDefinitions() == null) {
|
||||
return null;
|
||||
}
|
||||
return table.getAttributeDefinitions().stream()
|
||||
.filter(def -> keyName.equals(def.getAttributeName()))
|
||||
.map(AttributeDefinition::getAttributeType)
|
||||
.findFirst().orElse(null);
|
||||
}
|
||||
|
||||
// AWS validates GSI/LSI key attribute values on every write that produces the item,
|
||||
// but only when the attribute is present — sparse indexes allow it to be absent.
|
||||
private void validateIndexKeyTypes(TableDefinition table, JsonNode item, boolean isUpdate) {
|
||||
if (table.getGlobalSecondaryIndexes() != null) {
|
||||
for (GlobalSecondaryIndex gsi : table.getGlobalSecondaryIndexes()) {
|
||||
validateIndexKeySchema(table, item, gsi.getIndexName(), gsi.getKeySchema(), isUpdate);
|
||||
}
|
||||
}
|
||||
if (table.getLocalSecondaryIndexes() != null) {
|
||||
for (LocalSecondaryIndex lsi : table.getLocalSecondaryIndexes()) {
|
||||
validateIndexKeySchema(table, item, lsi.getIndexName(), lsi.getKeySchema(), isUpdate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void validateIndexKeySchema(TableDefinition table, JsonNode item,
|
||||
String indexName, List<KeySchemaElement> keySchema,
|
||||
boolean isUpdate) {
|
||||
if (keySchema == null) {
|
||||
return;
|
||||
}
|
||||
for (KeySchemaElement element : keySchema) {
|
||||
String attrName = element.getAttributeName();
|
||||
JsonNode attr = item.get(attrName);
|
||||
if (attr == null) {
|
||||
continue;
|
||||
}
|
||||
validateAttributeValueShape(attr);
|
||||
String expectedType = keyAttributeType(table, attrName);
|
||||
if (expectedType != null && !attr.has(expectedType)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"One or more parameter values were invalid: Type mismatch for Index Key " + attrName
|
||||
+ " Expected: " + expectedType + " Actual: " + attr.fieldNames().next()
|
||||
+ " IndexName: " + indexName, 400);
|
||||
}
|
||||
if (attr.has("S") && attr.get("S").asText().isEmpty()) {
|
||||
// AWS uses different wording for UpdateItem than for writes of a whole item.
|
||||
if (isUpdate) {
|
||||
throw new AwsException("ValidationException",
|
||||
"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.", 400);
|
||||
}
|
||||
throw new AwsException("ValidationException",
|
||||
"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: " + indexName + ", IndexKey: " + attrName, 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String buildItemKeyFromNode(JsonNode item, String pkName, String skName) {
|
||||
return buildItemKeyFromNode(item, pkName, skName == null ? List.of() : List.of(skName));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user