Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-64629] Fix uncaught exception for unauthorized users #104

Merged
merged 2 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions src/main/java/hudson/tasks/MailSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import jenkins.plugins.mailer.tasks.MimeMessageBuilder;
import jenkins.plugins.mailer.tasks.i18n.Messages;
import org.acegisecurity.Authentication;
import org.acegisecurity.AuthenticationException;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider;

Expand Down Expand Up @@ -456,6 +457,8 @@ String getCulpritsOfEmailList(AbstractProject upstreamProject, AbstractBuild<?,
static /* not final */ boolean SEND_TO_USERS_WITHOUT_READ = Boolean.getBoolean(MailSender.class.getName() + ".SEND_TO_USERS_WITHOUT_READ");
/** If set, send to unknown users. */
static /* not final */ boolean SEND_TO_UNKNOWN_USERS = Boolean.getBoolean(MailSender.class.getName() + ".SEND_TO_UNKNOWN_USERS");
/** If set, send to unauthorized users. Unauthorized users are users where {@link User#impersonate()} fails with a security-related exception. */
static /* not final */ boolean SEND_TO_UNAUTHORIZED_USERS = Boolean.getBoolean(MailSender.class.getName() + ".SEND_TO_UNAUTHORIZED_USERS");

@Nonnull
String getUserEmailList(TaskListener listener, AbstractBuild<?, ?> build) throws AddressException, UnsupportedEncodingException {
Expand Down Expand Up @@ -485,6 +488,13 @@ String getUserEmailList(TaskListener listener, AbstractBuild<?, ?> build) throws
listener.getLogger().println(Messages.MailSender_unknown_user(adrs));
continue;
}
} catch (AuthenticationException e) {
if (SEND_TO_UNAUTHORIZED_USERS) {
listener.getLogger().println(Messages.MailSender_warning_unauthorized_user(adrs));
} else {
listener.getLogger().println(Messages.MailSender_unauthorized_user(adrs));
jvz marked this conversation as resolved.
Show resolved Hide resolved
continue;
}
}
}
if (userEmails.length() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ MailSender.user_without_read=Not sending mail to user {0} with no permission to
MailSender.warning_user_without_read=Warning: user {0} has no permission to view {1}, but sending mail anyway
MailSender.unknown_user=Not sending mail to unregistered user {0}
MailSender.warning_unknown_user=Warning: {0} is not a recognized user, but sending mail anyway
MailSender.unauthorized_user=Not sending mail to unauthorized user {0}
MailSender.warning_unauthorized_user=Warning: {0} is not an authorized user, but sending mail anyway

Mailer.DisplayName=E-mail Notification
Mailer.Unknown.Host.Name=Unknown host name:
Expand Down