You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by GitBox <gi...@apache.org> on 2022/12/16 10:56:49 UTC

[GitHub] [james-project] ouvtam opened a new pull request, #1361: JAMES-3869 SPF mailet: exclude private networks in addition to 127.0.…

ouvtam opened a new pull request, #1361:
URL: https://github.com/apache/james-project/pull/1361

   …0.1 and make it configurable


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org


[GitHub] [james-project] chibenwa commented on pull request #1361: JAMES-3869 SPF mailet: exclude private networks in addition to 127.0.…

Posted by GitBox <gi...@apache.org>.
chibenwa commented on PR #1361:
URL: https://github.com/apache/james-project/pull/1361#issuecomment-1366511205

   >  I hope the vacation refilled your batteries :-)
   
   Yes, it definitly did!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org


[GitHub] [james-project] chibenwa commented on a diff in pull request #1361: JAMES-3869 SPF mailet: exclude private networks in addition to 127.0.…

Posted by GitBox <gi...@apache.org>.
chibenwa commented on code in PR #1361:
URL: https://github.com/apache/james-project/pull/1361#discussion_r1058016267


##########
server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/SPFTest.java:
##########
@@ -0,0 +1,238 @@
+/****************************************************************
+ * 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.mailets;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import static org.apache.james.transport.mailets.SPF.RESULT_ATTRIBUTE;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import javax.mail.MessagingException;
+
+import org.apache.james.core.MailAddress;
+import org.apache.james.dnsservice.api.DNSService;
+import org.apache.james.jspf.core.DNSRequest;
+import org.apache.james.jspf.core.exceptions.TimeoutException;
+import org.apache.mailet.AttributeUtils;
+import org.apache.mailet.Mailet;
+import org.apache.mailet.base.MailAddressFixture;
+import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMailContext;
+import org.apache.mailet.base.test.FakeMailetConfig;
+import org.junit.jupiter.api.Test;
+
+public class SPFTest {
+    private static final String MAILET_NAME = "spf-mail";
+
+    @Test
+    public void serviceShouldSkipSPFCheck() throws MessagingException {
+        FakeMail mail = fakeMail().build();
+        Mailet mailet = testMailet(false, false, "10.0.0.0/8");
+
+        mailet.service(mail);
+        assertThat(mail.getAttribute(RESULT_ATTRIBUTE).isEmpty()).isTrue();
+    }
+    @Test
+    public void serviceShouldPerformSPFCheckWithResultNone() throws MessagingException {
+        FakeMail mail = fakeMail().build();
+        Mailet mailet = testMailet();
+
+        mailet.service(mail);
+        assertThat(AttributeUtils.getValueAndCastFromMail(mail, RESULT_ATTRIBUTE, String.class)
+            .orElse(null)).isEqualTo("none");
+    }
+
+    @Test
+    public void serviceShouldPerformSPFCheckWithResultPass() throws MessagingException {
+        FakeMail mail = fakeMail().sender("hello@spf1.james.apache.org").build();
+        Mailet mailet = testMailet();
+
+        mailet.service(mail);
+        assertThat(AttributeUtils.getValueAndCastFromMail(mail, RESULT_ATTRIBUTE, String.class)
+            .orElse(null)).isEqualTo("pass");
+    }
+
+    @Test
+    public void serviceShouldPerformSPFCheckWithResultFail() throws MessagingException {
+        FakeMail mail = fakeMail().sender("hello@spf2.james.apache.org").build();
+        Mailet mailet = testMailet();
+
+        mailet.service(mail);
+        assertThat(AttributeUtils.getValueAndCastFromMail(mail, RESULT_ATTRIBUTE, String.class)
+            .orElse(null)).isEqualTo("fail");
+    }
+
+    @Test
+    public void serviceShouldPerformSPFCheckWithResultSoftFail() throws MessagingException {
+        FakeMail mail = fakeMail().sender("hello@spf3.james.apache.org").build();
+        Mailet mailet = testMailet();
+
+        mailet.service(mail);
+        assertThat(AttributeUtils.getValueAndCastFromMail(mail, RESULT_ATTRIBUTE, String.class)
+            .orElse(null)).isEqualTo("softfail");
+    }
+
+    @Test
+    public void serviceShouldPerformSPFCheckWithResultPermError() throws MessagingException {
+        FakeMail mail = fakeMail().sender("hello@spf4.james.apache.org").build();
+        Mailet mailet = testMailet();
+
+        mailet.service(mail);
+        assertThat(AttributeUtils.getValueAndCastFromMail(mail, RESULT_ATTRIBUTE, String.class)
+            .orElse(null)).isEqualTo("permerror");
+    }
+
+    @Test
+    public void serviceShouldPerformSPFCheckWithResultTempError() throws MessagingException {
+        FakeMail mail = fakeMail().sender("hello@spf5.james.apache.org").build();
+        Mailet mailet = testMailet();
+
+        mailet.service(mail);
+        assertThat(AttributeUtils.getValueAndCastFromMail(mail, RESULT_ATTRIBUTE, String.class)
+            .orElse(null)).isEqualTo("temperror");
+    }
+
+    private Mailet testMailet() throws MessagingException {
+        return testMailet(false, false, "127.0.0.0/8");
+    }
+
+    private Mailet testMailet(boolean debug, boolean addHeader, String ignoreNetworks)
+        throws MessagingException {
+        FakeMailetConfig mailetConfig = FakeMailetConfig.builder()
+            .mailetName(MAILET_NAME)
+            .mailetContext(FakeMailContext.defaultContext())
+            .setProperty("debug", String.valueOf(debug))
+            .setProperty("addHeader", String.valueOf(addHeader))
+            .setProperty("ignoreNetworks", ignoreNetworks)
+            .build();
+
+        SPF mailet = new SPF(mockedDnsService());
+        mailet.setSPFDnsService(mockedSPFDnsService());
+        mailet.init(mailetConfig);
+        return mailet;
+    }
+
+    private FakeMail.Builder fakeMail() {
+        return FakeMail.builder()
+            .name(MAILET_NAME)
+            .sender(MailAddress.nullSender())
+            .recipient(MailAddressFixture.ANY_AT_JAMES)
+            .remoteHost("some.host.local")
+            .remoteAddr("10.11.12.13");
+    }
+
+    private DNSService mockedDnsService() {
+        return new DNSService() {
+
+            @Override
+            public Collection<String> findMXRecords(String hostname) {
+                throw new UnsupportedOperationException("not supported");
+            }
+
+            @Override
+            public Collection<String> findTXTRecords(String hostname) {
+                throw new UnsupportedOperationException("not supported");
+            }
+
+            @Override
+            public Collection<InetAddress> getAllByName(String host) {
+                throw new UnsupportedOperationException("not supported");
+            }
+
+            @Override
+            public InetAddress getByName(String host) throws UnknownHostException {
+                return InetAddress.getByName(host);
+            }
+
+            @Override
+            public InetAddress getLocalHost() {
+                throw new UnsupportedOperationException("not supported");
+            }
+
+            @Override
+            public String getHostName(InetAddress addr) {
+                throw new UnsupportedOperationException("not supported");
+            }
+        };
+    }

Review Comment:
   Using mockito maybe?
   
   ```
   DNSServer mockDNSService = mock(DNSService.class);
   when(mockDNSService.getByName(any())).thenAnswer(...);
   return mockDNSService;
   ```
   
   This significantly reduces the boiler plate thus here should be favored.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org


[GitHub] [james-project] ouvtam commented on pull request #1361: JAMES-3869 SPF mailet: exclude private networks in addition to 127.0.…

Posted by GitBox <gi...@apache.org>.
ouvtam commented on PR #1361:
URL: https://github.com/apache/james-project/pull/1361#issuecomment-1366507255

   > Looks perfect to me!
   > 
   > Thanks for the Xmas gift ;-)
   > 
   > Regards,
   > 
   > Benoit
   
   Superb, thanks for the fast feedback!
   
   I was assuming you were on vacation when there was no feedback. I hope the vacation refilled your batteries :-)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org


[GitHub] [james-project] chibenwa merged pull request #1361: JAMES-3869 SPF mailet: exclude private networks in addition to 127.0.…

Posted by GitBox <gi...@apache.org>.
chibenwa merged PR #1361:
URL: https://github.com/apache/james-project/pull/1361


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org


[GitHub] [james-project] chibenwa commented on a diff in pull request #1361: JAMES-3869 SPF mailet: exclude private networks in addition to 127.0.…

Posted by GitBox <gi...@apache.org>.
chibenwa commented on code in PR #1361:
URL: https://github.com/apache/james-project/pull/1361#discussion_r1058015327


##########
server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/SPF.java:
##########
@@ -50,32 +58,52 @@
  * &lt;mailet match="All" class="SPF"&gt;
  *   &lt;addHeader&gt;true&lt;/addHeader&gt;
  *   &lt;debug&gt;false&lt;/debug&gt;
+ *   &lt;ignoreNetworks&gt;127.0.0.0/8&lt;/ignoreNetworks&gt;
  * &lt;/mailet&gt;
  * </pre>
  */
 @Experimental
 public class SPF extends GenericMailet {
     private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(SPF.class);
 
+    private boolean debug = false;
     private boolean addHeader = false;
     private org.apache.james.jspf.impl.SPF spf;
     private static final AttributeName EXPLANATION_ATTRIBUTE = AttributeName.of("org.apache.james.transport.mailets.spf.explanation");
     private static final AttributeName RESULT_ATTRIBUTE = AttributeName.of("org.apache.james.transport.mailets.spf.result");
+    private static final String privateNetworks = "127.0.0.0/8, 10.0.0.0/8, 172.16.0.0/16, 172.17.0.0/16, 172.18.0.0/16, 172.19.0.0/16, 172.20.0.0/16, 172.21.0.0/16, 172.22.0.0/16, 172.23.0.0/16, 172.24.0.0/16, 172.25.0.0/16, 172.26.0.0/16, 172.27.0.0/16, 172.28.0.0/16, 172.29.0.0/16, 172.30.0.0/16, 172.31.0.0/16";
+
+    private final DNSService dnsService;
+    private NetMatcher netMatcher;
+
+    @Inject
+    public SPF(DNSService dnsService) {
+        this.dnsService = dnsService;
+    }
 
     @Override
     public void init() {
+        debug = Boolean.parseBoolean(getInitParameter("debug", "false"));
         addHeader = Boolean.parseBoolean(getInitParameter("addHeader", "false"));
-        SPFLoggerAdapter logger = new SPFLoggerAdapter(Boolean.parseBoolean(getInitParameter("debug", "false")));
+        spf = new DefaultSPF(new SPFLoggerAdapter(debug));
+
+        Collection<String> ignoredNetworks = Splitter.on(',')
+            .trimResults()
+            .omitEmptyStrings()
+            .splitToList(getInitParameter("ignoreNetworks", privateNetworks));

Review Comment:
   ignoredNetworks ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org