Skip to content

Commit

Permalink
Merge pull request #4141 from Gepardgame/feat/email-notification-fix
Browse files Browse the repository at this point in the history
Feat: Fix that Emails render all symbols right
  • Loading branch information
nscuro authored Sep 23, 2024
2 parents 237ba91 + 014ec6c commit 8044252
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public enum DefaultNotificationPublishers {
SLACK("Slack", "Publishes notifications to a Slack channel", SlackPublisher.class, "/templates/notification/publisher/slack.peb", MediaType.APPLICATION_JSON, true),
MS_TEAMS("Microsoft Teams", "Publishes notifications to a Microsoft Teams channel", MsTeamsPublisher.class, "/templates/notification/publisher/msteams.peb", MediaType.APPLICATION_JSON, true),
MATTERMOST("Mattermost", "Publishes notifications to a Mattermost channel", MattermostPublisher.class, "/templates/notification/publisher/mattermost.peb", MediaType.APPLICATION_JSON, true),
EMAIL("Email", "Sends notifications to an email address", SendMailPublisher.class, "/templates/notification/publisher/email.peb", MediaType.TEXT_PLAIN, true),
EMAIL("Email", "Sends notifications to an email address", SendMailPublisher.class, "/templates/notification/publisher/email.peb", "text/plain; charset=utf-8", true),
CONSOLE("Console", "Displays notifications on the system console", ConsolePublisher.class, "/templates/notification/publisher/console.peb", MediaType.TEXT_PLAIN, true),
WEBHOOK("Outbound Webhook", "Publishes notifications to a configurable endpoint", WebhookPublisher.class, "/templates/notification/publisher/webhook.peb", MediaType.APPLICATION_JSON, true),
CS_WEBEX("Cisco Webex", "Publishes notifications to a Cisco Webex Teams channel", CsWebexPublisher.class, "/templates/notification/publisher/cswebex.peb", MediaType.APPLICATION_JSON, true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@
import alpine.server.mail.SendMailException;
import io.pebbletemplates.pebble.PebbleEngine;
import io.pebbletemplates.pebble.template.PebbleTemplate;

import org.apache.commons.text.StringEscapeUtils;
import org.dependencytrack.persistence.QueryManager;
import org.dependencytrack.util.DebugDataEncryption;

import jakarta.json.JsonObject;
import jakarta.json.JsonString;

import jakarta.ws.rs.core.MediaType;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -135,13 +139,14 @@ private void sendNotification(final PublishContext ctx, Notification notificatio
LOGGER.error("Failed to decrypt SMTP password (%s)".formatted(ctx), e);
return;
}
String unescapedContent = StringEscapeUtils.unescapeHtml4(content);

try {
final SendMail sendMail = new SendMail()
.from(smtpFrom)
.to(destinations)
.subject(emailSubjectPrefix + " " + notification.getTitle())
.body(content)
.body(mimeType == MediaType.TEXT_HTML ? StringEscapeUtils.escapeHtml4(unescapedContent): unescapedContent)
.bodyMimeType(mimeType)
.host(smtpHostname)
.port(smtpPort)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,20 @@ public void testInformWithProjectAuditChangeNotification() {
.isThrownBy(() -> publisherInstance.inform(PublishContext.from(notification), notification, createConfig()));
}

@Test
public void testInformWithEscapedData() {
final var notification = new Notification()
.scope(NotificationScope.SYSTEM)
.group(NotificationGroup.ANALYZER)
.title(NotificationConstants.Title.NOTIFICATION_TEST)
.content("! \" § $ % & / ( ) = ? \\ ' * Ö Ü Ä ®️")
.level(NotificationLevel.ERROR)
.timestamp(LocalDateTime.ofEpochSecond(66666, 666, ZoneOffset.UTC));

assertThatNoException()
.isThrownBy(() -> publisherInstance.inform(PublishContext.from(notification), notification, createConfig()));
}

private static Component createComponent(final Project project) {
final var component = new Component();
component.setProject(project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void testEmail() {
Assert.assertEquals("Sends notifications to an email address", DefaultNotificationPublishers.EMAIL.getPublisherDescription());
Assert.assertEquals(SendMailPublisher.class, DefaultNotificationPublishers.EMAIL.getPublisherClass());
Assert.assertEquals("/templates/notification/publisher/email.peb", DefaultNotificationPublishers.EMAIL.getPublisherTemplateFile());
Assert.assertEquals(MediaType.TEXT_PLAIN, DefaultNotificationPublishers.EMAIL.getTemplateMimeType());
Assert.assertEquals("text/plain; charset=utf-8", DefaultNotificationPublishers.EMAIL.getTemplateMimeType());
Assert.assertTrue(DefaultNotificationPublishers.EMAIL.isDefaultPublisher());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,37 @@ public void testInformWithProjectAuditChangeNotification() {
});
}

@Override
public void testInformWithEscapedData() {
super.testInformWithEscapedData();

assertThat(greenMail.getReceivedMessages()).satisfiesExactly(message -> {
assertThat(message.getSubject()).isEqualTo("[Dependency-Track] Notification Test");
assertThat(message.getContent()).isInstanceOf(MimeMultipart.class);
final MimeMultipart content = (MimeMultipart) message.getContent();
assertThat(content.getCount()).isEqualTo(1);
assertThat(content.getBodyPart(0)).isInstanceOf(MimeBodyPart.class);
assertThat((String) content.getBodyPart(0).getContent()).isEqualToIgnoringNewLines("""
Notification Test
--------------------------------------------------------------------------------
Level: ERROR
Scope: SYSTEM
Group: ANALYZER
--------------------------------------------------------------------------------
! " § $ % & / ( ) = ? \\ ' * Ö Ü Ä ®️
--------------------------------------------------------------------------------
1970-01-01T18:31:06.000000666
""");
});

}

@Override
JsonObjectBuilder extraConfig() {
return super.extraConfig()
Expand Down

0 comments on commit 8044252

Please sign in to comment.