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-mime4j.git
commit 30b324c417e668639f71b18cba650bfc2340cdde Author: Benoit TELLIER <[email protected]> AuthorDate: Sat Nov 21 07:08:06 2026 +0100 [FIX] LenientAddressParser: a delimiter inside a domain literal does not end it The search for the ']' closing a domain literal gave up at the first delimiter of the caller: in "a@[x,[email protected],]" the literal stopped at the comma, and the lenient parser read three mailboxes, one of them [email protected], where DefaultAddressParser reads the single domain "[x,[email protected],]". ",", ";" and ">" are dtext (RFC 5322 section 3.4.1). The search now ignores the delimiters, and gives up at an unescaped '[', which cannot be in a literal, or at the end of the header. Without a ']', the domain still ends at the next delimiter. The failed searches remembered for the parse no longer depend on the delimiter set, so the parse stays linear. Trade-off, on input the strict parser rejects: an unclosed '[' followed later by a stray ']', with no '[' in between, now makes one domain of the addresses in between ("a@[x, b@c, Bob]" is one mailbox), as the strict parser reads such a literal. --- .../mime4j/field/address/LenientAddressParser.java | 45 +++++++++++----------- .../field/address/LenientAddressBuilderTest.java | 26 +++++++++++++ 2 files changed, 48 insertions(+), 23 deletions(-) diff --git a/dom/src/main/java/org/apache/james/mime4j/field/address/LenientAddressParser.java b/dom/src/main/java/org/apache/james/mime4j/field/address/LenientAddressParser.java index cd6b7b3d..b464b3c4 100644 --- a/dom/src/main/java/org/apache/james/mime4j/field/address/LenientAddressParser.java +++ b/dom/src/main/java/org/apache/james/mime4j/field/address/LenientAddressParser.java @@ -23,9 +23,7 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.BitSet; import java.util.Collections; -import java.util.HashMap; import java.util.List; -import java.util.Map; import org.apache.james.mime4j.codec.DecodeMonitor; import org.apache.james.mime4j.codec.DecoderUtil; @@ -91,22 +89,21 @@ public class LenientAddressParser implements AddressParser { } /** - * The searches for the ']' of a domain literal that failed in one parse, by delimiter set: every - * '[' before the index where such a search stopped has no ']' before that index either. A - * search from a later '[' starts unescaped, as the earlier search went on after it, so it - * stops at the same index: it is not repeated for each domain, which would be quadratic. The - * domain itself, copied by copyContent, can end before that index, at an escaped delimiter. + * The searches for the ']' of a domain literal that failed in one parse: every '[' before the + * index where such a search stopped has no ']' before that index either. A search from a later + * '[' starts unescaped, as the earlier search went on after it, so it stops at the same index: + * it is not repeated for each domain, which would be quadratic. Public entry points start a new + * memo: parse a whole list with {@link #parseAddressList}. */ private static final class UnclosedLiterals { - private final Map<BitSet, Integer> until = new HashMap<BitSet, Integer>(4); + private int until = -1; - boolean isUnclosed(final BitSet delimiters, final int pos) { - Integer end = until.get(delimiters); - return end != null && pos < end; + boolean isUnclosed(final int pos) { + return pos < until; } - void unclosedUntil(final BitSet delimiters, final int end) { - until.put(delimiters, end); + void unclosedUntil(final int end) { + until = end; } } @@ -125,8 +122,8 @@ public class LenientAddressParser implements AddressParser { this.parser.skipWhiteSpace(buf, cursor); } else if (current == '(') { this.parser.skipComment(buf, cursor); - } else if (current == '[' && !unclosed.isUnclosed(delimiters, cursor.getPos()) - && copyDomainLiteral(buf, cursor, delimiters, dst, unclosed)) { + } else if (current == '[' && !unclosed.isUnclosed(cursor.getPos()) + && copyDomainLiteral(buf, cursor, dst, unclosed)) { continue; } else { this.parser.copyContent(buf, cursor, delimiters, dst); @@ -140,12 +137,14 @@ public class LenientAddressParser implements AddressParser { * (RFC 5322 section 3.4.1). A quoted pair (obs-dtext, section 4.4) is the escaped character alone, * as for the strict parser: its backslash is dropped, and an escaped ']' does not close the * literal. A line break after a backslash is unfolded, the quoted pair applying to the character - * after it. Returns true once copied. When no ']' comes before a delimiter, copies nothing, - * records in <code>unclosed</code> the index where the search stopped (the delimiter, or the - * upper bound) and returns false. + * after it. The delimiters of the caller (",", ";", ">") are dtext too: a literal + * "[x,y]" is one domain, as for the strict parser. Returns true once copied. When no ']' comes + * before another unescaped '[', which cannot be in a literal, copies nothing, records in + * <code>unclosed</code> the index where the search stopped (that '[', or the upper bound) and + * returns false: the domain is then copied up to the next delimiter. */ - private boolean copyDomainLiteral(final ByteSequence buf, final ParserCursor cursor, final BitSet delimiters, - final StringBuilder dst, final UnclosedLiterals unclosed) { + private boolean copyDomainLiteral(final ByteSequence buf, final ParserCursor cursor, final StringBuilder dst, + final UnclosedLiterals unclosed) { int from = cursor.getPos(); int to = cursor.getUpperBound(); for (int i = from + 1; i < to; i++) { @@ -172,12 +171,12 @@ public class LenientAddressParser implements AddressParser { cursor.updatePos(i + 1); return true; } - if (delimiters != null && delimiters.get(current)) { - unclosed.unclosedUntil(delimiters, i); + if (current == '[') { + unclosed.unclosedUntil(i); return false; } } - unclosed.unclosedUntil(delimiters, to); + unclosed.unclosedUntil(to); return false; } diff --git a/dom/src/test/java/org/apache/james/mime4j/field/address/LenientAddressBuilderTest.java b/dom/src/test/java/org/apache/james/mime4j/field/address/LenientAddressBuilderTest.java index 7f640f48..e7480389 100644 --- a/dom/src/test/java/org/apache/james/mime4j/field/address/LenientAddressBuilderTest.java +++ b/dom/src/test/java/org/apache/james/mime4j/field/address/LenientAddressBuilderTest.java @@ -708,6 +708,32 @@ public class LenientAddressBuilderTest { } } + @Test + public void domainLiteralHoldingADelimiterIsOneMailboxLikeTheStrictParser() throws Exception { + String[] texts = {"a@[x,[email protected],]", "h@[,]", "g: a@[;];", "b <a@[x>y]>", "<@[x,y]:a@b>", "a@[x,y], b@c"}; + for (String text : texts) { + MailboxList strict = DefaultAddressParser.DEFAULT.parseAddressList(text, DecodeMonitor.SILENT).flatten(); + MailboxList lenient = parser.parseAddressList(text).flatten(); + Assert.assertEquals(text, strict.size(), lenient.size()); + Assert.assertEquals(text, strict.get(0).getDomain(), lenient.get(0).getDomain()); + Assert.assertEquals(text, strict.get(0).getLocalPart(), lenient.get(0).getLocalPart()); + } + Assert.assertEquals("[x,[email protected],]", parser.parseMailbox("a@[x,[email protected],]").getDomain()); + // without a ']', the domain still ends at the comma + Assert.assertEquals(2, parser.parseAddressList("a@[x, b@c").size()); + } + + @Test(timeout = 5000) + public void parseAddressListShouldBeLinearOnUnclosedDomainLiteralsBetweenCommas() { + for (String unit : new String[] {"a@[x,", "a@[ \\\r\n[,", "g:a@[x;", "<a@[x>"}) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < 20000; i++) { + sb.append(unit); + } + parser.parseAddressList(sb); + } + } + @Test public void domainLiteralHoldingASemicolonKeepsTheGroupWhole() { Group group = new Group("team", new Mailbox("a", "[;]"), new Mailbox("b", "c")); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
