Skip to content

Commit e601fcd

Browse files
committed
New domain list checking
1 parent 9268c38 commit e601fcd

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/EmailService.java

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -299,46 +299,77 @@ public EmailSent send(Email email, Authentication auth, Profile profile, String
299299
"failed to send email with subject ["
300300
+ email.subject()
301301
+ "] and recipient domains "
302-
+ getRecipientDomains(email)
302+
+ getRecipients(email, true)
303303
+ ", one or more recipients is not specified in the domain allow list setting ["
304304
+ SETTING_DOMAIN_ALLOWLIST.getKey()
305305
+ "]."
306306
);
307307
}
308+
if (recipientAddressInAllowList(email, this.allowedRecipientPatterns) == false) {
309+
throw new IllegalArgumentException(
310+
"failed to send email with subject ["
311+
+ email.subject()
312+
+ "] and recipients "
313+
+ getRecipients(email, false)
314+
+ ", one or more recipients is not specified in the domain allow list setting ["
315+
+ SETTING_RECIPIENT_ALLOW_PATTERNS.getKey()
316+
+ "]."
317+
);
318+
}
308319
return send(email, auth, profile, account);
309320
}
310321

311322
// Visible for testing
312-
static Set<String> getRecipientDomains(Email email) {
313-
return Stream.concat(
323+
static Set<String> getRecipients(Email email, boolean domainsOnly) {
324+
var stream = Stream.concat(
314325
Optional.ofNullable(email.to()).map(addrs -> Arrays.stream(addrs.toArray())).orElse(Stream.empty()),
315326
Stream.concat(
316327
Optional.ofNullable(email.cc()).map(addrs -> Arrays.stream(addrs.toArray())).orElse(Stream.empty()),
317328
Optional.ofNullable(email.bcc()).map(addrs -> Arrays.stream(addrs.toArray())).orElse(Stream.empty())
318329
)
319-
)
320-
.map(InternetAddress::getAddress)
321-
// Pull out only the domain of the email address, so [email protected] -> bar.com
322-
.map(emailAddress -> emailAddress.substring(emailAddress.lastIndexOf('@') + 1))
323-
.collect(Collectors.toSet());
330+
).map(InternetAddress::getAddress);
331+
332+
if (domainsOnly) {
333+
// Pull out only the domain of the email address, so [email protected]
334+
stream = stream.map(emailAddress -> emailAddress.substring(emailAddress.lastIndexOf('@') + 1));
335+
}
336+
337+
return stream.collect(Collectors.toSet());
324338
}
325339

326340
// Visible for testing
327341
static boolean recipientDomainsInAllowList(Email email, Set<String> allowedDomainSet) {
328-
if (allowedDomainSet.size() == 0) {
342+
if (allowedDomainSet.isEmpty()) {
329343
// Nothing is allowed
330344
return false;
331345
}
332346
if (allowedDomainSet.contains("*")) {
333347
// Don't bother checking, because there is a wildcard all
334348
return true;
335349
}
336-
final Set<String> domains = getRecipientDomains(email);
350+
final Set<String> domains = getRecipients(email, true);
337351
final Predicate<String> matchesAnyAllowedDomain = domain -> allowedDomainSet.stream()
338352
.anyMatch(allowedDomain -> Regex.simpleMatch(allowedDomain, domain, true));
339353
return domains.stream().allMatch(matchesAnyAllowedDomain);
340354
}
341355

356+
// Visible for testing
357+
static boolean recipientAddressInAllowList(Email email, Set<String> allowedRecipientPatterns) {
358+
if (allowedRecipientPatterns.isEmpty()) {
359+
// Nothing is allowed
360+
return false;
361+
}
362+
if (allowedRecipientPatterns.contains("*")) {
363+
// Don't bother checking, because there is a wildcard all
364+
return true;
365+
}
366+
367+
final Set<String> recipients = getRecipients(email, false);
368+
final Predicate<String> matchesAnyAllowedRecipient = recipient -> recipients.stream()
369+
.anyMatch(allowedDomain -> Regex.simpleMatch(allowedDomain, recipient, true));
370+
return recipients.stream().allMatch(matchesAnyAllowedRecipient);
371+
}
372+
342373
private static EmailSent send(Email email, Authentication auth, Profile profile, Account account) throws MessagingException {
343374
assert account != null;
344375
try {

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/EmailServiceTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.List;
2222
import java.util.Properties;
2323
import java.util.Set;
24-
2524
import javax.mail.MessagingException;
2625

2726
import static org.hamcrest.Matchers.containsInAnyOrder;
@@ -140,7 +139,7 @@ public void testExtractDomains() throws Exception {
140139
Collections.emptyMap()
141140
);
142141
assertThat(
143-
EmailService.getRecipientDomains(email),
142+
EmailService.getRecipients(email, true),
144143
containsInAnyOrder("bar.com", "eggplant.com", "example.com", "another.com", "bcc.com")
145144
);
146145

@@ -158,7 +157,7 @@ public void testExtractDomains() throws Exception {
158157
"htmlbody",
159158
Collections.emptyMap()
160159
);
161-
assertThat(EmailService.getRecipientDomains(email), containsInAnyOrder("bar.com", "eggplant.com", "example.com"));
160+
assertThat(EmailService.getRecipients(email, true), containsInAnyOrder("bar.com", "eggplant.com", "example.com"));
162161
}
163162

164163
public void testAllowedDomain() throws Exception {

0 commit comments

Comments
 (0)