diff --git a/src/main/java/io/github/hectorvent/floci/core/common/SmithyValidationErrors.java b/src/main/java/io/github/hectorvent/floci/core/common/SmithyValidationErrors.java index b74b55a719..37c08d28f7 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/SmithyValidationErrors.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/SmithyValidationErrors.java @@ -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; } diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java index aa38fe3d1b..008f5abcd2 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java @@ -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) { diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CompositeAlarmRule.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CompositeAlarmRule.java index 38a583016d..444cadb3d5 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CompositeAlarmRule.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CompositeAlarmRule.java @@ -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(); diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java index d468b285a6..5a8b6b1255 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java @@ -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 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; diff --git a/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeService.java b/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeService.java index 21e5765f33..dec3a90d0d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeService.java +++ b/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeService.java @@ -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); diff --git a/src/test/java/io/github/hectorvent/floci/core/common/SmithyValidationErrorsTest.java b/src/test/java/io/github/hectorvent/floci/core/common/SmithyValidationErrorsTest.java index 72d49af967..92854195a2 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/SmithyValidationErrorsTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/SmithyValidationErrorsTest.java @@ -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)); + } + } diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CompositeAlarmServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CompositeAlarmServiceTest.java index 42797f93b4..7f50afc26a 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CompositeAlarmServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CompositeAlarmServiceTest.java @@ -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); diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbExportIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbExportIntegrationTest.java index 6ec9aef238..cf7eef7b51 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbExportIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbExportIntegrationTest.java @@ -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) { diff --git a/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeServiceTest.java index 01dd2c2648..86c743bf3d 100644 --- a/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeServiceTest.java @@ -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 = {