From d8d8fc40100f4a4cc42de06abf325b0d77fce063 Mon Sep 17 00:00:00 2001 From: hungphan227 <45198168+hungphan227@users.noreply.github.com> Date: Tue, 17 Sep 2024 14:35:15 +0700 Subject: [PATCH] JAMES-4054 Update SMIMECheckSignatureIntegrationTest to include IsSMIMESigned matcher (#2409) --- mailet/crypto/pom.xml | 5 ++ .../transport/matcher/IsSMIMESigned.java | 3 +- .../transport/matcher/IsSMIMESignedTest.java | 80 +++++++++++++++++++ .../resources/eml/mail_with_no_signature.eml | 1 + .../src/main/resources/eml/non_smime_mail.eml | 7 ++ .../SMIMECheckSignatureIntegrationTest.java | 14 ++++ ...natureWithKeyStoreFileIntegrationTest.java | 4 +- ...ckSignatureWithPemFileIntegrationTest.java | 4 +- 8 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 mailet/crypto/src/test/java/org/apache/james/transport/matcher/IsSMIMESignedTest.java create mode 100644 server/mailet/integration-testing/src/main/resources/eml/non_smime_mail.eml diff --git a/mailet/crypto/pom.xml b/mailet/crypto/pom.xml index bc7c11e2e5e..62ff7934542 100644 --- a/mailet/crypto/pom.xml +++ b/mailet/crypto/pom.xml @@ -41,6 +41,11 @@ ${james.groupId} apache-mailet-base + + ${james.groupId} + apache-mailet-test + test + ${james.groupId} james-server-core diff --git a/mailet/crypto/src/main/java/org/apache/james/transport/matcher/IsSMIMESigned.java b/mailet/crypto/src/main/java/org/apache/james/transport/matcher/IsSMIMESigned.java index 26a19eaa42e..343d87f9091 100644 --- a/mailet/crypto/src/main/java/org/apache/james/transport/matcher/IsSMIMESigned.java +++ b/mailet/crypto/src/main/java/org/apache/james/transport/matcher/IsSMIMESigned.java @@ -45,8 +45,7 @@ public Collection match(Mail mail) throws MessagingException { if (message == null) { return null; } - - + if (message.isMimeType("multipart/signed") || message.isMimeType("application/pkcs7-signature") || message.isMimeType("application/x-pkcs7-signature") diff --git a/mailet/crypto/src/test/java/org/apache/james/transport/matcher/IsSMIMESignedTest.java b/mailet/crypto/src/test/java/org/apache/james/transport/matcher/IsSMIMESignedTest.java new file mode 100644 index 00000000000..58f068a14eb --- /dev/null +++ b/mailet/crypto/src/test/java/org/apache/james/transport/matcher/IsSMIMESignedTest.java @@ -0,0 +1,80 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.transport.matcher; + +import static org.apache.mailet.base.MailAddressFixture.RECIPIENT1; +import static org.apache.mailet.base.MailAddressFixture.SENDER; +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.james.core.builder.MimeMessageBuilder; +import org.apache.mailet.base.test.FakeMail; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +public class IsSMIMESignedTest { + private IsSMIMESigned isSMIMESigned; + + @BeforeEach + void beforeEach() { + isSMIMESigned = new IsSMIMESigned(); + } + + @ParameterizedTest + @ValueSource(strings = {"multipart/signed", + "application/pkcs7-signature", + "application/x-pkcs7-signature", + "application/pkcs7-mime; smime-type=signed-data; name=\"smime.p7m\"", + "application/x-pkcs7-mime; smime-type=signed-data; name=\"smime.p7m\""}) + void matchShouldReturnNonEmptyListWhenMessageContentTypeIsSMIMERelated(String contentType) throws Exception { + FakeMail mail = FakeMail.builder() + .name("mail") + .mimeMessage(MimeMessageBuilder.mimeMessageBuilder().addHeader("Content-Type", contentType)) + .sender(SENDER) + .recipient(RECIPIENT1) + .build(); + assertThat(isSMIMESigned.match(mail)).isNotEmpty(); + } + + @Test + void matchShouldReturnNullWhenMessageContentTypeIsNotSMIMERelated() throws Exception { + FakeMail mail = FakeMail.builder() + .name("mail") + .mimeMessage(MimeMessageBuilder.mimeMessageBuilder().addHeader("Content-Type", "text/plain")) + .sender(SENDER) + .recipient(RECIPIENT1) + .build(); + assertThat(isSMIMESigned.match(mail)).isNull(); + } + + @Test + void matchShouldReturnNullWhenMailIsNull() throws Exception { + assertThat(isSMIMESigned.match(null)).isNull(); + } + + @Test + void matchShouldReturnNullWhenMessageIsNull() throws Exception { + FakeMail mail = FakeMail.builder() + .name("mail") + .build(); + assertThat(isSMIMESigned.match(mail)).isNull(); + } +} diff --git a/server/mailet/integration-testing/src/main/resources/eml/mail_with_no_signature.eml b/server/mailet/integration-testing/src/main/resources/eml/mail_with_no_signature.eml index 7787b0b1f1c..d9eac7ca340 100644 --- a/server/mailet/integration-testing/src/main/resources/eml/mail_with_no_signature.eml +++ b/server/mailet/integration-testing/src/main/resources/eml/mail_with_no_signature.eml @@ -3,5 +3,6 @@ To: user2@james.org Subject: test Message-ID: Date: Fri, 1 Nov 2019 10:21:39 +0700 +Content-Type: application/x-pkcs7-mime; smime-type=signed-data; name="smime.p7m" test SMIME \ No newline at end of file diff --git a/server/mailet/integration-testing/src/main/resources/eml/non_smime_mail.eml b/server/mailet/integration-testing/src/main/resources/eml/non_smime_mail.eml new file mode 100644 index 00000000000..7787b0b1f1c --- /dev/null +++ b/server/mailet/integration-testing/src/main/resources/eml/non_smime_mail.eml @@ -0,0 +1,7 @@ +From: user@james.org +To: user2@james.org +Subject: test +Message-ID: +Date: Fri, 1 Nov 2019 10:21:39 +0700 + +test SMIME \ No newline at end of file diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/crypto/SMIMECheckSignatureIntegrationTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/crypto/SMIMECheckSignatureIntegrationTest.java index 8d02701f069..e759d31374f 100644 --- a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/crypto/SMIMECheckSignatureIntegrationTest.java +++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/crypto/SMIMECheckSignatureIntegrationTest.java @@ -115,4 +115,18 @@ public void checkSMIMESignatureShouldAddNotSignedStatusWhenNoSignature() throws .awaitMessage(awaitAtMostOneMinute); assertThat(testIMAPClient().readFirstMessage()).containsSequence("X-SMIME-Status: Not signed"); } + + @Test + public void checkSMIMESignatureShouldDoNothingWhenItIsNonSMIMEMail() throws Exception { + messageSender().connect(LOCALHOST_IP, jamesServer().getProbe(SmtpGuiceProbe.class).getSmtpAuthRequiredPort()) + .authenticate(FROM, PASSWORD) + .sendMessageWithHeaders(FROM, RECIPIENT, + ClassLoaderUtils.getSystemResourceAsString("eml/non_smime_mail.eml")); + + testIMAPClient().connect(LOCALHOST_IP, jamesServer().getProbe(ImapGuiceProbe.class).getImapPort()) + .login(RECIPIENT, PASSWORD) + .select(TestIMAPClient.INBOX) + .awaitMessage(awaitAtMostOneMinute); + assertThat(testIMAPClient().readFirstMessage()).doesNotContain("X-SMIME-Status"); + } } diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/crypto/SMIMECheckSignatureWithKeyStoreFileIntegrationTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/crypto/SMIMECheckSignatureWithKeyStoreFileIntegrationTest.java index 1e0bdc08069..7f2a392b88c 100644 --- a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/crypto/SMIMECheckSignatureWithKeyStoreFileIntegrationTest.java +++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/crypto/SMIMECheckSignatureWithKeyStoreFileIntegrationTest.java @@ -31,7 +31,7 @@ import org.apache.james.mailets.configuration.MailetContainer; import org.apache.james.mailets.configuration.ProcessorConfiguration; import org.apache.james.transport.mailets.SMIMECheckSignature; -import org.apache.james.transport.matchers.All; +import org.apache.james.transport.matcher.IsSMIMESigned; import org.apache.james.util.date.ZonedDateTimeProvider; import org.apache.james.utils.DataProbeImpl; import org.apache.james.utils.SMTPMessageSender; @@ -58,7 +58,7 @@ public void setup(@TempDir File temporaryFolder) throws Exception { .addMailet(MailetConfiguration.BCC_STRIPPER) .addMailet(MailetConfiguration.builder() .mailet(SMIMECheckSignature.class) - .matcher(All.class) + .matcher(IsSMIMESigned.class) .addProperty("fileType", "keystore") .addProperty("keyStoreFileName", FileSystem.CLASSPATH_PROTOCOL + "trusted_cert_keystore") .addProperty("keyStorePassword", "secret") diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/crypto/SMIMECheckSignatureWithPemFileIntegrationTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/crypto/SMIMECheckSignatureWithPemFileIntegrationTest.java index 1c33661c599..fc4503afd9b 100644 --- a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/crypto/SMIMECheckSignatureWithPemFileIntegrationTest.java +++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/crypto/SMIMECheckSignatureWithPemFileIntegrationTest.java @@ -31,7 +31,7 @@ import org.apache.james.mailets.configuration.MailetContainer; import org.apache.james.mailets.configuration.ProcessorConfiguration; import org.apache.james.transport.mailets.SMIMECheckSignature; -import org.apache.james.transport.matchers.All; +import org.apache.james.transport.matcher.IsSMIMESigned; import org.apache.james.util.date.ZonedDateTimeProvider; import org.apache.james.utils.DataProbeImpl; import org.apache.james.utils.SMTPMessageSender; @@ -58,7 +58,7 @@ public void setup(@TempDir File temporaryFolder) throws Exception { .addMailet(MailetConfiguration.BCC_STRIPPER) .addMailet(MailetConfiguration.builder() .mailet(SMIMECheckSignature.class) - .matcher(All.class) + .matcher(IsSMIMESigned.class) .addProperty("fileType", "pem") .addProperty("pemFileName", FileSystem.CLASSPATH_PROTOCOL + "trusted_certificate.pem") .addProperty("debug", "true"))