From bd0d8af7b30d574373d8ce508a6566af567d7396 Mon Sep 17 00:00:00 2001 From: Weimin Yu Date: Thu, 5 Oct 2023 11:19:31 -0400 Subject: [PATCH] Make sure unsafe names can be sent in emails (#2169) Surround the dot in unsafe domain names with a square bracket. This is suggested by Gmail abuse-detection and allows outgoing messages to pass Gmail's check. This should also help with recipients' checks. --- .../reporting/spec11/Spec11EmailUtils.java | 12 +++++++++--- .../reporting/spec11/Spec11EmailUtilsTest.java | 18 +++++++++--------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/google/registry/reporting/spec11/Spec11EmailUtils.java b/core/src/main/java/google/registry/reporting/spec11/Spec11EmailUtils.java index a91716c2b..ce476df53 100644 --- a/core/src/main/java/google/registry/reporting/spec11/Spec11EmailUtils.java +++ b/core/src/main/java/google/registry/reporting/spec11/Spec11EmailUtils.java @@ -171,7 +171,7 @@ public class Spec11EmailUtils { gmailClient.sendEmail( EmailMessage.newBuilder() .setSubject(subject) - .setBody(getContent(date, soyTemplateInfo, registrarThreatMatches)) + .setBody(getEmailBody(date, soyTemplateInfo, registrarThreatMatches)) .setContentType(MediaType.HTML_UTF_8) .setFrom(outgoingEmailAddress) .addRecipient(getEmailAddressForRegistrar(registrarThreatMatches.clientId())) @@ -179,7 +179,7 @@ public class Spec11EmailUtils { .build()); } - private String getContent( + private String getEmailBody( LocalDate date, SoyTemplateInfo soyTemplateInfo, RegistrarThreatMatches registrarThreatMatches) { @@ -190,7 +190,7 @@ public class Spec11EmailUtils { .map( threatMatch -> ImmutableMap.of( - "domainName", threatMatch.domainName(), + "domainName", toEmailSafeString(threatMatch.domainName()), "threatType", threatMatch.threatType())) .collect(toImmutableList()); @@ -205,6 +205,12 @@ public class Spec11EmailUtils { return renderer.render(); } + // Mutates a known bad domain to pass spam checks by Email sender and clients, as suggested by + // the Gmail abuse-detection team. + private String toEmailSafeString(String knownUnsafeDomain) { + return knownUnsafeDomain.replace(".", "[.]"); + } + /** Sends an e-mail indicating the state of the spec11 pipeline, with a given subject and body. */ void sendAlertEmail(String subject, String body) { try { diff --git a/core/src/test/java/google/registry/reporting/spec11/Spec11EmailUtilsTest.java b/core/src/test/java/google/registry/reporting/spec11/Spec11EmailUtilsTest.java index 75071911e..dd4047fd2 100644 --- a/core/src/test/java/google/registry/reporting/spec11/Spec11EmailUtilsTest.java +++ b/core/src/test/java/google/registry/reporting/spec11/Spec11EmailUtilsTest.java @@ -165,7 +165,7 @@ class Spec11EmailUtilsTest { "the.registrar@example.com", ImmutableList.of("abuse@test.com", "bcc@test.com"), "Super Cool Registry Monthly Threat Detector [2018-07-15]", - String.format(MONTHLY_EMAIL_FORMAT, "a.comMALWARE"), + String.format(MONTHLY_EMAIL_FORMAT, "a[.]comMALWARE"), Optional.of(MediaType.HTML_UTF_8)); validateMessage( capturedContents.get(1), @@ -175,7 +175,7 @@ class Spec11EmailUtilsTest { "Super Cool Registry Monthly Threat Detector [2018-07-15]", String.format( MONTHLY_EMAIL_FORMAT, - "b.comMALWAREc.comMALWARE"), + "b[.]comMALWAREc[.]comMALWARE"), Optional.of(MediaType.HTML_UTF_8)); validateMessage( capturedContents.get(2), @@ -203,7 +203,7 @@ class Spec11EmailUtilsTest { "the.registrar@example.com", ImmutableList.of("abuse@test.com", "bcc@test.com"), "Super Cool Registry Daily Threat Detector [2018-07-15]", - String.format(DAILY_EMAIL_FORMAT, "a.comMALWARE"), + String.format(DAILY_EMAIL_FORMAT, "a[.]comMALWARE"), Optional.of(MediaType.HTML_UTF_8)); validateMessage( capturedMessages.get(1), @@ -213,7 +213,7 @@ class Spec11EmailUtilsTest { "Super Cool Registry Daily Threat Detector [2018-07-15]", String.format( DAILY_EMAIL_FORMAT, - "b.comMALWAREc.comMALWARE"), + "b[.]comMALWAREc[.]comMALWARE"), Optional.of(MediaType.HTML_UTF_8)); validateMessage( capturedMessages.get(2), @@ -244,7 +244,7 @@ class Spec11EmailUtilsTest { "new.registrar@example.com", ImmutableList.of("abuse@test.com", "bcc@test.com"), "Super Cool Registry Monthly Threat Detector [2018-07-15]", - String.format(MONTHLY_EMAIL_FORMAT, "c.comMALWARE"), + String.format(MONTHLY_EMAIL_FORMAT, "c[.]comMALWARE"), Optional.of(MediaType.HTML_UTF_8)); validateMessage( capturedContents.get(1), @@ -277,7 +277,7 @@ class Spec11EmailUtilsTest { "the.registrar@example.com", ImmutableList.of("abuse@test.com", "bcc@test.com"), "Super Cool Registry Monthly Threat Detector [2018-07-15]", - String.format(MONTHLY_EMAIL_FORMAT, "a.comMALWARE"), + String.format(MONTHLY_EMAIL_FORMAT, "a[.]comMALWARE"), Optional.of(MediaType.HTML_UTF_8)); validateMessage( capturedContents.get(1), @@ -287,7 +287,7 @@ class Spec11EmailUtilsTest { "Super Cool Registry Monthly Threat Detector [2018-07-15]", String.format( MONTHLY_EMAIL_FORMAT, - "b.comMALWAREc.comMALWARE"), + "b[.]comMALWAREc[.]comMALWARE"), Optional.of(MediaType.HTML_UTF_8)); validateMessage( capturedContents.get(2), @@ -332,7 +332,7 @@ class Spec11EmailUtilsTest { "the.registrar@example.com", ImmutableList.of("abuse@test.com", "bcc@test.com"), "Super Cool Registry Monthly Threat Detector [2018-07-15]", - String.format(MONTHLY_EMAIL_FORMAT, "a.comMALWARE"), + String.format(MONTHLY_EMAIL_FORMAT, "a[.]comMALWARE"), Optional.of(MediaType.HTML_UTF_8)); validateMessage( capturedMessages.get(1), @@ -342,7 +342,7 @@ class Spec11EmailUtilsTest { "Super Cool Registry Monthly Threat Detector [2018-07-15]", String.format( MONTHLY_EMAIL_FORMAT, - "b.comMALWAREc.comMALWARE"), + "b[.]comMALWAREc[.]comMALWARE"), Optional.of(MediaType.HTML_UTF_8)); validateMessage( capturedMessages.get(2),