fix(dynamodb): add missing request validation (#3287)

* fix(dynamodb): add missing request validation for AWS parity

- CreateTable rejects PAY_PER_REQUEST with ProvisionedThroughput
- an INCLUDE projection without NonKeyAttributes on a GSI or LSI
- a non INCLUDE projection carrying NonKeyAttributes
- a StreamSpecification with StreamEnabled false plus a StreamViewType
- Scan rejects TotalSegments above 1000000
- UpdateItem rejects ExpressionAttributeNames or Values supplied with no expression
- a SET through a missing intermediate map path.

* fix(dynamodb): validate GSI projection on UpdateTable create

UpdateTable accepted an INCLUDE projection without NonKeyAttributes
and a non INCLUDE projection carrying NonKeyAttributes. Real DynamoDB
rejects both with the same message as CreateTable, and it does so
before the table lookup.

* fix(dynamodb): reject an empty NonKeyAttributes list on index projections

An empty NonKeyAttributes array was accepted on CreateTable and
UpdateTable index projections. Real DynamoDB rejects it with a length
constraint on the 1-based member path, before the projection type
check, and treats a null NonKeyAttributes as not specified.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-09-09 10:04:54 -04:00
1 parent 9fa36e9741
commit 4d199b144e
4 files changed
+394 -22

No files matched your search

@@ -128,7 +128,9 @@ public class DynamoDbJsonHandler {
List<GlobalSecondaryIndex> gsis = new ArrayList<>();
JsonNode gsiArray = request.path("GlobalSecondaryIndexes");
if (!gsiArray.isMissingNode() && gsiArray.isArray()) {
var gsiPosition = 0;
for (JsonNode gsiNode : gsiArray) {
gsiPosition++;
String indexName = gsiNode.path("IndexName").asText();
List<KeySchemaElement> gsiKeySchema = new ArrayList<>();
gsiNode.path("KeySchema").forEach(ks ->
@@ -143,6 +145,8 @@ public class DynamoDbJsonHandler {
nonKeyAttributes.add(nonKeyAttr.asText());
}
}
rejectEmptyNonKeyAttributes(nonKeyAttrArray, "globalSecondaryIndexes." + gsiPosition + ".member");
validateProjectionSpec(projectionType, nonKeyAttributes);
GlobalSecondaryIndex gsi = new GlobalSecondaryIndex(indexName, gsiKeySchema, null, projectionType, nonKeyAttributes);
JsonNode gsiPt = gsiNode.path("ProvisionedThroughput");
if (!gsiPt.isMissingNode()) {
@@ -165,7 +169,9 @@ public class DynamoDbJsonHandler {
List<LocalSecondaryIndex> lsis = new ArrayList<>();
JsonNode lsiArray = request.path("LocalSecondaryIndexes");
if (!lsiArray.isMissingNode() && lsiArray.isArray()) {
var lsiPosition = 0;
for (JsonNode lsiNode : lsiArray) {
lsiPosition++;
String indexName = lsiNode.path("IndexName").asText();
List<KeySchemaElement> lsiKeySchema = new ArrayList<>();
lsiNode.path("KeySchema").forEach(ks ->
@@ -178,6 +184,8 @@ public class DynamoDbJsonHandler {
if (!lsiNonKeyAttrArray.isMissingNode() && lsiNonKeyAttrArray.isArray()) {
lsiNonKeyAttrArray.forEach(a -> lsiNonKeyAttributes.add(a.asText()));
}
rejectEmptyNonKeyAttributes(lsiNonKeyAttrArray, "localSecondaryIndexes." + lsiPosition + ".member");
validateProjectionSpec(projectionType, lsiNonKeyAttributes);
lsis.add(new LocalSecondaryIndex(indexName, lsiKeySchema, null, projectionType, lsiNonKeyAttributes));
}
}
@@ -197,6 +205,20 @@ public class DynamoDbJsonHandler {
validateSseType(sseType);
}
if ("PAY_PER_REQUEST".equals(billingMode) && !pt.isMissingNode()) {
throw new AwsException("ValidationException",
"One or more parameter values were invalid: Neither ReadCapacityUnits nor WriteCapacityUnits "
+ "can be specified when BillingMode is PAY_PER_REQUEST", 400);
}
JsonNode streamSpecCheck = request.path("StreamSpecification");
if (streamSpecCheck.isObject() && !streamSpecCheck.path("StreamEnabled").asBoolean(false)
&& streamSpecCheck.hasNonNull("StreamViewType")) {
throw new AwsException("ValidationException",
"One or more parameter values were invalid: StreamViewType cannot be specified "
+ "when StreamEnabled is false", 400);
}
TableDefinition table = dynamoDbService.createTable(tableName, keySchema, attrDefs,
readCapacity, writeCapacity, gsis, lsis, region);
table.setTableStatus(initialStatus);
@@ -271,6 +293,31 @@ public class DynamoDbJsonHandler {
}
}
// AWS reports an empty list as a length constraint on the 1-based member path, before the
// projection type check. A null NonKeyAttributes counts as not specified.
private static void rejectEmptyNonKeyAttributes(JsonNode nonKeyAttrArray, String memberPath) {
if (nonKeyAttrArray.isArray() && nonKeyAttrArray.isEmpty()) {
throw new AwsException("ValidationException",
"1 validation error detected: Value '[]' at '" + memberPath
+ ".projection.nonKeyAttributes' failed to satisfy constraint: "
+ "Member must have length greater than or equal to 1", 400);
}
}
// INCLUDE requires the NonKeyAttributes list; every other projection type forbids it.
private static void validateProjectionSpec(String projectionType, List<String> nonKeyAttributes) {
if ("INCLUDE".equals(projectionType) && nonKeyAttributes.isEmpty()) {
throw new AwsException("ValidationException",
"One or more parameter values were invalid: "
+ "ProjectionType is INCLUDE, but NonKeyAttributes is not specified", 400);
}
if (!"INCLUDE".equals(projectionType) && !nonKeyAttributes.isEmpty()) {
throw new AwsException("ValidationException",
"One or more parameter values were invalid: "
+ "ProjectionType is " + projectionType + ", but NonKeyAttributes is specified", 400);
}
}
private Response handleDeleteTable(JsonNode request, String region) {
String tableName = request.path("TableName").asText();
TableDefinition table = dynamoDbService.describeTable(tableName, region);
@@ -569,6 +616,11 @@ public class DynamoDbJsonHandler {
+ "Non-expression parameters: {AttributeUpdates} Expression parameters: {UpdateExpression}", 400);
}
// EAN/EAV with no expression to reference them, mirroring the PutItem guard.
rejectExprAttrsWithoutExpression(exprAttrNames, exprAttrValues,
updateExpression != null || conditionExpression != null,
"UpdateExpression is null, ConditionExpression is null");
ExpressionEvaluator.validateExpression(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
if (conditionExpression != null && expectedUpd != null) {
@@ -994,6 +1046,12 @@ public class DynamoDbJsonHandler {
throw new AwsException("ValidationException",
"The Segment parameter is required but was not present in the request when parameter TotalSegments is present", 400);
}
if (totalSegments != null && totalSegments > 1_000_000) {
throw new AwsException("ValidationException",
"1 validation error detected: Value '" + totalSegments
+ "' at 'totalSegments' failed to satisfy constraint: "
+ "Member must have value less than or equal to 1000000", 400);
}
if (segment != null && totalSegments != null && segment >= totalSegments) {
throw new AwsException("ValidationException",
"The Segment parameter is zero-based and must be less than parameter TotalSegments: "
@@ -1299,7 +1357,9 @@ public class DynamoDbJsonHandler {
List<JsonNode> gsiUpdatesToApply = new ArrayList<>();
JsonNode gsiUpdates = request.path("GlobalSecondaryIndexUpdates");
if (!gsiUpdates.isMissingNode() && gsiUpdates.isArray()) {
var updatePosition = 0;
for (JsonNode update : gsiUpdates) {
updatePosition++;
JsonNode createNode = update.path("Create");
if (!createNode.isMissingNode()) {
String indexName = createNode.path("IndexName").asText();
@@ -1316,6 +1376,9 @@ public class DynamoDbJsonHandler {
nonKeyAttributes.add(nonKeyAttr.asText());
}
}
rejectEmptyNonKeyAttributes(nonKeyAttrArray,
"globalSecondaryIndexUpdates." + updatePosition + ".member.create");
validateProjectionSpec(projectionType, nonKeyAttributes);
GlobalSecondaryIndex newGsi = new GlobalSecondaryIndex(indexName, gsiKeySchema, null, projectionType, nonKeyAttributes);
JsonNode newGsiPt = createNode.path("ProvisionedThroughput");
if (!newGsiPt.isMissingNode()) {
@@ -2630,22 +2630,15 @@ public class DynamoDbService implements ResourceProvider {
if (last) {
if (nextTok instanceof String finalAttr) {
if (child == null) {
ObjectNode newMap = objectMapper.createObjectNode();
newMap.set(finalAttr, value);
ObjectNode wrapper = objectMapper.createObjectNode();
wrapper.set("M", newMap);
obj.set(attrName, wrapper);
} else if (!child.has("M")) {
// AWS requires every intermediate of a document path to already
// exist as the right type; only the final element may be new.
if (child == null || !child.has("M")) {
throw new AwsException(
"ValidationException",
"The document path provided in the update expression is invalid for update",
400);
} else {
((ObjectNode) child.get("M")).set(finalAttr, value);
}
((ObjectNode) child.get("M")).set(finalAttr, value);
} else if (nextTok instanceof Long finalIdx) {
if (child == null || !child.has("L")) {
throw new AwsException(
@@ -2661,21 +2654,13 @@ public class DynamoDbService implements ResourceProvider {
}
if (nextTok instanceof String) {
if (child == null) {
ObjectNode newMap = objectMapper.createObjectNode();
ObjectNode wrapper = objectMapper.createObjectNode();
wrapper.set("M", newMap);
obj.set(attrName, wrapper);
container = newMap;
} else if (!child.has("M")) {
if (child == null || !child.has("M")) {
throw new AwsException(
"ValidationException",
"The document path provided in the update expression is invalid for update",
400);
} else {
container = child.get("M");
}
container = child.get("M");
} else if (nextTok instanceof Long) {
if (child == null || !child.has("L")) {