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 fd839dc0b685dd4801d3e488ba7c89d8e5df8fa3 Author: Benoit TELLIER <[email protected]> AuthorDate: Fri Nov 13 13:55:50 2026 +0100 [FIX] LenientAddressParser: a quoted pair in a domain literal is the escaped character alone copyDomainLiteral copied a domain literal byte for byte up to the first ']': the backslash of a quoted pair (obs-dtext, RFC 5322 section 4.4) was kept, and an escaped "\]" closed the literal. So "a@[1.2.3\.4]" gave the domain "[1.2.3\.4]" with the lenient parser, the default for From, To, Cc, Reply-To and Sender, and "[1.2.3.4]" with the strict one, which reads a quoted pair as the character alone (RFC 5322 section 3.2.1). The backslash of a quoted pair is now dropped and the escaped character kept, an escaped ']' does not end the literal, and a line break after the backslash is skipped, the quoted pair applying to the character after it, as in RawFieldParser. Fixes #135 --- .../mime4j/field/address/LenientAddressParser.java | 27 +++++++++++++++++++--- .../field/address/LenientAddressBuilderTest.java | 24 +++++++++++++++++++ 2 files changed, 48 insertions(+), 3 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 beeb5597..678e1524 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 @@ -113,8 +113,11 @@ public class LenientAddressParser implements AddressParser { /** * Copies a domain literal up to its closing bracket: '(' and ')' are dtext there, not a comment - * (RFC 5322 section 3.4.1). Returns -1 once copied. When no ']' comes before a delimiter, copies - * nothing and returns the index where the search stopped: the delimiter, or the upper bound. + * (RFC 5322 section 3.4.1). A quoted pair (obs-dtext, section 4.4) is the escaped character alone, + * as for the strict parser: its backslash is dropped, and an escaped ']' does not close the + * literal. A line break after a backslash is unfolded, the quoted pair applying to the character + * after it. Returns -1 once copied. When no ']' comes before a delimiter, copies nothing and + * returns the index where the search stopped: the delimiter, or the upper bound. */ private int copyDomainLiteral(final ByteSequence buf, final ParserCursor cursor, final BitSet delimiters, final StringBuilder dst) { @@ -122,11 +125,20 @@ public class LenientAddressParser implements AddressParser { int to = cursor.getUpperBound(); for (int i = from + 1; i < to; i++) { char current = (char) (buf.byteAt(i) & 0xff); + if (current == '\\') { + i = skipLineBreaks(buf, i + 1, to); + continue; + } if (current == ']') { ByteArrayBuffer literal = new ByteArrayBuffer(i + 1 - from); for (int j = from; j <= i; j++) { byte b = buf.byteAt(j); - if (b != '\r' && b != '\n') { + if (b == '\\') { + j = skipLineBreaks(buf, j + 1, i); + if (j < i) { + literal.append(buf.byteAt(j)); + } + } else if (b != '\r' && b != '\n') { literal.append(b); } } @@ -142,6 +154,15 @@ public class LenientAddressParser implements AddressParser { return to; } + /** Returns the index of the first byte from <code>from</code> that is not CR or LF, at most <code>to</code>. */ + private static int skipLineBreaks(final ByteSequence buf, final int from, final int to) { + int i = from; + while (i < to && (buf.byteAt(i) == '\r' || buf.byteAt(i) == '\n')) { + i++; + } + return i; + } + DomainList parseRoute(final ByteSequence buf, final ParserCursor cursor, final BitSet delimiters) { return parseRouteUntil(buf, cursor, union(ROUTE_STOP, delimiters)); } 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 cb5d72c4..98413b75 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 @@ -19,6 +19,7 @@ package org.apache.james.mime4j.field.address; +import org.apache.james.mime4j.codec.DecodeMonitor; import org.apache.james.mime4j.dom.address.Address; import org.apache.james.mime4j.dom.address.AddressList; import org.apache.james.mime4j.dom.address.DomainList; @@ -714,4 +715,27 @@ public class LenientAddressBuilderTest { 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()); } + + @Test + public void quotedPairInADomainLiteralIsUnescapedLikeTheStrictParser() throws Exception { + String[] texts = {"a@[1.2.3\\.4]", "a@[x\\]y]", "a@[#\\\\]", "a@[x\\(y]"}; + 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.getDomain(), lenient.getDomain()); + Assert.assertEquals(text, "a", lenient.getLocalPart()); + } + Assert.assertEquals("[1.2.3.4]", ((Mailbox) parser.parseAddressList("a@[1.2.3\\.4]").get(0)).getDomain()); + Assert.assertEquals("[x]y]", ((Mailbox) parser.parseAddressList("a@[x\\]y]").get(0)).getDomain()); + } + + @Test + public void escapedClosingBracketDoesNotEndTheDomainLiteral() { + AddressList list = parser.parseAddressList("a@[x\\] y], b@c"); + Assert.assertEquals(2, list.size()); + Assert.assertEquals("[x] y]", ((Mailbox) list.get(0)).getDomain()); + // a line break after the backslash is unfolded, the quoted pair escapes what follows it + Assert.assertEquals("[x]]", ((Mailbox) parser.parseAddressList("a@[x\\\r\n]]").get(0)).getDomain()); + Assert.assertEquals("b@c", ((Mailbox) list.get(1)).getAddress()); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
