fix(sns): validate Publish Subject (#4172)

* fix(sns): validate Publish Subject

* fix(sns): validate Subject for SMS publishes too

* fix(sns): accept 100-character subjects and leading spaces
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-09-23 10:52:50 -04:00
1 parent 625bcb80d7
commit b6d5d51bde
2 files changed
+68

No files matched your search

@@ -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 SMS
if (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"});
@@ -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));
}
@Test
void 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]);
}
@Test
void tagResource_and_listTags() {
Topic topic = snsService.createTopic("my-topic", null, null, REGION);