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 5513df0fd7274cb42f4751582011ccf99b865bf4 Author: Benoit TELLIER <[email protected]> AuthorDate: Wed Nov 25 09:23:44 2026 +0100 [FIX] MacroExpand: bound the length of an expanded explanation An exp= TXT record of up to 64 KB of macros expanded into megabytes, kept as the explanation of the result. An explanation longer than 1000 characters is now a permerror, so that the check goes on as without exp= (RFC 7208 6.2), and its expansion stops at that length. --- .../org/apache/james/jspf/core/MacroExpand.java | 19 ++++- .../apache/james/jspf/core/MacroExpandTest.java | 18 ++++ .../apache/james/jspf/terms/ExpModifierTest.java | 96 ++++++++++++++++++++++ 3 files changed, 132 insertions(+), 1 deletion(-) diff --git a/resolver/src/main/java/org/apache/james/jspf/core/MacroExpand.java b/resolver/src/main/java/org/apache/james/jspf/core/MacroExpand.java index 0827825..36076e0 100644 --- a/resolver/src/main/java/org/apache/james/jspf/core/MacroExpand.java +++ b/resolver/src/main/java/org/apache/james/jspf/core/MacroExpand.java @@ -62,6 +62,13 @@ public class MacroExpand { public static final boolean EXPLANATION = true; public static final boolean DOMAIN = false; + + /** + * The longest expanded explanation: an SMTP reply line is at most 512 octets (RFC 5321 4.5.3.1.5). A longer + * one is a permerror, so that the check goes on as without explanation (RFC 7208 6.2), and its expansion + * stops there instead of growing each macro of a DNS answer of up to 64 KB into megabytes. + */ + public static final int MAX_EXPLANATION_LENGTH = 1000; public static class RequireClientDomainException extends Exception { @@ -296,10 +303,11 @@ public class MacroExpand { LOGGER.debug("Start do expand explanation: {}", input); String[] parts = input.split(" "); - StringBuffer res = new StringBuffer(); + StringBuilder res = new StringBuilder(); for (int i = 0; i < parts.length; i++) { if (i > 0) res.append(" "); res.append(expandMacroString(parts[i], macroData, true)); + checkExplanationLength(res.length()); } LOGGER.debug("Done expand explanation: {}", res); @@ -377,11 +385,20 @@ public class MacroExpand { } decodedValue.append(input, pos, literalsEnd); pos = expandMacro(input, macro, decodedValue, macroData, isExplanation); + if (isExplanation) { + checkExplanationLength(decodedValue.length()); + } } return decodedValue == null ? input : decodedValue.toString(); } + private static void checkExplanationLength(int length) throws PermErrorException { + if (length > MAX_EXPLANATION_LENGTH) { + throw new PermErrorException("Expanded explanation longer than " + MAX_EXPLANATION_LENGTH + " characters"); + } + } + /** * ABNF: macro-literal = %x21-24 / %x26-7E * diff --git a/resolver/src/test/java/org/apache/james/jspf/core/MacroExpandTest.java b/resolver/src/test/java/org/apache/james/jspf/core/MacroExpandTest.java index 65c9fb3..3c7a86b 100644 --- a/resolver/src/test/java/org/apache/james/jspf/core/MacroExpandTest.java +++ b/resolver/src/test/java/org/apache/james/jspf/core/MacroExpandTest.java @@ -262,6 +262,24 @@ public class MacroExpandTest extends TestCase { } }, MacroExpand.DOMAIN)); } + public void testAnExplanationUnderTheLimitIsExpanded() throws PermErrorException { + String explanation = defIp4me.expand("%{d} ".repeat(50), new rfcIP4MacroData(), MacroExpand.EXPLANATION); + + assertEquals("email.example.com ".repeat(50).trim(), explanation); + } + + public void testAnExplanationExpandedPastTheLimitIsAPermError() { + // a few KB of macros, each growing into the whole sender domain + String[] inputs = {"%{d}".repeat(2000), "%{d} ".repeat(2000), "a".repeat(MacroExpand.MAX_EXPLANATION_LENGTH + 1)}; + for (String input : inputs) { + try { + defIp4me.expand(input, new rfcIP4MacroData(), MacroExpand.EXPLANATION); + fail("an explanation longer than " + MacroExpand.MAX_EXPLANATION_LENGTH + " characters should be a permerror"); + } catch (PermErrorException e) { + } + } + } + public void testPlainTargetIsReturnedAsIs() throws PermErrorException { assertEquals("_spf.mail.example.net", defIp4me.expand("_spf.mail.example.net", new rfcIP4MacroData(), MacroExpand.DOMAIN)); } 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 new file mode 100644 index 0000000..359d410 --- /dev/null +++ b/resolver/src/test/java/org/apache/james/jspf/terms/ExpModifierTest.java @@ -0,0 +1,96 @@ +/**************************************************************** + * 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.jspf.terms; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; + +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.SPFResult; +import org.apache.james.jspf.impl.SPF; +import org.junit.Test; + +public class ExpModifierTest { + + private static final String HELO = ("a".repeat(62) + ".").repeat(3) + "a".repeat(60) + ".com"; + + private static DNSService dns(Map<String, String> txtRecords) { + return new DNSService() { + public List<String> getRecords(DNSRequest request) { + String record = txtRecords.get(request.getHostname()); + return request.getRecordType() == DNSRequest.TXT && record != null ? new ArrayList<>(List.of(record)) : null; + } + + public CompletionStage<List<String>> getRecordsAsync(DNSRequest request) { + return CompletableFuture.completedFuture(getRecords(request)); + } + + public List<String> getLocalDomainNames() { + return List.of("localhost"); + } + + public void setTimeOut(int timeOut) { + } + + public int getRecordLimit() { + return 10; + } + + public void setRecordLimit(int recordLimit) { + } + }; + } + + @Test + public void anExplanationShouldBeExpanded() { + SPF spf = new SPF(dns(Map.of( + "example.com", "v=spf1 -all exp=exp.example.com", + "exp.example.com", "%{h} is not allowed"))); + + SPFResult result = spf.checkSPF("1.2.3.4", "[email protected]", HELO); + + assertEquals(SPFErrorConstants.FAIL_CONV, result.getResult()); + assertEquals(HELO + " is not allowed", result.getExplanation()); + } + + @Test + public void anExplanationExpandedPastTheLimitShouldBeIgnored() { + // about 64 KB of macros, each growing into the whole HELO name + SPF spf = new SPF(dns(Map.of( + "example.com", "v=spf1 -all exp=exp.example.com", + "exp.example.com", "%{h}".repeat(16000)))); + + SPFResult result = spf.checkSPF("1.2.3.4", "[email protected]", HELO); + + // RFC 7208 6.2: as if no exp= was given, so the default explanation + assertEquals(SPFErrorConstants.FAIL_CONV, result.getResult()); + assertTrue(result.getExplanation(), result.getExplanation().length() <= MacroExpand.MAX_EXPLANATION_LENGTH); + assertTrue(result.getExplanation(), result.getExplanation().startsWith("http://www.openspf.org/why.html?sender=")); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
