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 c78e8406bbdd8268d6df304d754ffa04297376b7
Author: Benoit TELLIER <[email protected]>
AuthorDate: Sat Nov 21 20:07:25 2026 +0100

    [FIX] LenientAddressParser: a quoted pair in an address stands for the 
escaped character
    
    The lenient parser read the quoted strings of an address like MIME
    parameter values, keeping the backslash of a quoted pair other than
    \" and \\: "\a"@d gave the local part \a where DefaultAddressParser
    gives a, and "J\ohn" <a@d> the display name J\ohn (group names too).
    Recipient matching and deduplication then saw two addresses.
    
    As in RFC 5322 section 3.2.4, a quoted pair in an address now stands
    for the escaped character: the local part overload of
    RawFieldParser#copyQuotedContent unescapes it, and the new
    RawFieldParser#parseAddressValue reads the opening text of an address
    (display name, group name, local part) the same way. Parameter values
    keep their backslash as before.
---
 .../apache/james/mime4j/stream/RawFieldParser.java | 23 ++++++++++++++---
 .../mime4j/field/address/LenientAddressParser.java |  6 ++---
 .../field/address/LenientAddressBuilderTest.java   | 29 ++++++++++++++++++++++
 3 files changed, 52 insertions(+), 6 deletions(-)

diff --git 
a/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java 
b/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
index cd2e5655..6c9c1a0b 100644
--- a/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
+++ b/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
@@ -228,6 +228,20 @@ public class RawFieldParser {
         return parseValue(buf, cursor, delimiters, false, false);
     }
 
