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

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

commit 18cbb56fddc7e243eba1f120950b72589dece7ae
Author: Benoit TELLIER <[email protected]>
AuthorDate: Wed Nov 11 23:16:16 2026 +0100

    [FIX] exp modifier: a spent time budget drops the explanation
    
    The time budget of a check also applied to the lookups of exp=: when it
    ran out after -all matched, before the TXT lookup of the explanation or
    its %{p}, the fail became a temperror. RFC 7208 6.2 says to proceed as
    if no exp modifier was given on a DNS error there, and exp= is already
    kept out of the lookup limit. A forged sender on a domain with exp=
    whose check ended just past the budget got a 4xx instead of the
    rejection.
    
    ExpModifier pushes, below its lookups, a catcher that drops the
    explanation on a TempErrorException and lets the check go on with its
    result. The lookups of exp= stay bounded by the budget.
    
    AsynchronousSPFExecutor completed the check after any handled error:
    like the other executors, it now goes on with the checkers below the
    catcher. The include and redirect catchers always rethrow, so nothing
    else changes.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../jspf/executor/AsynchronousSPFExecutor.java     |  5 +-
 .../org/apache/james/jspf/terms/ExpModifier.java   | 45 ++++++++++++++++
 .../org/apache/james/jspf/SPFExecutorBaseTest.java | 63 ++++++++++++++++++++++
 3 files changed, 112 insertions(+), 1 deletion(-)

diff --git 
a/resolver/src/main/java/org/apache/james/jspf/executor/AsynchronousSPFExecutor.java
 
b/resolver/src/main/java/org/apache/james/jspf/executor/AsynchronousSPFExecutor.java
index 91a2a8f..59286fd 100644
--- 
a/resolver/src/main/java/org/apache/james/jspf/executor/AsynchronousSPFExecutor.java
+++ 
b/resolver/src/main/java/org/apache/james/jspf/executor/AsynchronousSPFExecutor.java
@@ -165,9 +165,12 @@ public class AsynchronousSPFExecutor implements 
SPFExecutor {
             LOGGER.error("Unexpected error during the asynchronous SPF check", 
ex);
             session.setCurrentResult(SPFErrorConstants.TEMP_ERROR_CONV);
             
session.setCurrentResultExpanded(SPFErrorConstants.TEMP_ERROR_CONV);
-        } finally {
             result.setSPFResult(session);
+            return;
         }
+        // Go on with the checkers below the catcher, as the other executors: 
one may handle the error and let the
+        // check go on, as the catcher of the exp lookups
+        execute(session, result);
     }
 
     private void handleError(SPFSession session, Throwable e) {
diff --git 
a/resolver/src/main/java/org/apache/james/jspf/terms/ExpModifier.java 
b/resolver/src/main/java/org/apache/james/jspf/terms/ExpModifier.java
index 33e2cff..0dc5b95 100644
--- a/resolver/src/main/java/org/apache/james/jspf/terms/ExpModifier.java
+++ b/resolver/src/main/java/org/apache/james/jspf/terms/ExpModifier.java
@@ -30,6 +30,7 @@ import org.apache.james.jspf.core.MacroExpandEnabled;
 import org.apache.james.jspf.core.SPF1Constants;
 import org.apache.james.jspf.core.SPFChecker;
 import org.apache.james.jspf.core.SPFCheckerDNSResponseListener;
+import org.apache.james.jspf.core.SPFCheckerExceptionCatcher;
 import org.apache.james.jspf.core.SPFSession;
 import org.apache.james.jspf.core.SPFTermsRegexps;
 import org.apache.james.jspf.core.exceptions.NeutralException;
@@ -68,6 +69,47 @@ public class ExpModifier extends GenericModifier implements 
MacroExpandEnabled,
     }
 
 
