mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
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:
1 parent
16293b609b
commit
3ec1466014
8 files changed
+446
-18
No files matched your search
@@ -23,12 +23,16 @@ import java.util.function.Consumer;
|
||||
* remaps the code to BadRequestException. A v2 raw send checks its From header only without a
|
||||
* sender parameter, and then answers an invalid From address for every violation but a missing
|
||||
* {@code @}, which it reports later (probe-confirmed for the four JavaMail messages; a quoted CR
|
||||
* or LF is assumed to follow).
|
||||
* or LF is assumed to follow). A v1 raw send with a sender parameter checks every From header
|
||||
* address but lets a missing {@code @} pass, as JavaMail's non-strict header parse does, and
|
||||
* reports one only on the first address, later and in its own wording (probe-confirmed).
|
||||
*/
|
||||
final class SesAddressSyntax {
|
||||
|
||||
private static final List<String> RAW_CHECKED_HEADERS = List.of("To", "Cc", "Bcc");
|
||||
private static final String MISSING_FINAL_DOMAIN = "Missing final '@domain'";
|
||||
private static final String PHRASE_SPECIALS = "()<>@,;:\\\".[]";
|
||||
private static final String ADDRESS_SPECIALS = "()<>,;:\\\"[]";
|
||||
|
||||
private SesAddressSyntax() {
|
||||
}
|
||||
@@ -43,27 +47,58 @@ final class SesAddressSyntax {
|
||||
}
|
||||
|
||||
private static Consumer<String> fromHeaderCheck(SendEmailRequest request, EmailContent.Raw raw) {
|
||||
if (!raw.checksFromHeader(request.hasSource())) {
|
||||
return SesSendAddresses.UNCHECKED;
|
||||
if (readsFromHeaderAsSender(request, raw)) {
|
||||
return SesAddressSyntax::requireFallbackFrom;
|
||||
}
|
||||
return raw.fromHeaderAsFallback() ? SesAddressSyntax::requireFallbackFrom : SesAddressSyntax::require;
|
||||
if (checksFromHeaderBesideSender(request, raw)) {
|
||||
return SesAddressSyntax::requireExceptAt;
|
||||
}
|
||||
return raw.checksFromHeader(request.hasSource()) ? SesAddressSyntax::require : SesSendAddresses.UNCHECKED;
|
||||
}
|
||||
|
||||
private static boolean readsFromHeaderAsSender(SendEmailRequest request, EmailContent.Raw raw) {
|
||||
return raw.fromHeaderAsFallback() && !request.hasSource();
|
||||
}
|
||||
|
||||
private static boolean checksFromHeaderBesideSender(SendEmailRequest request, EmailContent.Raw raw) {
|
||||
return !raw.fromHeaderAsFallback() && request.hasSource();
|
||||
}
|
||||
|
||||
private static void requireFallbackFrom(String address) {
|
||||
String violation = violation(address);
|
||||
if (violation != null && !MISSING_FINAL_DOMAIN.equals(violation)) {
|
||||
if (violationExceptAt(address) != null) {
|
||||
throw new AwsException("InvalidParameterValue", "Invalid From address provided.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireExceptAt(String address) {
|
||||
String violation = violationExceptAt(address);
|
||||
if (violation != null) {
|
||||
throw new AwsException("InvalidParameterValue", violation, 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static String violationExceptAt(String address) {
|
||||
String violation = violation(address);
|
||||
return MISSING_FINAL_DOMAIN.equals(violation) ? null : violation;
|
||||
}
|
||||
|
||||
/**
|
||||
* The missing {@code @} of a v2 raw From header read in place of the sender, which AWS reports
|
||||
* only after the configuration set and the missing-destination checks, and before the length
|
||||
* check (probe-confirmed).
|
||||
* The missing {@code @} the From header check let pass, which AWS reports only after the
|
||||
* configuration set and the missing-destination checks (probe-confirmed). A v2 send reports it
|
||||
* on any From header address read in place of the sender, before the length check
|
||||
* (probe-confirmed). A v1 send with a sender parameter reports it on the first address only,
|
||||
* as JavaMail's "User name is missing" around that address's {@code toString()} form
|
||||
* (probe-confirmed; the same place relative to the length check is assumed).
|
||||
*/
|
||||
static void requireFallbackFromAt(SendEmailRequest request, EmailContent.Raw raw, Message message) {
|
||||
if (raw.fromHeaderAsFallback() && !request.hasSource()) {
|
||||
static void requireFromHeaderAt(SendEmailRequest request, EmailContent.Raw raw, Message message) {
|
||||
if (readsFromHeaderAsSender(request, raw)) {
|
||||
SesSendAddresses.forEachHeaderAddress(message, "From", SesAddressSyntax::requireAt);
|
||||
} else if (checksFromHeaderBesideSender(request, raw)) {
|
||||
String first = SesSendAddresses.firstHeaderAddress(message, "From");
|
||||
if (MISSING_FINAL_DOMAIN.equals(violation(first))) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"User name is missing: '" + javaMailString(first) + "'.", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,6 +108,75 @@ final class SesAddressSyntax {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A mailbox as JavaMail's {@code InternetAddress.toString()} writes it once parsed: the display
|
||||
* name as written from its first character outside a comment to the angle bracket, later
|
||||
* comments included, and dropped when it is blank once unquoted, or the first comment only when
|
||||
* only comments precede the angle bracket (probe-confirmed each way). The name is quoted again only when it holds a special character, then the addr-spec
|
||||
* follows in angle brackets; a bare addr-spec keeps no brackets unless it holds a special
|
||||
* character. A raw non-ASCII name comes out as
|
||||
* {@link SesEncodedWord} writes it and an encoded-word stays as written (probe-confirmed for a
|
||||
* short all non-ASCII name, which is one B encoded-word; longer and mixed names follow JavaMail
|
||||
* and are assumed).
|
||||
*/
|
||||
static String javaMailString(String mailbox) {
|
||||
String addrSpec = addrSpec(mailbox);
|
||||
String personal = personal(mailbox);
|
||||
if (personal != null) {
|
||||
return encodePhrase(personal) + " <" + addrSpec + ">";
|
||||
}
|
||||
return containsAny(addrSpec, ADDRESS_SPECIALS) ? "<" + addrSpec + ">" : addrSpec;
|
||||
}
|
||||
|
||||
private static String personal(String mailbox) {
|
||||
Stripped stripped = stripComments(mailbox);
|
||||
if (stripped.lastAngle() >= 0 && stripped.text().trim().endsWith(">")
|
||||
&& stripped.phraseStart() < stripped.lastAngle()) {
|
||||
String phrase = unquote(mailbox.substring(stripped.phraseStart(), stripped.lastAngle()).trim());
|
||||
return phrase.isBlank() ? null : phrase;
|
||||
}
|
||||
String comment = stripped.firstComment();
|
||||
return comment == null || comment.isBlank() ? null : comment.trim();
|
||||
}
|
||||
|
||||
private static String unquote(String phrase) {
|
||||
if (phrase.length() < 2 || !phrase.startsWith("\"") || !phrase.endsWith("\"")) {
|
||||
return phrase;
|
||||
}
|
||||
StringBuilder out = new StringBuilder();
|
||||
for (int i = 1; i < phrase.length() - 1; i++) {
|
||||
char c = phrase.charAt(i);
|
||||
if (c == '\\' && i + 1 < phrase.length() - 1) {
|
||||
c = phrase.charAt(++i);
|
||||
}
|
||||
out.append(c);
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
private static String encodePhrase(String personal) {
|
||||
if (SesEncodedWord.needsEncoding(personal)) {
|
||||
return SesEncodedWord.encode(personal);
|
||||
}
|
||||
boolean needsQuotes = false;
|
||||
StringBuilder quoted = new StringBuilder("\"");
|
||||
for (int i = 0; i < personal.length(); i++) {
|
||||
char c = personal.charAt(i);
|
||||
if (c == '"' || c == '\\') {
|
||||
quoted.append('\\');
|
||||
needsQuotes = true;
|
||||
} else if (PHRASE_SPECIALS.indexOf(c) >= 0) {
|
||||
needsQuotes = true;
|
||||
}
|
||||
quoted.append(c);
|
||||
}
|
||||
return needsQuotes ? quoted.append('"').toString() : personal;
|
||||
}
|
||||
|
||||
private static boolean containsAny(String value, String characters) {
|
||||
return value.chars().anyMatch(c -> characters.indexOf(c) >= 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks a bulk sender or return path (v2 FeedbackForwardingEmailAddress). A bulk send words a
|
||||
* malformed or over-long one as an invalid address for the whole request, echoing it as given,
|
||||
@@ -155,7 +259,7 @@ final class SesAddressSyntax {
|
||||
}
|
||||
|
||||
private static String addrSpec(String mailbox) {
|
||||
String withoutComments = withoutComments(mailbox).trim();
|
||||
String withoutComments = stripComments(mailbox).text().trim();
|
||||
int open = SesSendAddresses.lastUnquotedAngle(withoutComments);
|
||||
if (open >= 0 && withoutComments.endsWith(">")) {
|
||||
return withoutComments.substring(open + 1, withoutComments.length() - 1).trim();
|
||||
@@ -163,8 +267,15 @@ final class SesAddressSyntax {
|
||||
return withoutComments;
|
||||
}
|
||||
|
||||
private static String withoutComments(String mailbox) {
|
||||
private record Stripped(String text, String firstComment, int phraseStart, int lastAngle) {
|
||||
}
|
||||
|
||||
private static Stripped stripComments(String mailbox) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
String firstComment = null;
|
||||
int commentStart = -1;
|
||||
int phraseStart = -1;
|
||||
int lastAngle = -1;
|
||||
boolean quoted = false;
|
||||
int commentDepth = 0;
|
||||
for (int i = 0; i < mailbox.length(); i++) {
|
||||
@@ -179,8 +290,8 @@ final class SesAddressSyntax {
|
||||
if (commentDepth > 0) {
|
||||
if (c == '(') {
|
||||
commentDepth++;
|
||||
} else if (c == ')') {
|
||||
commentDepth--;
|
||||
} else if (c == ')' && --commentDepth == 0 && firstComment == null) {
|
||||
firstComment = mailbox.substring(commentStart, i);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -188,10 +299,16 @@ final class SesAddressSyntax {
|
||||
quoted = !quoted;
|
||||
} else if (!quoted && c == '(') {
|
||||
commentDepth = 1;
|
||||
commentStart = i + 1;
|
||||
continue;
|
||||
} else if (!quoted && c == '<') {
|
||||
lastAngle = i;
|
||||
}
|
||||
if (phraseStart < 0 && !Character.isWhitespace(c)) {
|
||||
phraseStart = i;
|
||||
}
|
||||
out.append(c);
|
||||
}
|
||||
return out.toString();
|
||||
return new Stripped(out.toString(), firstComment, phraseStart, lastAngle);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* A display name as JavaMail's {@code MimeUtility.encodeWord} writes it in UTF-8: B when at least
|
||||
* half its characters are non-ASCII and Q otherwise, halved recursively at the middle until each
|
||||
* encoded-word fits JavaMail's limit, and the words joined by a space. AWS echoes a name in this
|
||||
* form in the "User name is missing" message of a v1 raw send.
|
||||
*/
|
||||
final class SesEncodedWord {
|
||||
|
||||
private static final String CHARSET = "UTF-8";
|
||||
private static final int AVAILABLE = 68 - CHARSET.length();
|
||||
private static final String WORD_SPECIALS = "=_?\"#$%&'(),.:;<>@[\\]^`{|}~";
|
||||
private static final char[] HEX = "0123456789ABCDEF".toCharArray();
|
||||
|
||||
private SesEncodedWord() {
|
||||
}
|
||||
|
||||
static boolean needsEncoding(String text) {
|
||||
return text.chars().anyMatch(SesEncodedWord::isNonAscii);
|
||||
}
|
||||
|
||||
static String encode(String text) {
|
||||
long nonAscii = text.chars().filter(SesEncodedWord::isNonAscii).count();
|
||||
StringBuilder out = new StringBuilder();
|
||||
encode(text, nonAscii >= text.length() - nonAscii, true, out);
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
private static void encode(String text, boolean base64, boolean first, StringBuilder out) {
|
||||
byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
|
||||
int length = base64 ? (bytes.length + 2) / 3 * 4 : qLength(bytes);
|
||||
if (length > AVAILABLE && text.length() > 1) {
|
||||
int split = text.length() / 2;
|
||||
if (Character.isHighSurrogate(text.charAt(split - 1))) {
|
||||
split--;
|
||||
}
|
||||
if (split > 0) {
|
||||
encode(text.substring(0, split), base64, first, out);
|
||||
}
|
||||
encode(text.substring(split), base64, false, out);
|
||||
return;
|
||||
}
|
||||
if (!first) {
|
||||
out.append(' ');
|
||||
}
|
||||
out.append("=?").append(CHARSET).append(base64 ? "?B?" : "?Q?");
|
||||
if (base64) {
|
||||
out.append(Base64.getEncoder().encodeToString(bytes));
|
||||
} else {
|
||||
appendQ(bytes, out);
|
||||
}
|
||||
out.append("?=");
|
||||
}
|
||||
|
||||
private static int qLength(byte[] bytes) {
|
||||
int length = 0;
|
||||
for (byte b : bytes) {
|
||||
length += isQEscaped(b & 0xff) ? 3 : 1;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
private static void appendQ(byte[] bytes, StringBuilder out) {
|
||||
for (byte b : bytes) {
|
||||
int c = b & 0xff;
|
||||
if (c == ' ') {
|
||||
out.append('_');
|
||||
} else if (isQEscaped(c)) {
|
||||
out.append('=').append(HEX[c >> 4]).append(HEX[c & 0x0f]);
|
||||
} else {
|
||||
out.append((char) c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isQEscaped(int c) {
|
||||
return c < ' ' || c >= 0x7f || WORD_SPECIALS.indexOf(c) >= 0;
|
||||
}
|
||||
|
||||
private static boolean isNonAscii(int c) {
|
||||
return c >= 0x7f || (c < ' ' && c != '\r' && c != '\n' && c != '\t');
|
||||
}
|
||||
}
|
||||
@@ -133,6 +133,23 @@ final class SesSendAddresses {
|
||||
}
|
||||
}
|
||||
|
||||
/** The first address the named headers hold, or null when the message has none. */
|
||||
static String firstHeaderAddress(Message message, String name) {
|
||||
Header header = message == null ? null : message.getHeader();
|
||||
if (header == null) {
|
||||
return null;
|
||||
}
|
||||
for (Field field : header.getFields(name)) {
|
||||
if (field.getBody() != null) {
|
||||
List<String> addresses = splitAddressList(field.getBody());
|
||||
if (!addresses.isEmpty()) {
|
||||
return addresses.get(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static void forEachHeaderAddress(Message message, String name, Consumer<String> check) {
|
||||
Header header = message == null ? null : message.getHeader();
|
||||
if (header == null) {
|
||||
|
||||
@@ -563,7 +563,7 @@ public class SesService {
|
||||
if (effectiveDestinations.isEmpty()) {
|
||||
throw new AwsException("InvalidParameterValue", "Missing required header 'To'.", 400);
|
||||
}
|
||||
SesAddressSyntax.requireFallbackFromAt(request, raw, parsed.message());
|
||||
SesAddressSyntax.requireFromHeaderAt(request, raw, parsed.message());
|
||||
SesAddressLength.requireRaw(request, raw, parsed.message());
|
||||
if (requireVerifiedSender) {
|
||||
requireVerifiedSenders(rawSendingAddresses(request, raw, headers), region);
|
||||
|
||||
Reference in new issue
Block a user