This is an automated email from the ASF dual-hosted git repository.

chibenwa pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-jspf.git


The following commit(s) were added to refs/heads/master by this push:
     new 907c737  [FIX] PTRMechanism: skip a PTR name that is not a valid 
domain name
907c737 is described below

commit 907c737cbc678ec3ac09cb24d7359c93c9dd8a0d
Author: Benoit TELLIER <[email protected]>
AuthorDate: Wed Dec 2 10:03:49 2026 +0100

    [FIX] PTRMechanism: skip a PTR name that is not a valid domain name
    
    A PTR name such as the root "." gave an empty name, for which building
    the address lookup threw a NoneException out of the mechanism: the check
    ended as none (permerror under include: or redirect=) instead of going on
    with the next terms. The name is now skipped, as RFC 7208 5.5 says and as
    the mx mechanism and the %{p} macro already do.
---
 .../org/apache/james/jspf/terms/PTRMechanism.java  | 30 +++++++-------
 .../org/apache/james/jspf/SPFExecutorBaseTest.java | 48 ++++++++++++++++++++++
 2 files changed, 63 insertions(+), 15 deletions(-)

diff --git 
a/resolver/src/main/java/org/apache/james/jspf/terms/PTRMechanism.java 
b/resolver/src/main/java/org/apache/james/jspf/terms/PTRMechanism.java
index 6b5eb5f..48bfe74 100644
--- a/resolver/src/main/java/org/apache/james/jspf/terms/PTRMechanism.java
+++ b/resolver/src/main/java/org/apache/james/jspf/terms/PTRMechanism.java
@@ -198,26 +198,26 @@ public class PTRMechanism extends GenericMechanism 
implements DNSServiceEnabled,
         }
         
 
-        if (domainList.size() > 0) {
-            String currentDomain = (String) domainList.remove(0);
-    
+        // check if the connecting ip is ip6. If so lookup AAAA record, else A 
record
+        int recordType = IPAddr.isIPV6(spfSession.getIpAddress()) ? 
DNSRequest.AAAA : DNSRequest.A;
+        while (!domainList.isEmpty()) {
+            String currentDomain = domainList.remove(0);
+
             DNSRequest dnsRequest;
-            // check if the connecting ip is ip6. If so lookup AAAA record
-            if (IPAddr.isIPV6(spfSession.getIpAddress())) {
-                // Get aaaa record for this
-                dnsRequest = new DNSRequest(currentDomain, DNSRequest.AAAA);
-            } else {
-                // Get a record for this
-                dnsRequest = new DNSRequest(currentDomain, DNSRequest.A);
+            try {
+                dnsRequest = new DNSRequest(currentDomain, recordType);
+            } catch (NoneException e) {
+                // a PTR name that is not a valid domain name, such as the 
root ".", cannot be validated: RFC 7208
+                // 5.5, it is skipped
+                continue;
             }
-            
+
             spfSession.setAttribute(ATTRIBUTE_CURRENT_DOMAIN, currentDomain);
-            
+
             return new DNSLookupContinuation(dnsRequest, PTRMechanism.this);
-        } else {
-            spfSession.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT, 
Boolean.FALSE);
-            return null;
         }
+        spfSession.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT, 
Boolean.FALSE);
+        return null;
 
     }
 
diff --git 
a/resolver/src/test/java/org/apache/james/jspf/SPFExecutorBaseTest.java 
b/resolver/src/test/java/org/apache/james/jspf/SPFExecutorBaseTest.java
index 272b0f7..5c6cf24 100644
--- a/resolver/src/test/java/org/apache/james/jspf/SPFExecutorBaseTest.java
+++ b/resolver/src/test/java/org/apache/james/jspf/SPFExecutorBaseTest.java
@@ -860,6 +860,54 @@ public abstract class SPFExecutorBaseTest {
         assertEquals(SPFErrorConstants.FAIL_CONV, result.getResult());
     }
 
+    @Test(timeout = 10000)
+    public void ptrShouldSkipTheRootName() throws IOException {
+        String hostname = "ptrroot.spfexecutor.fake";
+        FakeResolver fakeResolver = new FakeResolver();
+        fakeResolver.addRecord(new TXTRecord(Name.fromString(hostname + "."), 
DClass.IN, 30L, "v=spf1 ptr -all"));
+        fakeResolver.addRecord(new 
PTRRecord(Name.fromString("4.3.2.1.in-addr.arpa."), DClass.IN, 30L, Name.root));
+        SPF spf = createCustomSPF(new DNSServiceXBillImpl(fakeResolver));
+
+        SPFResult result = spf.checkSPF("1.2.3.4", "a_user@" + hostname, 
hostname);
+
+        // RFC 7208 5.5: a name that cannot be validated is skipped, and the 
record goes on to -all
+        assertEquals(SPFErrorConstants.FAIL_CONV, result.getResult());
+    }
+
+    @Test(timeout = 10000)
+    public void ptrShouldValidateANameAfterTheRootName() throws IOException {
+        String hostname = "ptrrootthenname.spfexecutor.fake";
+        String mail = "mail." + hostname + ".";
+        FakeResolver fakeResolver = new FakeResolver();
+        fakeResolver.addRecord(new TXTRecord(Name.fromString(hostname + "."), 
DClass.IN, 30L, "v=spf1 ptr -all"));
+        fakeResolver.addRecord(new 
PTRRecord(Name.fromString("4.3.2.1.in-addr.arpa."), DClass.IN, 30L, Name.root));
+        fakeResolver.addRecord(new 
PTRRecord(Name.fromString("4.3.2.1.in-addr.arpa."), DClass.IN, 30L,
+            Name.fromString(mail)));
+        fakeResolver.addRecord(new ARecord(Name.fromString(mail), DClass.IN, 
30L, InetAddress.getByName("1.2.3.4")));
+        SPF spf = createCustomSPF(new DNSServiceXBillImpl(fakeResolver));
+
+        SPFResult result = spf.checkSPF("1.2.3.4", "a_user@" + hostname, 
hostname);
+
+        assertEquals(SPFErrorConstants.PASS_CONV, result.getResult());
+    }
+
+    @Test(timeout = 10000)
+    public void ptrShouldSkipTheRootNameInAnIncludedRecord() throws 
IOException {
+        String hostname = "ptrrootinclude.spfexecutor.fake";
+        String target = "target." + hostname;
+        FakeResolver fakeResolver = new FakeResolver();
+        fakeResolver.addRecord(new TXTRecord(Name.fromString(hostname + "."), 
DClass.IN, 30L,
+            "v=spf1 include:" + target + " -all"));
+        fakeResolver.addRecord(new TXTRecord(Name.fromString(target + "."), 
DClass.IN, 30L, "v=spf1 ptr ?all"));
+        fakeResolver.addRecord(new 
PTRRecord(Name.fromString("4.3.2.1.in-addr.arpa."), DClass.IN, 30L, Name.root));
+        SPF spf = createCustomSPF(new DNSServiceXBillImpl(fakeResolver));
+
+        SPFResult result = spf.checkSPF("1.2.3.4", "a_user@" + hostname, 
hostname);
+
+        // the include does not match, and the record goes on to -all
+        assertEquals(SPFErrorConstants.FAIL_CONV, result.getResult());
+    }
+
     @Test(timeout = 10000)
     public void pMacroShouldBeAValidatedNameAfterOneThatDoesNotValidate() 
throws IOException {
         String hostname = "pmacro.spfexecutor.fake";


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to