+    /**
+     * Below the lookups of the explanation: a TempErrorException of one, as 
when the time budget of the check is spent,
+     * drops the explanation and the check goes on with its result. RFC 7208 
6.2: "if there are any DNS processing
+     * errors [...] proceed as if no exp modifier was given"
+     */
+    private static final class ExplanationErrorCatcher implements SPFChecker, 
SPFCheckerExceptionCatcher {
+
+        /**
+         * @see 
org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession)
+         */
+        public DNSLookupContinuation checkSPF(SPFSession spfData) {
+            // the lookups of the explanation are over
+            return null;
+        }
+
+        /**
+         * @see 
org.apache.james.jspf.core.SPFCheckerExceptionCatcher#onException(java.lang.Throwable,
 org.apache.james.jspf.core.SPFSession)
+         */
+        public void onException(Throwable exception, SPFSession session)
+                throws PermErrorException, NoneException, TempErrorException, 
NeutralException {
+            if (exception instanceof TempErrorException) {
+                LOGGER.debug("No explanation: {}", exception.getMessage());
+                session.setExplanation(null);
+            } else if (exception instanceof PermErrorException) {
+                throw (PermErrorException) exception;
+            } else if (exception instanceof NoneException) {
+                throw (NoneException) exception;
+            } else if (exception instanceof NeutralException) {
+                throw (NeutralException) exception;
+            } else if (exception instanceof RuntimeException) {
+                throw (RuntimeException) exception;
+            } else {
+                throw new IllegalStateException(exception.getMessage(), 
exception);
+            }
+        }
+
+        public String toString() {
+            return "ExpModifier.ExplanationErrorCatcher";
+        }
+    }
+
     private final class ExpandedChecker implements SPFChecker {
         
         /**
@@ -103,6 +145,8 @@ public class ExpModifier extends GenericModifier implements 
MacroExpandEnabled,
 
     private ExpandedExplanationChecker expandedExplanationChecker = new 
ExpandedExplanationChecker();
 
+    private static final ExplanationErrorCatcher EXPLANATION_ERROR_CATCHER = 
new ExplanationErrorCatcher();
+
     /**
      * Generate the explanation and set it in SPF1Data so it can be accessed
      * easy later if needed
@@ -131,6 +175,7 @@ public class ExpModifier extends GenericModifier implements 
MacroExpandEnabled,
         if (spfData.getCurrentResult()== null || 
!spfData.getCurrentResult().equals(SPF1Constants.FAIL))
             return null;
 
+        spfData.pushChecker(EXPLANATION_ERROR_CATCHER);
         spfData.pushChecker(expandedChecker);
         // RFC 7208 4.6.4: exp= is "not subject to this limit"
         return macroExpand.checkExpand(host, spfData, MacroExpand.DOMAIN, 
false);
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 2cc5650..3e53137 100644
--- a/resolver/src/test/java/org/apache/james/jspf/SPFExecutorBaseTest.java
+++ b/resolver/src/test/java/org/apache/james/jspf/SPFExecutorBaseTest.java
@@ -50,6 +50,7 @@ import java.util.stream.Collectors;
 import java.util.stream.IntStream;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertTrue;
 
 public abstract class SPFExecutorBaseTest {
@@ -170,6 +171,33 @@ public abstract class SPFExecutorBaseTest {
         return dnsFailing(DNSRequest.SPF, new 
TXTRecord(Name.fromString(hostname + "."), DClass.IN, 30L, "v=spf1 -all"));
     }
 
+    // the lookups of slowHost answer after delayMillis, as from an 
authoritative server delaying its answers
+    private static DNSService slowFor(FakeResolver fakeResolver, String 
slowHost, long delayMillis) {
+        return new DNSServiceXBillImpl(fakeResolver) {
+            @Override
+            public List<String> getRecords(DNSRequest request) throws 
TimeoutException {
+                pauseFor(request);
+                return super.getRecords(request);
+            }
+
+            @Override
+            public CompletionStage<List<String>> getRecordsAsync(DNSRequest 
request) {
+                return CompletableFuture.runAsync(() -> 
pauseFor(request)).thenCompose(v -> super.getRecordsAsync(request));
+            }
+
+            private void pauseFor(DNSRequest request) {
+                if (!request.getHostname().equals(slowHost)) {
+                    return;
+                }
+                try {
+                    Thread.sleep(delayMillis);
+                } catch (InterruptedException e) {
+                    Thread.currentThread().interrupt();
+                }
+            }
+        };
+    }
+
     protected abstract SPF createSPF();
 
     protected abstract SPF createCustomSPF(DNSService dnsService);
@@ -1773,4 +1801,39 @@ public abstract class SPFExecutorBaseTest {
         // RFC 7208 4.6.4: once the time limit is exceeded, "the result of 
authorization SHOULD be "temperror""
         assertEquals(SPFErrorConstants.TEMP_ERROR_CONV, result.getResult());
     }
+
+    @Test(timeout = 10000)
+    public void shouldKeepTheFailWhenTheTimeBudgetIsSpentBeforeTheExpLookup() 
throws IOException {
+        String hostname = "expTimeBudget.spfexecutor.fake";
+        String slowHost = "slow." + hostname;
+        FakeResolver fakeResolver = new FakeResolver();
+        fakeResolver.addRecord(new TXTRecord(Name.fromString(hostname + "."), 
DClass.IN, 30L,
+            "v=spf1 a:" + slowHost + " -all exp=e." + hostname));
+        fakeResolver.addRecord(new ARecord(Name.fromString(slowHost + "."), 
DClass.IN, 30L, InetAddress.getByName("192.0.2.1")));
+        fakeResolver.addRecord(new TXTRecord(Name.fromString("e." + hostname + 
"."), DClass.IN, 30L, "explained"));
+        SPF spf = createCustomSPF(slowFor(fakeResolver, slowHost, 300));
+        spf.setTimeBudget(150);
+
+        SPFResult result = spf.checkSPF("1.2.3.4", "a_user@" + hostname, 
hostname);
+
+        // -all matched within the budget. RFC 7208 6.2: on a DNS error of the 
exp lookup, "proceed as if no exp
+        // modifier was given"
+        assertEquals(SPFErrorConstants.FAIL_CONV, result.getResult());
+        assertNotEquals("explained", result.getExplanation());
+    }
+
+    @Test(timeout = 10000)
+    public void 
shouldKeepTheFailWhenTheTimeBudgetIsSpentBeforeThePMacroLookupOfExp() throws 
IOException {
+        String hostname = "expPTimeBudget.spfexecutor.fake";
+        String slowHost = "slow." + hostname;
+        FakeResolver fakeResolver = pMacroRecord(hostname, "v=spf1 a:" + 
slowHost + " -all exp=%{p}._exp." + hostname);
+        fakeResolver.addRecord(new ARecord(Name.fromString(slowHost + "."), 
DClass.IN, 30L, InetAddress.getByName("192.0.2.1")));
+        SPF spf = createCustomSPF(slowFor(fakeResolver, slowHost, 300));
+        spf.setTimeBudget(150);
+
+        SPFResult result = spf.checkSPF("1.2.3.4", "a_user@" + hostname, 
hostname);
+
+        assertEquals(SPFErrorConstants.FAIL_CONV, result.getResult());
+        assertNotEquals("explained", result.getExplanation());
+    }
 }


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

Reply via email to