fix(ses): validate the topic in SetIdentityNotificationTopic (#8202)

* fix(ses): validate the topic in SetIdentityNotificationTopic
* test(ses): share the SNS topic helpers in the notification topic test
* refactor(ses): build the notificationType enum violation with the shared helper
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-11-13 15:58:23 -04:00
1 parent 8c31942e84
commit 898fee8bb1
10 files changed
+350 -51

No files matched your search

@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
@@ -18,6 +19,7 @@ import java.util.Base64;
import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -85,16 +87,7 @@ class SesIdentityNotificationTopicPublishV1IntegrationTest {
.extract().jsonPath().getString("Attributes.QueueArn");
assertNotNull(queueArn);
topicArn = given()
.contentType(JSON_10)
.header("Authorization", SNS_AUTH)
.header("X-Amz-Target", "SNS_20100331.CreateTopic")
.body("{\"Name\":\"ses-id-notif-topic\"}")
.when()
.post("/")
.then()
.statusCode(200)
.extract().jsonPath().getString("TopicArn");
topicArn = createSnsTopic(SNS_AUTH, "ses-id-notif-topic");
assertNotNull(topicArn);
given()
@@ -256,16 +249,7 @@ class SesIdentityNotificationTopicPublishV1IntegrationTest {
void aDeletedBounceTopicIsTakenOutOfTheIdentityAndForwardingTurnsBackOn() {
String gone = "id-notif-gone@floci.test";
verifyEmailIdentity(gone);
String goneTopic = given()
.contentType(JSON_10)
.header("Authorization", SNS_AUTH)
.header("X-Amz-Target", "SNS_20100331.CreateTopic")
.body("{\"Name\":\"ses-id-notif-gone-topic\"}")
.when()
.post("/")
.then()
.statusCode(200)
.extract().jsonPath().getString("TopicArn");
String goneTopic = createSnsTopic(SNS_AUTH, "ses-id-notif-gone-topic");
setIdentityNotificationTopic(gone, "Bounce", goneTopic);
setIdentityNotificationTopic(gone, "Complaint", goneTopic);
given()
@@ -278,15 +262,7 @@ class SesIdentityNotificationTopicPublishV1IntegrationTest {
.post("/")
.then()
.statusCode(200);
given()
.contentType(JSON_10)
.header("Authorization", SNS_AUTH)
.header("X-Amz-Target", "SNS_20100331.DeleteTopic")
.body("{\"TopicArn\":\"" + goneTopic + "\"}")
.when()
.post("/")
.then()
.statusCode(200);
deleteSnsTopic(SNS_AUTH, goneTopic);
sendEmail(gone, "bounce@simulator.amazonses.com", "id-notif-gone");
@@ -305,6 +281,152 @@ class SesIdentityNotificationTopicPublishV1IntegrationTest {
assertTrue(attributes.contains("<ForwardingEnabled>true</ForwardingEnabled>"), attributes);
}
@Test
@Order(11)
void aTopicThatDoesNotExistIsRefusedAndTheStoredTopicStays() {
String missing = topicArn + "-missing";
setIdentityNotificationTopicRaw(SENDER, "Bounce",
"&SnsTopic=" + URLEncoder.encode(missing, StandardCharsets.UTF_8))
.statusCode(400)
.body(containsString("<Type>Sender</Type>"))
.body(containsString("<Code>InvalidParameterValue</Code>"))
.body(containsString("<Message>SNS topic " + missing + " is invalid.</Message>"));
notificationAttributes(SENDER).body(containsString("<BounceTopic>" + topicArn + "</BounceTopic>"));
}
@Test
@Order(12)
void aTopicOfAnotherRegionIsRefused() {
String elsewhereAuth = SNS_AUTH.replace("us-east-1", "us-west-2");
String elsewhere = createSnsTopic(elsewhereAuth, "ses-id-notif-elsewhere-topic");
try {
setIdentityNotificationTopicRaw(SENDER, "Delivery",
"&SnsTopic=" + URLEncoder.encode(elsewhere, StandardCharsets.UTF_8))
.statusCode(400)
.body(containsString("<Message>SNS topic " + elsewhere + " is invalid.</Message>"));
} finally {
deleteSnsTopic(elsewhereAuth, elsewhere);
}
}
@Test
@Order(13)
void aValueThatIsNotATopicArnGetsTheSameMessage() {
setIdentityNotificationTopicRaw(SENDER, "Delivery", "&SnsTopic=not-an-arn")
.statusCode(400)
.body(containsString("<Message>SNS topic not-an-arn is invalid.</Message>"));
}
@Test
@Order(14)
void anEmptyTopicIsRefusedAndOnlyAnAbsentOneClears() {
setIdentityNotificationTopicRaw(SENDER, "Complaint", "&SnsTopic=")
.statusCode(400)
.body(containsString("<Code>InvalidParameterValue</Code>"))
.body(containsString("<Message>SNS topic cannot be empty.</Message>"));
notificationAttributes(SENDER).body(containsString("<ComplaintTopic>" + topicArn + "</ComplaintTopic>"));
setIdentityNotificationTopicRaw(SENDER, "Complaint", "").statusCode(200);
notificationAttributes(SENDER).body(containsString("<ComplaintTopic></ComplaintTopic>"));
}
@Test
@Order(15)
void theTopicIsCheckedBeforeTheIdentity() {
String unknown = "id-notif-unknown@floci.test";
setIdentityNotificationTopicRaw(unknown, "Bounce", "&SnsTopic=not-an-arn")
.statusCode(400)
.body(containsString("<Message>SNS topic not-an-arn is invalid.</Message>"));
setIdentityNotificationTopicRaw(unknown, "Bounce",
"&SnsTopic=" + URLEncoder.encode(topicArn, StandardCharsets.UTF_8))
.statusCode(400)
.body(containsString("<Code>InvalidParameterValue</Code>"))
.body(containsString("<Message>Identity " + unknown
+ " is invalid. Must be a verified email address or domain.</Message>"));
setIdentityNotificationTopicRaw(SENDER, "Bogus", "&SnsTopic=not-an-arn")
.statusCode(400)
.body(containsString("<Code>ValidationError</Code>"));
setIdentityNotificationTopicRaw(" " + SENDER, "Bounce", "&SnsTopic=not-an-arn")
.statusCode(400)
.body(containsString("<Code>InvalidParameterValue</Code>"))
.body(containsString("<Message>Identity " + SENDER
+ " is invalid. Must be a valid email address or domain or identity ARN.</Message>"));
}
@Test
@Order(16)
void theMissingMembersAreReportedTogether() {
given()
.contentType(FORM)
.header("Authorization", SES_AUTH)
.body("Action=SetIdentityNotificationTopic&SnsTopic=not-an-arn&Version=2010-12-01")
.when()
.post("/")
.then()
.statusCode(400)
.body(containsString("<Code>ValidationError</Code>"))
.body(containsString("<Message>2 validation errors detected: Value at &apos;identity&apos; "
+ "failed to satisfy constraint: Member must not be null; "
+ "Value at &apos;notificationType&apos; failed to satisfy constraint: "
+ "Member must not be null</Message>"));
}
private static String createSnsTopic(String auth, String name) {
return given()
.contentType(JSON_10)
.header("Authorization", auth)
.header("X-Amz-Target", "SNS_20100331.CreateTopic")
.body("{\"Name\":\"" + name + "\"}")
.when()
.post("/")
.then()
.statusCode(200)
.extract().jsonPath().getString("TopicArn");
}
private static void deleteSnsTopic(String auth, String topicArn) {
given()
.contentType(JSON_10)
.header("Authorization", auth)
.header("X-Amz-Target", "SNS_20100331.DeleteTopic")
.body("{\"TopicArn\":\"" + topicArn + "\"}")
.when()
.post("/")
.then()
.statusCode(200);
}
private ValidatableResponse setIdentityNotificationTopicRaw(String identity, String notificationType,
String topicParam) {
return given()
.contentType(FORM)
.header("Authorization", SES_AUTH)
.body("Action=SetIdentityNotificationTopic"
+ "&Identity=" + URLEncoder.encode(identity, StandardCharsets.UTF_8)
+ "&NotificationType=" + notificationType
+ topicParam
+ "&Version=2010-12-01")
.when()
.post("/")
.then();
}
private ValidatableResponse notificationAttributes(String identity) {
return given()
.contentType(FORM)
.header("Authorization", SES_AUTH)
.body("Action=GetIdentityNotificationAttributes&Identities.member.1="
+ URLEncoder.encode(identity, StandardCharsets.UTF_8) + "&Version=2010-12-01")
.when()
.post("/")
.then()
.statusCode(200);
}
private void verifyEmailIdentity(String email) {
given()
.contentType(FORM)
@@ -49,6 +49,8 @@ class SesIdentityServiceTest {
private static final String REGION = "us-east-1";
private static final String TOPIC = "arn:aws:sns:us-east-1:000000000000:feedback";
private static final String OTHER_TOPIC = "arn:aws:sns:us-east-1:000000000000:other-feedback";
private static final Predicate<String> ANY_TOPIC = topic -> true;
private static final Predicate<String> NO_TOPIC = topic -> false;
private SesIdentityService service;
@BeforeEach
@@ -343,11 +345,105 @@ class SesIdentityServiceTest {
service.createEmailIdentity(identity, null, null, REGION, null);
}
@Test
void setIdentityNotificationTopic_refusesATopicSesCannotPublishToAndKeepsTheStoredOne() {
service.verifyEmailIdentity("alice@example.com", REGION);
service.setIdentityNotificationTopic("alice@example.com", "Bounce", TOPIC, REGION, ANY_TOPIC);
AwsException e = assertThrows(AwsException.class, () -> service.setIdentityNotificationTopic(
"alice@example.com", "Bounce", OTHER_TOPIC, REGION, NO_TOPIC));
assertEquals("InvalidParameterValue", e.getErrorCode());
assertEquals("SNS topic " + OTHER_TOPIC + " is invalid.", e.getMessage());
assertEquals(400, e.getHttpStatus());
Identity identity = service.find("alice@example.com", REGION).orElseThrow();
assertEquals(TOPIC, identity.getNotificationAttributes().get("BounceTopic"));
}
@ParameterizedTest
@MethodSource("emptyTopics")
void setIdentityNotificationTopic_refusesAnEmptyTopicInsteadOfClearing(String empty) {
service.verifyEmailIdentity("alice@example.com", REGION);
service.setIdentityNotificationTopic("alice@example.com", "Bounce", TOPIC, REGION, ANY_TOPIC);
AwsException e = assertThrows(AwsException.class, () -> service.setIdentityNotificationTopic(
"alice@example.com", "Bounce", empty, REGION, ANY_TOPIC));
assertEquals("InvalidParameterValue", e.getErrorCode());
assertEquals("SNS topic cannot be empty.", e.getMessage());
Identity identity = service.find("alice@example.com", REGION).orElseThrow();
assertEquals(TOPIC, identity.getNotificationAttributes().get("BounceTopic"));
}
static Stream<String> emptyTopics() {
return Stream.of("", " ");
}
@Test
void setIdentityNotificationTopic_clearsWhenTheTopicIsAbsent() {
service.verifyEmailIdentity("alice@example.com", REGION);
service.setIdentityNotificationTopic("alice@example.com", "Bounce", TOPIC, REGION, ANY_TOPIC);
service.setIdentityNotificationTopic("alice@example.com", "Bounce", null, REGION, NO_TOPIC);
Identity identity = service.find("alice@example.com", REGION).orElseThrow();
assertFalse(identity.getNotificationAttributes().containsKey("BounceTopic"));
}
@Test
void setIdentityNotificationTopic_reportsTheMissingMembersTogether() {
AwsException both = assertThrows(AwsException.class, () -> service.setIdentityNotificationTopic(
null, null, TOPIC, REGION, NO_TOPIC));
assertEquals("ValidationError", both.getErrorCode());
assertEquals("2 validation errors detected: Value at 'identity' failed to satisfy constraint: "
+ "Member must not be null; Value at 'notificationType' failed to satisfy constraint: "
+ "Member must not be null", both.getMessage());
AwsException emptyType = assertThrows(AwsException.class, () -> service.setIdentityNotificationTopic(
"alice@example.com", "", TOPIC, REGION, NO_TOPIC));
assertEquals("1 validation error detected: Value at 'notificationType' failed to satisfy constraint: "
+ "Member must satisfy enum value set: [Bounce, Complaint, Delivery]", emptyType.getMessage());
}
@ParameterizedTest
@MethodSource("malformedIdentities")
void setIdentityNotificationTopic_refusesAMalformedIdentityBeforeTheTopic(String malformed) {
AwsException e = assertThrows(AwsException.class, () -> service.setIdentityNotificationTopic(
malformed, "Bounce", TOPIC, REGION, NO_TOPIC));
assertEquals("InvalidParameterValue", e.getErrorCode());
assertEquals("Identity " + malformed
+ " is invalid. Must be a valid email address or domain or identity ARN.", e.getMessage());
}
static Stream<String> malformedIdentities() {
return Stream.of("", " alice@example.com", "alice@example.com ");
}
@Test
void setIdentityNotificationTopic_checksTheTypeThenTheTopicThenTheIdentity() {
AwsException type = assertThrows(AwsException.class, () -> service.setIdentityNotificationTopic(
"nobody@example.com", "Bogus", TOPIC, REGION, NO_TOPIC));
assertEquals("ValidationError", type.getErrorCode());
assertEquals("1 validation error detected: Value at 'notificationType' failed to satisfy constraint: "
+ "Member must satisfy enum value set: [Bounce, Complaint, Delivery]", type.getMessage());
AwsException topic = assertThrows(AwsException.class, () -> service.setIdentityNotificationTopic(
"nobody@example.com", "Bounce", TOPIC, REGION, NO_TOPIC));
assertEquals("SNS topic " + TOPIC + " is invalid.", topic.getMessage());
AwsException identity = assertThrows(AwsException.class, () -> service.setIdentityNotificationTopic(
"nobody@example.com", "Bounce", TOPIC, REGION, ANY_TOPIC));
assertEquals("InvalidParameterValue", identity.getErrorCode());
assertEquals("Identity nobody@example.com is invalid. Must be a verified email address or domain.",
identity.getMessage());
}
@Test
void aRefusedTopicIsTakenOutOfEveryBounceAndComplaintSettingThatNamesIt() {
service.verifyEmailIdentity("alice@example.com", REGION);
for (String type : List.of("Bounce", "Complaint", "Delivery")) {
service.setIdentityNotificationTopic("alice@example.com", type, TOPIC, REGION);
service.setIdentityNotificationTopic("alice@example.com", type, TOPIC, REGION, ANY_TOPIC);
}
service.setFeedbackForwardingEnabled("alice@example.com", false, REGION);
@@ -363,8 +459,8 @@ class SesIdentityServiceTest {
@Test
void aSettingNamingAnotherTopicIsKept() {
service.verifyEmailIdentity("alice@example.com", REGION);
service.setIdentityNotificationTopic("alice@example.com", "Bounce", TOPIC, REGION);
service.setIdentityNotificationTopic("alice@example.com", "Complaint", OTHER_TOPIC, REGION);
service.setIdentityNotificationTopic("alice@example.com", "Bounce", TOPIC, REGION, ANY_TOPIC);
service.setIdentityNotificationTopic("alice@example.com", "Complaint", OTHER_TOPIC, REGION, ANY_TOPIC);
service.dropRefusedNotificationTopic("alice@example.com", TOPIC, REGION);
@@ -376,7 +472,7 @@ class SesIdentityServiceTest {
@Test
void aTopicNamedOnlyForDeliveriesStaysConfigured() {
service.verifyEmailIdentity("alice@example.com", REGION);
service.setIdentityNotificationTopic("alice@example.com", "Delivery", TOPIC, REGION);
service.setIdentityNotificationTopic("alice@example.com", "Delivery", TOPIC, REGION, ANY_TOPIC);
service.setFeedbackForwardingEnabled("alice@example.com", false, REGION);
service.dropRefusedNotificationTopic("alice@example.com", TOPIC, REGION);
@@ -389,7 +485,7 @@ class SesIdentityServiceTest {
@Test
void aTopicSetAgainSinceTheRefusalIsKept() {
service.verifyEmailIdentity("alice@example.com", REGION);
service.setIdentityNotificationTopic("alice@example.com", "Bounce", OTHER_TOPIC, REGION);
service.setIdentityNotificationTopic("alice@example.com", "Bounce", OTHER_TOPIC, REGION, ANY_TOPIC);
service.setFeedbackForwardingEnabled("alice@example.com", false, REGION);
service.dropRefusedNotificationTopic("alice@example.com", TOPIC, REGION);
@@ -403,7 +499,7 @@ class SesIdentityServiceTest {
return Stream.of(
Arguments.of("a topic",
(Consumer<SesIdentityService>) identities -> identities.setIdentityNotificationTopic(
"alice@example.com", "Bounce", OTHER_TOPIC, REGION),
"alice@example.com", "Bounce", OTHER_TOPIC, REGION, ANY_TOPIC),
(Predicate<Identity>) identity ->
OTHER_TOPIC.equals(identity.getNotificationAttributes().get("BounceTopic"))),
Arguments.of("feedback forwarding",
@@ -424,7 +520,7 @@ class SesIdentityServiceTest {
InMemoryStorage<String, Identity> store = Mockito.spy(new InMemoryStorage<String, Identity>());
SesIdentityService parked = new SesIdentityService(store, null, Clock.systemUTC());
parked.verifyEmailIdentity("alice@example.com", REGION);
parked.setIdentityNotificationTopic("alice@example.com", "Bounce", TOPIC, REGION);
parked.setIdentityNotificationTopic("alice@example.com", "Bounce", TOPIC, REGION, ANY_TOPIC);
CountDownLatch read = new CountDownLatch(1);
CountDownLatch release = new CountDownLatch(1);
// The removal parks right after reading the identity, before writing it back.
@@ -357,6 +357,18 @@ class SesIntegrationTest {
@Test
@Order(17)
void setIdentityNotificationTopic() {
// SES refuses a topic that does not exist.
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/sns/aws4_request")
.formParam("Action", "CreateTopic")
.formParam("Name", "bounce-topic")
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("arn:aws:sns:us-east-1:000000000000:bounce-topic"));
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/email/aws4_request")
@@ -186,14 +186,21 @@ class SesSnsPublishAuthorizationIntegrationTest {
.body(containsString("<DeliveryTopic>" + topicArn + "</DeliveryTopic>"));
}
/** Probed: SetIdentityNotificationTopic refuses a topic whose policy does not let SES publish. */
@Test
void anotherAccountsTopicWithoutAnSesGrantRefusesTheNotification() {
String topicArn = createTopic(OTHER, "foreign-" + suffix());
void anIdentityCannotNameATopicThatRefusesSes() {
String sender = createSender();
setIdentityNotificationTopic(sender, "Bounce", topicArn);
sendEmail(sender, BOUNCE, null);
String closed = createTopic(OWNER, "closed-" + suffix());
setTopicPolicy(OWNER, closed, rootOnly(closed));
String foreign = createTopic(OTHER, "foreign-" + suffix());
for (String topicArn : List.of(closed, foreign)) {
ses(OWNER, "SetIdentityNotificationTopic",
Map.of("Identity", sender, "NotificationType", "Bounce", "SnsTopic", topicArn))
.statusCode(400)
.body(containsString("<Code>InvalidParameterValue</Code>"))
.body(containsString("SNS topic " + topicArn + " is invalid."));
}
notificationAttributes(sender).body(containsString("<BounceTopic></BounceTopic>"));
}