mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): measure a table name the way each DynamoDB call does (#8538)
A bad DynamoDB table name now gets the errors each call gives for it. The table calls, and the older validator of a deeply nested PutItem, UpdateItem or DeleteItem, count the name in UTF-16 units and list every problem, the length first. GetItem, PutItem, DeleteItem, Query and Scan count characters and report the first problem. UpdateItem counts characters and lists the pattern before the length. A name inside a table ARN gets only its first problem, counted in UTF-16 units, with the whole ARN quoted and "Table name of ARN must".
This commit is contained in:
1 parent
e305ced3bd
commit
678bd347af
4 files changed
+139
-15
No files matched your search
@@ -26,6 +26,22 @@ public final class DynamoDbTableNames {
|
||||
return resolveInternal(input).name();
|
||||
}
|
||||
|
||||
/** Whether the input is a non-empty name rather than a table ARN. */
|
||||
static boolean isShortName(String input) {
|
||||
return input != null && !input.isEmpty() && !AwsArnUtils.looksLikeArn(input);
|
||||
}
|
||||
|
||||
/** Resolves the table of an item call, which reports only the first problem of a short name. */
|
||||
public static String resolveForItemCall(String input) {
|
||||
if (isShortName(input)) {
|
||||
String violation = nameViolation(input);
|
||||
if (violation != null) {
|
||||
throw SmithyValidationErrors.validationException(violation);
|
||||
}
|
||||
}
|
||||
return resolve(input);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a short table name for use with CreateTable.
|
||||
* CreateTable requires min length 3 and missing TableName is a distinct error.
|
||||
@@ -107,7 +123,11 @@ public final class DynamoDbTableNames {
|
||||
throw invalid("Invalid table ARN: " + input);
|
||||
}
|
||||
|
||||
validateTableName(tableName);
|
||||
// Every call reports only the first problem of the name in a table ARN, measured in UTF-16 units.
|
||||
List<String> violations = violations(input, "Table name of ARN must ", tableName, tableName.length());
|
||||
if (!violations.isEmpty()) {
|
||||
throw SmithyValidationErrors.validationException(violations.getFirst());
|
||||
}
|
||||
return new ResolvedTableRef(tableName, region);
|
||||
}
|
||||
|
||||
@@ -121,32 +141,48 @@ public final class DynamoDbTableNames {
|
||||
}
|
||||
|
||||
private static void validateTableName(String tableName) {
|
||||
String violation = nameViolation(tableName);
|
||||
if (violation != null) {
|
||||
throw SmithyValidationErrors.validationException(violation);
|
||||
List<String> violations = nameViolations(tableName);
|
||||
if (!violations.isEmpty()) {
|
||||
throw SmithyValidationErrors.validationException(violations);
|
||||
}
|
||||
}
|
||||
|
||||
/** The violation for a short table name, or {@code null} for a valid one. A bad length hides a bad pattern. */
|
||||
/**
|
||||
* The first violation of a short table name as an item call reports it, or {@code null} for a
|
||||
* valid one. The length counts characters and hides a bad pattern.
|
||||
*/
|
||||
static String nameViolation(String tableName) {
|
||||
List<String> violations = nameViolations(tableName);
|
||||
List<String> violations = violations(tableName, "Member must ", tableName,
|
||||
tableName.codePointCount(0, tableName.length()));
|
||||
return violations.isEmpty() ? null : violations.getFirst();
|
||||
}
|
||||
|
||||
/** Every violation of a short table name, the length first. */
|
||||
/**
|
||||
* Every violation of a short table name as the table calls and the older item validator
|
||||
* report them, the length first. The length counts UTF-16 units, so an emoji counts twice.
|
||||
*/
|
||||
static List<String> nameViolations(String tableName) {
|
||||
return violations(tableName, "Member must ", tableName, tableName.length());
|
||||
}
|
||||
|
||||
/** The length problem, then the pattern problem, of a table name the error quotes as {@code value}. */
|
||||
private static List<String> violations(String value, String rule, String tableName, int length) {
|
||||
List<String> violations = new ArrayList<>();
|
||||
String lengthViolation = SmithyValidationErrors.lengthOutOfRange("tableName", tableName, 3, 255);
|
||||
if (lengthViolation != null) {
|
||||
violations.add(lengthViolation);
|
||||
if (length < 3) {
|
||||
violations.add(violation(value, rule + "have length greater than or equal to 3"));
|
||||
} else if (255 < length) {
|
||||
violations.add(violation(value, rule + "have length less than or equal to 255"));
|
||||
}
|
||||
if (!TABLE_NAME_CHARS.matcher(tableName).matches()) {
|
||||
violations.add("Value '" + tableName
|
||||
+ "' at 'tableName' failed to satisfy constraint: Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+");
|
||||
violations.add(violation(value, rule + "satisfy regular expression pattern: [a-zA-Z0-9_.-]+"));
|
||||
}
|
||||
return violations;
|
||||
}
|
||||
|
||||
private static String violation(String value, String constraint) {
|
||||
return SmithyValidationErrors.Violations.QUOTED.constraint("tableName", value, constraint);
|
||||
}
|
||||
|
||||
private static AwsException invalid(String message) {
|
||||
return new AwsException("ValidationException", message, 400);
|
||||
}
|
||||
|
||||
@@ -912,6 +912,7 @@ public class NativeDynamoDbJsonHandler {
|
||||
ProjectionEvaluator.validateExpression(projectionExpression, exprAttrNames);
|
||||
}
|
||||
|
||||
DynamoDbTableNames.resolveForItemCall(tableName);
|
||||
JsonNode item = dynamoDbService.getItem(tableName, key, region);
|
||||
int itemBytes = item != null
|
||||
? DynamoDbItemSize.billedItemSize(dynamoDbService.requireTableForItemCall(tableName, region), item)
|
||||
@@ -979,7 +980,11 @@ public class NativeDynamoDbJsonHandler {
|
||||
requireValidOlderUpdate(request);
|
||||
}
|
||||
String tableName = request.path("TableName").asText();
|
||||
DynamoDbTableNames.resolve(tableName); // validate tableName first
|
||||
// UpdateItem lists every problem of a short name, the pattern first.
|
||||
if (DynamoDbTableNames.isShortName(tableName)) {
|
||||
throwValidationErrors(nameViolations(tableName, "tableName"));
|
||||
}
|
||||
DynamoDbTableNames.resolve(tableName);
|
||||
JsonNode key = request.path("Key");
|
||||
JsonNode attributeUpdates = request.path("AttributeUpdates");
|
||||
JsonNode exprAttrNames = request.has("ExpressionAttributeNames")
|
||||
@@ -1325,7 +1330,7 @@ public class NativeDynamoDbJsonHandler {
|
||||
|
||||
private Response handleQuery(JsonNode request, String region) {
|
||||
String tableName = request.path("TableName").asText();
|
||||
DynamoDbTableNames.resolve(tableName); // validate tableName first
|
||||
DynamoDbTableNames.resolveForItemCall(tableName); // validate tableName first
|
||||
JsonNode keyConditions = request.has("KeyConditions") ? request.get("KeyConditions") : null;
|
||||
JsonNode exprAttrValues = request.has("ExpressionAttributeValues")
|
||||
? request.get("ExpressionAttributeValues") : null;
|
||||
@@ -1536,7 +1541,7 @@ public class NativeDynamoDbJsonHandler {
|
||||
rejectMixedParameters(request,
|
||||
List.of("AttributesToGet", "ScanFilter", "ConditionalOperator"),
|
||||
List.of("ProjectionExpression", "FilterExpression"));
|
||||
DynamoDbTableNames.resolve(tableName);
|
||||
DynamoDbTableNames.resolveForItemCall(tableName);
|
||||
requireValidConditionalOperator(request, "ScanFilter");
|
||||
requireReadableValues(exprAttrValues, "");
|
||||
requireReadableValues(exclusiveStartKey, "");
|
||||
|
||||
Reference in new issue
Block a user