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 9885ff024ad6c94c456f1bfd8ff447ab92bb9292 Author: Benoit TELLIER <[email protected]> AuthorDate: Sun Nov 22 11:58:23 2026 +0100 [FIX] LenientAddressParser: route domains separated by "@" alone are read one by one The strict parser accepts route domains with no comma between them ("@a@[x,y]:b@c", route [a, [x,y]]). The lenient parser read "a@[x" as one route domain: the "[" was inside the atom, so the "," of the literal ended the domain and the local part became "y]:b". "@" now ends a route domain, and the loop reads the next one, so a literal after "@" is read as a literal. A route domain never holds "@", so routes with commas are unchanged. "<@[email protected]>" (MIME4J-207) now has the route [somehost.com, somehost.com] instead of one domain holding "@". --- .../mime4j/field/address/LenientAddressParser.java | 8 +++++-- .../field/address/LenientAddressBuilderTest.java | 25 ++++++++++++++++++++-- 2 files changed, 29 insertions(+), 4 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 3e8a629f..789c0162 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 @@ -57,7 +57,8 @@ public class LenientAddressParser implements AddressParser { private static final BitSet COMMA_ONLY = RawFieldParser.INIT_BITSET(COMMA); private static final BitSet COLON_ONLY = RawFieldParser.INIT_BITSET(COLON); private static final BitSet SEMICOLON_ONLY = RawFieldParser.INIT_BITSET(SEMICOLON); - private static final BitSet ROUTE_STOP = RawFieldParser.INIT_BITSET(COMMA, COLON); + // "@" too: route domains may follow each other with no comma ("@a@b:"), as for the strict parser + private static final BitSet ROUTE_STOP = RawFieldParser.INIT_BITSET(COMMA, COLON, AT); private static final BitSet MAILBOX_STOP = RawFieldParser.INIT_BITSET(AT, OPENING_BRACKET); private static final BitSet ADDRESS_STOP = RawFieldParser.INIT_BITSET(COLON, AT, OPENING_BRACKET); private static final BitSet BRACKETED_ROUTE_STOP = union(ROUTE_STOP, CLOSING_BRACKET_ONLY); @@ -220,7 +221,10 @@ public class LenientAddressParser implements AddressParser { } pos = cursor.getPos(); current = (char) (buf.byteAt(pos) & 0xff); - if (current == COMMA) { + if (current == AT) { + // the next route domain, consumed at the top of the loop + continue; + } else if (current == COMMA) { cursor.updatePos(pos + 1); // obs-domain-list: a comma may be followed by empty elements ("@a,,@b:"), // or end the list ("@a,:"). Anything else is left for the caller. 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 c2614ade..da7e14a6 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 @@ -253,8 +253,29 @@ public class LenientAddressBuilderTest { Assert.assertEquals(null, mailbox.getDomain()); DomainList route = mailbox.getRoute(); Assert.assertNotNull(route); - Assert.assertEquals(1, route.size()); - Assert.assertEquals("[email protected]", route.get(0)); + Assert.assertEquals(2, route.size()); + Assert.assertEquals("somehost.com", route.get(0)); + Assert.assertEquals("somehost.com", route.get(1)); + } + + @Test + public void testParseRouteDomainsSeparatedByAtMatchStrictParser() throws Exception { + String[] inputs = {"<@a@[x,y]:b@c>", "<@a@[x:y]:b@c>", "x <@a@b@[x,y]:b@c>", "<@a @b:b@c>", + "<@a(x)@b,,@d:b@c>"}; + for (String input : inputs) { + Mailbox strict = DefaultAddressParser.DEFAULT.parseMailbox(input); + Mailbox lenient = parser.parseMailbox(input); + Assert.assertEquals(input, "b@c", strict.getAddress()); + Assert.assertEquals(input, strict.getAddress(), lenient.getAddress()); + Assert.assertEquals(input, strict.getRoute().toRouteString(), lenient.getRoute().toRouteString()); + } + } + + @Test + public void testParseRouteEmptyDomainsBetweenAtsAreDropped() throws Exception { + Mailbox mailbox = parser.parseMailbox("<@@a@@b:b@c>"); + Assert.assertEquals("b@c", mailbox.getAddress()); + Assert.assertEquals("@a,@b", mailbox.getRoute().toRouteString()); } @Test --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
