feat(ses): add SES v2 GetEmailAddressInsights (#6573)

* feat(ses): add SES v2 GetEmailAddressInsights

Answer GetEmailAddressInsights offline with verdicts derived from the
address alone. Malformed and over-long addresses answer 200 with every
verdict LOW, the operation is not gated on Virtual Deliverability
Manager, and only suppressionlist@simulator.amazonses.com reports a
missing mailbox, as real SES does. Domains reserved by RFC 2606 and
RFC 6761 have no valid DNS records; every other domain is taken to
have them.

* docs(ses): scope the non-ASCII insights deviation to unquoted parts

A send does not inspect a quoted local part, so GetEmailAddressInsights
rates "é"@gmail.com like any other address. Say so in the deviation
note and pin it, with the unquoted and domain cases, in unit tests.
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-10-25 21:41:42 -04:00
1 parent b29c8f8a60
commit 7f5f1b554d
9 files changed
+442 -8

No files matched your search

@@ -1,10 +1,13 @@
package io.github.hectorvent.floci.services.ses;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.services.ses.SesMailboxValidation.Result;
import io.github.hectorvent.floci.services.ses.SesMailboxValidation.Verdict;
import io.github.hectorvent.floci.services.ses.model.EmailInsights;
import io.github.hectorvent.floci.services.ses.model.InsightsBounce;
import io.github.hectorvent.floci.services.ses.model.InsightsComplaint;
@@ -15,6 +18,7 @@ import io.github.hectorvent.floci.services.ses.model.SentEmail;
import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
@@ -26,6 +30,7 @@ import jakarta.ws.rs.core.Response;
import java.util.List;
import static io.github.hectorvent.floci.services.ses.SesV2Json.putTimestamp;
import static io.github.hectorvent.floci.services.ses.SesV2Json.readOptionBody;
/**
* SES V2 message insights ({@code /v2/email/insights/{MessageId}/}), the read side of the
@@ -35,6 +40,10 @@ import static io.github.hectorvent.floci.services.ses.SesV2Json.putTimestamp;
* gated on Virtual Deliverability Manager and the gate is checked before the message id, so an
* unparseable id in a VDM-disabled region still reports the VDM error. A message that is absent
* reports the account id alongside the id.
*
* <p>Email address insights ({@code /v2/email/email-address-insights/}) share the name but not the
* gate: they answer with Virtual Deliverability Manager disabled (probe-confirmed 2026-10-25), and
* {@link SesMailboxValidation} derives the verdicts.
*/
@Path("/v2/email")
@Produces(MediaType.APPLICATION_JSON)
@@ -68,6 +77,33 @@ public class SesInsightsController {
return Response.ok(render(email)).build();
}
@POST
@Path("/email-address-insights")
public Response getEmailAddressInsights(String body) {
JsonNode request = readOptionBody(objectMapper, body);
SesV2RequestShapes.check("GetEmailAddressInsights", request);
String emailAddress = request.path("EmailAddress").textValue();
if (emailAddress == null || emailAddress.isEmpty()) {
throw new AwsException("BadRequestException", "Email address must not be null nor empty", 400);
}
Result result = SesMailboxValidation.evaluate(emailAddress);
ObjectNode response = objectMapper.createObjectNode();
ObjectNode validation = response.putObject("MailboxValidation");
putVerdict(validation, "IsValid", result.isValid());
ObjectNode evaluations = validation.putObject("Evaluations");
putVerdict(evaluations, "HasValidSyntax", result.hasValidSyntax());
putVerdict(evaluations, "HasValidDnsRecords", result.hasValidDnsRecords());
putVerdict(evaluations, "MailboxExists", result.mailboxExists());
putVerdict(evaluations, "IsRoleAddress", result.isRoleAddress());
putVerdict(evaluations, "IsDisposable", result.isDisposable());
putVerdict(evaluations, "IsRandomInput", result.isRandomInput());
return Response.ok(response).build();
}
private static void putVerdict(ObjectNode parent, String field, Verdict verdict) {
parent.putObject(field).put("ConfidenceVerdict", verdict.name());
}
private ObjectNode render(SentEmail email) {
ObjectNode result = objectMapper.createObjectNode();
result.put("MessageId", email.getMessageId());
@@ -0,0 +1,88 @@
package io.github.hectorvent.floci.services.ses;
import io.github.hectorvent.floci.services.ses.address.SesAddressLength;
import io.github.hectorvent.floci.services.ses.address.SesAddressSyntax;
import java.util.List;
import java.util.Locale;
import java.util.Set;
/**
* The verdicts {@code GetEmailAddressInsights} answers for one address, derived offline so the same
* address always gets the same answer. Floci performs no DNS lookup and no mailbox probe, so the
* rules reproduce what AWS was seen to return (probe-confirmed 2026-10-25, ap-northeast-1) and fill
* the rest with fixed values.
*
* <p>Probe-confirmed: a malformed or over-long address answers 200 with every evaluation LOW, not a
* 400; a display-name form or an unrecognised top-level domain lowers the syntax verdict to MEDIUM;
* {@code example.com} and an {@code .invalid} domain have no valid DNS records; the mailbox
* simulator's domain has them, its suppression-list address is the only one whose mailbox is LOW,
* and its bounce and complaint addresses are not special; an ordinary mailbox is MEDIUM; and the
* overall verdict is LOW whenever the syntax, DNS or mailbox verdict is LOW, HIGH otherwise (no
* MEDIUM overall verdict was observed).
*
* <p>Floci's own choices: every domain that is not reserved by RFC 2606 or RFC 6761 is taken to have
* valid DNS records, the role-address check is a fixed list of local parts, and the disposable and
* random-input checks always answer LOW (AWS rated a long run of one letter as random input).
*/
final class SesMailboxValidation {
enum Verdict { LOW, MEDIUM, HIGH }
record Result(Verdict isValid, Verdict hasValidSyntax, Verdict hasValidDnsRecords, Verdict mailboxExists,
Verdict isRoleAddress, Verdict isDisposable, Verdict isRandomInput) {
}
private static final String SIMULATOR_DOMAIN = "simulator.amazonses.com";
private static final Set<String> RESERVED_TLDS = Set.of("example", "invalid", "localhost", "test");
private static final List<String> RESERVED_DOMAINS = List.of("example.com", "example.net", "example.org");
private static final Set<String> ROLE_LOCAL_PARTS = Set.of(
"abuse", "admin", "administrator", "billing", "contact", "help", "hostmaster", "info",
"marketing", "noc", "no-reply", "noreply", "postmaster", "sales", "security", "support",
"webmaster");
private static final Result MALFORMED = new Result(Verdict.LOW, Verdict.LOW, Verdict.LOW, Verdict.LOW,
Verdict.LOW, Verdict.LOW, Verdict.LOW);
private SesMailboxValidation() {
}
static Result evaluate(String emailAddress) {
if (!SesAddressSyntax.isWellFormed(emailAddress) || SesAddressLength.exceedsLimit(emailAddress)) {
return MALFORMED;
}
String addrSpec = SesAddressSyntax.addrSpec(emailAddress);
int at = addrSpec.lastIndexOf('@');
String localPart = addrSpec.substring(0, at);
String domain = addrSpec.substring(at + 1).toLowerCase(Locale.ROOT);
boolean displayName = !addrSpec.equals(emailAddress.trim());
Verdict syntax = displayName || hasUnrecognisedTld(domain) ? Verdict.MEDIUM : Verdict.HIGH;
Verdict dns = isReserved(domain) ? Verdict.LOW : Verdict.HIGH;
Verdict mailbox = SimulatorAddresses.isSuppressionList(addrSpec) ? Verdict.LOW : Verdict.MEDIUM;
Verdict role = ROLE_LOCAL_PARTS.contains(SimulatorAddresses.withoutLabel(localPart).toLowerCase(Locale.ROOT))
? Verdict.HIGH : Verdict.LOW;
Verdict overall = syntax == Verdict.LOW || dns == Verdict.LOW || mailbox == Verdict.LOW
? Verdict.LOW : Verdict.HIGH;
return new Result(overall, syntax, dns, mailbox, role, Verdict.LOW, Verdict.LOW);
}
private static boolean hasUnrecognisedTld(String domain) {
int dot = domain.lastIndexOf('.');
return dot < 0 || RESERVED_TLDS.contains(domain.substring(dot + 1));
}
private static boolean isReserved(String domain) {
if (domain.equals(SIMULATOR_DOMAIN)) {
return false;
}
if (hasUnrecognisedTld(domain)) {
return true;
}
for (String reserved : RESERVED_DOMAINS) {
if (domain.equals(reserved) || domain.endsWith("." + reserved)) {
return true;
}
}
return false;
}
}
@@ -47,11 +47,11 @@ final class SimulatorAddresses {
if (at < 0) {
return trimmed;
}
String localPart = trimmed.substring(0, at);
return withoutLabel(trimmed.substring(0, at)) + trimmed.substring(at);
}
static String withoutLabel(String localPart) {
int plus = localPart.indexOf('+');
if (plus < 0) {
return trimmed;
}
return localPart.substring(0, plus) + trimmed.substring(at);
return plus < 0 ? localPart : localPart.substring(0, plus);
}
}
@@ -47,7 +47,7 @@ public final class SesAddressLength {
}
}
static boolean exceedsLimit(String address) {
public static boolean exceedsLimit(String address) {
String normalized = normalized(address);
return normalized != null && normalized.length() > MAX_LENGTH;
}
@@ -227,6 +227,11 @@ public final class SesAddressSyntax {
}
}
/** Whether a present address passes the check every send applies; a blank one does not. */
public static boolean isWellFormed(String address) {
return address != null && !address.isBlank() && violation(address) == null;
}
/**
* Returns the message AWS answers for a malformed address, or null when the address passes or
* is blank (a missing address is reported by the caller's own required-field check).
@@ -273,7 +278,8 @@ public final class SesAddressSyntax {
return c <= ' ' || c >= 0x7f;
}
private static String addrSpec(String mailbox) {
/** The bare address of a mailbox, without its display name, comments and angle brackets. */
public static String addrSpec(String mailbox) {
String withoutComments = stripComments(mailbox).text().trim();
int open = SesSendAddresses.lastUnquotedAngle(withoutComments);
if (open >= 0 && withoutComments.endsWith(">")) {