Light, fluffy, and always free - The AWS Local Emulator alternative
fix(sns): validate Publish Subject (#4172)
Aniket Shuklab6d5d51bdea1
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.javaindex 503207ab6f..1f9573d357 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@@ -75,6 +75,7 @@ public class SnsService implements Resettable, ResourceProvider {private static final int LARGE_PAYLOAD_SUBSCRIPTION_LIMIT = 100;private static final String MAXIMUM_MESSAGE_SIZE = "MaximumMessageSize";private static final int PUSH_CAPTURE_LIMIT = 1000;+ private static final int MAX_SUBJECT_LENGTH = 100;private static final String CONTROL_TOWER_AGGREGATE_SECURITY_TOPIC ="aws-controltower-AggregateSecurityNotifications";private static final List<String> PENDING_CONFIRMATION_PROTOCOLS =@@ -550,6 +551,7 @@ public class SnsService implements Resettable, ResourceProvider {// The limit is a per-topic attribute, so it cannot be applied until the topic is in// hand. SMS and mobile-push publishes never reach a topic and keep the AWS default.int payloadSize = computePublishSize(messageBytes, messageAttributes);+ validateSubject(subject);// Send SMSif (phoneNumber != null) {@@ -909,6 +911,23 @@ public class SnsService implements Resettable, ResourceProvider {}}+ /**+ * Subjects must be UTF-8 text with no line breaks or control characters and at most 100+ * characters long. A null or empty subject is the absent optional parameter and is accepted.+ */+ static void validateSubject(String subject) {+ if (subject == null || subject.isEmpty()) {+ return;+ }+ if (subject.length() > MAX_SUBJECT_LENGTH+ || subject.chars().anyMatch(Character::isISOControl)) {+ throw new AwsException("InvalidParameter",+ "Invalid parameter: Subject Reason: Subjects must be UTF-8 text with no line"+ + " breaks or control characters, and at most 100 characters long.",+ 400);+ }+ }+private void recordPushNotification(PushNotification notification) {pushCapture.addFirst(notification);while (pushCapture.size() > PUSH_CAPTURE_LIMIT) {@@ -1023,6 +1042,7 @@ public class SnsService implements Resettable, ResourceProvider {String messageDeduplicationId = (String) entry.get("MessageDeduplicationId");try {+ validateSubject(subject);validateTopicMessageStructure(message, messageStructure);} catch (AwsException e) {failed.add(new String[]{id, e.getErrorCode(), e.getMessage(), "true"});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.javaindex 0da2fdf9dd..9c335b6279 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@@ -215,6 +215,12 @@ class SnsServiceTest {assertNotNull(messageId);}+ @Test+ void publish_smsWithInvalidSubject_throwsInvalidParameter() {+ assertThrows(AwsException.class, () ->+ snsService.publish(null, null, "+819012345678", "Hello phone!", "x".repeat(150), null, REGION));+ }+@Testvoid publish_requiresTopicArn() {assertThrows(AwsException.class,@@ -235,6 +241,48 @@ class SnsServiceTest {assertNotNull(messageId);}+ @Test+ void publish_subjectWithinLimits_succeeds() {+ Topic topic = snsService.createTopic("my-topic", null, null, REGION);+ String messageId = snsService.publish(topic.getTopicArn(), null, "Hello!", "a".repeat(100), REGION);+ assertNotNull(messageId);+ }++ @Test+ void publish_subjectTooLong_throwsInvalidParameter() {+ Topic topic = snsService.createTopic("my-topic", null, null, REGION);+ AwsException ex = assertThrows(AwsException.class, () ->+ snsService.publish(topic.getTopicArn(), null, "Hello!", "a".repeat(101), REGION));+ assertEquals("InvalidParameter", ex.getErrorCode());+ }++ @Test+ void publish_subjectWithLineBreak_throwsInvalidParameter() {+ Topic topic = snsService.createTopic("my-topic", null, null, REGION);+ AwsException ex = assertThrows(AwsException.class, () ->+ snsService.publish(topic.getTopicArn(), null, "Hello!", "line one\nline two", REGION));+ assertEquals("InvalidParameter", ex.getErrorCode());+ }++ @Test+ void publish_subjectStartingWithSpace_succeeds() {+ Topic topic = snsService.createTopic("my-topic", null, null, REGION);+ String messageId = snsService.publish(topic.getTopicArn(), null, "Hello!", " leading space", REGION);+ assertNotNull(messageId);+ }++ @Test+ void publishBatch_subjectTooLong_marksEntryFailed() {+ Topic topic = snsService.createTopic("my-topic", null, null, REGION);+ List<Map<String, Object>> entries = List.of(+ Map.of("Id", "1", "Message", "Hello!", "Subject", "b".repeat(150)));+ SnsService.BatchPublishResult result =+ snsService.publishBatch(topic.getTopicArn(), entries, REGION);+ assertTrue(result.successful().isEmpty());+ assertEquals(1, result.failed().size());+ assertEquals("InvalidParameter", result.failed().get(0)[1]);+ }+@Testvoid tagResource_and_listTags() {Topic topic = snsService.createTopic("my-topic", null, null, REGION);