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
commit fad1f974cfe7967cf5c8a53e0bea69207cd4641c Author: Benoit TELLIER <[email protected]> AuthorDate: Thu Nov 26 08:43:39 2026 +0100 [FIX] DefaultExplanationPolicy: a too long default explanation must not turn a fail into a permerror The bound on the expanded explanation throws a PermErrorException, which the default explanation did not catch: a MAIL FROM of a few hundred characters, expanded by %{S}, made the check a permerror instead of a fail. As for exp=, keep the fail without an explanation (RFC 7208 6.2). --- .../policies/local/DefaultExplanationPolicy.java | 16 +++++-- .../apache/james/jspf/terms/ExpModifierTest.java | 52 ++++++++++++++++++++++ 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/resolver/src/main/java/org/apache/james/jspf/policies/local/DefaultExplanationPolicy.java b/resolver/src/main/java/org/apache/james/jspf/policies/local/DefaultExplanationPolicy.java index 73a8336..62b8f12 100644 --- a/resolver/src/main/java/org/apache/james/jspf/policies/local/DefaultExplanationPolicy.java +++ b/resolver/src/main/java/org/apache/james/jspf/policies/local/DefaultExplanationPolicy.java @@ -57,8 +57,9 @@ public final class DefaultExplanationPolicy implements PolicyPostFilter { spfData.setExplanation(explanation); } catch (PermErrorException e) { - // Should never happen ! - LOGGER.debug("Invalid defaulfExplanation: {}", attExplanation); + // an invalid default explanation, or one expanded past MacroExpand.MAX_EXPLANATION_LENGTH once the + // PTR names of %{p} are known: RFC 7208 6.2, the explanation never changes the result + LOGGER.warn("Default explanation not expanded: {}", e.getMessage()); } return null; } @@ -87,8 +88,15 @@ public final class DefaultExplanationPolicy implements PolicyPostFilter { explanation = defExplanation; } spfData.setAttribute(ATTRIBUTE_DEFAULT_EXPLANATION_POLICY_EXPLANATION, explanation); - spfData.pushChecker(explanationCheckr); - return macroExpand.checkExpand(explanation, spfData, MacroExpand.EXPLANATION); + try { + DNSLookupContinuation cont = macroExpand.checkExpand(explanation, spfData, MacroExpand.EXPLANATION); + spfData.pushChecker(explanationCheckr); + return cont; + } catch (PermErrorException e) { + // RFC 7208 6.2: the explanation never changes the result, keep the fail without one + LOGGER.warn("Default explanation not expanded: {}", e.getMessage()); + return null; + } } } diff --git a/resolver/src/test/java/org/apache/james/jspf/terms/ExpModifierTest.java b/resolver/src/test/java/org/apache/james/jspf/terms/ExpModifierTest.java index 359d410..f1a9035 100644 --- a/resolver/src/test/java/org/apache/james/jspf/terms/ExpModifierTest.java +++ b/resolver/src/test/java/org/apache/james/jspf/terms/ExpModifierTest.java @@ -32,6 +32,7 @@ import org.apache.james.jspf.core.DNSRequest; import org.apache.james.jspf.core.DNSService; import org.apache.james.jspf.core.MacroExpand; import org.apache.james.jspf.core.exceptions.SPFErrorConstants; +import org.apache.james.jspf.executor.AsynchronousSPFExecutor; import org.apache.james.jspf.executor.SPFResult; import org.apache.james.jspf.impl.SPF; import org.junit.Test; @@ -93,4 +94,55 @@ public class ExpModifierTest { assertTrue(result.getExplanation(), result.getExplanation().length() <= MacroExpand.MAX_EXPLANATION_LENGTH); assertTrue(result.getExplanation(), result.getExplanation().startsWith("http://www.openspf.org/why.html?sender=")); } + + private static final String LONG_SENDER = "!".repeat(400) + "@example.com"; + + private static void assertFailWithBoundedExplanation(SPFResult result) { + // RFC 7208 6.2: the fail is kept, without an explanation + assertEquals(SPFErrorConstants.FAIL_CONV, result.getResult()); + assertEquals("", result.getExplanation()); + } + + @Test + public void aDefaultExplanationExpandedPastTheLimitShouldKeepTheFail() { + SPF spf = new SPF(dns(Map.of("example.com", "v=spf1 -all"))); + + assertFailWithBoundedExplanation(spf.checkSPF("1.2.3.4", LONG_SENDER, HELO)); + } + + @Test + public void aConfiguredDefaultExplanationExpandedPastTheLimitShouldKeepTheFail() { + SPF spf = new SPF(dns(Map.of("example.com", "v=spf1 -all"))); + spf.setDefaultExplanation("%{S} is not allowed"); + + assertFailWithBoundedExplanation(spf.checkSPF("1.2.3.4", LONG_SENDER, HELO)); + } + + @Test + public void aDefaultExplanationExpandedPastTheLimitShouldKeepTheFailWithTheAsynchronousExecutor() { + DNSService dns = dns(Map.of("example.com", "v=spf1 -all")); + SPF spf = new SPF(dns, new AsynchronousSPFExecutor(dns)); + + assertFailWithBoundedExplanation(spf.checkSPF("1.2.3.4", LONG_SENDER, HELO)); + } + + @Test + public void aDefaultExplanationExpandedPastTheLimitAfterThePTRLookupShouldKeepTheFail() { + // %{p} first needs the PTR lookup: the limit is only reached on the expansion that follows it + SPF spf = new SPF(dns(Map.of("example.com", "v=spf1 -all"))); + spf.setDefaultExplanation("%{p} %{S}"); + + assertFailWithBoundedExplanation(spf.checkSPF("1.2.3.4", LONG_SENDER, HELO)); + } + + @Test + public void aDefaultExplanationShouldBeExpandedAfterThePTRLookup() { + SPF spf = new SPF(dns(Map.of("example.com", "v=spf1 -all"))); + spf.setDefaultExplanation("%{p} %{s}"); + + SPFResult result = spf.checkSPF("1.2.3.4", "[email protected]", HELO); + + assertEquals(SPFErrorConstants.FAIL_CONV, result.getResult()); + assertEquals("unknown [email protected]", result.getExplanation()); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
