fix: count Unicode code points in Smithy length validation (#6456)

* fix: count Unicode code points in Smithy length validation

Count supplementary characters once, align existing formatter guards, and migrate remaining DynamoDB string messages while preserving validation order and operation-specific errors.

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>

* fix: centralize length checks in the Smithy helper

Use the helper null return in CloudWatch, EventBridge and DynamoDB vector-index validation. Preserve null, whitespace and aggregation errors.

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>

---------

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-10-24 20:32:02 -04:00
1 parent f6126964aa
commit 452633fe7d
9 files changed
+113 -30

No files matched your search

@@ -82,10 +82,11 @@ public final class SmithyValidationErrors {
/** The violation for a string outside its length bounds, or {@code null} for one inside. */
public static String lengthOutOfRange(String path, String value, int min, int max) {
if (value.length() < min) {
int length = value.codePointCount(0, value.length());
if (length < min) {
return lengthTooShort(path, value, min);
}
if (value.length() > max) {
if (length > max) {
return "Value '" + value + "' at '" + path + "' failed to satisfy constraint: "
+ "Member must have length less than or equal to " + max;
}
@@ -369,11 +369,13 @@ public class CloudWatchMetricsService {
private void putAlarm(Alarm alarm, String region) {
String name = alarm.getAlarmName();
if (name == null || name.isBlank() || name.length() > 255) {
throw SmithyValidationErrors.validationError(name == null ? SmithyValidationErrors.notNull("alarmName")
: name.length() > 255
? SmithyValidationErrors.lengthOutOfRange("alarmName", name, 1, 255)
: SmithyValidationErrors.lengthTooShort("alarmName", name, 1));
String violation = name == null ? SmithyValidationErrors.notNull("alarmName")
: SmithyValidationErrors.lengthOutOfRange("alarmName", name, 1, 255);
if (violation == null && name.isBlank()) {
violation = SmithyValidationErrors.lengthTooShort("alarmName", name, 1);
}
if (violation != null) {
throw SmithyValidationErrors.validationError(violation);
}
Alarm previous = alarmStore.get(region + "::" + name).orElse(null);
if (previous != null) {
@@ -19,11 +19,13 @@ final class CompositeAlarmRule {
}
static Rule parse(String input) {
if (input == null || input.isBlank() || input.length() > 10240) {
throw SmithyValidationErrors.validationError(input == null ? SmithyValidationErrors.notNull("alarmRule")
: input.length() > 10240
? SmithyValidationErrors.lengthOutOfRange("alarmRule", input, 1, 10240)
: SmithyValidationErrors.lengthTooShort("alarmRule", input, 1));
String violation = input == null ? SmithyValidationErrors.notNull("alarmRule")
: SmithyValidationErrors.lengthOutOfRange("alarmRule", input, 1, 10240);
if (violation == null && input.isBlank()) {
violation = SmithyValidationErrors.lengthTooShort("alarmRule", input, 1);
}
if (violation != null) {
throw SmithyValidationErrors.validationError(violation);
}
CompositeAlarmRule parser = new CompositeAlarmRule(input);
Expression expression = parser.or();
@@ -708,11 +708,13 @@ public class DynamoDbService {
requireVectorIndexMember(index.getProjectionType(), memberPath + ".projection");
requireVectorIndexMember(index.getDistanceFunction(), memberPath + ".distanceFunction");
String indexName = index.getIndexName();
if (indexName == null || indexName.length() < MIN_VECTOR_INDEX_NAME_LENGTH) {
throw SmithyValidationErrors.validationException(
"Value '" + indexName + "' at '"
+ memberPath + ".indexName' failed to satisfy constraint: "
+ "Member must have length greater than or equal to " + MIN_VECTOR_INDEX_NAME_LENGTH);
String nameViolation = indexName == null
? SmithyValidationErrors.lengthTooShort(
memberPath + ".indexName", null, MIN_VECTOR_INDEX_NAME_LENGTH)
: SmithyValidationErrors.lengthOutOfRange(
memberPath + ".indexName", indexName, MIN_VECTOR_INDEX_NAME_LENGTH, Integer.MAX_VALUE);
if (nameViolation != null) {
throw SmithyValidationErrors.validationException(nameViolation);
}
String distanceFunction = index.getDistanceFunction();
if (!VECTOR_DISTANCE_FUNCTIONS.contains(distanceFunction)) {
@@ -4932,9 +4934,10 @@ public class DynamoDbService {
String s3BucketOwner = request.containsKey("S3BucketOwner") ? (String) request.get("S3BucketOwner") : null;
List<String> violations = new ArrayList<>();
if (s3Prefix != null && 1024 < s3Prefix.codePointCount(0, s3Prefix.length())) {
violations.add("Value '" + s3Prefix
+ "' at 's3Prefix' failed to satisfy constraint: Member must have length less than or equal to 1024");
String prefixLengthViolation = s3Prefix == null ? null
: SmithyValidationErrors.lengthOutOfRange("s3Prefix", s3Prefix, 0, 1024);
if (prefixLengthViolation != null) {
violations.add(prefixLengthViolation);
}
if (!EXPORT_FORMATS.contains(exportFormat)) {
violations.add(SmithyValidationErrors.notInEnum(exportFormat, "exportFormat", EXPORT_FORMATS));
@@ -4942,10 +4945,11 @@ public class DynamoDbService {
if (!violations.isEmpty()) {
throw SmithyValidationErrors.validationException(violations);
}
if (s3Prefix != null && s3Prefix.codePointCount(0, s3Prefix.length()) < 3) {
throw new AwsException("ValidationException", "Invalid Request: " + SmithyValidationErrors.message(
List.of("Value '" + s3Prefix + "' at 's3Prefix' failed to satisfy constraint: "
+ "Member must have length greater than or equal to 3")), 400);
prefixLengthViolation = s3Prefix == null ? null
: SmithyValidationErrors.lengthOutOfRange("s3Prefix", s3Prefix, 3, Integer.MAX_VALUE);
if (prefixLengthViolation != null) {
throw new AwsException("ValidationException", "Invalid Request: "
+ SmithyValidationErrors.message(List.of(prefixLengthViolation)), 400);
}
if ("INCREMENTAL_EXPORT".equals(exportType)) {
throw new AwsException("ValidationException",
@@ -5478,8 +5482,7 @@ public class DynamoDbService {
var clientToken = request.path("ClientToken").asText(null);
if (clientToken != null && clientToken.isBlank()) {
throw SmithyValidationErrors.validationException(
"Value '" + clientToken
+ "' at 'clientToken' failed to satisfy constraint: Member must have length greater than or equal to 1");
SmithyValidationErrors.lengthTooShort("clientToken", clientToken, 1));
}
if (clientToken == null || importStore == null) {
return null;
@@ -1438,11 +1438,15 @@ public class EventBridgeService implements ResourceProvider {
violations.add("Value '" + retentionDays + "' at 'retentionDays' failed to satisfy constraint: "
+ "Member must have value greater than or equal to 0");
}
if (description != null && description.length() > 512) {
violations.add(SmithyValidationErrors.lengthOutOfRange("description", description, 0, 512));
String descriptionViolation = description == null ? null
: SmithyValidationErrors.lengthOutOfRange("description", description, 0, 512);
if (descriptionViolation != null) {
violations.add(descriptionViolation);
}
if (eventPattern != null && eventPattern.length() > 4096) {
violations.add(SmithyValidationErrors.lengthOutOfRange("eventPattern", eventPattern, 0, 4096));
String eventPatternViolation = eventPattern == null ? null
: SmithyValidationErrors.lengthOutOfRange("eventPattern", eventPattern, 0, 4096);
if (eventPatternViolation != null) {
violations.add(eventPatternViolation);
}
if (!violations.isEmpty()) {
throw SmithyValidationErrors.validationException(violations);