Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,19 @@
import java.net.SocketException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import jenkins.model.Jenkins;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.groovy.control.CompilerConfiguration;
Expand Down Expand Up @@ -582,12 +586,18 @@
return false;
}

Address[] allRecipients = msg.getAllRecipients();
Address[] allRecipients;

int retries = 0;
if (executePresendScript(context, msg)) {
// presend script might have modified recipients:
allRecipients = msg.getAllRecipients();
if (allRecipients != null) {
if (allRecipients == null || allRecipients.length == 0) {

Check warning on line 595 in src/main/java/hudson/plugins/emailext/ExtendedEmailPublisher.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 595 is only partially covered, one branch is missing
context.getListener().getLogger().println("No recipients found, email couldn't be sent.");

return false;
}
{
if (StringUtils.isNotBlank(getDescriptor().getEmergencyReroute())) {
// clear out all the existing recipients
msg.setRecipients(Message.RecipientType.TO, (Address[]) null);
Expand Down Expand Up @@ -708,11 +718,11 @@

if (context.getRun().getAction(MailMessageIdAction.class) == null) {
context.getRun().addAction(new MailMessageIdAction(msg.getMessageID()));
} else {
context.getListener()
.getLogger()
.println("An attempt to send an e-mail" + " to empty list of recipients, ignored.");
}
} else {
context.getListener()
.getLogger()
.println("An attempt to send an e-mail" + " to empty list of recipients, ignored.");
}
} else {
context.getListener().getLogger().println("Email sending was cancelled" + " by user script.");
Expand Down Expand Up @@ -984,6 +994,52 @@
return descriptor.getMailAccount();
}

void logDuplicateRecipients(
ExtendedEmailPublisherContext context,
Set<InternetAddress> to,
Set<InternetAddress> cc,
Set<InternetAddress> bcc) {

Set<String> toEmails =
to.stream().map(a -> a.getAddress().toLowerCase()).collect(Collectors.toSet());

Set<String> seen = new HashSet<>();
to.removeIf(addr -> !seen.add(addr.getAddress().toLowerCase()));

Check warning on line 1007 in src/main/java/hudson/plugins/emailext/ExtendedEmailPublisher.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 1007 is only partially covered, one branch is missing

seen.clear();
cc.removeIf(addr -> {
String email = addr.getAddress().toLowerCase();
return !seen.add(email) || toEmails.contains(email);

Check warning on line 1012 in src/main/java/hudson/plugins/emailext/ExtendedEmailPublisher.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 1012 is only partially covered, one branch is missing
});

Set<String> ccEmails =
cc.stream().map(a -> a.getAddress().toLowerCase()).collect(Collectors.toSet());

List<String> order = Arrays.asList("TO", "CC", "BCC");

bcc.removeIf(addr -> {
String email = addr.getAddress().toLowerCase();

if (toEmails.contains(email)) {
return true;
}

if (ccEmails.contains(email)) {

List<String> locations = new ArrayList<>(Arrays.asList("CC", "BCC"));
locations.sort(Comparator.comparingInt(order::indexOf));

context.getListener()
.getLogger()
.println("Duplicate recipient detected: " + email + " in " + locations);

return false;
}

return false;
});
}

private MimeMessage createMail(ExtendedEmailPublisherContext context, InternetAddress fromAddress, Session session)
throws MessagingException, UnsupportedEncodingException {
ExtendedEmailPublisherDescriptor descriptor = getDescriptor();
Expand Down Expand Up @@ -1099,6 +1155,8 @@
excludeNotAllowedDomains(context, cc);
excludeNotAllowedDomains(context, bcc);

logDuplicateRecipients(context, to, cc, bcc);

//
msg.setRecipients(Message.RecipientType.TO, to.toArray(new InternetAddress[0]));
if (!cc.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Launcher;
Expand All @@ -18,6 +19,7 @@
import hudson.model.FreeStyleProject;
import hudson.model.Item;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.User;
import hudson.plugins.emailext.plugins.EmailTrigger;
Expand All @@ -41,18 +43,23 @@
import hudson.security.SecurityRealm;
import hudson.tasks.Builder;
import hudson.tasks.Mailer;
import hudson.util.StreamTaskListener;
import jakarta.mail.Address;
import jakarta.mail.BodyPart;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.Session;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeBodyPart;
import jakarta.mail.internet.MimeMessage;
import jakarta.mail.internet.MimeMultipart;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import jenkins.model.Jenkins;
import jenkins.model.JenkinsLocationConfiguration;
Expand Down Expand Up @@ -2166,4 +2173,99 @@ void testStillFailingTriggerShouldBypassThrottling() throws Exception {
Mailbox.get("[email protected]").size(),
"We should only have one email since the first failure doesn't count as 'still failing'.");
}

@Test
public void testDuplicateLoggedForCcAndBcc() throws Exception {

ExtendedEmailPublisher publisher = new ExtendedEmailPublisher();

ByteArrayOutputStream logStream = new ByteArrayOutputStream();
TaskListener listener = new StreamTaskListener(logStream);

Run<?, ?> run = mock(Run.class);

ExtendedEmailPublisherContext context = new ExtendedEmailPublisherContext(publisher, run, null, null, listener);

InternetAddress addr1 = new InternetAddress("[email protected]");
InternetAddress addr2 = new InternetAddress("[email protected]");

Set<InternetAddress> to = new LinkedHashSet<>();

Set<InternetAddress> cc = new LinkedHashSet<>();
cc.add(addr1);

Set<InternetAddress> bcc = new LinkedHashSet<>();
bcc.add(addr2);

publisher.logDuplicateRecipients(context, to, cc, bcc);

String logs = logStream.toString();

assertTrue(logs.contains("Duplicate recipient detected"));
assertTrue(logs.contains("[email protected]"));
assertTrue(logs.contains("CC"));
assertTrue(logs.contains("BCC"));
}

@Test
public void testDuplicateRemovedFromCcWhenPresentInTo() throws Exception {

ExtendedEmailPublisher publisher = new ExtendedEmailPublisher();

ByteArrayOutputStream logStream = new ByteArrayOutputStream();
TaskListener listener = new StreamTaskListener(logStream);

Run<?, ?> run = mock(Run.class);

ExtendedEmailPublisherContext context = new ExtendedEmailPublisherContext(publisher, run, null, null, listener);

InternetAddress addr1 = new InternetAddress("[email protected]");
InternetAddress addr2 = new InternetAddress("[email protected]");

Set<InternetAddress> to = new LinkedHashSet<>();
to.add(addr1);

Set<InternetAddress> cc = new LinkedHashSet<>();
cc.add(addr2);

Set<InternetAddress> bcc = new LinkedHashSet<>();

publisher.logDuplicateRecipients(context, to, cc, bcc);

// CC should be removed
assertTrue(cc.isEmpty());
}

@Test
public void testDuplicateRemovedFromBccWhenPresentInTo() throws Exception {

ExtendedEmailPublisher publisher = new ExtendedEmailPublisher();

ByteArrayOutputStream logStream = new ByteArrayOutputStream();
TaskListener listener = new StreamTaskListener(logStream);

Run<?, ?> run = mock(Run.class);

ExtendedEmailPublisherContext context = new ExtendedEmailPublisherContext(publisher, run, null, null, listener);

InternetAddress addr1 = new InternetAddress("[email protected]");
InternetAddress addr2 = new InternetAddress("[email protected]");

Set<InternetAddress> to = new LinkedHashSet<>();
to.add(addr1);

Set<InternetAddress> cc = new LinkedHashSet<>();

Set<InternetAddress> bcc = new LinkedHashSet<>();
bcc.add(addr2);

publisher.logDuplicateRecipients(context, to, cc, bcc);

// BCC should be removed
assertTrue(bcc.isEmpty());

// No logging expected
String logs = logStream.toString();
assertTrue(!logs.contains("Duplicate recipient detected"));
}
}
Loading