fix(ses): word a v1 raw From header without @ as a missing user name (#5612)

* fix(ses): word a v1 raw From header without @ as a missing user name

A v1 SendRawEmail with a Source parameter answers "User name is
missing: '<address>'." for a From header address without @, not
JavaMail's "Missing final '@domain'". AWS parses the From header the
way JavaMail's non-strict header parse does: every address still gets
the other syntax checks, a missing @ passes, and only the first address
is then rejected for it, rendered in InternetAddress.toString() form.
The check runs after the configuration set and the missing-destination
checks, in the slot the v2 raw From header already uses. Without a
Source, and on v2, the behaviour is unchanged.

* fix(ses): render long and blank display names as JavaMail does

Encode a non-ASCII display name with JavaMail's encodeWord rules,
halving it at the middle until each encoded-word fits, instead of
mime4j's splitter, whose word boundaries differ. Drop a display name
that is blank once unquoted, as JavaMail does, so the address is echoed
without an empty name.

* test(ses): pin an address-shaped display name on a v1 raw From header

* fix(ses): keep comments in a v1 raw From display name before the angle

AWS renders "Alice (Bob) <a.b>" in the "User name is missing" message
as "\"Alice (Bob)\" <a.b>": the phrase before the angle bracket is the
display name as written, comments included. A comment after the address
is still dropped.

* fix(ses): do not fall back to a comment for a blank v1 raw From name

A display name before the angle bracket that is blank once unquoted is
dropped without falling back to a trailing comment, so AWS echoes
"\" \" <a.b> (Alice)" as "a.b". The comment becomes the name only when
nothing precedes the angle bracket.

* fix(ses): start a v1 raw From display name after a leading comment

AWS echoes "(Bob) Alice <a.b>" as "Alice <a.b>" and "(Bob) <a.b>" as
"Bob <a.b>": the display name starts at its first character outside a
comment, and the first comment becomes the name only when nothing else
precedes the angle bracket.
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-10-14 07:36:22 -04:00
1 parent 16293b609b
commit 3ec1466014
8 files changed
+446 -18

No files matched your search

@@ -126,4 +126,62 @@ class SesAddressSyntaxTest {
assertEquals(MISSING_DOMAIN, e.getMessage());
assertDoesNotThrow(() -> SesAddressSyntax.require("sender@example.com"));
}
@Test
void javaMailStringRendersTheProbedForms() {
assertEquals("probe.example.invalid", SesAddressSyntax.javaMailString("probe.example.invalid"));
assertEquals("Alice <probe.example.invalid>", SesAddressSyntax.javaMailString("Alice <probe.example.invalid>"));
assertEquals("Alice <probe.example.invalid>", SesAddressSyntax.javaMailString("probe.example.invalid (Alice)"));
assertEquals("<\"probe example\">", SesAddressSyntax.javaMailString("\"probe example\""));
assertEquals("=?UTF-8?B?5bGx55Sw?= <probe.example.invalid>",
SesAddressSyntax.javaMailString("山田 <probe.example.invalid>"));
assertEquals("\"Alice, B\" <probe.example.invalid>",
SesAddressSyntax.javaMailString("\"Alice, B\" <probe.example.invalid>"));
}
@Test
void javaMailStringQuotesTheNameOnlyWhenItHoldsASpecial() {
assertEquals("Alice B <probe.example.invalid>",
SesAddressSyntax.javaMailString("\"Alice B\" <probe.example.invalid>"));
assertEquals("\"Alice B.\" <probe.example.invalid>",
SesAddressSyntax.javaMailString("Alice B. <probe.example.invalid>"));
assertEquals("\"Alice \\\"B\\\"\" <probe.example.invalid>",
SesAddressSyntax.javaMailString("\"Alice \\\"B\\\"\" <probe.example.invalid>"));
}
@Test
void javaMailStringKeepsAnEncodedWordAndPrefersTheDisplayNameToAComment() {
assertEquals("=?UTF-8?B?5bGx55Sw?= <probe.example.invalid>",
SesAddressSyntax.javaMailString("=?UTF-8?B?5bGx55Sw?= <probe.example.invalid>"));
assertEquals("Alice <probe.example.invalid>",
SesAddressSyntax.javaMailString("Alice <probe.example.invalid> (Bob)"));
assertEquals("\"Alice (Bob)\" <probe.example.invalid>",
SesAddressSyntax.javaMailString("Alice (Bob) <probe.example.invalid>"));
assertEquals("Bob <probe.example.invalid>", SesAddressSyntax.javaMailString("(Bob) <probe.example.invalid>"));
assertEquals("Alice <probe.example.invalid>",
SesAddressSyntax.javaMailString("(Bob) Alice <probe.example.invalid>"));
assertEquals("probe.example.invalid", SesAddressSyntax.javaMailString("<probe.example.invalid>"));
}
@Test
void javaMailStringEncodesAMostlyAsciiNameAsQAndHalvesALongName() {
assertEquals("=?UTF-8?Q?Alice_=E5=B1=B1=E7=94=B0?= <probe.example.invalid>",
SesAddressSyntax.javaMailString("Alice 山田 <probe.example.invalid>"));
String word = "=?UTF-8?B?5bGx55Sw5bGx55Sw5bGx55Sw5bGx55Sw5bGx55Sw?=";
assertEquals(String.join(" ", word, word, word, word) + " <probe.example.invalid>",
SesAddressSyntax.javaMailString("山田".repeat(20) + " <probe.example.invalid>"));
}
@Test
void javaMailStringDropsABlankDisplayName() {
assertEquals("probe.example.invalid", SesAddressSyntax.javaMailString("\"\" <probe.example.invalid>"));
assertEquals("probe.example.invalid", SesAddressSyntax.javaMailString("\" \" <probe.example.invalid> (Alice)"));
assertEquals("Alice <probe.example.invalid>", SesAddressSyntax.javaMailString("<probe.example.invalid> (Alice)"));
}
@Test
void javaMailStringUnquotesAPhraseOnlyAtItsOuterQuotesLikeJavaMail() {
assertEquals("\"Alice\\\" \\\"Bob\" <probe.example.invalid>",
SesAddressSyntax.javaMailString("\"Alice\" \"Bob\" <probe.example.invalid>"));
}
}
@@ -0,0 +1,41 @@
package io.github.hectorvent.floci.services.ses;
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;
/**
* The JavaMail {@code MimeUtility.encodeWord} rules {@link SesEncodedWord} follows: which names are
* encoded, the B or Q choice by character count, Q's word specials, and the recursive halving.
*/
class SesEncodedWordTest {
@Test
void onlyNonAsciiOrControlCharactersNeedEncoding() {
assertFalse(SesEncodedWord.needsEncoding("Alice B. (x)"));
assertFalse(SesEncodedWord.needsEncoding("Alice\tB"));
assertTrue(SesEncodedWord.needsEncoding("山田"));
assertTrue(SesEncodedWord.needsEncoding("Alice\u007f"));
assertTrue(SesEncodedWord.needsEncoding("Alice\u0001"));
}
@Test
void anEvenSplitOfAsciiAndNonAsciiIsB() {
assertEquals("=?UTF-8?B?YeWxsQ==?=", SesEncodedWord.encode("a山"));
}
@Test
void qEscapesWordSpecialsAndWritesASpaceAsUnderscore() {
assertEquals("=?UTF-8?Q?Al_B=2E=E5=B1=B1?=", SesEncodedWord.encode("Al B.山"));
}
@Test
void halvingNeverSplitsASurrogatePair() {
String encoded = SesEncodedWord.encode("a" + "😀".repeat(16));
assertEquals("=?UTF-8?B?YfCfmIDwn5iA8J+YgPCfmIDwn5iA8J+YgPCfmIA=?="
+ " =?UTF-8?B?8J+YgPCfmIDwn5iA8J+YgPCfmIDwn5iA8J+YgPCfmIDwn5iA?=", encoded);
}
}
@@ -402,6 +402,55 @@ class SesSendAddressSyntaxIntegrationTest {
.body("ErrorResponse.Error.Message", equalTo(message));
}
@ParameterizedTest
@CsvSource(delimiter = '|', value = {
"sender.example.com | User name is missing: 'sender.example.com'.",
"Alice <sender.example.com> | User name is missing: 'Alice <sender.example.com>'.",
"sender.example.com (Alice) | User name is missing: 'Alice <sender.example.com>'.",
"\"sender example\" | User name is missing: '<\"sender example\">'.",
"山田 <sender.example.com> | User name is missing: '=?UTF-8?B?5bGx55Sw?= <sender.example.com>'.",
"\"Alice, B\" <sender.example.com> | User name is missing: '\"Alice, B\" <sender.example.com>'.",
"sender@example.com <sender.example.com> | User name is missing: '\"sender@example.com\" <sender.example.com>'.",
"Alice (Bob) <sender.example.com> | User name is missing: '\"Alice (Bob)\" <sender.example.com>'.",
"Alice <sender.example.com> (Bob) | User name is missing: 'Alice <sender.example.com>'.",
"\" \" <sender.example.com> (Alice) | User name is missing: 'sender.example.com'.",
"<sender.example.com> (Alice) | User name is missing: 'Alice <sender.example.com>'.",
"(Bob) <sender.example.com> | User name is missing: 'Bob <sender.example.com>'.",
"(Bob) Alice <sender.example.com> | User name is missing: 'Alice <sender.example.com>'."
})
void v1SendRawEmail_fromHeaderWithoutAtAndSource_isAMissingUserName(String from, String message) {
given()
.contentType("application/x-www-form-urlencoded; charset=UTF-8")
.header("Authorization", AUTH)
.formParam("Action", "SendRawEmail")
.formParam("Source", "sender@example.com")
.formParam("Destinations.member.1", TO)
.formParam("RawMessage.Data", rawMessage(from))
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"))
.body("ErrorResponse.Error.Message", equalTo(message));
}
@Test
void v1SendRawEmail_otherFromHeaderViolationIsReportedBeforeMissingUserName() {
given()
.contentType("application/x-www-form-urlencoded; charset=UTF-8")
.header("Authorization", AUTH)
.formParam("Action", "SendRawEmail")
.formParam("Source", "sender@example.com")
.formParam("Destinations.member.1", TO)
.formParam("RawMessage.Data", rawMessage("sender.example.com, sender@"))
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"))
.body("ErrorResponse.Error.Message", equalTo("Missing domain"));
}
private static String rawMessage(String from) {
return Base64.getEncoder().encodeToString(("From: " + from + "\r\nTo: " + TO + "\r\nSubject: s\r\n\r\nb")
.getBytes(StandardCharsets.UTF_8));
@@ -142,6 +142,61 @@ class SesServiceAddressSyntaxTest {
}
}
@Test
void v1RawSendWithSender_fromHeaderWithoutAtIsAMissingUserName() {
assertRejects("User name is missing: 'sender.example.com'.", () -> service.sendEmail(request(SENDER)
.content(fromHeaderRaw(NO_AT, false)).build()));
assertRejects("User name is missing: '=?UTF-8?B?5bGx55Sw?= <sender.example.com>'.",
() -> service.sendEmail(request(SENDER).content(fromHeaderRaw("山田 <" + NO_AT + ">", false)).build()));
}
@Test
void v1RawSendWithSender_addressInTheDisplayNameIsNotSwappedIn() {
assertRejects("User name is missing: '\"sender@example.com\" <sender.example.com>'.",
() -> service.sendEmail(request(SENDER).content(fromHeaderRaw(SENDER + " <" + NO_AT + ">", false)).build()));
}
@Test
void v1RawSendWithSender_onlyTheFirstFromHeaderAddressNeedsAnAt() {
assertRejects("User name is missing: 'sender.example.com'.", () -> service.sendEmail(request(SENDER)
.content(fromHeaderRaw(NO_AT + ", " + SENDER, false)).build()));
AwsException e = assertThrows(AwsException.class, () -> service.sendEmail(request(SENDER)
.content(fromHeaderRaw(SENDER + ", " + NO_AT, false)).build()));
assertEquals("MessageRejected", e.getErrorCode(), "a later address without @ reaches sender verification");
}
@Test
void v1RawSendWithSender_otherFromHeaderViolationsAreReportedFirst() {
assertRejects("Missing domain", () -> service.sendEmail(request(SENDER)
.content(fromHeaderRaw(NO_AT + ", " + EMPTY_DOMAIN, false)).build()));
}
@Test
void v1RawSendWithSender_missingUserNameIsReportedAfterRecipientSyntax() {
assertRejects(MISSING_FINAL_DOMAIN, () -> service.sendEmail(request(SENDER).toAddresses(List.of())
.content(fromHeaderRaw(NO_AT, false, "success.simulator.amazonses.com")).build()));
}
@Test
void v1RawSendWithSender_missingUserNameIsReportedAfterUnknownConfigurationSet() {
AwsException e = assertThrows(AwsException.class, () -> service.sendEmail(request(SENDER)
.configurationSetName("missing").content(fromHeaderRaw(NO_AT, false)).build()));
assertEquals("ConfigurationSetDoesNotExist", e.getErrorCode());
}
@Test
void v1RawSendWithSender_missingUserNameIsReportedAfterMissingDestination() {
assertRejects("Missing required header 'To'.", () -> service.sendEmail(request(SENDER).toAddresses(List.of())
.content(rawWithoutTo(NO_AT, false)).build()));
}
@Test
void v1RawSendWithSender_missingUserNameIsReportedBeforeLength() {
assertRejects("User name is missing: 'sender.example.com'.", () -> service.sendEmail(request(SENDER)
.toAddresses(List.of(address(321))).content(fromHeaderRaw(NO_AT, false)).build()));
}
@Test
void v2RawSend_fromHeaderWithoutAtKeepsJavaMailMessage() {
assertRejects(MISSING_FINAL_DOMAIN, () -> service.sendEmail(request(null)
@@ -324,8 +379,12 @@ class SesServiceAddressSyntaxTest {
}
private static EmailContent.Raw v2RawWithoutTo(String from) {
return rawWithoutTo(from, true);
}
private static EmailContent.Raw rawWithoutTo(String from, boolean fromHeaderAsFallback) {
return new EmailContent.Raw(Base64.getEncoder().encodeToString(("From: " + from
+ "\r\nSubject: s\r\n\r\nbody").getBytes(StandardCharsets.UTF_8)), true);
+ "\r\nSubject: s\r\n\r\nbody").getBytes(StandardCharsets.UTF_8)), fromHeaderAsFallback);
}
private static EmailContent.Raw raw(String from, String to, String extraHeaders) {