This is an automated email from the ASF dual-hosted git repository.

chibenwa pushed a commit to branch fuzz-10
in repository https://gitbox.apache.org/repos/asf/james-mime4j.git

commit a9dc794a7a4c0755b46372f67a92c3599ebd095b
Author: Benoit TELLIER <[email protected]>
AuthorDate: Sun Nov 22 07:32:36 2026 +0100

    [FIX] LenientAddressParser: an empty element in an obsolete route is skipped
    
    "<@a,,@b:c@d>" is a valid obs-domain-list (RFC 5322 4.4). The route loop
    stopped on the second comma, so the rest of the route was read as the
    address: local part "," at domain "b:c@d", where the strict parser reads
    c@d with route [a, b].
    
    After a comma, CFWS and further commas are skipped when an "@domain" or
    the closing ":" follows. Otherwise the cursor stays after the comma, as
    before.
    
    "<@a,:c@d>" is now read as c@d with route [a], as the RFC grammar allows,
    although the strict parser rejects it.
---
 .../mime4j/field/address/LenientAddressParser.java | 28 ++++++++++
 .../field/address/LenientAddressBuilderTest.java   | 59 ++++++++++++++++++++++
 2 files changed, 87 insertions(+)

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 edc5c59e..3e8a629f 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
@@ -222,6 +222,16 @@ public class LenientAddressParser implements AddressParser 
{
             current = (char) (buf.byteAt(pos) & 0xff);
             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.
+                int afterComma = cursor.getPos();
+                current = skipEmptyRouteElements(buf, cursor);
+                if (current == COLON) {
+                    cursor.updatePos(cursor.getPos() + 1);
+                    break;
+                } else if (current != AT) {
+                    cursor.updatePos(afterComma);
+                }
             } else if (current == COLON) {
                 cursor.updatePos(pos + 1);
                 break;
@@ -232,6 +242,24 @@ public class LenientAddressParser implements AddressParser 
{
         return domains != null ? new DomainList(domains) : null;
     }
 
+    /**
+     * Skips CFWS and commas. Returns the character the cursor then stands on, 
or -1 at the end.
+     */
+    private int skipEmptyRouteElements(final ByteSequence buf, final 
ParserCursor cursor) {
+        for (;;) {
+            this.parser.skipAllWhiteSpace(buf, cursor);
+            if (cursor.atEnd()) {
+                return -1;
+            }
+            int pos = cursor.getPos();
+            int current = (char) (buf.byteAt(pos) & 0xff);
+            if (current != COMMA) {
+                return current;
+            }
+            cursor.updatePos(pos + 1);
+        }
+    }
+
     private Mailbox createMailbox(
             final String name, final DomainList route, final String localPart, 
final String domain) {
         return new Mailbox(
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 f282d384..c2614ade 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
@@ -183,6 +183,65 @@ public class LenientAddressBuilderTest {
         Assert.assertEquals('m', raw.byteAt(cursor.getPos()));
     }
 
+    @Test
+    public void testParseRouteWithEmptyElement() throws Exception {
+        Mailbox mailbox = parser.parseMailbox("<@a,,@b:c@d>");
+        Assert.assertEquals("c", mailbox.getLocalPart());
+        Assert.assertEquals("d", mailbox.getDomain());
+        DomainList route = mailbox.getRoute();
+        Assert.assertNotNull(route);
+        Assert.assertEquals(2, route.size());
+        Assert.assertEquals("a", route.get(0));
+        Assert.assertEquals("b", route.get(1));
+
+        AddressList list = parser.parseAddressList("x <@a, ,@b:c@d>, e@f");
+        Assert.assertEquals(2, list.size());
+        Assert.assertEquals("c@d", ((Mailbox) list.get(0)).getAddress());
+        Assert.assertEquals("e@f", ((Mailbox) list.get(1)).getAddress());
+    }
+
+    @Test
+    public void testParseRouteEndingWithComma() throws Exception {
+        Mailbox mailbox = parser.parseMailbox("<@a, (x) ,:c@d>");
+        Assert.assertEquals("c@d", mailbox.getAddress());
+        Assert.assertEquals(1, mailbox.getRoute().size());
+        Assert.assertEquals("a", mailbox.getRoute().get(0));
+    }
+
+    @Test
+    public void testParseRouteEmptyElementsMatchStrictParser() throws 
Exception {
+        String[] inputs = {"<@a,,@b:c@d>", "<@a, ,@b:c@d>", "<@a,(x),@b:c@d>", 
"<@a,,,@b,,@e:c@d>",
+            "x <@a,,@b:c@d>"};
+        for (String input : inputs) {
+            Mailbox strict = DefaultAddressParser.DEFAULT.parseMailbox(input);
+            Mailbox lenient = parser.parseMailbox(input);
+            Assert.assertEquals(input, strict.getAddress(), 
lenient.getAddress());
+            Assert.assertEquals(input, strict.getRoute().toRouteString(), 
lenient.getRoute().toRouteString());
+        }
+    }
+
+    @Test
+    public void testParseRouteCommaNotFollowedByDomainKeepsCursor() throws 
Exception {
+        String s = "@a,,b";
+        ByteSequence raw = ContentUtil.encode(s);
+        ParserCursor cursor = new ParserCursor(0, s.length());
+
+        DomainList route = parser.parseRoute(raw, cursor, null);
+        Assert.assertEquals(1, route.size());
+        Assert.assertEquals(3, cursor.getPos());
+    }
+
+    @Test
+    public void testParseRouteEmptyElementsBeforeColonConsumeTheColon() throws 
Exception {
+        String s = "@a,,:x";
+        ByteSequence raw = ContentUtil.encode(s);
+        ParserCursor cursor = new ParserCursor(0, s.length());
+
+        DomainList route = parser.parseRoute(raw, cursor, null);
+        Assert.assertEquals(1, route.size());
+        Assert.assertEquals(5, cursor.getPos());
+    }
+
     @Test
     public void testParseAddressStartingWithAt() throws Exception {
         String s = "<@[email protected]>";


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to