diff --git a/src/main/java/io/github/hectorvent/floci/services/sns/SnsJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/sns/SnsJsonHandler.java index 1736d19a50..9ec8fb7469 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sns/SnsJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/sns/SnsJsonHandler.java @@ -228,10 +228,12 @@ public class SnsJsonHandler { JsonNode attrsNode = entryNode.path("MessageAttributes"); if (attrsNode.isObject()) { - Map messageAttributes = new HashMap<>(); - attrsNode.fields().forEachRemaining(field -> - messageAttributes.put(field.getKey(), field.getValue().path("StringValue").asText()) - ); + Map messageAttributes = new HashMap<>(); + attrsNode.fields().forEachRemaining(field -> { + String dataType = field.getValue().path("DataType").asText("String"); + String stringValue = field.getValue().path("StringValue").asText(); + messageAttributes.put(field.getKey(), new MessageAttributeValue(stringValue, dataType)); + }); entry.put("MessageAttributes", messageAttributes); } diff --git a/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java b/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java index 6158d13e5e..51e96e6aad 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java @@ -41,6 +41,7 @@ public class SnsService { private static final Logger LOG = Logger.getLogger(SnsService.class); private static final Duration FIFO_DEDUP_WINDOW = Duration.ofMinutes(5); + private static final int MAX_PUBLISH_SIZE = 262_144; private static final List PENDING_CONFIRMATION_PROTOCOLS = List.of("http", "https", "email", "email-json", "sms"); @@ -273,6 +274,12 @@ public class SnsService { public String publish(String topicArn, String targetArn, String phoneNumber, String message, String subject, Map messageAttributes, String messageGroupId, String messageDeduplicationId, String region) { + int payloadSize = computePublishSize(message, subject, messageAttributes); + if (payloadSize > MAX_PUBLISH_SIZE) { + throw new AwsException("InvalidParameterException", + "Invalid parameter: Message too long", 400); + } + // Send SMS if (phoneNumber != null) { return UUID.randomUUID().toString(); @@ -355,6 +362,21 @@ public class SnsService { String topicStoreKey = topicKey(region, topicArn); Topic topic = topicStore.get(topicStoreKey) .orElseThrow(() -> new AwsException("NotFound", "Topic does not exist.", 404)); + + int batchSize = 0; + for (Map entry : entries) { + String message = (String) entry.get("Message"); + String subject = (String) entry.get("Subject"); + @SuppressWarnings("unchecked") + Map attrs = + (Map) entry.get("MessageAttributes"); + batchSize += computePublishSize(message, subject, attrs); + } + if (batchSize > MAX_PUBLISH_SIZE) { + throw new AwsException("BatchRequestTooLong", + "Batch requests cannot be longer than " + MAX_PUBLISH_SIZE + " bytes.", 400); + } + boolean isFifo = "true".equals(topic.getAttributes().get("FifoTopic")); List successful = new ArrayList<>(); List failed = new ArrayList<>(); @@ -768,6 +790,32 @@ public class SnsService { return AwsArnUtils.arnToQueueUrl(arn, baseUrl); } + private static int computePublishSize(String message, String subject, + Map attributes) { + int total = message == null ? 0 : message.getBytes(StandardCharsets.UTF_8).length; + if (subject != null) { + total += subject.getBytes(StandardCharsets.UTF_8).length; + } + if (attributes != null) { + for (Map.Entry entry : attributes.entrySet()) { + total += entry.getKey().getBytes(StandardCharsets.UTF_8).length; + MessageAttributeValue value = entry.getValue(); + if (value == null) { + continue; + } + if (value.getDataType() != null) { + total += value.getDataType().getBytes(StandardCharsets.UTF_8).length; + } + if (value.getBinaryValue() != null) { + total += value.getBinaryValue().length; + } else if (value.getStringValue() != null) { + total += value.getStringValue().getBytes(StandardCharsets.UTF_8).length; + } + } + } + return total; + } + private static String sha256(String message) { try { MessageDigest md = MessageDigest.getInstance("SHA-256"); diff --git a/src/test/java/io/github/hectorvent/floci/services/sns/SnsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/sns/SnsServiceTest.java index 762bc6f476..754f993ae2 100644 --- a/src/test/java/io/github/hectorvent/floci/services/sns/SnsServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/sns/SnsServiceTest.java @@ -220,6 +220,41 @@ class SnsServiceTest { assertEquals("ops", tags.get("team")); } + @Test + void publish_messageExceedsSizeLimit_throwsInvalidParameterException() { + Topic topic = snsService.createTopic("size-topic", null, null, REGION); + String tooBig = "x".repeat(262_145); + AwsException ex = assertThrows(AwsException.class, () -> + snsService.publish(topic.getTopicArn(), null, tooBig, null, REGION)); + assertEquals("InvalidParameterException", ex.getErrorCode()); + assertTrue(ex.getMessage().toLowerCase().contains("too long")); + } + + @Test + void publish_messagePlusAttributesExceedsLimit_throws() { + Topic topic = snsService.createTopic("size-topic", null, null, REGION); + String body = "x".repeat(262_100); + Map attrs = Map.of( + "longAttribute", new io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue( + "y".repeat(200), "String")); + AwsException ex = assertThrows(AwsException.class, () -> + snsService.publish(topic.getTopicArn(), null, null, body, null, attrs, REGION)); + assertEquals("InvalidParameterException", ex.getErrorCode()); + } + + @Test + void publishBatch_totalSizeExceedsLimit_throwsBatchRequestTooLong() { + Topic topic = snsService.createTopic("size-topic", null, null, REGION); + String halfMeg = "x".repeat(130_000); + List> entries = List.of( + Map.of("Id", "1", "Message", halfMeg), + Map.of("Id", "2", "Message", halfMeg), + Map.of("Id", "3", "Message", halfMeg)); + AwsException ex = assertThrows(AwsException.class, () -> + snsService.publishBatch(topic.getTopicArn(), entries, REGION)); + assertEquals("BatchRequestTooLong", ex.getErrorCode()); + } + @Test void subscriptionsConfirmed_countsCorrectly() { Topic topic = snsService.createTopic("my-topic", null, null, REGION);