fix(cognito): send email from the pool's EmailConfiguration sender (#5150)

* fix(cognito): send email from the pool's EmailConfiguration sender

Cognito email always came from no-reply@verificationemail.com. It now
follows EmailConfigurationType: with DEVELOPER, From when set, otherwise
the SourceArn identity's address; with COGNITO_DEFAULT, the SourceArn
address. A domain identity takes its address from From. Without a
SourceArn, the default address is kept.

* test(cognito): read captured mail without the ?email= filter

* fix(cognito): send from the configured sender only when SES has verified it

A pool's EmailConfiguration sender is now used only when its SourceArn
names an SES identity in the pool's account that SES has verified in the
SourceArn Region (the pool's Region for a wildcard Region), and a From
that supplies the sender is one mailbox at that address or in that
domain. Anything else sends from no-reply@verificationemail.com and logs
a warning, so a pool can no longer send as an address its account has
not verified.

When From has a sender name, the bare address now goes in ReturnPath.
SES takes Source as the default return path, so the captured message's
ReturnPath, and the envelope sender handed to the SMTP relay, was
"Name <address>". The relay's mail client happened to strip the name
from MAIL FROM; the envelope no longer depends on that.

* style(cognito): use one parameterised message in the sender fallback log

* fix(cognito): accept subdomains of a domain identity and match email identities exactly

A verified SES domain covers its subdomains, so a From in one of them now sends instead of falling back. Email address identities are case sensitive, domain part included, so the From must equal the identity. The SourceArn must also be in the user pool's partition, not only its account.

* refactor(ses): keep one definition of a verified identity

SesService.isVerifiedIdentity now delegates to SesIdentityService.isIdentityVerified, made package-private, instead of repeating its check.
This commit is contained in:
jwoa95 authored and GitHub committed 2026-10-06 13:51:51 -04:00
1 parent a33eea05cd
commit 45f45e590e
5 files changed
+511 -1

No files matched your search

@@ -720,6 +720,101 @@ class CognitoIntegrationTest {
assertEquals("CONFIRMED", confirmedUser.path("UserStatus").asText());
}
@Test
void signUpCodeIsSentFromTheEmailConfigurationFrom() throws Exception {
verifySesEmailIdentity("noreply@repro.example");
String fromUsername = signUpWithEmailConfiguration("EmailConfigurationFromPool", """
{
"EmailSendingAccount": "DEVELOPER",
"SourceArn": "arn:aws:ses:us-east-1:000000000000:identity/noreply@repro.example",
"From": "Repro App <noreply@repro.example>"
}
""");
// The sender name stays in Source, the From header, while the return path, which the SMTP
// relay uses as the envelope sender, is the bare address.
given()
.when()
.get("/_aws/ses")
.then()
.statusCode(200)
.body("messages.findAll { it.Destination?.ToAddresses?.contains('" + fromUsername + "') }",
hasSize(1))
.body("messages.find { it.Destination?.ToAddresses?.contains('" + fromUsername + "') }.Source",
equalTo("Repro App <noreply@repro.example>"))
.body("messages.find { it.Destination?.ToAddresses?.contains('" + fromUsername + "') }.ReturnPath",
equalTo("noreply@repro.example"));
}
@Test
void signUpCodeIsSentFromTheDefaultAddressWhenSesHasNotVerifiedTheSourceArn() throws Exception {
String unverified = "noreply-" + UUID.randomUUID() + "@unverified.example";
String fromUsername = signUpWithEmailConfiguration("UnverifiedSourceArnPool", """
{
"EmailSendingAccount": "DEVELOPER",
"SourceArn": "arn:aws:ses:us-east-1:000000000000:identity/%s",
"From": "Repro App <%s>"
}
""".formatted(unverified, unverified));
given()
.when()
.get("/_aws/ses")
.then()
.statusCode(200)
.body("messages.findAll { it.Destination?.ToAddresses?.contains('" + fromUsername + "') }",
hasSize(1))
.body("messages.find { it.Destination?.ToAddresses?.contains('" + fromUsername + "') }.Source",
equalTo("no-reply@verificationemail.com"));
}
/** Creates a pool with {@code emailConfiguration}, signs a new user up to it and returns the username. */
private static String signUpWithEmailConfiguration(String poolName, String emailConfiguration) throws Exception {
JsonNode poolResponse = cognitoJson("CreateUserPool", """
{
"PoolName": "%s",
"AutoVerifiedAttributes": ["email"],
"EmailConfiguration": %s
}
""".formatted(poolName, emailConfiguration));
String fromPoolId = poolResponse.path("UserPool").path("Id").asText();
JsonNode clientResponse = cognitoJson("CreateUserPoolClient", """
{
"UserPoolId": "%s",
"ClientName": "email-configuration-from-client"
}
""".formatted(fromPoolId));
String fromClientId = clientResponse.path("UserPoolClient").path("ClientId").asText();
String fromUsername = "from+" + UUID.randomUUID() + "@example.com";
cognitoAction("SignUp", """
{
"ClientId": "%s",
"Username": "%s",
"Password": "Passw0rd!",
"UserAttributes": [
{ "Name": "email", "Value": "%s" }
]
}
""".formatted(fromClientId, fromUsername, fromUsername))
.then()
.statusCode(200);
return fromUsername;
}
private static void verifySesEmailIdentity(String emailAddress) {
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/email/aws4_request")
.formParam("Action", "VerifyEmailIdentity")
.formParam("EmailAddress", emailAddress)
.when()
.post("/")
.then()
.statusCode(200);
}
@Test
void resendConfirmationCodeReplacesTheSignUpCode() throws Exception {
given().delete("/_aws/ses").then().statusCode(200);
@@ -23,6 +23,7 @@ import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
class CognitoMessageDispatcherTest {
@@ -206,6 +207,250 @@ class CognitoMessageDispatcherTest {
assertEquals("cn-northwest-1", sentEmail().region());
}
@Test
void dispatch_withoutEmailConfiguration_sendsFromTheDefaultAddress() {
dispatcher.dispatch(pool(Map.of()), user("alice@example.com", null),
VerificationCode.Purpose.SIGNUP_CONFIRMATION, "123456", List.of("EMAIL"));
assertEquals("no-reply@verificationemail.com", sentEmail().source());
}
@Test
void dispatch_developerSendingAccount_sendsFromTheConfiguredFrom() {
verified("noreply@repro.example", "us-east-1");
UserPool pool = emailConfiguredPool(Map.of(
"EmailSendingAccount", "DEVELOPER",
"SourceArn", "arn:aws:ses:us-east-1:000000000000:identity/noreply@repro.example",
"From", "Repro App <noreply@repro.example>"));
dispatcher.dispatch(pool, user("alice@example.com", null),
VerificationCode.Purpose.SIGNUP_CONFIRMATION, "123456", List.of("EMAIL"));
assertEquals("Repro App <noreply@repro.example>", sentEmail().source());
}
@Test
void dispatch_senderName_staysInTheFromHeaderAndLeavesTheReturnPathBare() {
// SES takes Source as the default return path, the envelope sender, which takes a bare
// address.
verified("noreply@repro.example", "us-east-1");
UserPool pool = emailConfiguredPool(Map.of(
"EmailSendingAccount", "DEVELOPER",
"SourceArn", "arn:aws:ses:us-east-1:000000000000:identity/noreply@repro.example",
"From", "Repro App <noreply@repro.example>"));
dispatcher.dispatch(pool, user("alice@example.com", null),
VerificationCode.Purpose.SIGNUP_CONFIRMATION, "123456", List.of("EMAIL"));
SendEmailRequest sent = sentEmail();
assertEquals("Repro App <noreply@repro.example>", sent.source());
assertEquals("noreply@repro.example", sent.returnPath());
}
@Test
void dispatch_developerSendingAccountWithoutFrom_sendsFromTheSourceArnAddress() {
verified("noreply@repro.example", "us-east-1");
UserPool pool = emailConfiguredPool(Map.of(
"EmailSendingAccount", "DEVELOPER",
"SourceArn", "arn:aws:ses:us-east-1:000000000000:identity/noreply@repro.example"));
dispatcher.dispatch(pool, user("alice@example.com", null),
VerificationCode.Purpose.SIGNUP_CONFIRMATION, "123456", List.of("EMAIL"));
assertEquals("noreply@repro.example", sentEmail().source());
}
@Test
void dispatch_cognitoDefaultSendingAccount_sendsFromTheSourceArnAddressWithoutTheSenderName() {
// With COGNITO_DEFAULT the SourceArn address is the custom FROM address; a sender name in
// From is available only with the pool's own SES (DEVELOPER).
verified("noreply@repro.example", "us-east-1");
UserPool pool = emailConfiguredPool(Map.of(
"EmailSendingAccount", "COGNITO_DEFAULT",
"SourceArn", "arn:aws:ses:us-east-1:000000000000:identity/noreply@repro.example",
"From", "Repro App <noreply@repro.example>"));
dispatcher.dispatch(pool, user("alice@example.com", null),
VerificationCode.Purpose.SIGNUP_CONFIRMATION, "123456", List.of("EMAIL"));
assertEquals("noreply@repro.example", sentEmail().source());
}
@Test
void dispatch_cognitoDefaultSendingAccountWithoutSourceArn_ignoresFrom() {
UserPool pool = emailConfiguredPool(Map.of(
"EmailSendingAccount", "COGNITO_DEFAULT",
"From", "noreply@repro.example"));
dispatcher.dispatch(pool, user("alice@example.com", null),
VerificationCode.Purpose.SIGNUP_CONFIRMATION, "123456", List.of("EMAIL"));
assertEquals("no-reply@verificationemail.com", sentEmail().source());
}
@Test
void dispatch_domainIdentity_sendsFromTheConfiguredFrom() {
// A domain identity names no address of its own, so From supplies it.
verified("repro.example", "us-east-1");
UserPool pool = emailConfiguredPool(Map.of(
"SourceArn", "arn:aws:ses:us-east-1:000000000000:identity/repro.example",
"From", "noreply@repro.example"));
dispatcher.dispatch(pool, user("alice@example.com", null),
VerificationCode.Purpose.SIGNUP_CONFIRMATION, "123456", List.of("EMAIL"));
assertEquals("noreply@repro.example", sentEmail().source());
}
@Test
void dispatch_unverifiedSourceArnIdentity_sendsFromTheDefaultAddress() {
UserPool pool = emailConfiguredPool(Map.of(
"EmailSendingAccount", "DEVELOPER",
"SourceArn", "arn:aws:ses:us-east-1:000000000000:identity/noreply@repro.example",
"From", "Repro App <noreply@repro.example>"));
dispatcher.dispatch(pool, user("alice@example.com", null),
VerificationCode.Purpose.SIGNUP_CONFIRMATION, "123456", List.of("EMAIL"));
assertEquals("no-reply@verificationemail.com", sentEmail().source());
}
@Test
void dispatch_developerSendingAccountFromWithoutSourceArn_sendsFromTheDefaultAddress() {
UserPool pool = emailConfiguredPool(Map.of(
"EmailSendingAccount", "DEVELOPER",
"From", "Repro App <noreply@repro.example>"));
dispatcher.dispatch(pool, user("alice@example.com", null),
VerificationCode.Purpose.SIGNUP_CONFIRMATION, "123456", List.of("EMAIL"));
assertEquals("no-reply@verificationemail.com", sentEmail().source());
}
@Test
void dispatch_fromOutsideTheEmailIdentity_sendsFromTheDefaultAddress() {
verified("noreply@repro.example", "us-east-1");
UserPool pool = emailConfiguredPool(Map.of(
"EmailSendingAccount", "DEVELOPER",
"SourceArn", "arn:aws:ses:us-east-1:000000000000:identity/noreply@repro.example",
"From", "Billing <billing@bank.example>"));
dispatcher.dispatch(pool, user("alice@example.com", null),
VerificationCode.Purpose.SIGNUP_CONFIRMATION, "123456", List.of("EMAIL"));
assertEquals("no-reply@verificationemail.com", sentEmail().source());
}
@Test
void dispatch_fromOutsideTheDomainIdentity_sendsFromTheDefaultAddress() {
verified("repro.example", "us-east-1");
UserPool pool = emailConfiguredPool(Map.of(
"EmailSendingAccount", "DEVELOPER",
"SourceArn", "arn:aws:ses:us-east-1:000000000000:identity/repro.example",
"From", "noreply@bank.example"));
dispatcher.dispatch(pool, user("alice@example.com", null),
VerificationCode.Purpose.SIGNUP_CONFIRMATION, "123456", List.of("EMAIL"));
assertEquals("no-reply@verificationemail.com", sentEmail().source());
}
@Test
void dispatch_fromInASubdomainOfTheDomainIdentity_sendsFromTheConfiguredFrom() {
// SES: a verified domain covers its subdomains without identities of their own.
verified("repro.example", "us-east-1");
UserPool pool = emailConfiguredPool(Map.of(
"EmailSendingAccount", "DEVELOPER",
"SourceArn", "arn:aws:ses:us-east-1:000000000000:identity/repro.example",
"From", "no-reply@auth.Repro.example"));
dispatcher.dispatch(pool, user("alice@example.com", null),
VerificationCode.Purpose.SIGNUP_CONFIRMATION, "123456", List.of("EMAIL"));
assertEquals("no-reply@auth.Repro.example", sentEmail().source());
}
@Test
void dispatch_fromThatDiffersFromTheEmailIdentityOnlyInCase_sendsFromTheDefaultAddress() {
// SES: email address identities are case sensitive, domain part included.
verified("noreply@REPRO.example", "us-east-1");
UserPool pool = emailConfiguredPool(Map.of(
"EmailSendingAccount", "DEVELOPER",
"SourceArn", "arn:aws:ses:us-east-1:000000000000:identity/noreply@REPRO.example",
"From", "noreply@repro.example"));
dispatcher.dispatch(pool, user("alice@example.com", null),
VerificationCode.Purpose.SIGNUP_CONFIRMATION, "123456", List.of("EMAIL"));
assertEquals("no-reply@verificationemail.com", sentEmail().source());
}
@Test
void dispatch_fromThatIsNotOneMailbox_sendsFromTheDefaultAddress() {
verified("repro.example", "us-east-1");
UserPool pool = emailConfiguredPool(Map.of(
"EmailSendingAccount", "DEVELOPER",
"SourceArn", "arn:aws:ses:us-east-1:000000000000:identity/repro.example",
"From", "noreply@repro.example, billing@bank.example"));
dispatcher.dispatch(pool, user("alice@example.com", null),
VerificationCode.Purpose.SIGNUP_CONFIRMATION, "123456", List.of("EMAIL"));
assertEquals("no-reply@verificationemail.com", sentEmail().source());
}
@Test
void dispatch_sourceArnInAnotherAccount_sendsFromTheDefaultAddress() {
when(ses.isVerifiedIdentity(anyString(), anyString())).thenReturn(true);
UserPool pool = emailConfiguredPool(Map.of(
"EmailSendingAccount", "DEVELOPER",
"SourceArn", "arn:aws:ses:us-east-1:111122223333:identity/noreply@repro.example",
"From", "noreply@repro.example"));
dispatcher.dispatch(pool, user("alice@example.com", null),
VerificationCode.Purpose.SIGNUP_CONFIRMATION, "123456", List.of("EMAIL"));
assertEquals("no-reply@verificationemail.com", sentEmail().source());
}
@Test
void dispatch_sourceArnInAnotherPartition_sendsFromTheDefaultAddress() {
verified("noreply@repro.example", "cn-north-1");
UserPool pool = emailConfiguredPool(Map.of(
"EmailSendingAccount", "DEVELOPER",
"SourceArn", "arn:aws-cn:ses:cn-north-1:000000000000:identity/noreply@repro.example"));
dispatcher.dispatch(pool, user("alice@example.com", null),
VerificationCode.Purpose.SIGNUP_CONFIRMATION, "123456", List.of("EMAIL"));
assertEquals("no-reply@verificationemail.com", sentEmail().source());
}
@Test
void dispatch_identityVerifiedOnlyOutsideTheSourceArnRegion_sendsFromTheDefaultAddress() {
verified("noreply@repro.example", "us-east-1");
UserPool pool = emailConfiguredPool(Map.of(
"EmailSendingAccount", "DEVELOPER",
"SourceArn", "arn:aws:ses:eu-west-1:000000000000:identity/noreply@repro.example"));
dispatcher.dispatch(pool, user("alice@example.com", null),
VerificationCode.Purpose.SIGNUP_CONFIRMATION, "123456", List.of("EMAIL"));
assertEquals("no-reply@verificationemail.com", sentEmail().source());
}
@Test
void dispatch_wildcardRegionSourceArn_checksTheIdentityInThePoolsRegion() {
verified("noreply@repro.example", "us-east-1");
UserPool pool = emailConfiguredPool(Map.of(
"SourceArn", "arn:aws:ses:*:000000000000:identity/noreply@repro.example"));
dispatcher.dispatch(pool, user("alice@example.com", null),
VerificationCode.Purpose.SIGNUP_CONFIRMATION, "123456", List.of("EMAIL"));
assertEquals("noreply@repro.example", sentEmail().source());
}
private SendEmailRequest sentEmail() {
ArgumentCaptor<SendEmailRequest> captor = ArgumentCaptor.forClass(SendEmailRequest.class);
verify(ses).sendEmail(captor.capture());
@@ -220,6 +465,17 @@ class CognitoMessageDispatcherTest {
return assertInstanceOf(EmailContent.Simple.class, sent.content()).bodyText();
}
private void verified(String identity, String region) {
when(ses.isVerifiedIdentity(identity, region)).thenReturn(true);
}
private UserPool emailConfiguredPool(Map<String, Object> emailConfiguration) {
UserPool p = pool(Map.of());
p.setArn("arn:aws:cognito-idp:us-east-1:000000000000:userpool/us-east-1_AbCdEfGhI");
p.setEmailConfiguration(emailConfiguration);
return p;
}
private UserPool pool(Map<String, Object> template) {
UserPool p = new UserPool();
p.setVerificationMessageTemplate(new HashMap<>(template));