Dlp extends GenericMatcher { @Override public Collection<MailAddress> match(Mail mail) { Optional<DLPConfigurationItem.Id> firstMatchingRuleId = findFirstMatchingRule(mail); if (firstMatchingRuleId.isPresent()) { DLPConfigurationItem.Id ruleId = firstMatchingRuleId.get(); setRuleIdAsMailAttribute(mail, ruleId); return mail.getRecipients(); } return ImmutableList.of(); } @VisibleForTesting  Dlp(DlpRulesLoader rulesLoader); @Inject  Dlp(DLPConfigurationStore configurationStore); @Override Collection<MailAddress> match(Mail mail); @Override String getMatcherInfo();  }
@Test void matchShouldReturnEmptyWhenNoRecipient() throws Exception { Dlp dlp = new Dlp(MATCH_ALL_FOR_ALL_DOMAINS); FakeMail mail = FakeMail.builder().name("name").sender(RECIPIENT1).build(); assertThat(dlp.match(mail)).isEmpty(); }
@Test void matchShouldReturnEmptyWhenNoSender() throws Exception { Dlp dlp = new Dlp(MATCH_ALL_FOR_ALL_DOMAINS); FakeMail mail = FakeMail.builder().name("name").recipient(RECIPIENT1).build(); assertThat(dlp.match(mail)).isEmpty(); }
@Test void matchShouldThrowOnNullMail() { Dlp dlp = new Dlp(MATCH_ALL_FOR_ALL_DOMAINS); assertThatThrownBy(() -> dlp.match(null)).isInstanceOf(NullPointerException.class); }
@Test void matchShouldReturnEmptyWhenNoRuleMatch() throws Exception { Dlp dlp = new Dlp(MATCH_NOTHING_FOR_ALL_DOMAINS); FakeMail mail = FakeMail.builder() .name("name") .sender(ANY_AT_JAMES) .recipient(RECIPIENT1) .recipient(RECIPIENT2) .build(); assertThat(dlp.match(mail)).isEmpty(); }
@Test void matchSenderShouldReturnRecipientsWhenEnvelopSenderMatches() throws Exception { Dlp dlp = new Dlp( asRulesLoaderFor( JAMES_APACHE_ORG_DOMAIN, DlpDomainRules.builder().senderRule(Id.of("match sender"), Pattern.compile(ANY_AT_JAMES.asString())).build())); FakeMail mail = FakeMail.builder().name("name").sender(ANY_AT_JAMES).recipient(RECIPIENT1).build(); assertThat(dlp.match(mail)).contains(RECIPIENT1); }
@Test void nullSenderShouldBeIgnored() throws Exception { Dlp dlp = new Dlp( asRulesLoaderFor( JAMES_APACHE_ORG_DOMAIN, DlpDomainRules.builder().recipientRule(Id.of("match all recipient"), Pattern.compile(".*")).build())); FakeMail mail = FakeMail.builder().name("name").sender(MailAddress.nullSender()).recipient(RECIPIENT1).build(); assertThat(dlp.match(mail)).isEmpty(); }
@Test void matchSenderShouldReturnRecipientsWhenFromHeaderMatches() throws Exception { Dlp dlp = new Dlp( asRulesLoaderFor( JAMES_APACHE_ORG_DOMAIN, DlpDomainRules.builder().senderRule(Id.of("match sender"), Pattern.compile(ANY_AT_JAMES.asString())).build())); FakeMail mail = FakeMail .builder() .name("name") .sender(OTHER_AT_JAMES) .recipient(RECIPIENT1) .mimeMessage(MimeMessageBuilder .mimeMessageBuilder() .addFrom(ANY_AT_JAMES.toInternetAddress())) .build(); assertThat(dlp.match(mail)).contains(RECIPIENT1); }
@Test void matchShouldReturnRecipientsWhenEnvelopRecipientsMatches() throws Exception { Dlp dlp = new Dlp( asRulesLoaderFor( JAMES_APACHE_ORG_DOMAIN, DlpDomainRules.builder().recipientRule(Id.of("match recipient"), Pattern.compile(RECIPIENT1.asString())).build())); FakeMail mail = FakeMail.builder() .name("name") .sender(ANY_AT_JAMES) .recipient(RECIPIENT1) .recipient(RECIPIENT2) .build(); assertThat(dlp.match(mail)).contains(RECIPIENT1, RECIPIENT2); }
@Test void matchShouldReturnRecipientsWhenToHeaderMatches() throws Exception { Dlp dlp = new Dlp( asRulesLoaderFor( JAMES_APACHE_ORG_DOMAIN, DlpDomainRules.builder().recipientRule(Id.of("match recipient"), Pattern.compile(RECIPIENT2.asString())).build())); FakeMail mail = FakeMail .builder() .name("name") .sender(OTHER_AT_JAMES) .recipient(RECIPIENT1) .mimeMessage(MimeMessageBuilder .mimeMessageBuilder() .addToRecipient(RECIPIENT2.toInternetAddress())) .build(); assertThat(dlp.match(mail)).contains(RECIPIENT1); }
@Test void matchShouldReturnRecipientsWhenCcHeaderMatches() throws Exception { Dlp dlp = new Dlp( asRulesLoaderFor( JAMES_APACHE_ORG_DOMAIN, DlpDomainRules.builder().recipientRule(Id.of("match recipient"), Pattern.compile(RECIPIENT2.asString())).build())); FakeMail mail = FakeMail .builder() .name("name") .sender(OTHER_AT_JAMES) .recipient(RECIPIENT1) .mimeMessage(MimeMessageBuilder .mimeMessageBuilder() .addCcRecipient(RECIPIENT2.toInternetAddress())) .build(); assertThat(dlp.match(mail)).contains(RECIPIENT1); }
@Test void matchShouldReturnRecipientsWhenBccHeaderMatches() throws Exception { Dlp dlp = new Dlp( asRulesLoaderFor( JAMES_APACHE_ORG_DOMAIN, DlpDomainRules.builder().recipientRule(Id.of("match recipient"), Pattern.compile(RECIPIENT2.asString())).build())); FakeMail mail = FakeMail .builder() .name("name") .sender(OTHER_AT_JAMES) .recipient(RECIPIENT1) .mimeMessage(MimeMessageBuilder .mimeMessageBuilder() .addBccRecipient(RECIPIENT2.toInternetAddress())) .build(); assertThat(dlp.match(mail)).contains(RECIPIENT1); }
@Test void matchShouldReturnRecipientsWhenSubjectHeaderMatches() throws Exception { Dlp dlp = new Dlp( asRulesLoaderFor( JAMES_APACHE_ORG_DOMAIN, DlpDomainRules.builder().contentRule(Id.of("match subject"), Pattern.compile("pony")).build())); FakeMail mail = FakeMail .builder() .name("name") .sender(OTHER_AT_JAMES) .recipient(RECIPIENT1) .mimeMessage(MimeMessageBuilder .mimeMessageBuilder() .setSubject("I just bought a pony")) .build(); assertThat(dlp.match(mail)).contains(RECIPIENT1); }
@Test void matchShouldReturnRecipientsWhenMessageBodyMatches() throws Exception { Dlp dlp = new Dlp( asRulesLoaderFor( JAMES_APACHE_ORG_DOMAIN, DlpDomainRules.builder().contentRule(Id.of("match content"), Pattern.compile("horse")).build())); FakeMail mail = FakeMail .builder() .name("name") .sender(OTHER_AT_JAMES) .recipient(RECIPIENT1) .mimeMessage(MimeMessageBuilder .mimeMessageBuilder() .setSubject("I just bought a pony") .setText("It's actually a horse, not a pony")) .build(); assertThat(dlp.match(mail)).contains(RECIPIENT1); }
@Test void matchShouldReturnRecipientsWhenMessageBodyMatchesWithNoSubject() throws Exception { Dlp dlp = new Dlp( asRulesLoaderFor( JAMES_APACHE_ORG_DOMAIN, DlpDomainRules.builder().contentRule(Id.of("match content"), Pattern.compile("horse")).build())); FakeMail mail = FakeMail .builder() .name("name") .sender(OTHER_AT_JAMES) .recipient(RECIPIENT1) .mimeMessage(MimeMessageBuilder .mimeMessageBuilder() .setText("It's actually a horse, not a pony")) .build(); assertThat(dlp.match(mail)).contains(RECIPIENT1); }
@Test void matchShouldReturnRecipientsWhenMessageMultipartBodyMatches() throws Exception { Dlp dlp = new Dlp( asRulesLoaderFor( JAMES_APACHE_ORG_DOMAIN, DlpDomainRules.builder().contentRule(Id.of("match content"), Pattern.compile("horse")).build())); FakeMail mail = FakeMail .builder() .name("name") .sender(OTHER_AT_JAMES) .recipient(RECIPIENT1) .mimeMessage(MimeMessageBuilder .mimeMessageBuilder() .setSubject("I just bought a pony") .setMultipartWithBodyParts( MimeMessageBuilder.bodyPartBuilder() .data("It's actually a donkey, not a pony"), MimeMessageBuilder.bodyPartBuilder() .data("What??? No it's a horse!!!"))) .build(); assertThat(dlp.match(mail)).contains(RECIPIENT1); }
@Test void matchShouldReturnRecipientsWhenEmbeddedMessageContentMatches() throws Exception { Dlp dlp = new Dlp( asRulesLoaderFor( JAMES_APACHE_ORG_DOMAIN, DlpDomainRules.builder().contentRule(Id.of("match content"), Pattern.compile("horse")).build())); FakeMail mail = FakeMail .builder() .name("name") .sender(OTHER_AT_JAMES) .recipient(RECIPIENT1) .mimeMessage(MimeMessageBuilder .mimeMessageBuilder() .setSubject("I just bought a pony") .setContent( MimeMessageBuilder.multipartBuilder() .addBody( MimeMessageBuilder.bodyPartBuilder() .data("It's actually a donkey, not a pony")) .addBody( MimeMessageBuilder.mimeMessageBuilder() .setSender(RECIPIENT2.asString()) .setSubject("Embedded message with truth") .setText("What??? No it's a horse!!!")))) .build(); assertThat(dlp.match(mail)).contains(RECIPIENT1); }
@Test void matchShouldReturnEmptyWhenEmbeddedSenderMatchesInSubMessage() throws Exception { Dlp dlp = new Dlp( asRulesLoaderFor( JAMES_APACHE_ORG_DOMAIN, DlpDomainRules.builder() .senderRule(Id.of("match content"), Pattern.compile(RECIPIENT2.asString())) .build())); FakeMail mail = FakeMail .builder() .name("name") .sender(OTHER_AT_JAMES) .recipient(RECIPIENT1) .mimeMessage(MimeMessageBuilder .mimeMessageBuilder() .setSubject("I just bought a pony") .setSender(RECIPIENT1.asString()) .setContent( MimeMessageBuilder.multipartBuilder() .addBody( MimeMessageBuilder.bodyPartBuilder() .data("It's actually a donkey, not a pony")) .addBody( MimeMessageBuilder.mimeMessageBuilder() .setSender(RECIPIENT2.asString()) .setSubject("Embedded message with truth") .setText("What??? No it's a horse!!!")))) .build(); assertThat(dlp.match(mail)).isEmpty(); }
@Test void matchShouldReturnEmptyWhenEmbeddedRecipientMatchesInSubMessage() throws Exception { Dlp dlp = new Dlp( asRulesLoaderFor( JAMES_APACHE_ORG_DOMAIN, DlpDomainRules.builder() .recipientRule(Id.of("match content"), Pattern.compile(RECIPIENT2.asString())) .build())); FakeMail mail = FakeMail .builder() .name("name") .sender(OTHER_AT_JAMES) .recipient(RECIPIENT1) .mimeMessage(MimeMessageBuilder .mimeMessageBuilder() .setSubject("I just bought a pony") .setSender(RECIPIENT1.asString()) .setContent( MimeMessageBuilder.multipartBuilder() .addBody( MimeMessageBuilder.bodyPartBuilder() .data("It's actually a donkey, not a pony")) .addBody( MimeMessageBuilder.mimeMessageBuilder() .addToRecipient(RECIPIENT1.asString()) .setSubject("Embedded message with truth") .setText("What??? No it's a horse!!!")))) .build(); assertThat(dlp.match(mail)).isEmpty(); }
@Test void matchShouldReturnRecipientsWhenEncodedSubjectMatchesContentRule() throws Exception { Dlp dlp = new Dlp( asRulesLoaderFor( JAMES_APACHE_ORG_DOMAIN, DlpDomainRules.builder() .contentRule(Id.of("match content"), Pattern.compile("poné")) .build())); FakeMail mail = FakeMail .builder() .name("name") .sender(OTHER_AT_JAMES) .recipient(RECIPIENT1) .mimeMessage(MimeMessageBuilder .mimeMessageBuilder() .setSubject("=?ISO-8859-1?Q?I_just_bought_a_pon=E9?=") .setSender(RECIPIENT1.asString()) .setText("Meaningless text")) .build(); assertThat(dlp.match(mail)).containsOnly(RECIPIENT1); }
@Test void matchShouldReturnRecipientsWhenEncodedTextMatchesContentRule() throws Exception { Dlp dlp = new Dlp( asRulesLoaderFor( JAMES_APACHE_ORG_DOMAIN, DlpDomainRules.builder() .contentRule(Id.of("match content"), Pattern.compile("poné")) .build())); FakeMail mail = FakeMail .builder() .name("name") .sender(OTHER_AT_JAMES) .recipient(RECIPIENT1) .mimeMessage(MimeMessageBuilder .mimeMessageBuilder() .setSubject("you know what ?") .setSender(RECIPIENT1.asString()) .setText("I bought a poné", "text/plain; charset=" + StandardCharsets.ISO_8859_1.name())) .build(); assertThat(dlp.match(mail)).containsOnly(RECIPIENT1); }
@Test void matchShouldReturnRecipientsWhenRulesMatchesAMailboxRecipient() throws Exception { Dlp dlp = new Dlp( asRulesLoaderFor( JAMES_APACHE_ORG_DOMAIN, DlpDomainRules.builder() .recipientRule(Id.of("id1"), Pattern.compile(RECIPIENT1.asString())) .build())); MimeMessageBuilder meaninglessText = MimeMessageBuilder .mimeMessageBuilder() .addToRecipient("Name <" + RECIPIENT1.asString() + " >") .setSubject("=?ISO-8859-1?Q?I_just_bought_a_pon=E9?=") .setText("Meaningless text"); FakeMail mail = FakeMail .builder() .name("name") .sender(OTHER_AT_JAMES) .recipient(RECIPIENT2) .mimeMessage(meaninglessText) .build(); assertThat(dlp.match(mail)).containsOnly(RECIPIENT2); }
@Test void matchShouldReturnRecipientsWhenRulesMatchesAQuotedPrintableRecipient() throws Exception { Dlp dlp = new Dlp( asRulesLoaderFor( JAMES_APACHE_ORG_DOMAIN, DlpDomainRules.builder() .recipientRule(Id.of("id1"), Pattern.compile("Benoît")) .build())); MimeMessageBuilder meaninglessText = MimeMessageBuilder .mimeMessageBuilder() .addToRecipient("=?ISO-8859-1?Q?Beno=EEt_TELLIER?=") .setSubject("=?ISO-8859-1?Q?I_just_bought_a_pon=E9?=") .setText("Meaningless text"); FakeMail mail = FakeMail .builder() .name("name") .sender(OTHER_AT_JAMES) .recipient(RECIPIENT2) .mimeMessage(meaninglessText) .build(); assertThat(dlp.match(mail)).containsOnly(RECIPIENT2); }
@Test void matchShouldReturnRecipientsWhenRulesMatchesAQuotedPrintableSender() throws Exception { Dlp dlp = new Dlp( asRulesLoaderFor( JAMES_APACHE_ORG_DOMAIN, DlpDomainRules.builder() .senderRule(Id.of("id1"), Pattern.compile("Benoît")) .build())); MimeMessageBuilder meaninglessText = MimeMessageBuilder .mimeMessageBuilder() .addFrom("=?ISO-8859-1?Q?Beno=EEt_TELLIER?=") .setSubject("=?ISO-8859-1?Q?I_just_bought_a_pon=E9?=") .setText("Meaningless text"); FakeMail mail = FakeMail .builder() .name("name") .sender(OTHER_AT_JAMES) .recipient(RECIPIENT2) .mimeMessage(meaninglessText) .build(); assertThat(dlp.match(mail)).containsOnly(RECIPIENT2); }
@Test void matchShouldAttachMatchingRuleNameToMail() throws Exception { String attributeValue = "should match sender"; Dlp dlp = new Dlp( asRulesLoaderFor( JAMES_APACHE_ORG_DOMAIN, DlpDomainRules.builder() .recipientRule(Id.of("should not match recipient"), Pattern.compile(RECIPIENT3.asString())) .senderRule(Id.of(attributeValue), Pattern.compile(JAMES_APACHE_ORG)) .build())); FakeMail mail = FakeMail.builder() .name("name") .sender(ANY_AT_JAMES) .recipient(RECIPIENT1) .recipient(RECIPIENT2) .build(); dlp.match(mail); AttributeName name = AttributeName.of("DlpMatchedRule"); assertThat(mail.getAttribute(name)).contains(new Attribute(name, AttributeValue.of(attributeValue))); }