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

@@ -201,6 +201,17 @@ public class SesEventPublisher {
ed.getName(), eventType);
}
/**
* Whether an identity may name {@code topicArn} for its notifications: the topic exists in
* {@code region}, the identity's own, and lets SES publish for {@code identityArn}. Probed:
* {@code SetIdentityNotificationTopic} refuses a topic of another region and one whose policy
* denies SES, not only a missing one.
*/
public boolean mayNotifyIdentityTopic(String topicArn, String identityArn, String identityAccountId,
String region) {
return snsService.topicExists(topicArn, region) && sesMayPublish(topicArn, identityArn, identityAccountId);
}
/** Whether the topic's policy lets SES publish to it for {@code sourceArn}, owned by {@code sourceAccount}. */
private boolean sesMayPublish(String topicArn, String sourceArn, String sourceAccount) {
return serviceCallAuthorizer.authorize("ses", "sns:Publish", topicArn, sourceArn, sourceAccount)
@@ -30,6 +30,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -401,13 +402,51 @@ public class SesIdentityService implements SesTaggable {
static final List<String> NOTIFICATION_TYPES = List.of("Bounce", "Complaint", "Delivery");
/**
* Probed: SES answers in this order. The members the model requires come first, reported
* together; then the shape of the identity, where an empty value and one with whitespace around
* it are refused; then the topic; and only then whether the identity exists, so a bad topic on
* an unknown identity reports the topic. Every topic SES cannot use (malformed, missing, in
* another region, or one whose policy refuses SES) gets the same message, an empty value is
* refused instead of clearing, and only an absent {@code SnsTopic} clears the setting. A
* refused call leaves the stored topic as it was.
*
* @param publishableTopic whether SES may publish to the topic for this identity
*/
public void setIdentityNotificationTopic(String identityValue, String notificationType,
String snsTopic, String region) {
String snsTopic, String region,
Predicate<String> publishableTopic) {
List<String> violations = new ArrayList<>();
if (identityValue == null) {
violations.add(SmithyValidationErrors.notNullWithoutValue("identity"));
}
if (notificationType == null) {
violations.add(SmithyValidationErrors.notNullWithoutValue("notificationType"));
} else if (!NOTIFICATION_TYPES.contains(notificationType)) {
violations.add(SmithyValidationErrors.Violations.WITHOUT_VALUES.notInEnum(
notificationType, "notificationType", NOTIFICATION_TYPES));
}
if (!violations.isEmpty()) {
throw SmithyValidationErrors.validationError(violations);
}
if (identityValue.isBlank() || !identityValue.equals(identityValue.strip())) {
throw new AwsException("InvalidParameterValue", "Identity " + identityValue
+ " is invalid. Must be a valid email address or domain or identity ARN.", 400);
}
if (snsTopic != null) {
if (snsTopic.isBlank()) {
throw new AwsException("InvalidParameterValue", "SNS topic cannot be empty.", 400);
}
if (!publishableTopic.test(snsTopic)) {
throw new AwsException("InvalidParameterValue", "SNS topic " + snsTopic + " is invalid.", 400);
}
}
synchronized (identityCreationLock) {
Identity identity = identityStore.get(identityKey(region, identityValue))
.orElseThrow(() -> new AwsException("InvalidParameterValue",
"Identity does not exist: " + identityValue, 400));
if (snsTopic != null && !snsTopic.isBlank()) {
"Identity " + identityValue
+ " is invalid. Must be a verified email address or domain.", 400));
if (snsTopic != null) {
identity.getNotificationAttributes().put(notificationType + "Topic", snsTopic);
} else {
identity.getNotificationAttributes().remove(notificationType + "Topic");
@@ -472,9 +511,8 @@ public class SesIdentityService implements SesTaggable {
}
if (!NOTIFICATION_TYPES.contains(notificationType)) {
throw SmithyValidationErrors.validationError(
"Value at 'notificationType' failed to satisfy "
+ "constraint: Member must satisfy enum value set: "
+ NOTIFICATION_TYPES);
SmithyValidationErrors.Violations.WITHOUT_VALUES.notInEnum(
notificationType, "notificationType", NOTIFICATION_TYPES));
}
synchronized (identityCreationLock) {
Identity identity = identityStore.get(identityKey(region, identityValue))
@@ -376,8 +376,7 @@ public class SesQueryHandler {
String identityValue = getParam(params, "Identity");
String notificationType = getParam(params, "NotificationType");
String snsTopic = getParam(params, "SnsTopic");
identityService.setIdentityNotificationTopic(identityValue, notificationType, snsTopic,
region);
sesService.setIdentityNotificationTopic(identityValue, notificationType, snsTopic, region);
return Response.ok(AwsQueryResponse.envelopeEmptyResult("SetIdentityNotificationTopic", AwsNamespaces.SES)).build();
}
@@ -769,6 +769,15 @@ public class SesService {
return targets;
}
public void setIdentityNotificationTopic(String identityValue, String notificationType,
String snsTopic, String region) {
String accountId = callerAccountId();
identityService.setIdentityNotificationTopic(identityValue, notificationType, snsTopic, region,
topicArn -> eventPublisher == null || eventPublisher.mayNotifyIdentityTopic(topicArn,
AwsArnUtils.Arn.of("ses", region, accountId, "identity/" + identityValue).toString(),
accountId, region));
}
private static String notificationTopicFor(Identity identity, String type) {
if (identity == null) {
return null;