mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
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:
1 parent
1a42aafcfb
commit
c267d3ae1c
3 files changed
+323
-15
No files matched your search
@@ -42,6 +42,7 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Matcher;
|
||||
@@ -1169,17 +1170,26 @@ public class SesService {
|
||||
String normalized = normalizeSuppressionEmail(emailAddress);
|
||||
validateSuppressionReason(reason, "reason", false);
|
||||
String key = suppressionKey(region, normalized);
|
||||
SuppressedDestination existing = suppressionStore.get(key).orElse(null);
|
||||
SuppressedDestination entry = existing != null ? existing : new SuppressedDestination(normalized, reason);
|
||||
SuppressionMatch match = existingSuppressionMatch(region, emailAddress, normalized).orElse(null);
|
||||
SuppressedDestination entry = match != null ? match.entry() : new SuppressedDestination(normalized, reason);
|
||||
entry.setEmailAddress(normalized);
|
||||
entry.setReason(reason);
|
||||
entry.setLastUpdateTime(Instant.now());
|
||||
// Write the canonical key first, then drop a legacy key it migrated from,
|
||||
// so a failed write can't lose the entry. The legacy form was persisted by
|
||||
// a pre-canonicalization Floci (trim-only key); migrating it avoids leaving
|
||||
// a stuck duplicate after a re-PUT.
|
||||
suppressionStore.put(key, entry);
|
||||
if (match != null && !match.key().equals(key)) {
|
||||
suppressionStore.delete(match.key());
|
||||
}
|
||||
LOG.infov("Suppressed destination {0} in region {1} (reason={2})", normalized, region, reason);
|
||||
}
|
||||
|
||||
public SuppressedDestination getSuppressedDestination(String region, String emailAddress) {
|
||||
String normalized = normalizeSuppressionEmail(emailAddress);
|
||||
return suppressionStore.get(suppressionKey(region, normalized))
|
||||
return existingSuppressionMatch(region, emailAddress, normalized)
|
||||
.map(SuppressionMatch::entry)
|
||||
.orElseThrow(() -> new AwsException("NotFoundException",
|
||||
"Email address " + normalized + " does not exist on your suppression list.",
|
||||
404));
|
||||
@@ -1187,16 +1197,40 @@ public class SesService {
|
||||
|
||||
public void deleteSuppressedDestination(String region, String emailAddress) {
|
||||
String normalized = normalizeSuppressionEmail(emailAddress);
|
||||
String key = suppressionKey(region, normalized);
|
||||
if (suppressionStore.get(key).isEmpty()) {
|
||||
throw new AwsException("NotFoundException",
|
||||
"Email address " + normalized + " does not exist on your suppression list.",
|
||||
404);
|
||||
}
|
||||
suppressionStore.delete(key);
|
||||
SuppressionMatch match = existingSuppressionMatch(region, emailAddress, normalized)
|
||||
.orElseThrow(() -> new AwsException("NotFoundException",
|
||||
"Email address " + normalized + " does not exist on your suppression list.",
|
||||
404));
|
||||
suppressionStore.delete(match.key());
|
||||
LOG.infov("Removed suppression entry for {0} in region {1}", normalized, region);
|
||||
}
|
||||
|
||||
/** A suppression entry together with the storage key it currently lives under. */
|
||||
private record SuppressionMatch(String key, SuppressedDestination entry) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a suppression entry by its canonical (domain-lower-cased) key, falling
|
||||
* back to the legacy raw-trimmed key used by a pre-canonicalization Floci. Returns
|
||||
* the entry and the key it was found under in a single read per candidate, so
|
||||
* callers don't re-fetch the store.
|
||||
*/
|
||||
private Optional<SuppressionMatch> existingSuppressionMatch(String region, String rawEmail, String normalized) {
|
||||
String canonical = suppressionKey(region, normalized);
|
||||
Optional<SuppressedDestination> hit = suppressionStore.get(canonical);
|
||||
if (hit.isPresent()) {
|
||||
return Optional.of(new SuppressionMatch(canonical, hit.get()));
|
||||
}
|
||||
String legacy = suppressionKey(region, rawEmail.trim());
|
||||
if (!legacy.equals(canonical)) {
|
||||
Optional<SuppressedDestination> legacyHit = suppressionStore.get(legacy);
|
||||
if (legacyHit.isPresent()) {
|
||||
return Optional.of(new SuppressionMatch(legacy, legacyHit.get()));
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public List<SuppressedDestination> listSuppressedDestinations(String region, List<String> reasonFilters) {
|
||||
Set<String> filters = new HashSet<>();
|
||||
if (reasonFilters != null) {
|
||||
@@ -1251,7 +1285,8 @@ public class SesService {
|
||||
continue;
|
||||
}
|
||||
String normalized = normalizeSuppressionEmail(address);
|
||||
SuppressedDestination entry = suppressionStore.get(suppressionKey(region, normalized))
|
||||
SuppressedDestination entry = existingSuppressionMatch(region, address, normalized)
|
||||
.map(SuppressionMatch::entry)
|
||||
.orElse(null);
|
||||
if (entry != null && entry.getReason() != null
|
||||
&& reasonFilter.contains(entry.getReason())) {
|
||||
@@ -1306,9 +1341,11 @@ public class SesService {
|
||||
return null;
|
||||
}
|
||||
// Share the same normalization used when entries are stored
|
||||
// (`normalizeSuppressionEmail`) so lookups can't drift apart from inserts.
|
||||
// (`normalizeSuppressionEmail`) so lookups can't drift apart from inserts,
|
||||
// with the same legacy-key fallback as GET/DELETE.
|
||||
String normalized = normalizeSuppressionEmail(emailAddress);
|
||||
SuppressedDestination entry = suppressionStore.get(suppressionKey(region, normalized))
|
||||
SuppressedDestination entry = existingSuppressionMatch(region, emailAddress, normalized)
|
||||
.map(SuppressionMatch::entry)
|
||||
.orElse(null);
|
||||
if (entry == null || entry.getReason() == null) {
|
||||
return null;
|
||||
@@ -1324,8 +1361,19 @@ public class SesService {
|
||||
if (emailAddress == null || emailAddress.isBlank()) {
|
||||
throw new AwsException("BadRequestException", "EmailAddress is required.", 400);
|
||||
}
|
||||
// AWS trims leading/trailing whitespace from EmailAddress before storing.
|
||||
return emailAddress.trim();
|
||||
// AWS trims the EmailAddress and canonicalizes only the domain to lower
|
||||
// case; the local-part keeps its case. Verified against real AWS SES V2
|
||||
// (2026-06-15): `Foo@Example.COM` and `Foo@example.com` collapse to one
|
||||
// suppression entry (`Foo@example.com`), but `Foo@x` and `foo@x` are two
|
||||
// distinct entries. Lower-casing the whole address would wrongly merge
|
||||
// local-part variants and alter the stored value on read-back.
|
||||
// Locale.ROOT avoids the JVM-locale Turkish-i pitfall.
|
||||
String trimmed = emailAddress.trim();
|
||||
int at = trimmed.lastIndexOf('@');
|
||||
if (at < 0) {
|
||||
return trimmed;
|
||||
}
|
||||
return trimmed.substring(0, at) + "@" + trimmed.substring(at + 1).toLowerCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user