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 cd29e0789a0522be127b0ac654dcc9cacadc47e7 Author: Benoit TELLIER <[email protected]> AuthorDate: Fri Nov 13 13:56:33 2026 +0100 [FIX] LenientAddressParser: the quoted local part of a mailbox without domain is not decoded For an address with no "@" nor "<", createMailbox RFC 2047-decoded the whole opening text, whose quotes parseValue had already removed: the content of a quoted string was decoded like bare text. The fix of #125 covered the local@domain and angle-addr forms only. So the domainless mailbox "a =?utf-8?q?x?=", which AddressFormatter writes as "\"a =?utf-8?q?x?=\"", was read back as "a x", and a quoted text decoding to nothing, such as "\"=?x???=\"", dropped the mailbox from the list. As in the local@domain form, a domainless text holding a quoted string is read again as a local part, without decoding. Bare text keeps the decoding of MIME4J-253. Fixes #130 --- .../mime4j/field/address/LenientAddressParser.java | 23 ++++++++++++++--- .../field/address/LenientAddressBuilderTest.java | 30 ++++++++++++++++++++++ 2 files changed, 49 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 678e1524..c5fdff85 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 @@ -322,6 +322,21 @@ public class LenientAddressParser implements AddressParser { } } + /** + * A mailbox without domain, read from <code>buf</code> between <code>from</code> and + * <code>to</code>, of which <code>openingText</code> is the value. As in the local@domain form, a + * text holding a quoted string is read again as a local part: RFC 2047 section 5 forbids encoded + * words in a quoted string, and the opening text decodes one that starts with "=?". Bare text + * keeps its encoded words decoded (MIME4J-253). + */ + private Mailbox createMailbox(final String openingText, final ByteSequence buf, final int from, final int to) { + if (!holdsQuote(buf, from, to)) { + return createMailbox(openingText); + } + String localPart = parseLocalPart(buf, new ParserCursor(from, to), null); + return localPart.length() > 0 ? new Mailbox(null, null, localPart, null) : null; + } + public Mailbox parseMailbox( final ByteSequence buf, final ParserCursor cursor, final BitSet delimiters) { return parseMailbox(buf, cursor, delimiters, union(MAILBOX_STOP, delimiters)); @@ -332,7 +347,7 @@ public class LenientAddressParser implements AddressParser { int start = cursor.getPos(); String openingText = this.parser.parseValue(buf, cursor, stopSet); if (cursor.atEnd()) { - return createMailbox(openingText); + return createMailbox(openingText, buf, start, cursor.getPos()); } int pos = cursor.getPos(); char current = (char) (buf.byteAt(pos) & 0xff); @@ -351,7 +366,7 @@ public class LenientAddressParser implements AddressParser { String domain = parseDomain(buf, cursor, delimiters); return new Mailbox(null, null, localPart, domain); } else { - return createMailbox(openingText); + return createMailbox(openingText, buf, start, cursor.getPos()); } } @@ -413,7 +428,7 @@ public class LenientAddressParser implements AddressParser { int start = cursor.getPos(); String openingText = this.parser.parseValue(buf, cursor, stopSet); if (cursor.atEnd()) { - return createMailbox(openingText); + return createMailbox(openingText, buf, start, cursor.getPos()); } int pos = cursor.getPos(); char current = (char) (buf.byteAt(pos) & 0xff); @@ -444,7 +459,7 @@ public class LenientAddressParser implements AddressParser { } return new Group(decodeGroupName(openingText), mboxes); } else { - return createMailbox(openingText); + 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 98413b75..d33023b1 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 @@ -738,4 +738,34 @@ public class LenientAddressBuilderTest { Assert.assertEquals("[x]]", ((Mailbox) parser.parseAddressList("a@[x\\\r\n]]").get(0)).getDomain()); Assert.assertEquals("b@c", ((Mailbox) list.get(1)).getAddress()); } + + @Test + public void quotedDomainlessLocalPartLookingLikeAnEncodedWordIsNotDecoded() { + Mailbox read = (Mailbox) parser.parseAddressList("\"a =?utf-8?q?x?=\"").get(0); + Assert.assertEquals("a =?utf-8?q?x?=", read.getLocalPart()); + Assert.assertNull(read.getDomain()); + + Mailbox first = (Mailbox) parser.parseAddressList("a =?utf-8?q?x?=@").get(0); + Mailbox again = (Mailbox) parser.parseAddressList(AddressFormatter.DEFAULT.encode(first)).get(0); + Assert.assertEquals(first.getLocalPart(), again.getLocalPart()); + + Assert.assertEquals("=?utf-8?q?x?=", parser.parseMailbox("\"=?utf-8?q?x?=\"").getLocalPart()); + AddressList mailboxes = parser.parseAddressList("\"=?utf-8?q?x?=nnn\", b@c"); + Assert.assertEquals(2, mailboxes.size()); + Assert.assertEquals("=?utf-8?q?x?=nnn", ((Mailbox) mailboxes.get(0)).getLocalPart()); + Group group = (Group) parser.parseAddressList("g: \"=?utf-8?q?x?=]\";").get(0); + Assert.assertEquals("=?utf-8?q?x?=]", group.getMailboxes().get(0).getLocalPart()); + } + + @Test + public void quotedDomainlessLocalPartDecodingToNothingKeepsTheMailbox() { + AddressList list = parser.parseAddressList("\"=?x???=\", a@b"); + Assert.assertEquals(2, list.size()); + Assert.assertEquals("=?x???=", ((Mailbox) list.get(0)).getLocalPart()); + } + + @Test + public void bareDomainlessEncodedLocalPartIsStillDecoded() { + Assert.assertEquals("a x", ((Mailbox) parser.parseAddressList("a =?utf-8?q?x?=").get(0)).getLocalPart()); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
