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 1beb6980ce67e3f824e32777d9e7c58c256ac688 Author: Benoit TELLIER <[email protected]> AuthorDate: Tue Nov 10 09:13:14 2026 +0100 [FIX] exists/mx mechanisms: an invalid expanded target is no match The exists and mx mechanisms built their DNS request from the expanded target without handling the NoneException DNSRequest throws when the name does not parse (an empty label, a label of more than 63 octets). The exception reached DefaultSPFChecker, so the result was none and the later terms, -all included, were never evaluated. The sender chooses %{l}, %{s} and %{h}: with the RFC 7208 7.4 example "exists:%{ir}.%{l1r+-}._spf.%{d} -all", MAIL FROM:<[email protected]> expands to 4.3.2.1.._spf.example.com and turned the fail of a forged sender into none. RFC 7208 gives none only when no record exists or the initial domain is malformed (2.6.1, 4.3). Treat such a target as no match, as the a mechanism does, and skip an MX name that does not parse instead of aborting the mx check. --- .../apache/james/jspf/terms/ExistsMechanism.java | 8 ++- .../org/apache/james/jspf/terms/MXMechanism.java | 19 ++++++-- .../org/apache/james/jspf/SPFExecutorBaseTest.java | 57 ++++++++++++++++++++++ 3 files changed, 79 insertions(+), 5 deletions(-) diff --git a/resolver/src/main/java/org/apache/james/jspf/terms/ExistsMechanism.java b/resolver/src/main/java/org/apache/james/jspf/terms/ExistsMechanism.java index 117c7d8..6aaea2a 100644 --- a/resolver/src/main/java/org/apache/james/jspf/terms/ExistsMechanism.java +++ b/resolver/src/main/java/org/apache/james/jspf/terms/ExistsMechanism.java @@ -50,7 +50,13 @@ public class ExistsMechanism extends GenericMechanism implements SPFCheckerDNSRe public DNSLookupContinuation checkSPF(SPFSession spfData) throws PermErrorException, TempErrorException, NeutralException, NoneException { String host = expandHost(spfData); - return new DNSLookupContinuation(new DNSRequest(host,DNSRequest.A), ExistsMechanism.this); + try { + return new DNSLookupContinuation(new DNSRequest(host, DNSRequest.A), ExistsMechanism.this); + } catch (NoneException e) { + // the expanded target is not a valid domain name: no match, as the a mechanism + spfData.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT, Boolean.FALSE); + return null; + } } } diff --git a/resolver/src/main/java/org/apache/james/jspf/terms/MXMechanism.java b/resolver/src/main/java/org/apache/james/jspf/terms/MXMechanism.java index f79e968..cd33a6f 100644 --- a/resolver/src/main/java/org/apache/james/jspf/terms/MXMechanism.java +++ b/resolver/src/main/java/org/apache/james/jspf/terms/MXMechanism.java @@ -59,8 +59,14 @@ public class MXMechanism extends AMechanism implements DNSServiceEnabled, SPFChe // Get the right host. String host = expandHost(spfData); - - return new DNSLookupContinuation(new DNSRequest(host, DNSRequest.MX), MXMechanism.this); + + try { + return new DNSLookupContinuation(new DNSRequest(host, DNSRequest.MX), MXMechanism.this); + } catch (NoneException e) { + // the expanded target is not a valid domain name: no match, as the a mechanism + spfData.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT, Boolean.FALSE); + return null; + } } } @@ -147,8 +153,13 @@ public class MXMechanism extends AMechanism implements DNSServiceEnabled, SPFChe while (records.size() > 0 && (mx = records.remove(0)) != null && mx.length() > 0) { LOGGER.debug("Add MX-Record {} to list", mx); - return new DNSLookupContinuation(new DNSRequest(mx, isIPv6 ? DNSRequest.AAAA : DNSRequest.A), MXMechanism.this); - + try { + return new DNSLookupContinuation(new DNSRequest(mx, isIPv6 ? DNSRequest.AAAA : DNSRequest.A), MXMechanism.this); + } catch (NoneException e) { + // an MX name that is not a valid domain name has no address: skip it + LOGGER.debug("Skip invalid MX name {}", mx); + } + } // no mx record found 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 f611195..12c04c9 100644 --- a/resolver/src/test/java/org/apache/james/jspf/SPFExecutorBaseTest.java +++ b/resolver/src/test/java/org/apache/james/jspf/SPFExecutorBaseTest.java @@ -193,6 +193,13 @@ public abstract class SPFExecutorBaseTest { return createCustomSPF(new DNSServiceXBillImpl(fakeResolver)).checkSPF("192.0.2.10", "a_user@" + hostname, hostname); } + // hostname has the record, checked for the sender localPart@<hostname> and the HELO helo + private SPFResult checkSender(String hostname, String record, String localPart, String helo) throws IOException { + FakeResolver fakeResolver = new FakeResolver(); + fakeResolver.addRecord(new TXTRecord(Name.fromString(hostname + "."), DClass.IN, 30L, record)); + return createCustomSPF(new DNSServiceXBillImpl(fakeResolver)).checkSPF("1.2.3.4", localPart + "@" + hostname, helo); + } + // the lookups of broken.<hostname> fail with an unchecked exception, as dnsjava may do on an answer it cannot handle private SPFResult checkRecordWithBrokenHost(String hostname, String record) throws IOException { FakeResolver fakeResolver = new FakeResolver(); @@ -1607,4 +1614,54 @@ public abstract class SPFExecutorBaseTest { // RFC 7208 7.3: 253 characters "in this format", without the trailing dot of a fully qualified name assertEquals(SPFErrorConstants.PASS_CONV, result.getResult()); } + + @Test(timeout = 10000) + public void existsShouldNotMatchATargetWithAnEmptyLabelFromTheLocalPart() throws IOException { + // RFC 7208 7.4 example: %{l1r+-} of "-x" is "", so the target 4.3.2.1.._spf.<hostname> has an empty label + SPFResult result = checkSender("existsEmptyLabel.spfexecutor.fake", + "v=spf1 exists:%{ir}.%{l1r+-}._spf.%{d} -all", "-x", "helo.spfexecutor.fake"); + + // an invalid target is no match: the record goes on to -all, as for the a mechanism + assertEquals(SPFErrorConstants.FAIL_CONV, result.getResult()); + } + + @Test(timeout = 10000) + public void existsShouldNotMatchATargetWithALabelOf64Octets() throws IOException { + SPFResult result = checkSender("existsLongLabel.spfexecutor.fake", + "v=spf1 exists:%{l}._spf.%{d} -all", "x".repeat(64), "helo.spfexecutor.fake"); + + assertEquals(SPFErrorConstants.FAIL_CONV, result.getResult()); + } + + @Test(timeout = 10000) + public void existsShouldNotMatchATargetWithAnEmptyLabelFromTheHelo() throws IOException { + SPFResult result = checkSender("existsInvalidHelo.spfexecutor.fake", + "v=spf1 exists:%{h}._spf.%{d} -all", "a_user", "a..b"); + + assertEquals(SPFErrorConstants.FAIL_CONV, result.getResult()); + } + + @Test(timeout = 10000) + public void mxShouldNotMatchATargetWithAnEmptyLabelFromTheHelo() throws IOException { + SPFResult result = checkSender("mxInvalidHelo.spfexecutor.fake", + "v=spf1 mx:%{h} -all", "a_user", "a..b"); + + assertEquals(SPFErrorConstants.FAIL_CONV, result.getResult()); + } + + @Test(timeout = 10000) + public void mxShouldNotMatchATargetWithALabelOf64Octets() throws IOException { + SPFResult result = checkSender("mxLongLabel.spfexecutor.fake", + "v=spf1 mx:%{l}.%{d} -all", "x".repeat(64), "helo.spfexecutor.fake"); + + assertEquals(SPFErrorConstants.FAIL_CONV, result.getResult()); + } + + @Test(timeout = 10000) + public void mxShouldNotMatchATargetWithAnEmptyLabelFromTheLocalPart() throws IOException { + SPFResult result = checkSender("mxEmptyLabel.spfexecutor.fake", + "v=spf1 mx:%{l1r+-}.%{d} -all", "-x", "helo.spfexecutor.fake"); + + assertEquals(SPFErrorConstants.FAIL_CONV, result.getResult()); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
