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


The following commit(s) were added to refs/heads/master by this push:
     new bc6d0626 [FIX] LenientAddressParser: a bare local part decoding to an 
address special is not decoded (#149)
bc6d0626 is described below

commit bc6d062696bc99423958c1490bfc63ab3166bed4
Author: Benoit TELLIER <[email protected]>
AuthorDate: Fri Nov 27 08:13:28 2026 +0100

    [FIX] LenientAddressParser: a bare local part decoding to an address 
special is not decoded (#149)
    
    A mailbox without domain keeps the encoded words of its bare local part
    decoded (MIME4J-253). "=?UTF-8?Q?ceo=40corp.com?=" then gave the local
    part "[email protected]", with no domain, and getAddress() returned it as is:
    an address the header does not hold, read as a sender by callers.
    
    Like a control character (#147), an RFC 5322 special other than "." is
    not part of a plain local part, so the encoded form is kept. The
    MIME4J-253 test decoded a whole encoded mailbox, which is that spoof: it
    now uses an encoded dot-atom.
---
 .../mime4j/field/address/LenientAddressParser.java | 18 +++++----
 .../field/address/LenientAddressBuilderTest.java   | 45 +++++++++++++++++++++-
 2 files changed, 54 insertions(+), 9 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 9a00ab71..0e24e0ae 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
@@ -64,6 +64,9 @@ public class LenientAddressParser implements AddressParser {
     private static final BitSet BRACKETED_ROUTE_STOP   = union(ROUTE_STOP, 
CLOSING_BRACKET_ONLY);
     private static final BitSet ADDRESS_IN_LIST_STOP   = union(ADDRESS_STOP, 
COMMA_ONLY);
 
+    /** The RFC 5322 specials but ".", which a plain (dot-atom) local part 
does not hold. */
+    private static final String ADDRESS_SPECIALS       = "()<>[]:;@\\,\"";
+
     /**
      * Returns the union of a stop set with optional extra delimiters, sharing 
the
      * base set when there is nothing to add. Callers must not mutate the 
result.
@@ -380,26 +383,27 @@ public class LenientAddressParser implements 
AddressParser {
 
     /**
      * A mailbox without domain whose bare local part keeps its encoded words 
decoded (MIME4J-253).
-     * A decoded text holding a control character, such as CR or LF, is not a 
local part: the encoded
-     * form is kept, so that the address cannot carry a line break into a 
header it is written to.
+     * A decoded text holding a control character, such as CR or LF, or an RFC 
5322 special, such as
+     * "@", is not a plain local part: the encoded form is kept, so that the 
address cannot carry a
+     * line break into a header it is written to, nor read as an address the 
header does not hold.
      */
     private Mailbox createMailbox(final String localPart) {
         if (localPart != null && localPart.length() > 0) {
             String decoded = DecoderUtil.decodeEncodedWords(localPart, 
this.monitor);
-            return new Mailbox(null, null, holdsControlChar(decoded) ? 
localPart : decoded, null);
+            return new Mailbox(null, null, isPlainLocalPart(decoded) ? decoded 
: localPart, null);
         } else {
             return null;
         }
     }
 
-    private static boolean holdsControlChar(final String text) {
+    private static boolean isPlainLocalPart(final String text) {
         for (int i = 0; i < text.length(); i++) {
             char c = text.charAt(i);
-            if (c < 0x20 || c == 0x7f) {
-                return true;
+            if (c < 0x20 || c == 0x7f || ADDRESS_SPECIALS.indexOf(c) >= 0) {
+                return false;
             }
         }
-        return false;
+        return true;
     }
 
     /**
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 c73317cb..98bfd017 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
@@ -330,8 +330,17 @@ public class LenientAddressBuilderTest {
 
     @Test
     public void testParseMailboxEncodedLocalPart() throws ParseException {
-        Mailbox mailbox1 = 
parser.parseMailbox("=?utf-8?B?IkvDpXJlIFPDpnRyZSIgPGtzQGRvbWFpbi5jb20+?=");
-        Assert.assertEquals("\"Kåre Sætre\" <[email protected]>", 
mailbox1.getLocalPart());
+        Mailbox mailbox1 = parser.parseMailbox("=?utf-8?B?S8OlcmUuU8OmdHJl?=");
+        Assert.assertEquals("Kåre.Sætre", mailbox1.getLocalPart());
+    }
+
+    @Test
+    public void 
testParseMailboxEncodedLocalPartDecodingToAMailboxIsNotDecoded() throws 
ParseException {
+        // "\"Kåre Sætre\" <[email protected]>" in base64
+        String encoded = 
"=?utf-8?B?IkvDpXJlIFPDpnRyZSIgPGtzQGRvbWFpbi5jb20+?=";
+        Mailbox mailbox1 = parser.parseMailbox(encoded);
+        Assert.assertEquals(encoded, mailbox1.getLocalPart());
+        Assert.assertNull(mailbox1.getDomain());
     }
 
     @Test
@@ -953,4 +962,36 @@ public class LenientAddressBuilderTest {
         String encoded = "=?UTF-8?Q?a=00b?=";
         Assert.assertEquals(encoded, 
parser.parseMailbox(encoded).getLocalPart());
     }
+
+    @Test
+    public void 
bareDomainlessEncodedLocalPartDecodingToAnAddressIsNotDecoded() {
+        // "[email protected]" in Q and B encodings
+        for (String encoded : new String[] {"=?UTF-8?Q?ceo=40corp.com?=", 
"=?UTF-8?B?Y2VvQGNvcnAuY29t?="}) {
+            Mailbox fromAddressList = (Mailbox) 
parser.parseAddressList(encoded).get(0);
+            Mailbox secondInList = (Mailbox) 
parser.parseAddressList("[email protected], " + encoded).get(1);
+            Mailbox fromAddress = (Mailbox) parser.parseAddress(encoded);
+            Mailbox fromMailbox = parser.parseMailbox(encoded);
+
+            for (Mailbox mailbox : new Mailbox[] {fromAddressList, 
secondInList, fromAddress, fromMailbox}) {
+                Assert.assertEquals(encoded, mailbox.getAddress());
+                Assert.assertNull(mailbox.getDomain());
+            }
+        }
+    }
+
+    @Test
+    public void bareDomainlessEncodedLocalPartDecodingToASpecialIsNotDecoded() 
{
+        // "<", ">", ",", ";", ":", "\"", "(", ")", "[", "]" and "\\" Q-encoded
+        for (String special : new String[] {"=3C", "=3E", "=2C", "=3B", "=3A", 
"=22", "=28", "=29", "=5B", "=5D", "=5C"}) {
+            String encoded = "=?UTF-8?Q?a" + special + "b?=";
+            Assert.assertEquals(encoded, 
parser.parseMailbox(encoded).getLocalPart());
+        }
+    }
+
+    @Test
+    public void 
bareDomainlessEncodedLocalPartDecodingToAPlainLocalPartIsDecoded() {
+        Assert.assertEquals("j\u00f6rg.m\u00fcller", 
parser.parseMailbox("=?UTF-8?Q?j=C3=B6rg.m=C3=BCller?=").getAddress());
+        Assert.assertEquals("j\u00f6rg.m\u00fcller",
+            ((Mailbox) parser.parseAddressList("[email protected], 
=?UTF-8?B?asO2cmcubcO8bGxlcg==?=").get(1)).getAddress());
+    }
 }


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

Reply via email to