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 462651a7d0fbb73dde9254e78acfa7cd9c0716d6 Author: Benoit TELLIER <[email protected]> AuthorDate: Tue Nov 10 12:05:06 2026 +0100 [FIX] LenientAddressParser: a quoted local part is never decoded as an encoded word RawFieldParser#copyQuotedContent decodes a quoted string that starts with "=?", for parameters such as filename="=?...?=", and LenientAddressParser read local parts through it: the angle-addr form called it directly, and the local@domain form took the local part from parseValue, which decodes too. So "\"=?utf-8?q?x?=\"@example.com", which AddressFormatter writes for a built Mailbox("=?utf-8?q?x?=", ...), was read back as [email protected]. RFC 2047 section 5 forbids encoded words in an addr-spec and in a quoted string. RawFieldParser gets copyQuotedContent(buf, cursor, dst, decodeEncodedWords), which local parts use with false, and a local part holding a quoted string is always read again as a local part. Display names and group names are still decoded. --- .../apache/james/mime4j/stream/RawFieldParser.java | 19 ++++++++++++++ .../mime4j/field/address/LenientAddressParser.java | 24 ++++++++++++++---- .../field/address/LenientAddressBuilderTest.java | 29 ++++++++++++++++++++++ 3 files changed, 67 insertions(+), 5 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 9e696fdf..9c72c6e9 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 @@ -486,10 +486,29 @@ public class RawFieldParser { copyQuotedContent(buf, cursor, dst, false, false); } + /** + * Transfers content enclosed with quote marks 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. + * + * @param buf buffer with the sequence of bytes to be parsed + * @param cursor defines the bounds and current position of the buffer + * @param dst destination buffer + * @param decodeEncodedWords whether a content starting with <code>=?</code> is decoded + */ + public void copyQuotedContent(final ByteSequence buf, final ParserCursor cursor, + final StringBuilder dst, final boolean decodeEncodedWords) { + copyQuotedContent(buf, cursor, dst, !decodeEncodedWords, false); + } + private static boolean isLineBreak(final char c) { return c == '\r' || c == '\n'; } + /** + * @param boundary when set, the content is never decoded as encoded words (a boundary, an + * address local part) + */ private void copyQuotedContent(final ByteSequence buf, final ParserCursor cursor, final StringBuilder dst, final boolean boundary, final boolean unescape) { if (cursor.atEnd()) { 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 761b995b..beeb5597 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 @@ -249,6 +249,15 @@ public class LenientAddressParser implements AddressParser { * Like {@link RawFieldParser#parseValue}, but the unquoted whitespace around dots is not part * of the local part (RFC 5322 obs-local-part: <code>john . doe</code> is <code>john.doe</code>). */ + private static boolean holdsQuote(final ByteSequence buf, final int from, final int to) { + for (int i = from; i < to; i++) { + if (buf.byteAt(i) == '\"') { + return true; + } + } + return false; + } + private String parseLocalPart(final ByteSequence buf, final ParserCursor cursor, final BitSet delimiters) { StringBuilder dst = new StringBuilder(); boolean whitespace = false; @@ -266,7 +275,8 @@ public class LenientAddressParser implements AddressParser { dst.append(' '); } if (current == '\"') { - this.parser.copyQuotedContent(buf, cursor, dst); + // RFC 2047 section 5: no encoded word in an addr-spec + this.parser.copyQuotedContent(buf, cursor, dst, false); } else { this.parser.copyUnquotedContent(buf, cursor, delimiters, dst); } @@ -310,8 +320,10 @@ public class LenientAddressParser implements AddressParser { return parseMailboxAddress(openingText, buf, cursor); } else if (current == AT) { // localPart @ domain form - // Without joined whitespace, parsing it as a local part gives the same text - String localPart = openingText.indexOf(' ') < 0 + // Without joined whitespace nor quoted string, parsing it as a local part gives the + // same text; a quoted string must be read again, as the opening text decodes one + // that looks like an encoded word, which RFC 2047 section 5 forbids in an addr-spec + String localPart = openingText.indexOf(' ') < 0 && !holdsQuote(buf, start, pos) ? openingText : parseLocalPart(buf, new ParserCursor(start, pos), null); cursor.updatePos(pos + 1); @@ -389,8 +401,10 @@ public class LenientAddressParser implements AddressParser { return parseMailboxAddress(openingText, buf, cursor); } else if (current == AT) { // localPart @ domain form - // Without joined whitespace, parsing it as a local part gives the same text - String localPart = openingText.indexOf(' ') < 0 + // Without joined whitespace nor quoted string, parsing it as a local part gives the + // same text; a quoted string must be read again, as the opening text decodes one + // that looks like an encoded word, which RFC 2047 section 5 forbids in an addr-spec + String localPart = openingText.indexOf(' ') < 0 && !holdsQuote(buf, start, pos) ? openingText : parseLocalPart(buf, new ParserCursor(start, pos), null); cursor.updatePos(pos + 1); 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 71423be5..cb5d72c4 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 @@ -685,4 +685,33 @@ public class LenientAddressBuilderTest { Assert.assertEquals(header, 1, read.size()); Assert.assertEquals(header, 2, read.flatten().size()); } + + @Test + public void quotedLocalPartLookingLikeAnEncodedWordIsNotDecoded() { + AddressList list = LenientAddressParser.DEFAULT.parseAddressList("\"=?utf-8?q?x?=\"@example.com"); + Mailbox mailbox = (Mailbox) list.get(0); + Assert.assertEquals("=?utf-8?q?x?=", mailbox.getLocalPart()); + + mailbox = (Mailbox) LenientAddressParser.DEFAULT.parseAddressList("A <\"=?utf-8?q?x?=\"@example.com>").get(0); + Assert.assertEquals("=?utf-8?q?x?=", mailbox.getLocalPart()); + + mailbox = LenientAddressParser.DEFAULT.parseMailbox("\"=?utf-8?q?x?=\"@example.com"); + Assert.assertEquals("=?utf-8?q?x?=", mailbox.getLocalPart()); + } + + @Test + public void builtLocalPartLookingLikeAnEncodedWordRoundTrips() { + String[] localParts = {"=?utf-8?q?x?=", "=?utf-8?q?x?=\"\"", "=?utf-8?q?x?= y"}; + for (String localPart : localParts) { + Mailbox written = new Mailbox(localPart, "example.com"); + AddressList read = LenientAddressParser.DEFAULT.parseAddressList(AddressFormatter.DEFAULT.encode(written)); + Assert.assertEquals(localPart, ((Mailbox) read.get(0)).getLocalPart()); + } + } + + @Test + public void encodedWordsInADisplayNameAreStillDecoded() { + Mailbox mailbox = (Mailbox) LenientAddressParser.DEFAULT.parseAddressList("\"=?utf-8?q?J=C3=A9r=C3=B4me?=\" <[email protected]>").get(0); + Assert.assertEquals("J\u00e9r\u00f4me", mailbox.getName()); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
