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 3be47815013463ba5e5a30b38326f5c378753c86 Author: Benoit TELLIER <[email protected]> AuthorDate: Thu Nov 19 17:32:29 2026 +0100 [PERF] MacroExpand: walk a macro-string without a regex per character expandMacroString tokenized its input with a regex whose literal branch matches one character: a plain include, a or mx target cost a Matcher.find, a group String and an appendReplacement per character, 73% of the expansion of a target without macro, what most terms expand. Walk the input by hand: check and copy the macro-literals up to the next "%" at once, parse the macro-expand there, and return the input itself when it has none. A character that is not a macro-literal, or a malformed "%", is still a permerror, and the macro-letters of an explanation are still accepted here for a domain, replaceCell rejecting the others. Without appendReplacement, the values need no escaping of "$" and "\" any more. A test compares the expansions with the former tokenizer on random inputs. --- .../org/apache/james/jspf/core/MacroExpand.java | 168 +++++++++++++-------- .../apache/james/jspf/core/MacroExpandTest.java | 80 ++++++++++ 2 files changed, 183 insertions(+), 65 deletions(-) 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 5e42811..0827825 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 @@ -47,8 +47,6 @@ public class MacroExpand { private Pattern domainSpecPattern; - private Pattern macroStringPattern; - private Pattern macroLettersPattern; private Pattern macroLettersExpPattern; @@ -153,8 +151,6 @@ public class MacroExpand { */ public MacroExpand(DNSService dnsProbe) { domainSpecPattern = Pattern.compile(SPFTermsRegexps.DOMAIN_SPEC_REGEX); - // The real pattern replacer - macroStringPattern = Pattern.compile(SPFTermsRegexps.MACRO_STRING_REGEX_TOKEN); // The macro letters pattern macroLettersExpPattern = Pattern.compile(SPFTermsRegexps.MACRO_LETTER_PATTERN_EXP); macroLettersPattern = Pattern.compile(SPFTermsRegexps.MACRO_LETTER_PATTERN); @@ -360,51 +356,115 @@ public class MacroExpand { */ private String expandMacroString(String input, MacroData macroData, boolean isExplanation) throws PermErrorException, RequireClientDomainException { - StringBuffer decodedValue = new StringBuffer(); - Matcher inputMatcher = macroStringPattern.matcher(input); - String macroCell; + // ABNF: macro-string = *( macro-expand / macro-literal ), walked by hand: a regex find per + // macro-literal cost a Matcher.find and a String per character of every target + StringBuilder decodedValue = null; int pos = 0; - - while (inputMatcher.find()) { - String match2 = inputMatcher.group(); - if (pos != inputMatcher.start()) { - throw new PermErrorException("Middle part does not match: "+input.substring(0,pos)+">>"+input.substring(pos, inputMatcher.start())+"<<"+input.substring(inputMatcher.start())+" ["+input+"]"); - } - if (match2.length() > 0) { - if (match2.startsWith("%{")) { - macroCell = input.substring(inputMatcher.start() + 2, inputMatcher - .end() - 1); - inputMatcher - .appendReplacement(decodedValue, escapeForMatcher(replaceCell(macroCell, macroData, isExplanation))); - } else if (match2.length() == 2 && match2.startsWith("%")) { - // handle the % escaping - /* - * From RFC4408: - * - * A literal "%" is expressed by "%%". - * "%_" expands to a single " " space. - * "%-" expands to a URL-encoded space, viz., "%20". - */ - if ("%_".equals(match2)) { - inputMatcher.appendReplacement(decodedValue, " "); - } else if ("%-".equals(match2)) { - inputMatcher.appendReplacement(decodedValue, "%20"); - } else { - inputMatcher.appendReplacement(decodedValue, escapeForMatcher(match2.substring(1))); - } + while (pos < input.length()) { + int macro = input.indexOf('%', pos); + int literalsEnd = macro < 0 ? input.length() : macro; + checkMacroLiterals(input, pos, literalsEnd); + if (macro < 0) { + if (decodedValue == null) { + // no macro-expand: the input is its own expansion + return input; } + decodedValue.append(input, pos, literalsEnd); + break; + } + if (decodedValue == null) { + decodedValue = new StringBuilder(input.length() + 32); } - - pos = inputMatcher.end(); + decodedValue.append(input, pos, literalsEnd); + pos = expandMacro(input, macro, decodedValue, macroData, isExplanation); } - - if (input.length() != pos) { - throw new PermErrorException("End part does not match: "+input.substring(pos)); + + return decodedValue == null ? input : decodedValue.toString(); + } + + /** + * ABNF: macro-literal = %x21-24 / %x26-7E + * + * @throws PermErrorException when a character of input, from start to end, is not a macro-literal + */ + private static void checkMacroLiterals(String input, int start, int end) throws PermErrorException { + for (int i = start; i < end; i++) { + char c = input.charAt(i); + if (c < 0x21 || c > 0x7e || c == '%') { + throw new PermErrorException("Invalid macro-literal at " + i + ": " + input); + } } - - inputMatcher.appendTail(decodedValue); + } - return decodedValue.toString(); + /** + * ABNF: macro-expand = ( "%{" macro-letter transformers *delimiter "}" ) / "%%" / "%_" / "%-" + * + * The macro-letters of an explanation are accepted here for a domain too: replaceCell rejects + * the ones a domain may not use. + * + * @param input the macro-string + * @param start the position of the "%" of the macro-expand + * @param out where the expansion is appended + * @return the position after the macro-expand + * @throws PermErrorException when there is no valid macro-expand at start + */ + private int expandMacro(String input, int start, StringBuilder out, MacroData macroData, boolean isExplanation) throws PermErrorException, RequireClientDomainException { + if (start + 1 >= input.length()) { + throw new PermErrorException("Invalid macro-expand at " + start + ": " + input); + } + switch (input.charAt(start + 1)) { + /* + * From RFC4408: + * + * A literal "%" is expressed by "%%". + * "%_" expands to a single " " space. + * "%-" expands to a URL-encoded space, viz., "%20". + */ + case '%': + out.append('%'); + return start + 2; + case '_': + out.append(' '); + return start + 2; + case '-': + out.append("%20"); + return start + 2; + case '{': + int end = macroCellEnd(input, start + 2); + if (end < 0) { + throw new PermErrorException("Invalid macro-expand at " + start + ": " + input); + } + out.append(replaceCell(input.substring(start + 2, end), macroData, isExplanation)); + return end + 1; + default: + throw new PermErrorException("Invalid macro-expand at " + start + ": " + input); + } + } + + /** + * ABNF: macro-letter transformers *delimiter "}", with transformers = *DIGIT [ "r" ] + * + * @return the position of the closing "}", or -1 when there is no such macro cell at start + */ + private static int macroCellEnd(String input, int start) { + int i = start; + if (i >= input.length() || "rctlsodipvhRCTLSODIPVH".indexOf(input.charAt(i)) < 0) { + return -1; + } + i++; + while (i < input.length() && input.charAt(i) >= '0' && input.charAt(i) <= '9') { + i++; + } + if (i < input.length() && input.charAt(i) == 'r') { + i++; + } + while (i < input.length() && ".-+,/_=".indexOf(input.charAt(i)) >= 0) { + i++; + } + if (i < input.length() && input.charAt(i) == '}') { + return i; + } + return -1; } /** @@ -636,27 +696,5 @@ public class MacroExpand { return data.replaceAll("\\+", "%20"); } - - /** - * Because Dollar signs may be treated as references to captured subsequences in method Matcher.appendReplacement - * its necessary to escape Dollar signs because its allowed in the local-part of an emailaddress. - * - * See JSPF-71 for the bugreport - * - * @param raw - * @return escaped string - */ - private String escapeForMatcher(String raw) { - StringBuffer sb = new StringBuffer(); - - for (int i = 0; i < raw.length(); i++) { - char c = raw.charAt(i); - if (c == '$' || c == '\\') { - sb.append('\\'); - } - sb.append(c); - } - return sb.toString(); - } } 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 37ff8e4..65c9fb3 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,4 +262,84 @@ public class MacroExpandTest extends TestCase { } }, MacroExpand.DOMAIN)); } + public void testPlainTargetIsReturnedAsIs() throws PermErrorException { + assertEquals("_spf.mail.example.net", defIp4me.expand("_spf.mail.example.net", new rfcIP4MacroData(), MacroExpand.DOMAIN)); + } + + public void testInvalidMacroExpandsAreErrors() { + String[] invalid = {"a%", "%{", "%{d", "%{dR}", "%{x}", "%{}", "%a", "a\tb%{d}", "\u00e9%{d}", "%{d1.2}"}; + for (String input : invalid) { + try { + defIp4me.expand(input, new rfcIP4MacroData(), MacroExpand.EXPLANATION); + fail(input + " should be a permerror"); + } catch (PermErrorException e) { + // expected + } + } + } + + public void testMacroValueWithReplacementCharactersIsCopied() throws PermErrorException { + assertEquals("a$1\\b.x", defIp4me.expand("%{l}.x", new rfcIP4MacroData() { + public String getCurrentSenderPart() { + return "a$1\\b"; + } + }, MacroExpand.EXPLANATION)); + } + + /** + * The former tokenizer: a regex find per macro-expand or macro-literal, each cell expanded alone. + */ + private String referenceExpansion(String input, MacroData data) throws PermErrorException { + java.util.regex.Matcher matcher = java.util.regex.Pattern.compile(SPFTermsRegexps.MACRO_STRING_REGEX_TOKEN).matcher(input); + StringBuilder out = new StringBuilder(); + int pos = 0; + while (matcher.find()) { + if (pos != matcher.start()) { + throw new PermErrorException("Middle part does not match"); + } + String token = matcher.group(); + if (token.startsWith("%{")) { + out.append(defIp4me.expand(token, data, MacroExpand.EXPLANATION)); + } else if ("%_".equals(token)) { + out.append(' '); + } else if ("%-".equals(token)) { + out.append("%20"); + } else if ("%%".equals(token)) { + out.append('%'); + } else { + out.append(token); + } + pos = matcher.end(); + } + if (pos != input.length()) { + throw new PermErrorException("End part does not match"); + } + return out.toString(); + } + + public void testExpansionIsTheOneOfTheFormerTokenizer() { + String[] pieces = {"%", "{", "}", "%{", "%%", "%_", "%-", "d", "l", "i", "r", "c", "k", "R", "1", "2", "0", + ".", "-", "+", "=", "/", ",", "_", "$", "\\", "a", "\u00e9", "\t", "~", "!"}; + java.util.Random random = new java.util.Random(42); + for (int n = 0; n < 20000; n++) { + StringBuilder input = new StringBuilder(); + int length = random.nextInt(8); + for (int k = 0; k < length; k++) { + input.append(pieces[random.nextInt(pieces.length)]); + } + String expected; + try { + expected = referenceExpansion(input.toString(), new rfcIP4MacroData()); + } catch (PermErrorException e) { + expected = "permerror"; + } + String actual; + try { + actual = defIp4me.expand(input.toString(), new rfcIP4MacroData(), MacroExpand.EXPLANATION); + } catch (PermErrorException e) { + actual = "permerror"; + } + assertEquals(input.toString(), expected, actual); + } + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
