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);
@@ -65,4 +65,15 @@ class SmithyValidationErrorsTest {
+ "Member must have length greater than or equal to 1",
SmithyValidationErrors.lengthTooShort("name", " ", 1));
}
@Test
void lengthBoundsCountSupplementaryCharactersOnce() {
String value = "\uD83D\uDE00";
assertNull(SmithyValidationErrors.lengthOutOfRange("name", value, 1, 1));
assertEquals(SmithyValidationErrors.lengthTooShort("name", value, 2),
SmithyValidationErrors.lengthOutOfRange("name", value, 2, 3));
assertEquals("Value '" + value.repeat(2) + "' at 'name' failed to satisfy constraint: "
+ "Member must have length less than or equal to 1",
SmithyValidationErrors.lengthOutOfRange("name", value.repeat(2), 0, 1));
}
}
@@ -259,6 +259,39 @@ class CompositeAlarmServiceTest {
assertEquals(remove ? Map.of() : Map.of("team", "new"), result.getTags());
}
@Test
void supplementaryCharactersRespectAlarmAndRuleLengthBounds() {
String scalar = "\uD83D\uDE00";
put(scalar.repeat(255), "TRUE");
assertThrows(AwsException.class, () -> put(scalar.repeat(256), "TRUE"));
String rule = "ALARM(\"" + scalar.repeat(10231) + "\")";
assertDoesNotThrow(() -> CompositeAlarmRule.parse(rule));
assertThrows(AwsException.class, () -> CompositeAlarmRule.parse(rule + " "));
}
@Test
void blankAlarmFieldsRetainLengthErrorPrecedence() {
for (String name : List.of("", " ", " ".repeat(256))) {
AwsException error = assertThrows(AwsException.class, () -> put(name, "TRUE"));
String bound = name.length() > 255 ? "less than or equal to 255" : "greater than or equal to 1";
assertEquals("1 validation error detected: Value '" + name
+ "' at 'alarmName' failed to satisfy constraint: Member must have length " + bound,
error.getMessage());
}
for (String rule : List.of("", " ", " ".repeat(10241))) {
AwsException error = assertThrows(AwsException.class, () -> CompositeAlarmRule.parse(rule));
String bound = rule.length() > 10240 ? "less than or equal to 10240" : "greater than or equal to 1";
assertEquals("1 validation error detected: Value '" + rule
+ "' at 'alarmRule' failed to satisfy constraint: Member must have length " + bound,
error.getMessage());
}
assertEquals("1 validation error detected: Value null at 'alarmName' failed to satisfy constraint: "
+ "Member must not be null", assertThrows(AwsException.class, () -> put(null, "TRUE")).getMessage());
assertEquals("1 validation error detected: Value null at 'alarmRule' failed to satisfy constraint: "
+ "Member must not be null", assertThrows(AwsException.class,
() -> CompositeAlarmRule.parse(null)).getMessage());
}
private CompositeAlarm put(String name, String rule) {
CompositeAlarm alarm = new CompositeAlarm();
alarm.setAlarmName(name);
@@ -381,6 +381,21 @@ class DynamoDbExportIntegrationTest {
.body("ExportDescription.S3Prefix", equalTo("ends-with/"));
}
@Test
void s3PrefixLengthCountsSupplementaryCharactersOnce() {
String scalar = "\uD83D\uDE00";
String shortPrefix = scalar.repeat(2);
expectError(exportWithPrefix("ExportPrefixMissing", shortPrefix), "ValidationException",
"Invalid Request: 1 validation error detected: Value '" + shortPrefix
+ "' at 's3Prefix' failed to satisfy constraint: Member must have length greater than or equal to 3");
expectError(exportWithPrefix("ExportPrefixMissing", scalar.repeat(1024)), "TableNotFoundException",
"Table not found: " + TABLE_ARN_PREFIX + "ExportPrefixMissing");
String longPrefix = scalar.repeat(1025);
expectError(exportWithPrefix("ExportPrefixMissing", longPrefix), "ValidationException",
"1 validation error detected: Value '" + longPrefix
+ "' at 's3Prefix' failed to satisfy constraint: Member must have length less than or equal to 1024");
}
// --- Helpers ---
private static ValidatableResponse exportWithPrefix(String table, String prefix) {
@@ -308,6 +308,18 @@ class EventBridgeServiceTest {
.getEventSourceArn());
}
@Test
void archiveLengthBoundsCountSupplementaryCharactersOnce() {
String scalar = "\uD83D\uDE00";
String pattern = "{\"detail\":[\"" + scalar.repeat(4081) + "\"]}";
assertDoesNotThrow(() -> service.createArchive(
"unicode-archive", DEFAULT_BUS, scalar.repeat(512), pattern, 0, REGION));
assertThrows(AwsException.class, () -> service.createArchive(
"long-description", DEFAULT_BUS, scalar.repeat(513), null, 0, REGION));
assertThrows(AwsException.class, () -> service.createArchive(
"long-pattern", DEFAULT_BUS, null, pattern + " ", 0, REGION));
}
/** The messages AWS returns; the account is checked before the region. */
@ParameterizedTest
@CsvSource(delimiter = '|', value = {