fix(ses): canonicalize only the domain of suppression-list email addresses (#1350)

* fix(ses): canonicalize only the domain of suppression-list emails

The previous change lower-cased the whole EmailAddress, but real AWS SES V2
(verified 2026-06-15) is case-sensitive on the local-part and case-insensitive
only on the domain: PutSuppressedDestination for Foo@Example.COM stores and
reads back Foo@example.com, Foo@x and foo@x are two distinct entries, and a
GET/DELETE whose local-part case differs returns NotFound. Full lower-casing
wrongly merged local-part variants and altered the address on read-back.

normalizeSuppressionEmail now lower-cases only the part after the last '@'
and preserves the local-part. The integration tests are rewritten to pin the
verified behavior: domain-case-insensitive match and de-dup, local-part
case-sensitive (distinct entries), latest Reason wins on a domain-only
collision.

Suppression entries persisted by a pre-canonicalization Floci (trim-only key)
would otherwise become unreachable once the domain is normalized. All five
suppression lookups — PUT / GET / DELETE / send-time check / event resolution —
fall back to the legacy raw-trimmed key via existingSuppressionKey, and PUT
migrates the entry onto the canonical key, so older entries stay readable,
deletable, and still suppress sends, without a re-PUT leaving a stuck
duplicate. A unit test seeds a legacy-keyed entry directly to pin these paths.

* refactor(ses): single-read suppression lookup and safe legacy migration

Address review on the suppression-list case fix:
- existingSuppressionMatch returns the resolved entry with its storage key,
  so GET/DELETE/send-time/event lookups read the store once instead of
  probing with isPresent() and re-fetching (was 2 reads on a canonical hit,
  3 on a legacy hit; per-address in collectSuppressedReasons).
- PutSuppressedDestination writes the canonical key before deleting the
  legacy one it migrated from, so a failed write can't drop the entry.
- The domain-case GET test seeds its own entry, removing the dependency on
  an earlier ordered test.
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-06-16 00:06:28 -05:00
1 parent 1a42aafcfb
commit c267d3ae1c
3 files changed
+323 -15

No files matched your search

@@ -0,0 +1,90 @@
package io.github.hectorvent.floci.services.ses;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.ses.model.AccountSuppressionAttributes;
import io.github.hectorvent.floci.services.ses.model.ConfigurationSet;
import io.github.hectorvent.floci.services.ses.model.EmailTemplate;
import io.github.hectorvent.floci.services.ses.model.Identity;
import io.github.hectorvent.floci.services.ses.model.SentEmail;
import io.github.hectorvent.floci.services.ses.model.SuppressedDestination;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
/**
* Backward-compat for suppression entries persisted by a pre-canonicalization Floci,
* which stored the address trim-only — so an entry with an upper-case domain lives under
* a key the new domain-lower-casing lookup would otherwise miss. GET/DELETE fall back to
* the legacy key, and PUT migrates it onto the canonical key. Only reachable by seeding
* the store directly (the HTTP API always normalizes), so it lives at the unit layer.
*/
class SesServiceSuppressionLegacyKeyTest {
private static final String REGION = "us-east-1";
// A pre-canonicalization entry: trim-only key with an upper-case domain.
private static final String LEGACY_ADDR = "Foo.Bar@Example.COM";
private static final String LEGACY_KEY = "suppression::" + REGION + "::" + LEGACY_ADDR;
private static final String CANONICAL_KEY = "suppression::" + REGION + "::Foo.Bar@example.com";
private SesService service;
private InMemoryStorage<String, SuppressedDestination> suppressionStore;
@BeforeEach
void setUp() {
suppressionStore = new InMemoryStorage<>();
service = new SesService(
new InMemoryStorage<String, Identity>(),
new InMemoryStorage<String, SentEmail>(),
new InMemoryStorage<String, Boolean>(),
new InMemoryStorage<String, EmailTemplate>(),
new InMemoryStorage<String, ConfigurationSet>(),
suppressionStore,
new InMemoryStorage<String, AccountSuppressionAttributes>(),
mock(SmtpRelay.class),
new ObjectMapper());
}
private void seedLegacyEntry() {
suppressionStore.put(LEGACY_KEY, new SuppressedDestination(LEGACY_ADDR, "BOUNCE"));
}
@Test
void getReachesLegacyEntryByExactAddress() {
seedLegacyEntry();
SuppressedDestination got = service.getSuppressedDestination(REGION, LEGACY_ADDR);
assertEquals(LEGACY_ADDR, got.getEmailAddress());
assertEquals("BOUNCE", got.getReason());
}
@Test
void deleteRemovesLegacyEntry() {
seedLegacyEntry();
service.deleteSuppressedDestination(REGION, LEGACY_ADDR);
assertFalse(suppressionStore.get(LEGACY_KEY).isPresent());
}
@Test
void sendTimeSuppressionReasonReachesLegacyEntry() {
// The event / send-time path (resolveSuppressionReason, collectSuppressedReasons)
// must also honor the legacy key, or a legacy entry stays deletable but no longer
// suppresses sends. Default fresh account suppresses [BOUNCE, COMPLAINT].
seedLegacyEntry();
assertEquals("BOUNCE", service.resolveSuppressionReason(LEGACY_ADDR, null, REGION));
}
@Test
void putMigratesLegacyEntryOntoCanonicalKeyWithoutDuplicate() {
seedLegacyEntry();
service.putSuppressedDestination(REGION, LEGACY_ADDR, "COMPLAINT");
assertFalse(suppressionStore.get(LEGACY_KEY).isPresent(), "legacy key should be migrated away");
assertTrue(suppressionStore.get(CANONICAL_KEY).isPresent(), "entry should live under canonical key");
SuppressedDestination migrated = suppressionStore.get(CANONICAL_KEY).orElseThrow();
assertEquals("Foo.Bar@example.com", migrated.getEmailAddress());
assertEquals("COMPLAINT", migrated.getReason());
}
}
@@ -336,4 +336,174 @@ class SesSuppressionV2IntegrationTest {
.body("SuppressedDestinationSummaries.EmailAddress",
hasItems("multi-bounce@example.com", "complaint-1@example.com"));
}
@Test
@Order(20)
void putSuppressedDestination_lowercasesDomainPreservesLocalPart() {
// Verified against real AWS SES V2 (2026-06-15): AWS canonicalizes only the
// domain to lower case; the local-part keeps its case. PUT MixedCase@Example.COM
// is stored and read back as MixedCase@example.com.
given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("""
{"EmailAddress": "MixedCase@Example.COM", "Reason": "BOUNCE"}
""")
.when()
.put("/v2/email/suppression/addresses")
.then()
.statusCode(200);
given()
.header("Authorization", AUTH_HEADER)
.when()
.get("/v2/email/suppression/addresses/MixedCase@Example.COM")
.then()
.statusCode(200)
.body("SuppressedDestination.EmailAddress", equalTo("MixedCase@example.com"))
.body("SuppressedDestination.Reason", equalTo("BOUNCE"));
}
@Test
@Order(21)
void getSuppressedDestination_domainCaseInsensitive_localCaseSensitive() {
// Self-contained: seed its own entry so the test is order-independent.
// A differing domain case still hits the entry; a differing local-part case
// does not (it is a different key).
given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("""
{"EmailAddress": "CaseProbe@Example.COM", "Reason": "BOUNCE"}
""")
.when()
.put("/v2/email/suppression/addresses")
.then()
.statusCode(200);
given()
.header("Authorization", AUTH_HEADER)
.when()
.get("/v2/email/suppression/addresses/CaseProbe@EXAMPLE.com")
.then()
.statusCode(200)
.body("SuppressedDestination.EmailAddress", equalTo("CaseProbe@example.com"));
given()
.header("Authorization", AUTH_HEADER)
.when()
.get("/v2/email/suppression/addresses/caseprobe@example.com")
.then()
.statusCode(404);
}
@Test
@Order(22)
void deleteSuppressedDestination_domainCaseInsensitive() {
// PUT one domain case, DELETE a different domain case (same local-part), GET 404.
given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("""
{"EmailAddress": "DeleteMe@Example.com", "Reason": "COMPLAINT"}
""")
.when()
.put("/v2/email/suppression/addresses")
.then()
.statusCode(200);
given()
.header("Authorization", AUTH_HEADER)
.when()
.delete("/v2/email/suppression/addresses/DeleteMe@EXAMPLE.com")
.then()
.statusCode(200);
given()
.header("Authorization", AUTH_HEADER)
.when()
.get("/v2/email/suppression/addresses/DeleteMe@example.com")
.then()
.statusCode(404);
}
@Test
@Order(23)
void putSuppressedDestination_domainCaseCollision_lastPutWins() {
// Same local-part, domain differing only in case: one entry, latest Reason wins.
given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("""
{"EmailAddress": "UpdateTarget@example.com", "Reason": "BOUNCE"}
""")
.when()
.put("/v2/email/suppression/addresses")
.then()
.statusCode(200);
given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("""
{"EmailAddress": "UpdateTarget@EXAMPLE.com", "Reason": "COMPLAINT"}
""")
.when()
.put("/v2/email/suppression/addresses")
.then()
.statusCode(200);
given()
.header("Authorization", AUTH_HEADER)
.when()
.get("/v2/email/suppression/addresses/UpdateTarget@example.com")
.then()
.statusCode(200)
.body("SuppressedDestination.EmailAddress", equalTo("UpdateTarget@example.com"))
.body("SuppressedDestination.Reason", equalTo("COMPLAINT"));
}
@Test
@Order(24)
void putSuppressedDestination_localPartCaseDifference_distinctEntries() {
// Local-part case differences are distinct entries (not merged), each keeping
// its own Reason. Verified against real AWS SES V2 (2026-06-15).
given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("""
{"EmailAddress": "LocalCase@distinct.example.com", "Reason": "BOUNCE"}
""")
.when()
.put("/v2/email/suppression/addresses")
.then()
.statusCode(200);
given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("""
{"EmailAddress": "localcase@distinct.example.com", "Reason": "COMPLAINT"}
""")
.when()
.put("/v2/email/suppression/addresses")
.then()
.statusCode(200);
given()
.header("Authorization", AUTH_HEADER)
.when()
.get("/v2/email/suppression/addresses/LocalCase@distinct.example.com")
.then()
.statusCode(200)
.body("SuppressedDestination.Reason", equalTo("BOUNCE"));
given()
.header("Authorization", AUTH_HEADER)
.when()
.get("/v2/email/suppression/addresses/localcase@distinct.example.com")
.then()
.statusCode(200)
.body("SuppressedDestination.Reason", equalTo("COMPLAINT"));
}
}