mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(ses): reject send addresses longer than 320 characters (#4749)
* fix(ses): reject send addresses longer than 320 characters * fix(ses): ignore quotes and commas inside comments when splitting address headers
This commit is contained in:
1 parent
b0f30ae94d
commit
0b53580969
6 files changed
+787
-17
No files matched your search
@@ -0,0 +1,167 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.ses.model.SendEmailRequest;
|
||||
import org.apache.james.mime4j.dom.Header;
|
||||
import org.apache.james.mime4j.dom.Message;
|
||||
import org.apache.james.mime4j.stream.Field;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The 320-character limit AWS applies to every address of a send (probe-confirmed). AWS measures
|
||||
* an address in UTF-16 units in the {@code name <address>} form that JavaMail's
|
||||
* {@code InternetAddress.toUnicodeString()} prints: the display name is quoted only when it holds
|
||||
* a non-ASCII character or an RFC 822 special, an encoded-word name is measured undecoded, and the
|
||||
* error echoes that form. The v2 controller remaps the code to BadRequestException.
|
||||
*/
|
||||
final class SesAddressLength {
|
||||
|
||||
static final int MAX_LENGTH = 320;
|
||||
|
||||
private static final String PHRASE_SPECIALS = "()<>@,;:\\\".[]";
|
||||
private static final List<String> RAW_RECIPIENT_HEADERS = List.of("To", "Cc", "Bcc", "Reply-To", "Return-Path");
|
||||
|
||||
private SesAddressLength() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the envelope fields in the order AWS reports them: the sender, To, Cc, Bcc, the return
|
||||
* path (v2 FeedbackForwardingEmailAddress), then Reply-To.
|
||||
*/
|
||||
static void requireEnvelope(SendEmailRequest request) {
|
||||
require(request.source());
|
||||
requireRecipients(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks a raw send: the sender parameter, the From header, the envelope fields of the request,
|
||||
* and then the remaining address headers of the MIME message. Header addresses are measured as
|
||||
* written, like the request fields: an encoded-word name stays encoded and escaped quotes count
|
||||
* (probe-confirmed). Each address of a header is checked on its own, so a header listing
|
||||
* several short addresses passes however long it is.
|
||||
*/
|
||||
static void requireRaw(SendEmailRequest request, Message message) {
|
||||
require(request.source());
|
||||
requireHeader(message, "From");
|
||||
requireRecipients(request);
|
||||
for (String name : RAW_RECIPIENT_HEADERS) {
|
||||
requireHeader(message, name);
|
||||
}
|
||||
}
|
||||
|
||||
static void require(String address) {
|
||||
if (address == null || address.isBlank()) {
|
||||
return;
|
||||
}
|
||||
String trimmed = address.trim();
|
||||
int open = trimmed.lastIndexOf('<');
|
||||
if (open < 0 || !trimmed.endsWith(">")) {
|
||||
requireNormalized(trimmed);
|
||||
return;
|
||||
}
|
||||
String name = trimmed.substring(0, open).trim();
|
||||
if (name.length() >= 2 && name.startsWith("\"") && name.endsWith("\"")) {
|
||||
name = name.substring(1, name.length() - 1);
|
||||
}
|
||||
requireNormalized(format(name, trimmed.substring(open + 1, trimmed.length() - 1).trim()));
|
||||
}
|
||||
|
||||
private static void requireRecipients(SendEmailRequest request) {
|
||||
requireAll(request.toAddresses());
|
||||
requireAll(request.ccAddresses());
|
||||
requireAll(request.bccAddresses());
|
||||
require(request.returnPath());
|
||||
requireAll(request.replyToAddresses());
|
||||
}
|
||||
|
||||
private static void requireAll(List<String> addresses) {
|
||||
if (addresses != null) {
|
||||
addresses.forEach(SesAddressLength::require);
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireHeader(Message message, String name) {
|
||||
Header header = message == null ? null : message.getHeader();
|
||||
if (header == null) {
|
||||
return;
|
||||
}
|
||||
for (Field field : header.getFields(name)) {
|
||||
if (field.getBody() != null) {
|
||||
splitAddressList(field.getBody()).forEach(SesAddressLength::require);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits an RFC 5322 address-list header body at the commas that separate its addresses,
|
||||
* ignoring commas inside a quoted display name, a comment or an angle-bracketed address. A
|
||||
* quote inside a comment is literal. A group's {@code name:} prefix and closing semicolon are
|
||||
* dropped, leaving its member addresses.
|
||||
*/
|
||||
static List<String> splitAddressList(String body) {
|
||||
List<String> addresses = new ArrayList<>();
|
||||
StringBuilder current = new StringBuilder();
|
||||
boolean quoted = false;
|
||||
boolean angled = false;
|
||||
int commentDepth = 0;
|
||||
for (int i = 0; i < body.length(); i++) {
|
||||
char c = body.charAt(i);
|
||||
if ((quoted || commentDepth > 0) && c == '\\' && i + 1 < body.length()) {
|
||||
current.append(c).append(body.charAt(++i));
|
||||
continue;
|
||||
}
|
||||
if (commentDepth > 0) {
|
||||
if (c == '(') {
|
||||
commentDepth++;
|
||||
} else if (c == ')') {
|
||||
commentDepth--;
|
||||
}
|
||||
} else if (c == '"') {
|
||||
quoted = !quoted;
|
||||
} else if (!quoted && c == '(') {
|
||||
commentDepth = 1;
|
||||
} else if (!quoted && c == '<') {
|
||||
angled = true;
|
||||
} else if (!quoted && c == '>') {
|
||||
angled = false;
|
||||
} else if (!quoted && !angled && c == ':') {
|
||||
current.setLength(0);
|
||||
continue;
|
||||
} else if (!quoted && !angled && (c == ',' || c == ';')) {
|
||||
addresses.add(current.toString());
|
||||
current.setLength(0);
|
||||
continue;
|
||||
}
|
||||
current.append(c);
|
||||
}
|
||||
addresses.add(current.toString());
|
||||
addresses.removeIf(String::isBlank);
|
||||
return addresses;
|
||||
}
|
||||
|
||||
private static String format(String name, String address) {
|
||||
if (name == null || name.isEmpty()) {
|
||||
return address;
|
||||
}
|
||||
return (needsQuoting(name) ? "\"" + name + "\"" : name) + " <" + address + ">";
|
||||
}
|
||||
|
||||
private static boolean needsQuoting(String name) {
|
||||
for (int i = 0; i < name.length(); i++) {
|
||||
char c = name.charAt(i);
|
||||
if ((c < 0x20 && c != '\t') || c >= 0x7f || PHRASE_SPECIALS.indexOf(c) >= 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void requireNormalized(String address) {
|
||||
if (address.length() > MAX_LENGTH) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"Address length is more than " + MAX_LENGTH + " characters long: '" + address + "'.", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -215,32 +215,48 @@ public class SesService {
|
||||
}
|
||||
|
||||
public String sendEmail(SendEmailRequest request) {
|
||||
return switch (request.content()) {
|
||||
case EmailContent.Simple simple -> sendSimpleEmail(request, simple);
|
||||
if (request.content() instanceof EmailContent.Raw raw) {
|
||||
return sendRawEmail(request, raw);
|
||||
}
|
||||
if (request.content() instanceof EmailContent.InlineTemplate inline) {
|
||||
requireInlineTemplateContent(inline.subject(), inline.textPart(), inline.htmlPart());
|
||||
}
|
||||
// AWS reports an unknown configuration set, then an over-long address, and only then a
|
||||
// missing stored template (probe-confirmed), so the content is resolved after these checks.
|
||||
String effectiveConfigSet = validateEnvelope(request);
|
||||
SesAddressLength.requireEnvelope(request);
|
||||
EmailContent.Simple content = switch (request.content()) {
|
||||
case EmailContent.Simple simple -> simple;
|
||||
case EmailContent.Template template ->
|
||||
sendInlineTemplatedEmail(request, templateService.inline(template, request.region()));
|
||||
case EmailContent.InlineTemplate inline -> sendInlineTemplatedEmail(request, inline);
|
||||
case EmailContent.Raw raw -> sendRawEmail(request, raw);
|
||||
renderInlineTemplate(templateService.inline(template, request.region()));
|
||||
case EmailContent.InlineTemplate inline -> SesTemplateService.render(inline);
|
||||
case EmailContent.Raw raw -> throw new IllegalStateException("Raw content is sent above");
|
||||
};
|
||||
return sendSimpleEmail(request.toBuilder().content(content).build(), content, effectiveConfigSet);
|
||||
}
|
||||
|
||||
private String sendInlineTemplatedEmail(SendEmailRequest request, EmailContent.InlineTemplate template) {
|
||||
private static EmailContent.Simple renderInlineTemplate(EmailContent.InlineTemplate template) {
|
||||
requireInlineTemplateContent(template.subject(), template.textPart(), template.htmlPart());
|
||||
EmailContent.Simple rendered = SesTemplateService.render(template);
|
||||
return sendSimpleEmail(request.toBuilder().content(rendered).build(), rendered);
|
||||
return SesTemplateService.render(template);
|
||||
}
|
||||
|
||||
private String sendSimpleEmail(SendEmailRequest request, EmailContent.Simple content) {
|
||||
private String validateEnvelope(SendEmailRequest request) {
|
||||
String source = request.source();
|
||||
String region = request.region();
|
||||
if (source == null || source.isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "Source email is required.", 400);
|
||||
}
|
||||
if (!request.hasRecipients()) {
|
||||
throw new AwsException("InvalidParameterValue", "At least one destination address is required.", 400);
|
||||
}
|
||||
String effectiveConfigSet = resolveDefaultConfigurationSet(request.configurationSetName(), source, region);
|
||||
configSetService.validateForSending(effectiveConfigSet, region);
|
||||
String effectiveConfigSet = resolveDefaultConfigurationSet(request.configurationSetName(), source,
|
||||
request.region());
|
||||
configSetService.validateForSending(effectiveConfigSet, request.region());
|
||||
return effectiveConfigSet;
|
||||
}
|
||||
|
||||
private String sendSimpleEmail(SendEmailRequest request, EmailContent.Simple content, String effectiveConfigSet) {
|
||||
String source = request.source();
|
||||
String region = request.region();
|
||||
|
||||
// Resolve suppression before recording the message so a bad ListManagementOptions (e.g. an
|
||||
// unknown contact list) fails the whole send without leaving an orphaned SentEmail record.
|
||||
@@ -363,6 +379,7 @@ public class SesService {
|
||||
// both with this message.
|
||||
throw new AwsException("InvalidParameterValue", "Missing required header 'From'.", 400);
|
||||
}
|
||||
SesAddressLength.requireRaw(request, parsed.message());
|
||||
// FromEmailAddress was omitted, so the configuration set couldn't be resolved from the
|
||||
// sender until the MIME "From" was parsed. Re-resolve from the effective sender now so an
|
||||
// email identity's default configuration set still applies to a Raw send without an
|
||||
@@ -1499,7 +1516,10 @@ public class SesService {
|
||||
.region(request.region())
|
||||
.content(rendered)
|
||||
.build();
|
||||
String messageId = sendSimpleEmail(entryRequest, rendered);
|
||||
// SesAddressLength is not applied: AWS rejects an over-long bulk sender as "Invalid
|
||||
// email address", and an over-long destination did not fail the length check ahead
|
||||
// of sender verification (probe-confirmed; the verified-sender case is unprobed).
|
||||
String messageId = sendSimpleEmail(entryRequest, rendered, validateEnvelope(entryRequest));
|
||||
results.add(BulkEmailEntryResult.success(messageId));
|
||||
} catch (AwsException e) {
|
||||
results.add(BulkEmailEntryResult.failure(
|
||||
|
||||
Reference in new issue
Block a user