diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesEmailAddressInsightsTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesEmailAddressInsightsTest.java new file mode 100644 index 0000000000..18bf765387 --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesEmailAddressInsightsTest.java @@ -0,0 +1,79 @@ +package com.floci.test; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.services.sesv2.SesV2Client; +import software.amazon.awssdk.services.sesv2.model.BadRequestException; +import software.amazon.awssdk.services.sesv2.model.EmailAddressInsightsConfidenceVerdict; +import software.amazon.awssdk.services.sesv2.model.EmailAddressInsightsMailboxEvaluations; +import software.amazon.awssdk.services.sesv2.model.GetEmailAddressInsightsRequest; +import software.amazon.awssdk.services.sesv2.model.MailboxValidation; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** + * SDK compatibility test for SES v2 GetEmailAddressInsights: the response deserializes into the + * SDK's verdict model, and an empty address is a BadRequestException. + */ +@DisplayName("SES v2 Email Address Insights") +class SesEmailAddressInsightsTest { + + private static SesV2Client sesV2; + + @BeforeAll + static void setup() { + sesV2 = TestFixtures.sesV2Client(); + } + + @AfterAll + static void cleanup() { + if (sesV2 != null) { + sesV2.close(); + } + } + + @Test + @DisplayName("GetEmailAddressInsights returns every verdict") + void returnsEveryVerdict() { + MailboxValidation validation = sesV2.getEmailAddressInsights(GetEmailAddressInsightsRequest.builder() + .emailAddress("support@simulator.amazonses.com").build()).mailboxValidation(); + EmailAddressInsightsMailboxEvaluations evaluations = validation.evaluations(); + + assertThat(validation.isValid().confidenceVerdict()).isEqualTo(EmailAddressInsightsConfidenceVerdict.HIGH); + assertThat(evaluations.hasValidSyntax().confidenceVerdict()) + .isEqualTo(EmailAddressInsightsConfidenceVerdict.HIGH); + assertThat(evaluations.hasValidDnsRecords().confidenceVerdict()) + .isEqualTo(EmailAddressInsightsConfidenceVerdict.HIGH); + assertThat(evaluations.mailboxExists().confidenceVerdict()) + .isEqualTo(EmailAddressInsightsConfidenceVerdict.MEDIUM); + assertThat(evaluations.isRoleAddress().confidenceVerdict()) + .isEqualTo(EmailAddressInsightsConfidenceVerdict.HIGH); + assertThat(evaluations.isDisposable().confidenceVerdict()) + .isEqualTo(EmailAddressInsightsConfidenceVerdict.LOW); + assertThat(evaluations.isRandomInput().confidenceVerdict()) + .isEqualTo(EmailAddressInsightsConfidenceVerdict.LOW); + } + + @Test + @DisplayName("GetEmailAddressInsights answers a malformed address with LOW verdicts") + void malformedAddressIsLow() { + MailboxValidation validation = sesV2.getEmailAddressInsights(GetEmailAddressInsightsRequest.builder() + .emailAddress("no-at-sign").build()).mailboxValidation(); + + assertThat(validation.isValid().confidenceVerdict()).isEqualTo(EmailAddressInsightsConfidenceVerdict.LOW); + assertThat(validation.evaluations().hasValidSyntax().confidenceVerdict()) + .isEqualTo(EmailAddressInsightsConfidenceVerdict.LOW); + } + + @Test + @DisplayName("GetEmailAddressInsights rejects an empty address") + void emptyAddressIsRejected() { + assertThatThrownBy(() -> sesV2.getEmailAddressInsights(GetEmailAddressInsightsRequest.builder() + .emailAddress("").build())) + .isInstanceOf(BadRequestException.class) + .hasMessageContaining("Email address must not be null nor empty"); + } +} diff --git a/docs/services/ses.md b/docs/services/ses.md index 00a96fbdcc..1b6ea3a724 100644 --- a/docs/services/ses.md +++ b/docs/services/ses.md @@ -425,6 +425,7 @@ Alongside the classic Query API, Floci implements a subset of the SES v2 REST JS | `GET` | `/v2/email/reputation/entities/{ReputationEntityType}/{ReputationEntityReference}` | `GetReputationEntity` | | `PUT` | `/v2/email/reputation/entities/{ReputationEntityType}/{ReputationEntityReference}/policy` | `UpdateReputationEntityPolicy` | | `PUT` | `/v2/email/reputation/entities/{ReputationEntityType}/{ReputationEntityReference}/customer-managed-status` | `UpdateReputationEntityCustomerManagedStatus` | +| `POST` | `/v2/email/email-address-insights/` | `GetEmailAddressInsights` | Floci models no leased dedicated IPs: `GetDedicatedIps` is empty and IP-targeted operations return `NotFoundException`, as real AWS does for an account with no leased IPs, with required request members validated first (`BadRequestException`). `PutDedicatedIpPoolScalingAttributes` rejects downgrading a `MANAGED` pool to `STANDARD`, and `PutAccountDedicatedIpWarmupAttributes` stores the flag behind `GetAccount.DedicatedIpAutoWarmupEnabled` (default `true`). @@ -510,9 +511,21 @@ registered Easy DKIM domain do not report this SES-hosted zone. S/MIME certificate associations are stored per region and never used: Floci does not sign mail. Associating validates the request as SES does (a domain identity needs a `FromAddress` in the domain or one of its subdomains, one certificate per address regardless of case) but, like SES, does not inspect the certificate. `ListEmailIdentityCertificates` reports `ACTIVE` while the association meets the [S/MIME certificate considerations](https://docs.aws.amazon.com/ses/latest/dg/send-email-authentication-smime.html#send-email-authentication-smime-certificate-considerations), plus the requirements a probe found SES also enforces (an end-entity certificate whose key usage includes `digitalSignature` and whose extended key usage includes `emailProtection`), checked against Floci's ACM and identity verification when the list is read, with the certificate's expiry as `CertificateExpiryTime`, and `FAILED` otherwise. There is no `PROVISIONING` phase, `DisassociateEmailIdentityCertificate` removes an association at once, and the certificate is not listed as in use by ACM. As on SES, `DeleteEmailIdentity` refuses an identity that still has a certificate associated, whatever its status. The v1 `DeleteIdentity` and `DeleteVerifiedEmailAddress` share the identities and refuse it too, with the same `BadRequestException` the tenant-association guard returns there; SES's v1 behaviour was not probed. +[`GetEmailAddressInsights`](https://docs.aws.amazon.com/ses/latest/dg/email-validation-api.html) is answered offline: Floci performs no DNS lookup and no mailbox probe, and charges nothing. As on AWS (probed 2026-10-25), it answers with Virtual Deliverability Manager disabled, a malformed or over-long address returns 200 with every verdict `LOW` rather than an error, an empty `EmailAddress` is a `BadRequestException` (an absent one, which the AWS CLI refuses to send, is assumed to answer the same), and `IsValid` is `LOW` whenever `HasValidSyntax`, `HasValidDnsRecords` or `MailboxExists` is `LOW` and `HIGH` otherwise. The verdicts follow fixed rules: + +| Evaluation | Floci's verdict | +|---|---| +| `HasValidSyntax` | `LOW` for an address a send would reject; `MEDIUM` when the address carries a display name, a comment or angle brackets, or when the domain has a single label or ends in `.example`, `.invalid`, `.localhost` or `.test`; otherwise `HIGH` | +| `HasValidDnsRecords` | `LOW` for those top-level domains and for `example.com`, `example.net`, `example.org` and their subdomains; otherwise `HIGH`, including `simulator.amazonses.com` | +| `MailboxExists` | `LOW` for `suppressionlist@simulator.amazonses.com` (with or without a `+label`); otherwise `MEDIUM`. The other simulator addresses are not special, as on AWS | +| `IsRoleAddress` | `HIGH` when the local part, without a `+label`, is a common role name such as `admin`, `info`, `noreply`, `postmaster` or `support`; otherwise `LOW` | +| `IsDisposable`, `IsRandomInput` | Always `LOW` | + +Deviations from AWS: every domain that is not reserved is taken to have valid DNS records, Floci keeps no disposable-domain list and detects no random input, and an address with a non-ASCII character in its domain (an internationalized domain name included) or in an unquoted local part is rated `LOW` everywhere, as a send would reject it, while a quoted local part, which a send does not inspect, is rated like any other; AWS's verdicts for such addresses were not probed. + ### Request member types -The operations whose body nests a structure in a structure, or holds a list of structures, check the request body against the SES v2 service model before anything else is validated, as AWS's restJson1 deserializer does: the send operations (`SendEmail`, `SendBulkEmail`, `SendCustomVerificationEmail`), `CreateConfigurationSet`, `UpdateConfigurationSet`, `PutConfigurationSetSuppressionOptions`, `PutConfigurationSetVdmOptions`, `CreateConfigurationSetEventDestination`, `UpdateConfigurationSetEventDestination`, `CreateContactList`, `UpdateContactList`, `CreateContact`, `UpdateContact`, `ListContacts`, `PutAccountSuppressionAttributes`, `PutAccountVdmAttributes`, `BatchGetMetricData`, `CreateImportJob` and `CreateExportJob`. `ListReputationEntities`, `UpdateReputationEntityPolicy` and `UpdateReputationEntityCustomerManagedStatus` run the same check although their bodies are flat. In those operations, the first body member of the wrong JSON type, in body order, is a `SerializationException` with AWS's deserializer message: +The operations whose body nests a structure in a structure, or holds a list of structures, check the request body against the SES v2 service model before anything else is validated, as AWS's restJson1 deserializer does: the send operations (`SendEmail`, `SendBulkEmail`, `SendCustomVerificationEmail`), `CreateConfigurationSet`, `UpdateConfigurationSet`, `PutConfigurationSetSuppressionOptions`, `PutConfigurationSetVdmOptions`, `CreateConfigurationSetEventDestination`, `UpdateConfigurationSetEventDestination`, `CreateContactList`, `UpdateContactList`, `CreateContact`, `UpdateContact`, `ListContacts`, `PutAccountSuppressionAttributes`, `PutAccountVdmAttributes`, `BatchGetMetricData`, `CreateImportJob` and `CreateExportJob`. `ListReputationEntities`, `UpdateReputationEntityPolicy`, `UpdateReputationEntityCustomerManagedStatus` and `GetEmailAddressInsights` run the same check although their bodies are flat. In those operations, the first body member of the wrong JSON type, in body order, is a `SerializationException` with AWS's deserializer message: | Member type | Value sent | Message | |---|---|---| diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesInsightsController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesInsightsController.java index 714242aeef..b0c5fa0e9e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesInsightsController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesInsightsController.java @@ -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. + * + *

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()); diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesMailboxValidation.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesMailboxValidation.java new file mode 100644 index 0000000000..36c217c6c7 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesMailboxValidation.java @@ -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. + * + *

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). + * + *

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 RESERVED_TLDS = Set.of("example", "invalid", "localhost", "test"); + private static final List RESERVED_DOMAINS = List.of("example.com", "example.net", "example.org"); + private static final Set 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; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SimulatorAddresses.java b/src/main/java/io/github/hectorvent/floci/services/ses/SimulatorAddresses.java index 1ae54f2311..b21d36fc2f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SimulatorAddresses.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SimulatorAddresses.java @@ -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); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/address/SesAddressLength.java b/src/main/java/io/github/hectorvent/floci/services/ses/address/SesAddressLength.java index 1e8a08e193..984d091457 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/address/SesAddressLength.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/address/SesAddressLength.java @@ -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; } diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/address/SesAddressSyntax.java b/src/main/java/io/github/hectorvent/floci/services/ses/address/SesAddressSyntax.java index da9e116470..d8e7f33cb3 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/address/SesAddressSyntax.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/address/SesAddressSyntax.java @@ -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(">")) { diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesEmailAddressInsightsV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesEmailAddressInsightsV2IntegrationTest.java new file mode 100644 index 0000000000..3c71364f81 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesEmailAddressInsightsV2IntegrationTest.java @@ -0,0 +1,76 @@ +package io.github.hectorvent.floci.services.ses; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; + +/** + * Integration tests for {@code POST /v2/email/email-address-insights/} (GetEmailAddressInsights). + * The verdict rules are covered by {@link SesMailboxValidationTest}; these pin the wire shape, the + * missing VDM gate and the request errors. Uses a region no other test enables VDM in. + */ +@QuarkusTest +class SesEmailAddressInsightsV2IntegrationTest { + + private static final String AUTH = + "AWS4-HMAC-SHA256 Credential=AKID/20260101/ca-west-1/ses/aws4_request"; + private static final String PATH = "/v2/email/email-address-insights/"; + + @Test + void answersEveryVerdictWithoutVdm() { + given().contentType("application/json").header("Authorization", AUTH) + .body("{\"EmailAddress\":\"admin@example.com\"}") + .when().post(PATH).then().statusCode(200) + .body("MailboxValidation.IsValid.ConfidenceVerdict", equalTo("LOW")) + .body("MailboxValidation.Evaluations.HasValidSyntax.ConfidenceVerdict", equalTo("HIGH")) + .body("MailboxValidation.Evaluations.HasValidDnsRecords.ConfidenceVerdict", equalTo("LOW")) + .body("MailboxValidation.Evaluations.MailboxExists.ConfidenceVerdict", equalTo("MEDIUM")) + .body("MailboxValidation.Evaluations.IsRoleAddress.ConfidenceVerdict", equalTo("HIGH")) + .body("MailboxValidation.Evaluations.IsDisposable.ConfidenceVerdict", equalTo("LOW")) + .body("MailboxValidation.Evaluations.IsRandomInput.ConfidenceVerdict", equalTo("LOW")); + } + + @Test + void malformedAddressAnswersOkWithLowVerdicts() { + given().contentType("application/json").header("Authorization", AUTH) + .body("{\"EmailAddress\":\"no-at-sign\"}") + .when().post(PATH).then().statusCode(200) + .body("MailboxValidation.IsValid.ConfidenceVerdict", equalTo("LOW")) + .body("MailboxValidation.Evaluations.HasValidSyntax.ConfidenceVerdict", equalTo("LOW")); + } + + @Test + void pathWithoutTrailingSlashIsServed() { + given().contentType("application/json").header("Authorization", AUTH) + .body("{\"EmailAddress\":\"alice@gmail.com\"}") + .when().post("/v2/email/email-address-insights").then().statusCode(200) + .body("MailboxValidation.IsValid.ConfidenceVerdict", equalTo("HIGH")); + } + + @Test + void emptyAddressIsRejected() { + given().contentType("application/json").header("Authorization", AUTH) + .body("{\"EmailAddress\":\"\"}") + .when().post(PATH).then().statusCode(400) + .header("x-amzn-ErrorType", equalTo("BadRequestException")) + .body("message", equalTo("Email address must not be null nor empty")); + } + + @Test + void missingAddressIsRejected() { + given().contentType("application/json").header("Authorization", AUTH) + .body("{}") + .when().post(PATH).then().statusCode(400) + .body("message", equalTo("Email address must not be null nor empty")); + } + + @Test + void nonStringAddressIsASerializationError() { + given().contentType("application/json").header("Authorization", AUTH) + .body("{\"EmailAddress\":1}") + .when().post(PATH).then().statusCode(400) + .header("x-amzn-ErrorType", equalTo("SerializationException")); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesMailboxValidationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesMailboxValidationTest.java new file mode 100644 index 0000000000..4a009b38df --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesMailboxValidationTest.java @@ -0,0 +1,136 @@ +package io.github.hectorvent.floci.services.ses; + +import io.github.hectorvent.floci.services.ses.SesMailboxValidation.Result; +import io.github.hectorvent.floci.services.ses.SesMailboxValidation.Verdict; +import org.junit.jupiter.api.Test; + +import static io.github.hectorvent.floci.services.ses.SesMailboxValidation.Verdict.HIGH; +import static io.github.hectorvent.floci.services.ses.SesMailboxValidation.Verdict.LOW; +import static io.github.hectorvent.floci.services.ses.SesMailboxValidation.Verdict.MEDIUM; +import static org.junit.jupiter.api.Assertions.assertEquals; + +class SesMailboxValidationTest { + + private static final Result ALL_LOW = new Result(LOW, LOW, LOW, LOW, LOW, LOW, LOW); + + private static Result result(Verdict isValid, Verdict syntax, Verdict dns, Verdict mailbox, Verdict role) { + return new Result(isValid, syntax, dns, mailbox, role, LOW, LOW); + } + + // The expected rows below marked "probed" are AWS's answers for the same address (2026-10-25). + + @Test + void ordinaryDomainIsValid() { + assertEquals(result(HIGH, HIGH, HIGH, MEDIUM, LOW), SesMailboxValidation.evaluate("alice@gmail.com")); + } + + @Test + void exampleDomainHasNoDnsRecords() { + // probed + assertEquals(result(LOW, HIGH, LOW, MEDIUM, LOW), SesMailboxValidation.evaluate("user@example.com")); + } + + @Test + void subdomainOfAReservedDomainHasNoDnsRecords() { + assertEquals(result(LOW, HIGH, LOW, MEDIUM, LOW), SesMailboxValidation.evaluate("user@mail.example.org")); + } + + @Test + void invalidTopLevelDomainLowersSyntaxAndHasNoDnsRecords() { + // probed + assertEquals(result(LOW, MEDIUM, LOW, MEDIUM, LOW), + SesMailboxValidation.evaluate("user@floci-probe-nonexistent-7f3a.invalid")); + } + + @Test + void singleLabelDomainIsTreatedAsAnUnrecognisedTopLevelDomain() { + assertEquals(result(LOW, MEDIUM, LOW, MEDIUM, LOW), SesMailboxValidation.evaluate("user@localhost")); + } + + @Test + void displayNameLowersSyntax() { + // probed + assertEquals(result(LOW, MEDIUM, LOW, MEDIUM, LOW), + SesMailboxValidation.evaluate("Alice ")); + } + + @Test + void displayNameOnAnOrdinaryDomainStaysValid() { + assertEquals(result(HIGH, MEDIUM, HIGH, MEDIUM, LOW), + SesMailboxValidation.evaluate("Alice ")); + } + + @Test + void angleBracketsWithoutANameLowerSyntax() { + assertEquals(result(HIGH, MEDIUM, HIGH, MEDIUM, LOW), SesMailboxValidation.evaluate("")); + } + + @Test + void simulatorBounceAndComplaintAreNotSpecial() { + // probed + Result expected = result(HIGH, HIGH, HIGH, MEDIUM, LOW); + assertEquals(expected, SesMailboxValidation.evaluate("success@simulator.amazonses.com")); + assertEquals(expected, SesMailboxValidation.evaluate("bounce@simulator.amazonses.com")); + assertEquals(expected, SesMailboxValidation.evaluate("complaint@simulator.amazonses.com")); + } + + @Test + void simulatorSuppressionListMailboxDoesNotExist() { + // probed + assertEquals(result(LOW, HIGH, HIGH, LOW, LOW), + SesMailboxValidation.evaluate("suppressionlist@simulator.amazonses.com")); + } + + @Test + void labelledSimulatorSuppressionListMailboxDoesNotExist() { + assertEquals(result(LOW, HIGH, HIGH, LOW, LOW), + SesMailboxValidation.evaluate("suppressionlist+run-1@simulator.amazonses.com")); + } + + @Test + void roleAddressIsReportedWithoutChangingValidity() { + // probed on example.com; gmail.com isolates the role from the DNS verdict + assertEquals(result(LOW, HIGH, LOW, MEDIUM, HIGH), SesMailboxValidation.evaluate("admin@example.com")); + assertEquals(result(HIGH, HIGH, HIGH, MEDIUM, HIGH), SesMailboxValidation.evaluate("Support+eu@gmail.com")); + } + + @Test + void malformedAddressIsLowEverywhere() { + // probed + assertEquals(ALL_LOW, SesMailboxValidation.evaluate("no-at-sign")); + assertEquals(ALL_LOW, SesMailboxValidation.evaluate("user@")); + assertEquals(ALL_LOW, SesMailboxValidation.evaluate("a b@example.com")); + } + + @Test + void nonAsciiOutsideQuotesIsLowEverywhere() { + assertEquals(ALL_LOW, SesMailboxValidation.evaluate("é@gmail.com")); + assertEquals(ALL_LOW, SesMailboxValidation.evaluate("user@bücher.de")); + } + + @Test + void nonAsciiInAQuotedLocalPartIsRatedAsASendAcceptsIt() { + // a send does not inspect a quoted local part, so the verdicts follow the send check + assertEquals(result(HIGH, HIGH, HIGH, MEDIUM, LOW), SesMailboxValidation.evaluate("\"é\"@gmail.com")); + } + + @Test + void blankAddressIsLowEverywhere() { + assertEquals(ALL_LOW, SesMailboxValidation.evaluate(" ")); + } + + @Test + void overLongAddressIsLowEverywhere() { + // probed for the verdicts other than IsRandomInput, which AWS rated HIGH for a run of one letter + String address = "a".repeat(309) + "@example.com"; + assertEquals(321, address.length()); + assertEquals(ALL_LOW, SesMailboxValidation.evaluate(address)); + } + + @Test + void addressAtTheLengthLimitIsEvaluated() { + String address = "a".repeat(310) + "@gmail.com"; + assertEquals(320, address.length()); + assertEquals(result(HIGH, HIGH, HIGH, MEDIUM, LOW), SesMailboxValidation.evaluate(address)); + } +}