+    /**
+     * Like {@link #parseValue(ByteSequence, ParserCursor, BitSet)}, for a 
value of an address (a display
+     * name, a group name, a local part): as in RFC 5322 section 3.2.4, a 
quoted pair in a quoted string
+     * stands for the escaped character, no backslash is kept.
+     *
+     * @param buf buffer with the sequence of bytes to be parsed
+     * @param cursor defines the bounds and current position of the buffer
+     * @param delimiters set of delimiting characters. Can be 
<code>null</code> if the value
+     *  is not delimited by any character.
+     */
+    public String parseAddressValue(final ByteSequence buf, final ParserCursor 
cursor, final BitSet delimiters) {
+        return parseValue(buf, cursor, delimiters, false, true);
+    }
+
     private String parseValue(final ByteSequence buf, final ParserCursor 
cursor, final BitSet delimiters,
             final boolean boundary, final boolean unescape) {
         StringBuilder dst = new StringBuilder(initialCapacity(cursor));
@@ -481,9 +495,11 @@ public class RawFieldParser {
     }
 
     /**
-     * Transfers content enclosed with quote marks into the destination 
buffer, decoding the
+     * Transfers the quoted string of an address into the destination buffer, 
decoding the
      * RFC 2047 encoded words it starts with only when asked to. RFC 2047 
section 5 forbids encoded
      * words in an addr-spec and in a quoted-string: an address local part 
must not be decoded.
+     * As in RFC 5322 section 3.2.4, a quoted pair stands for the escaped 
character: unlike
+     * {@link #copyQuotedContent(ByteSequence, ParserCursor, StringBuilder)}, 
no backslash is kept.
      *
      * @param buf buffer with the sequence of bytes to be parsed
      * @param cursor defines the bounds and current position of the buffer
@@ -492,7 +508,7 @@ public class RawFieldParser {
      */
     public void copyQuotedContent(final ByteSequence buf, final ParserCursor 
cursor,
             final StringBuilder dst, final boolean decodeEncodedWords) {
-        copyQuotedContent(buf, cursor, dst, !decodeEncodedWords, false);
+        copyQuotedContent(buf, cursor, dst, !decodeEncodedWords, true);
     }
 
     /**
@@ -538,7 +554,8 @@ public class RawFieldParser {
                     // applies to the character after it
                     continue;
                 }
-                // A boundary or a charset cannot hold a backslash: the quoted 
pair is unescaped
+                // A boundary, a charset, an address: the quoted pair is 
unescaped. Elsewhere, a
+                // quoted pair other than \" and \\ keeps its backslash
                 if (!unescape && current != '\"' && current != '\\') {
                     dstRaw.append('\\');
                 }
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 b464b3c4..edc5c59e 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
@@ -377,7 +377,7 @@ public class LenientAddressParser implements AddressParser {
     private Mailbox parseMailbox(final ByteSequence buf, final ParserCursor 
cursor, final BitSet delimiters,
             final BitSet stopSet, final UnclosedLiterals unclosed) {
         int start = cursor.getPos();
-        String openingText = this.parser.parseValue(buf, cursor, stopSet);
+        String openingText = this.parser.parseAddressValue(buf, cursor, 
stopSet);
         if (cursor.atEnd()) {
             return createMailbox(openingText, buf, start, cursor.getPos());
         }
@@ -436,7 +436,7 @@ public class LenientAddressParser implements AddressParser {
     }
 
     public Group parseGroup(final ByteSequence buf, final ParserCursor cursor) 
{
-        String name = this.parser.parseToken(buf, cursor, COLON_ONLY);
+        String name = this.parser.parseAddressValue(buf, cursor, COLON_ONLY);
         if (cursor.atEnd()) {
             return new Group(decodeGroupName(name), 
Collections.<Mailbox>emptyList());
         }
@@ -463,7 +463,7 @@ public class LenientAddressParser implements AddressParser {
     private Address parseAddress(final ByteSequence buf, final ParserCursor 
cursor, final BitSet delimiters,
             final BitSet stopSet, final UnclosedLiterals unclosed) {
         int start = cursor.getPos();
-        String openingText = this.parser.parseValue(buf, cursor, stopSet);
+        String openingText = this.parser.parseAddressValue(buf, cursor, 
stopSet);
         if (cursor.atEnd()) {
             return createMailbox(openingText, buf, start, cursor.getPos());
         }
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 e7480389..f282d384 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
@@ -785,6 +785,35 @@ public class LenientAddressBuilderTest {
         Assert.assertEquals("[x]y]", ((Mailbox) 
parser.parseAddressList("a@[x\\]y]").get(0)).getDomain());
     }
 
+    @Test
+    public void quotedPairInAQuotedLocalPartIsUnescapedLikeTheStrictParser() 
throws Exception {
+        String[] texts = {"\"\\a\"@d", "\"a\\\"b\\\\c\"@d", "\"x\\ y\"@d", "J 
<\"\\a\"@d>"};
+        for (String text : texts) {
+            Mailbox strict = (Mailbox) 
DefaultAddressParser.DEFAULT.parseAddressList(text, 
DecodeMonitor.SILENT).get(0);
+            Mailbox lenient = (Mailbox) parser.parseAddressList(text).get(0);
+            Assert.assertEquals(text, strict.getLocalPart(), 
lenient.getLocalPart());
+        }
+        Assert.assertEquals("a", 
parser.parseMailbox("\"\\a\"@d").getLocalPart());
+        Assert.assertEquals("a", ((Mailbox) 
parser.parseAddressList("\"\\a\"").get(0)).getLocalPart());
+        Assert.assertEquals("a\"b\\c", 
parser.parseMailbox("\"a\\\"b\\\\c\"@d").getLocalPart());
+    }
+
+    @Test
+    public void 
quotedPairInADisplayNameOrGroupNameIsUnescapedLikeTheStrictParser() throws 
Exception {
+        String[] names = {"\"J\\ohn\" <a@d>", "\"J\\\"o\" <a@d>", "\"J\\\\o\" 
<a@d>", "\"\\=?utf-8?q?x?=\" <a@d>"};
+        for (String text : names) {
+            Mailbox strict = (Mailbox) 
DefaultAddressParser.DEFAULT.parseAddressList(text, 
DecodeMonitor.SILENT).get(0);
+            Mailbox lenient = (Mailbox) parser.parseAddressList(text).get(0);
+            Assert.assertEquals(text, strict.getName(), lenient.getName());
+        }
+        Assert.assertEquals("John", ((Mailbox) 
parser.parseAddressList("\"J\\ohn\" <a@d>").get(0)).getName());
+        String group = "\"G\\x\": a@d;";
+        Group strict = (Group) 
DefaultAddressParser.DEFAULT.parseAddressList(group, 
DecodeMonitor.SILENT).get(0);
+        Assert.assertEquals("Gx", strict.getName());
+        Assert.assertEquals("Gx", ((Group) 
parser.parseAddressList(group).get(0)).getName());
+        Assert.assertEquals("Gx", parser.parseGroup(group).getName());
+    }
+
     @Test
     public void escapedClosingBracketDoesNotEndTheDomainLiteral() {
         AddressList list = parser.parseAddressList("a@[x\\] y], b@c");


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

Reply via email to